Skip to content

Commit d1ef7a0

Browse files
add double click delegate & notification
1 parent 7b3284c commit d1ef7a0

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

FatSidebar/FatSidebar.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ class FlippedView: NSView {
88

99
public protocol FatSidebarDelegate: class {
1010
func sidebar(_ sidebar: FatSidebar, didMoveItemFrom oldIndex: Int, to newIndex: Int)
11-
}
12-
13-
public protocol FatSidebarSelectionChangeDelegate: class {
1411
func sidebar(_ sidebar: FatSidebar, didChangeSelection selectionIndex: Int)
12+
func sidebar(_ sidebar: FatSidebar, editItem index: Int)
1513
}
1614

1715

1816
/// Top-level module facade, acting as a composite view that scrolls.
1917
public class FatSidebar: NSView {
2018

2119
public weak var delegate: FatSidebarDelegate?
22-
public weak var selectionDelegate: FatSidebarSelectionChangeDelegate?
2320

2421
let scrollView = NSScrollView()
2522
let sidebarView = FatSidebarView()
@@ -103,6 +100,7 @@ public class FatSidebar: NSView {
103100

104101
private var selectionChangeObserver: AnyObject!
105102
private var moveObserver: AnyObject!
103+
private var doubleClickObserver: AnyObject!
106104

107105
fileprivate func observeSidebarSelectionChanges() {
108106

@@ -117,13 +115,18 @@ public class FatSidebar: NSView {
117115
forName: FatSidebarView.didReorderItemNotification,
118116
object: sidebarView,
119117
queue: nil) { [weak self] in self?.itemDidMove($0) }
118+
119+
doubleClickObserver = notificationCenter.addObserver(
120+
forName: FatSidebarView.didDoubleClickItemNotification,
121+
object: sidebarView,
122+
queue: nil) { [weak self] in self?.itemWasDoubleClicked($0) }
120123
}
121124

122125
fileprivate func selectionDidChange(_ notification: Notification) {
123126

124127
guard let index = notification.userInfo?["index"] as? Int else { return }
125128

126-
selectionDelegate?.sidebar(self, didChangeSelection: index)
129+
delegate?.sidebar(self, didChangeSelection: index)
127130
}
128131

129132
fileprivate func itemDidMove(_ notification: Notification) {
@@ -136,6 +139,16 @@ public class FatSidebar: NSView {
136139
self.delegate?.sidebar(self, didMoveItemFrom: fromIndex, to: toIndex)
137140
}
138141

142+
fileprivate func itemWasDoubleClicked(_ notification: Notification) {
143+
144+
guard let userInfo = notification.userInfo,
145+
let index = userInfo["index"] as? Int
146+
else { return }
147+
148+
self.delegate?.sidebar(self, editItem: index)
149+
}
150+
151+
139152
// MARK: - Sidebar Façade
140153

141154
public var itemCount: Int {

FatSidebar/FatSidebarItem.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class FatSidebarItem: NSView {
3535
public let style: Style
3636
public let callback: ((FatSidebarItem) -> Void)?
3737
public var selectionHandler: ((FatSidebarItem) -> Void)?
38+
public var doubleClickHandler: ((FatSidebarItem) -> Void)?
3839
public var animated: Bool
3940

4041
let label: NSTextField
@@ -328,6 +329,7 @@ public class FatSidebarItem: NSView {
328329

329330
public override func mouseDown(with event: NSEvent) {
330331

332+
331333
isHighlighted = true
332334

333335
let dragTimer = Timer.scheduledTimer(
@@ -367,6 +369,10 @@ public class FatSidebarItem: NSView {
367369
dragging = nil
368370
}
369371

372+
if event.clickCount == 2 {
373+
doubleClickHandler?(self)
374+
}
375+
370376
guard let dragging = dragging,
371377
!dragging.isDragging
372378
else { return }
@@ -442,6 +448,10 @@ public class FatSidebarItem: NSView {
442448
overlay.isSelected = self.isSelected
443449
overlay.translatesAutoresizingMaskIntoConstraints = true
444450

451+
overlay.doubleClickHandler = { [weak self] _ in
452+
guard let item = self else { return }
453+
item.doubleClickHandler?(item)
454+
}
445455
overlay.selectionHandler = { [weak self] _ in
446456
guard let item = self else { return }
447457
item.selectionHandler?(item)

FatSidebar/FatSidebarView.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ public class FatSidebarView: NSView, DragViewContainer {
1818
static let didToggleItemNotification = Notification.Name("FatSidebarView_didToggleItemNotification")
1919
static let didSelectItemNotification = Notification.Name("FatSidebarView_didSelectItemNotification")
2020
static let didDeselectItemNotification = Notification.Name("FatSidebarView_didDeselectItemNotification")
21+
2122
static let didReorderItemNotification = Notification.Name("FatSidebarView_didReorderItemNotification")
2223

24+
static let didDoubleClickItemNotification = Notification.Name("FatSidebarView_didDoubleClickItemNotification")
25+
2326
public var theme: FatSidebarTheme = DefaultTheme() {
2427
didSet {
2528
applyThemeToItems()
@@ -80,6 +83,7 @@ public class FatSidebarView: NSView, DragViewContainer {
8083
let item = FatSidebarItem(configuration: configuration)
8184
item.menu = self.itemContextualMenu
8285
item.selectionHandler = { [unowned self] in self.itemSelected($0) }
86+
item.doubleClickHandler = { [unowned self] in self.itemDoubleClicked($0) }
8387

8488
return item
8589
}
@@ -166,6 +170,15 @@ public class FatSidebarView: NSView, DragViewContainer {
166170
}
167171
}
168172

173+
fileprivate func itemDoubleClicked(_ item: FatSidebarItem) {
174+
175+
guard let index = self.items.index(of: item) else { return }
176+
177+
NotificationCenter.default.post(
178+
name: FatSidebarView.didDoubleClickItemNotification,
179+
object: self,
180+
userInfo: ["index" : index])
181+
}
169182

170183
// MARK: Removal
171184

0 commit comments

Comments
 (0)