Skip to content

Commit

Permalink
Handling multiple selection
Browse files Browse the repository at this point in the history
  • Loading branch information
onekiloparsec committed Oct 24, 2018
1 parent 59ca459 commit 94329c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions KPCJumpBarControl/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ internal func >= <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
}
}

extension IndexPath {
static func commonAncestor(indexPaths: [IndexPath]) -> IndexPath? {
let shortestLength = indexPaths.reduce(1000) { $0 < $1.count ? $0 : $1.count }
var common = IndexPath()
for index in 0..<shortestLength {
var s: Set = Set(indexPaths.map() { $0[index] })
if s.count == 1 {
common.append(s.popFirst()!)
} else {
break
}
}
return common.count == 0 ? nil : common
}
}
20 changes: 19 additions & 1 deletion KPCJumpBarControl/JumpBarControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ open class JumpBarControl: NSControl, JumpBarSegmentControlDelegate {
fileprivate var contentBinding: JumpBarControlBinding?
fileprivate var selectionBinding: JumpBarControlBinding?
public var isBound: Bool { get { return self.contentBinding != nil } }
fileprivate var previousSelectionIndexPaths: [IndexPath] = []
fileprivate var hasMultipleSelection: Bool = false

// MARK: - Overrides

Expand Down Expand Up @@ -146,14 +148,30 @@ open class JumpBarControl: NSControl, JumpBarSegmentControlDelegate {
}

if (keyPath == "arrangedObjects") {
self.hasMultipleSelection = false
self.useItemsTree(self.contentBinding!.treeController!.arrangedRootObjects())
} else if (keyPath == "selectionIndexPaths") {
let treeController: NSTreeController = object as! NSTreeController
if (treeController.selectionIndexPaths.count == 1) {
self.hasMultipleSelection = false
self.select(itemAtIndexPath: treeController.selectionIndexPaths.first!)
} else {
print("Bounded multiple selection! \(treeController.selectionIndexPaths)")
self.hasMultipleSelection = true
let newIndexPaths = Set(treeController.selectionIndexPaths).subtracting(Set(self.previousSelectionIndexPaths))
let indexedItems: [(IndexPath, JumpBarItemProtocol)] = newIndexPaths.compactMap { (indexPath) in
let item = self.segmentItem(atIndexPath: indexPath)
return (item != nil) ? (indexPath, item!) : nil
}
for indexedItem in indexedItems {
self.delegate?.jumpBarControl(self, willSelectItem: indexedItem.1, atIndexPath: indexedItem.0)
}
let commonAncestorIndexPath = IndexPath.commonAncestor(indexPaths: treeController.selectionIndexPaths)
self.update(withIndexPath: commonAncestorIndexPath ?? IndexPath())
for indexedItem in indexedItems {
self.delegate?.jumpBarControl(self, didSelectItem: indexedItem.1, atIndexPath: indexedItem.0)
}
}
self.previousSelectionIndexPaths = treeController.selectionIndexPaths
}
}

Expand Down

0 comments on commit 94329c4

Please sign in to comment.