diff --git a/NvimView/Sources/NvimView/NvimView+Types.swift b/NvimView/Sources/NvimView/NvimView+Types.swift index 4c74fd36..78208f69 100644 --- a/NvimView/Sources/NvimView/NvimView+Types.swift +++ b/NvimView/Sources/NvimView/NvimView+Types.swift @@ -10,10 +10,13 @@ import Tabs extension NvimView { - struct TabEntry: Equatable, Hashable, TabRepresentative { - var title: String - var tabpage: RxNeovimApi.Tabpage - var isSelected = false + public struct TabEntry: Hashable, TabRepresentative { + public static func ==(lhs: TabEntry, rhs: TabEntry) -> Bool { lhs.tabpage == rhs.tabpage } + + public var title: String + public var isSelected = false + + public var tabpage: RxNeovimApi.Tabpage } public struct Config { diff --git a/NvimView/Sources/NvimView/NvimView+UiBridge.swift b/NvimView/Sources/NvimView/NvimView+UiBridge.swift index f3d2b002..def2df6f 100644 --- a/NvimView/Sources/NvimView/NvimView+UiBridge.swift +++ b/NvimView/Sources/NvimView/NvimView+UiBridge.swift @@ -594,10 +594,10 @@ extension NvimView { let tabpageValue = dict[.string("tab")], let tabpage = RxNeovimApi.Tabpage(tabpageValue) else { return nil } - return TabEntry(title: name, tabpage: tabpage, isSelected: tabpage.handle == curTab.handle) + return TabEntry(title: name, isSelected: tabpage.handle == curTab.handle, tabpage: tabpage) } - Swift.print("!!!!!!!!!!!! all tabs: \(tabEntries)") + gui.async { self.tabBar?.update(tabRepresentatives: tabEntries) } } private func bufferWritten(_ handle: Int) { diff --git a/NvimView/Sources/NvimView/NvimView.swift b/NvimView/Sources/NvimView/NvimView.swift index 713f486f..c81fd898 100644 --- a/NvimView/Sources/NvimView/NvimView.swift +++ b/NvimView/Sources/NvimView/NvimView.swift @@ -29,7 +29,7 @@ public class NvimView: NSView, public static let maxLinespacing = 8.cgf public let usesCustomTabBar: Bool - public private(set) var tabBar: TabBar? + public let tabBar: TabBar? public var isLeftOptionMeta = false public var isRightOptionMeta = false @@ -157,7 +157,8 @@ public class NvimView: NSView, self.sourceFileUrls = config.sourceFiles self.usesCustomTabBar = config.usesCustomTabBar - if self.usesCustomTabBar { self.tabBar = TabBar(withTheme: .default) } + if self.usesCustomTabBar { self.tabBar = TabBar(withTheme: .default) } + else { self.tabBar = nil } super.init(frame: .zero) self.bridge.consumer = self diff --git a/Tabs/Sources/Tabs/Tab.swift b/Tabs/Sources/Tabs/Tab.swift index ce043f8a..5481eb8d 100644 --- a/Tabs/Sources/Tabs/Tab.swift +++ b/Tabs/Sources/Tabs/Tab.swift @@ -52,14 +52,9 @@ class Tab: NSView { @available(*, unavailable) required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") } - var isSelected: Bool = false { - didSet { - self.adjustToSelectionChange() - self.needsDisplay = true - } - } + var isSelected: Bool { self.tabRepresentative.isSelected } - weak private(set) var tabBar: TabBar? + private(set) weak var tabBar: TabBar? var position = Position.inBetween { didSet { self.needsDisplay = true } } diff --git a/Tabs/Sources/Tabs/TabBar.swift b/Tabs/Sources/Tabs/TabBar.swift index dadf9154..df73f9cb 100644 --- a/Tabs/Sources/Tabs/TabBar.swift +++ b/Tabs/Sources/Tabs/TabBar.swift @@ -6,8 +6,9 @@ import Cocoa import PureLayout -public protocol TabRepresentative: Hashable, Equatable { +public protocol TabRepresentative: Hashable { var title: String { get } + var isSelected: Bool { get } } public class TabBar: NSView { @@ -24,7 +25,24 @@ public class TabBar: NSView { self.addViews() } -// public func update(tabRepresentatives _: [Entry]) {} + public func update(tabRepresentatives entries: [Entry]) { + var result = [Tab]() + entries.forEach { entry in + if let existingTab = self.tabs.first(where: { $0.tabRepresentative == entry }) { + existingTab.tabRepresentative = entry + result.append(existingTab) + } else { + result.append(Tab(withTabRepresentative: entry, in: self)) + } + } + + result.forEach { $0.position = .inBetween } + result.first?.position = .first + result.last?.position = .last + + self.tabs = result + self.stackView.update(views: self.tabs) + } override public func draw(_: NSRect) { self.drawSeparator() @@ -41,9 +59,9 @@ public class TabBar: NSView { } extension TabBar { - func select(tab: Tab) { - self.stackView.arrangedSubviews.forEach { ($0 as? Tab)?.isSelected = false } - tab.isSelected = true + func select(tab _: Tab) { +// self.stackView.arrangedSubviews.forEach { ($0 as? Tab)?.isSelected = false } +// tab.isSelected = true } } diff --git a/Tabs/Support/TabsSupport/AppDelegate.swift b/Tabs/Support/TabsSupport/AppDelegate.swift index c501c6c7..fca425ca 100644 --- a/Tabs/Support/TabsSupport/AppDelegate.swift +++ b/Tabs/Support/TabsSupport/AppDelegate.swift @@ -25,6 +25,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { self.tabBar.autoPinEdge(toSuperviewEdge: .left) self.tabBar.autoPinEdge(toSuperviewEdge: .right) self.tabBar.autoSetDimension(.height, toSize: Theme().tabBarHeight) + + self.tabBar.update(tabRepresentatives: [ + DummyTabEntry(title: "Test 1"), + DummyTabEntry(title: "Test 2"), + DummyTabEntry(title: "Test 3"), + DummyTabEntry(title: "Very long long long title, and some more text!"), + ]) } func applicationWillTerminate(_: Notification) { @@ -34,6 +41,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { private let tabBar: TabBar } -struct DummyTabEntry: Hashable, Equatable, TabRepresentative { +struct DummyTabEntry: Hashable, TabRepresentative { var title: String + var isSelected = false }