1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-01 01:32:04 +03:00

Adapt example app

This commit is contained in:
Tae Won Ha 2020-12-06 21:23:23 +01:00
parent 17c13dc8bf
commit 965637a3e2
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
4 changed files with 46 additions and 18 deletions

View File

@ -171,7 +171,7 @@ public class NvimView: NSView,
.subscribe() .subscribe()
.disposed(by: db) .disposed(by: db)
} }
self.tabBar?.reorderHandler = { [weak self] index, entry in self.tabBar?.reorderHandler = { [weak self] index, _, _ in
self?.api self?.api
.command(command: "tabm \(index)") .command(command: "tabm \(index)")
.subscribe() .subscribe()

View File

@ -17,14 +17,17 @@ class Tab<Entry: TabRepresentative>: NSView {
var title: String { self.tabRepresentative.title } var title: String { self.tabRepresentative.title }
var tabRepresentative: Entry { 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 { didSet {
if self.titleView.stringValue != self.title { if self.titleView.stringValue == self.title { return }
self.titleView.stringValue = self.title self.titleView.stringValue = self.title
self.adjustWidth() self.adjustWidth()
} }
} }
}
init(withTabRepresentative tabRepresentative: Entry, in tabBar: TabBar<Entry>) { init(withTabRepresentative tabRepresentative: Entry, in tabBar: TabBar<Entry>) {
self.tabBar = tabBar self.tabBar = tabBar
@ -44,9 +47,7 @@ class Tab<Entry: TabRepresentative>: NSView {
self.adjustWidth() self.adjustWidth()
} }
override func mouseUp(with _: NSEvent) { override func mouseUp(with _: NSEvent) { self.tabBar?.select(tab: self) }
self.tabBar?.select(tab: self)
}
override func draw(_: NSRect) { override func draw(_: NSRect) {
self.drawSeparators() self.drawSeparators()

View File

@ -14,7 +14,7 @@ public protocol TabRepresentative: Hashable {
public class TabBar<Entry: TabRepresentative>: NSView { public class TabBar<Entry: TabRepresentative>: NSView {
public var theme: Theme { self._theme } public var theme: Theme { self._theme }
public var selectHandler: ((Int, Entry) -> Void)? public var selectHandler: ((Int, Entry) -> Void)?
public var reorderHandler: ((Int, Entry) -> Void)? public var reorderHandler: ((Int, Entry, [Entry]) -> Void)?
public init(withTheme theme: Theme) { public init(withTheme theme: Theme) {
self._theme = theme self._theme = theme
@ -102,7 +102,11 @@ extension TabBar {
if let draggedTab = draggedView as? Tab<Entry>, if let draggedTab = draggedView as? Tab<Entry>,
let indexOfDraggedTab = self.tabs.firstIndex(where: { $0 == draggedTab }) 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 let endIndex = stackView.arrangedSubviews.endIndex - 1

View File

@ -21,18 +21,40 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_: Notification) { func applicationDidFinishLaunching(_: Notification) {
let contentView = self.window.contentView! let contentView = self.window.contentView!
contentView.addSubview(self.tabBar) 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 1"),
DummyTabEntry(title: "Test 2"), DummyTabEntry(title: "Test 2"),
DummyTabEntry(title: "Test 3"), DummyTabEntry(title: "Test 3"),
DummyTabEntry(title: "Very long long long title, and some more text!"), 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) { func applicationWillTerminate(_: Notification) {
@ -40,6 +62,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
private let tabBar: TabBar<DummyTabEntry> private let tabBar: TabBar<DummyTabEntry>
private var tabEntries = [DummyTabEntry]()
} }
struct DummyTabEntry: Hashable, TabRepresentative { struct DummyTabEntry: Hashable, TabRepresentative {