1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 03:25:03 +03:00

Display tabs

This commit is contained in:
Tae Won Ha 2020-12-06 16:50:49 +01:00
parent 0572cec9eb
commit c550da8845
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
6 changed files with 46 additions and 21 deletions

View File

@ -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 {

View File

@ -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) {

View File

@ -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<TabEntry>?
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<TabEntry>(withTheme: .default) }
else { self.tabBar = nil }
super.init(frame: .zero)
self.bridge.consumer = self

View File

@ -52,14 +52,9 @@ class Tab<Entry: TabRepresentative>: 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<Entry>?
private(set) weak var tabBar: TabBar<Entry>?
var position = Position.inBetween {
didSet { self.needsDisplay = true }
}

View File

@ -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<Entry: TabRepresentative>: NSView {
@ -24,7 +25,24 @@ public class TabBar<Entry: TabRepresentative>: NSView {
self.addViews()
}
// public func update(tabRepresentatives _: [Entry]) {}
public func update(tabRepresentatives entries: [Entry]) {
var result = [Tab<Entry>]()
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<Entry: TabRepresentative>: NSView {
}
extension TabBar {
func select(tab: Tab<Entry>) {
self.stackView.arrangedSubviews.forEach { ($0 as? Tab<Entry>)?.isSelected = false }
tab.isSelected = true
func select(tab _: Tab<Entry>) {
// self.stackView.arrangedSubviews.forEach { ($0 as? Tab<Entry>)?.isSelected = false }
// tab.isSelected = true
}
}

View File

@ -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<DummyTabEntry>
}
struct DummyTabEntry: Hashable, Equatable, TabRepresentative {
struct DummyTabEntry: Hashable, TabRepresentative {
var title: String
var isSelected = false
}