/
NeoVimView+TouchBar.swift
91 lines (77 loc) · 3.48 KB
/
NeoVimView+TouchBar.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Greg Omelaenko - http://omelaen.co
* See LICENSE
*/
import Cocoa
@available(OSX 10.12.2, *)
extension NeoVimView : NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate {
private static let touchBarIdentifier = NSTouchBarCustomizationIdentifier("com.qvacua.VimR.SwiftNeoVim.touchBar")
private static let touchBarTabSwitcherIdentifier = NSTouchBarItemIdentifier("com.qvacua.VimR.SwiftNeoVim.touchBar.tabSwitcher")
private static let touchBarTabSwitcherItem = "com.qvacua.VimR.SwiftNeoVim.touchBar.tabSwitcher.item"
override public func makeTouchBar() -> NSTouchBar? {
let bar = NSTouchBar()
bar.delegate = self
bar.customizationIdentifier = NeoVimView.touchBarIdentifier
bar.defaultItemIdentifiers = [NeoVimView.touchBarTabSwitcherIdentifier]
bar.customizationRequiredItemIdentifiers = [NeoVimView.touchBarTabSwitcherIdentifier]
return bar
}
public func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {
switch identifier {
case NeoVimView.touchBarTabSwitcherIdentifier:
let item = NSCustomTouchBarItem(identifier: identifier)
item.customizationLabel = "Tab Switcher"
let tabsControl = NSScrubber()
tabsControl.register(NSScrubberTextItemView.self, forItemIdentifier: NeoVimView.touchBarTabSwitcherItem)
tabsControl.mode = .fixed
tabsControl.dataSource = self
tabsControl.delegate = self
tabsControl.selectionOverlayStyle = .outlineOverlay
tabsControl.selectedIndex = selectedTabIndex()
let layout = NSScrubberProportionalLayout()
layout.numberOfVisibleItems = 1
tabsControl.scrubberLayout = layout
item.view = tabsControl
return item
default:
return nil
}
}
private func selectedTabIndex() -> Int {
return tabsCache.index(where: { $0.isCurrent }) ?? -1
}
private func getTabsControl() -> NSScrubber? {
return (self.touchBar?.item(forIdentifier: NeoVimView.touchBarTabSwitcherIdentifier) as? NSCustomTouchBarItem)?.view as? NSScrubber
}
func updateTouchBarCurrentBuffer() {
guard let tabsControl = getTabsControl() else { return }
tabsCache = self.agent.tabs()
tabsControl.reloadData()
(tabsControl.scrubberLayout as! NSScrubberProportionalLayout).numberOfVisibleItems = tabsControl.numberOfItems > 0 ? tabsControl.numberOfItems : 1
tabsControl.selectedIndex = selectedTabIndex()
}
func updateTouchBarTab() {
guard let tabsControl = getTabsControl() else { return }
tabsCache = self.agent.tabs()
if tabsControl.numberOfItems != tabsCache.count {
tabsControl.reloadData()
}
tabsControl.selectedIndex = selectedTabIndex()
tabsControl.reloadItems(at: [tabsControl.selectedIndex])
}
public func numberOfItems(for scrubber: NSScrubber) -> Int {
return tabsCache.count
}
public func scrubber(_ scrubber: NSScrubber, viewForItemAt index: Int) -> NSScrubberItemView {
let itemView = scrubber.makeItem(withIdentifier: type(of: self).touchBarTabSwitcherItem, owner: nil) as! NSScrubberTextItemView
guard tabsCache.count > index else { return itemView }
let tab = tabsCache[index]
itemView.title = tab.currentWindow()?.buffer.name ?? "[No Name]"
return itemView
}
public func scrubber(_ scrubber: NSScrubber, didSelectItemAt selectedIndex: Int) {
let tab = tabsCache[selectedIndex]
guard tab.windows.count > 0 else { return }
self.agent.select(tab.currentWindow() ?? tab.windows[0])
}
}