Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from greg/develop
Rudimentary Touch Bar support (#358)
- Loading branch information
Showing
11 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters