mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
macos: attach gotoTab listener to contentView and filter on focused win
This commit is contained in:
@ -26,6 +26,9 @@ struct ContentView: View {
|
|||||||
NSApplication.shared.reply(toApplicationShouldTerminate: true)
|
NSApplication.shared.reply(toApplicationShouldTerminate: true)
|
||||||
}
|
}
|
||||||
case .ready:
|
case .ready:
|
||||||
|
let center = NotificationCenter.default
|
||||||
|
let gotoTab = center.publisher(for: Ghostty.Notification.ghosttyGotoTab)
|
||||||
|
|
||||||
let confirmQuitting = Binding<Bool>(get: {
|
let confirmQuitting = Binding<Bool>(get: {
|
||||||
self.appDelegate.confirmQuit && (self.window?.isKeyWindow ?? false)
|
self.appDelegate.confirmQuit && (self.window?.isKeyWindow ?? false)
|
||||||
}, set: {
|
}, set: {
|
||||||
@ -35,6 +38,7 @@ struct ContentView: View {
|
|||||||
Ghostty.TerminalSplit(onClose: Self.closeWindow)
|
Ghostty.TerminalSplit(onClose: Self.closeWindow)
|
||||||
.ghosttyApp(ghostty.app!)
|
.ghosttyApp(ghostty.app!)
|
||||||
.background(WindowAccessor(window: $window))
|
.background(WindowAccessor(window: $window))
|
||||||
|
.onReceive(gotoTab) { onGotoTab(notification: $0) }
|
||||||
.confirmationDialog(
|
.confirmationDialog(
|
||||||
"Quit Ghostty?",
|
"Quit Ghostty?",
|
||||||
isPresented: confirmQuitting) {
|
isPresented: confirmQuitting) {
|
||||||
@ -57,4 +61,27 @@ struct ContentView: View {
|
|||||||
guard let currentWindow = NSApp.keyWindow else { return }
|
guard let currentWindow = NSApp.keyWindow else { return }
|
||||||
currentWindow.close()
|
currentWindow.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func onGotoTab(notification: SwiftUI.Notification) {
|
||||||
|
// Notification center indiscriminately sends to every subscriber (makes sense)
|
||||||
|
// but we only want to process this once. In order to process it once lets only
|
||||||
|
// handle it if we're the focused window.
|
||||||
|
guard let window = self.window else { return }
|
||||||
|
guard window.isKeyWindow else { return }
|
||||||
|
|
||||||
|
// 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 windowController = window.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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,8 @@ struct GhosttyApp: App {
|
|||||||
@FocusedValue(\.ghosttySurfaceView) private var focusedSurface
|
@FocusedValue(\.ghosttySurfaceView) private var focusedSurface
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
let center = NotificationCenter.default
|
|
||||||
let gotoTab = center.publisher(for: Ghostty.Notification.ghosttyGotoTab)
|
|
||||||
|
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ContentView(ghostty: ghostty)
|
ContentView(ghostty: ghostty)
|
||||||
// TODO: This is wrong. This fires for every open tab.
|
|
||||||
.onReceive(gotoTab) { onGotoTab(notification: $0) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.backport.defaultSize(width: 800, height: 600)
|
.backport.defaultSize(width: 800, height: 600)
|
||||||
@ -107,26 +102,6 @@ struct GhosttyApp: App {
|
|||||||
guard let surface = surfaceView.surface else { return }
|
guard let surface = surfaceView.surface else { return }
|
||||||
ghostty.splitMoveFocus(surface: surface, direction: direction)
|
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 {
|
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
|
||||||
|
Reference in New Issue
Block a user