diff --git a/macos/Sources/Features/Primary Window/PrimaryWindowController.swift b/macos/Sources/Features/Primary Window/PrimaryWindowController.swift index bd85972fc..eb99e0500 100644 --- a/macos/Sources/Features/Primary Window/PrimaryWindowController.swift +++ b/macos/Sources/Features/Primary Window/PrimaryWindowController.swift @@ -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() + } } diff --git a/macos/Sources/Features/Primary Window/PrimaryWindowManager.swift b/macos/Sources/Features/Primary Window/PrimaryWindowManager.swift index 7440a8bcb..52449d24c 100644 --- a/macos/Sources/Features/Primary Window/PrimaryWindowManager.swift +++ b/macos/Sources/Features/Primary Window/PrimaryWindowManager.swift @@ -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 + } + } + } }