macos: add tab index labels

This commit is contained in:
Gregory Anders
2023-09-26 21:31:14 -05:00
committed by Mitchell Hashimoto
parent 752aa11930
commit 59ba6fac2b
2 changed files with 27 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import Cocoa
class PrimaryWindowController: NSWindowController {
class PrimaryWindowController: NSWindowController, NSWindowDelegate {
// This is used to programmatically control tabs.
weak var windowManager: PrimaryWindowManager?
@ -21,4 +21,12 @@ class PrimaryWindowController: NSWindowController {
window.contentView = nil
}
}
func windowDidBecomeKey(_ notification: Notification) {
self.windowManager?.indexTabs()
}
func windowWillClose(_ notification: Notification) {
self.windowManager?.indexTabs()
}
}

View File

@ -164,6 +164,7 @@ class PrimaryWindowManager {
let managed = ManagedWindow(windowController: windowController, window: window, closePublisher: pubClose)
managedWindows.append(managed)
window.delegate = windowController
return managed
}
@ -178,4 +179,21 @@ class PrimaryWindowManager {
Self.lastCascadePoint = NSPoint(x: frame.minX, y: frame.maxY)
}
}
/// Update the accessory view of the first 9 tabs. This is called when the
/// key window changes and when a window is closed.
func indexTabs() {
if let windows = self.mainWindow?.tabbedWindows {
for (index, window) in windows.enumerated().prefix(9) {
let string = "\(index + 1) "
let attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.labelFont(ofSize: 0),
.foregroundColor: window.isKeyWindow ? NSColor.labelColor : NSColor.secondaryLabelColor,
]
let attributedString = NSAttributedString(string: string, attributes: attributes)
let text = NSTextField(labelWithAttributedString: attributedString)
window.tab.accessoryView = text
}
}
}
}