macOS: implement cmd+[0-9] to goto tab

This is my attempt at fixing #63. It works! But:

1. The `NotificationCenter` subscription is triggered once for every
   open tab. That's obviously wrong. But I'm not sure and could use some
   pointers where else to put the subscription. That leads me to...
2. I'm _not_ knowledgable in Swift/AppKit/SwiftUI, so I might have put
   the wrong/right things in the wrong/right places. For example: wasn't
   sure what's to be handled in Swift and what's to be handled by the
   core in Zig.

Would love some pointers :)
This commit is contained in:
Thorsten Ball
2023-07-01 17:12:55 +02:00
committed by Mitchell Hashimoto
parent cb775b5d56
commit 6ff3f62e3a
5 changed files with 58 additions and 1 deletions

View File

@@ -17,10 +17,17 @@ struct GhosttyApp: App {
@FocusedValue(\.ghosttySurfaceView) private var focusedSurface
var body: some Scene {
let center = NotificationCenter.default
let gotoTab = center.publisher(for: Ghostty.Notification.ghosttyGotoTab)
WindowGroup {
ContentView(ghostty: ghostty)
// TODO: This is wrong. This fires for every open tab.
.onReceive(gotoTab) { onGotoTab(notification: $0) }
}
.backport.defaultSize(width: 800, height: 600)
.commands {
CommandGroup(after: .newItem) {
Button("New Tab", action: Self.newTab).keyboardShortcut("t", modifiers: [.command])
@@ -100,6 +107,26 @@ struct GhosttyApp: App {
guard let surface = surfaceView.surface else { return }
ghostty.splitMoveFocus(surface: surface, direction: direction)
}
private func onGotoTab(notification: SwiftUI.Notification) {
// Get the tab index from the notification
guard let tabIndexAny = notification.userInfo?[Ghostty.Notification.GotoTabKey] else { return }
guard let tabIndex = tabIndexAny as? Int32 else { return }
guard let currentWindow = NSApp.keyWindow else { return }
guard let windowController = currentWindow.windowController else { return }
guard let tabGroup = windowController.window?.tabGroup else { return }
let tabbedWindows = tabGroup.windows
// Tabs are 0-indexed here, so we subtract one from the key the user hit.
let adjustedIndex = Int(tabIndex - 1);
guard adjustedIndex >= 0 && adjustedIndex < tabbedWindows.count else { return }
let targetWindow = tabbedWindows[adjustedIndex]
targetWindow.makeKeyAndOrderFront(nil)
}
}
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {