1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 11:37:32 +03:00
This commit is contained in:
Tae Won Ha 2020-12-10 21:59:34 +01:00
parent 0c49176474
commit 3166fe0b6c
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -5,11 +5,10 @@
*/
import Cocoa
import RxSwift
import RxPack
import RxSwift
extension NvimView: NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate {
override public func makeTouchBar() -> NSTouchBar? {
let bar = NSTouchBar()
bar.delegate = self
@ -20,22 +19,24 @@ extension NvimView: NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate
return bar
}
public func touchBar(_ touchBar: NSTouchBar,
makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
public func touchBar(
_: NSTouchBar,
makeItemForIdentifier identifier: NSTouchBarItem.Identifier
) -> NSTouchBarItem? {
switch identifier {
case touchBarTabSwitcherIdentifier:
let item = NSCustomTouchBarItem(identifier: identifier)
item.customizationLabel = "Tab Switcher"
let tabsControl = NSScrubber()
tabsControl.register(NSScrubberTextItemView.self,
forItemIdentifier: NSUserInterfaceItemIdentifier(touchBarTabSwitcherItem))
tabsControl.register(
NSScrubberTextItemView.self,
forItemIdentifier: NSUserInterfaceItemIdentifier(touchBarTabSwitcherItem)
)
tabsControl.mode = .fixed
tabsControl.dataSource = self
tabsControl.delegate = self
tabsControl.selectionOverlayStyle = .outlineOverlay
tabsControl.selectedIndex = selectedTabIndex()
tabsControl.selectedIndex = self.selectedTabIndex()
let layout = NSScrubberProportionalLayout()
layout.numberOfVisibleItems = 1
@ -46,16 +47,15 @@ extension NvimView: NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate
default:
return nil
}
}
private func selectedTabIndex() -> Int {
return tabsCache.firstIndex { $0.isCurrent } ?? -1
}
private func selectedTabIndex() -> Int { tabsCache.firstIndex { $0.isCurrent } ?? -1 }
private func getTabsControl() -> NSScrubber? {
let item = self.touchBar?.item(forIdentifier: touchBarTabSwitcherIdentifier) as? NSCustomTouchBarItem
let item = self
.touchBar?
.item(forIdentifier: touchBarTabSwitcherIdentifier) as? NSCustomTouchBarItem
return item?.view as? NSScrubber
}
@ -66,14 +66,13 @@ extension NvimView: NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate
.subscribe(onSuccess: { [weak self] in
self?.tabsCache = $0
guard let tabsControl = self?.getTabsControl() else {
return
}
guard let tabsControl = self?.getTabsControl() else { return }
tabsControl.reloadData()
let scrubberProportionalLayout = tabsControl.scrubberLayout as! NSScrubberProportionalLayout
scrubberProportionalLayout.numberOfVisibleItems = tabsControl.numberOfItems > 0 ? tabsControl.numberOfItems : 1
scrubberProportionalLayout.numberOfVisibleItems = tabsControl
.numberOfItems > 0 ? tabsControl.numberOfItems : 1
tabsControl.selectedIndex = self?.selectedTabIndex() ?? tabsControl.selectedIndex
}, onError: { [weak self] error in
self?.eventsSubject.onNext(.apiError(msg: "Could not get all tabpages.", cause: error))
@ -88,9 +87,7 @@ extension NvimView: NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate
.subscribe(onSuccess: { [weak self] in
self?.tabsCache = $0
guard let tabsControl = self?.getTabsControl() else {
return
}
guard let tabsControl = self?.getTabsControl() else { return }
tabsControl.reloadData()
tabsControl.selectedIndex = self?.selectedTabIndex() ?? tabsControl.selectedIndex
@ -100,19 +97,16 @@ extension NvimView: NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate
.disposed(by: self.disposeBag)
}
public func numberOfItems(for scrubber: NSScrubber) -> Int {
return tabsCache.count
}
public func numberOfItems(for _: NSScrubber) -> Int { tabsCache.count }
public func scrubber(_ scrubber: NSScrubber, viewForItemAt index: Int) -> NSScrubberItemView {
let item = scrubber.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(touchBarTabSwitcherItem), owner: nil)
guard let itemView = item as? NSScrubberTextItemView else {
return NSScrubberTextItemView()
}
let item = scrubber.makeItem(
withIdentifier: NSUserInterfaceItemIdentifier(touchBarTabSwitcherItem),
owner: nil
)
guard let itemView = item as? NSScrubberTextItemView else { return NSScrubberTextItemView() }
guard tabsCache.count > index else {
return itemView
}
guard tabsCache.count > index else { return itemView }
let tab = self.tabsCache[index]
itemView.title = tab.currentWindow?.buffer.name ?? "[No Name]"
@ -120,25 +114,26 @@ extension NvimView: NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate
return itemView
}
public func scrubber(_ scrubber: NSScrubber, didSelectItemAt selectedIndex: Int) {
public func scrubber(_: NSScrubber, didSelectItemAt selectedIndex: Int) {
let tab = self.tabsCache[selectedIndex]
guard tab.windows.count > 0 else {
return
}
guard tab.windows.count > 0 else { return }
let window = tab.currentWindow ?? tab.windows[0]
self.api
.setCurrentWin(window: RxNeovimApi.Window(window.handle))
.subscribeOn(self.scheduler)
.subscribe(onError: { [weak self] error in
self?.eventsSubject.onNext(.apiError(msg: "Could not set current window to \(window.handle).", cause: error))
self?.eventsSubject
.onNext(.apiError(msg: "Could not set current window to \(window.handle).", cause: error))
})
.disposed(by: self.disposeBag)
}
}
private let touchBarIdentifier = NSTouchBar.CustomizationIdentifier("com.qvacua.VimR.NvimView.touchBar")
private let touchBarIdentifier = NSTouchBar
.CustomizationIdentifier("com.qvacua.VimR.NvimView.touchBar")
private let touchBarTabSwitcherIdentifier = NSTouchBarItem.Identifier("com.qvacua.VimR.NvimView.touchBar.tabSwitcher")
private let touchBarTabSwitcherIdentifier = NSTouchBarItem
.Identifier("com.qvacua.VimR.NvimView.touchBar.tabSwitcher")
private let touchBarTabSwitcherItem = "com.qvacua.VimR.NvimView.touchBar.tabSwitcher.item"