diff --git a/NvimView/Sources/NvimView/NvimView.swift b/NvimView/Sources/NvimView/NvimView.swift index 16b5c1bb..f0aa88ad 100644 --- a/NvimView/Sources/NvimView/NvimView.swift +++ b/NvimView/Sources/NvimView/NvimView.swift @@ -171,7 +171,7 @@ public class NvimView: NSView, .subscribe() .disposed(by: db) } - self.tabBar?.reorderHandler = { [weak self] index, entry in + self.tabBar?.reorderHandler = { [weak self] index, _, _ in self?.api .command(command: "tabm \(index)") .subscribe() diff --git a/Tabs/Sources/Tabs/Tab.swift b/Tabs/Sources/Tabs/Tab.swift index f9c62a73..cb7574da 100644 --- a/Tabs/Sources/Tabs/Tab.swift +++ b/Tabs/Sources/Tabs/Tab.swift @@ -17,12 +17,15 @@ class Tab: NSView { var title: String { self.tabRepresentative.title } var tabRepresentative: Entry { - willSet { if self.isSelected != newValue.isSelected { self.needsDisplay = true } } + willSet { + if self.isSelected == newValue.isSelected { return } + self.adjustToSelectionChange() + self.needsDisplay = true + } didSet { - if self.titleView.stringValue != self.title { - self.titleView.stringValue = self.title - self.adjustWidth() - } + if self.titleView.stringValue == self.title { return } + self.titleView.stringValue = self.title + self.adjustWidth() } } @@ -44,9 +47,7 @@ class Tab: NSView { self.adjustWidth() } - override func mouseUp(with _: NSEvent) { - self.tabBar?.select(tab: self) - } + override func mouseUp(with _: NSEvent) { self.tabBar?.select(tab: self) } override func draw(_: NSRect) { self.drawSeparators() diff --git a/Tabs/Sources/Tabs/TabBar.swift b/Tabs/Sources/Tabs/TabBar.swift index 0efb507e..68478e62 100644 --- a/Tabs/Sources/Tabs/TabBar.swift +++ b/Tabs/Sources/Tabs/TabBar.swift @@ -14,7 +14,7 @@ public protocol TabRepresentative: Hashable { public class TabBar: NSView { public var theme: Theme { self._theme } public var selectHandler: ((Int, Entry) -> Void)? - public var reorderHandler: ((Int, Entry) -> Void)? + public var reorderHandler: ((Int, Entry, [Entry]) -> Void)? public init(withTheme theme: Theme) { self._theme = theme @@ -102,7 +102,11 @@ extension TabBar { if let draggedTab = draggedView as? Tab, let indexOfDraggedTab = self.tabs.firstIndex(where: { $0 == draggedTab }) { - self.reorderHandler?(indexOfDraggedTab, draggedTab.tabRepresentative) + self.reorderHandler?( + indexOfDraggedTab, + draggedTab.tabRepresentative, + self.tabs.map(\.tabRepresentative) + ) } let endIndex = stackView.arrangedSubviews.endIndex - 1 diff --git a/Tabs/Support/TabsSupport/AppDelegate.swift b/Tabs/Support/TabsSupport/AppDelegate.swift index d363d3eb..658ba3bd 100644 --- a/Tabs/Support/TabsSupport/AppDelegate.swift +++ b/Tabs/Support/TabsSupport/AppDelegate.swift @@ -21,18 +21,40 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_: Notification) { let contentView = self.window.contentView! contentView.addSubview(self.tabBar) - self.tabBar.autoPinEdge(toSuperviewEdge: .top) - self.tabBar.autoPinEdge(toSuperviewEdge: .left) - self.tabBar.autoPinEdge(toSuperviewEdge: .right) - self.tabBar.autoSetDimension(.height, toSize: Theme().tabBarHeight) - self.tabBar.selectHandler = { _, entry in Swift.print("selected \(entry)") } - self.tabBar.update(tabRepresentatives: [ + let tb = self.tabBar + tb.autoPinEdge(toSuperviewEdge: .top) + tb.autoPinEdge(toSuperviewEdge: .left) + tb.autoPinEdge(toSuperviewEdge: .right) + tb.autoSetDimension(.height, toSize: Theme().tabBarHeight) + tb.selectHandler = { [weak self] _, selectedEntry in + self?.tabEntries.enumerated().forEach { index, entry in + self?.tabEntries[index].isSelected = (entry == selectedEntry) + } + DispatchQueue.main.async { + Swift.print("select: \(self!.tabEntries)") + self?.tabBar.update(tabRepresentatives: self?.tabEntries ?? []) + } + } + tb.reorderHandler = { [weak self] index, reorderedEntry, entries in + self?.tabEntries = entries + self?.tabEntries.enumerated().forEach { index, entry in + self?.tabEntries[index].isSelected = (entry == reorderedEntry) + } + DispatchQueue.main.async { + Swift.print("reorder: \(entries)") + self?.tabBar.update(tabRepresentatives: self?.tabEntries ?? []) + } + } + + self.tabEntries = [ DummyTabEntry(title: "Test 1"), DummyTabEntry(title: "Test 2"), DummyTabEntry(title: "Test 3"), DummyTabEntry(title: "Very long long long title, and some more text!"), - ]) + ] + self.tabEntries[0].isSelected = true + self.tabBar.update(tabRepresentatives: self.tabEntries) } func applicationWillTerminate(_: Notification) { @@ -40,6 +62,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { } private let tabBar: TabBar + private var tabEntries = [DummyTabEntry]() } struct DummyTabEntry: Hashable, TabRepresentative {