diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index a102beb91..eb9734f6c 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -706,20 +706,42 @@ class AppDelegate: NSObject, /// Toggles visibility of all Ghosty Terminal windows. When hidden, activates Ghostty as the frontmost application @IBAction func toggleVisibility(_ sender: Any) { - // We only care about terminal windows. - for window in NSApp.windows.filter({ $0.windowController is BaseTerminalController }) { - if isVisible { - window.orderOut(nil) - } else { - window.makeKeyAndOrderFront(nil) + if let mainWindow = terminalManager.mainWindow { + guard let parent = mainWindow.controller.window else { + Self.logger.debug("could not get parent window") + return + } + + guard let controller = parent.windowController as? TerminalController, + let primaryWindow = controller.window else { + Self.logger.debug("Could not retrieve primary window") + return + } + + // Fetch all terminal windows controlled by BaseTerminalController + for terminalWindow in NSApp.windows.filter({ $0.windowController is BaseTerminalController }) { + if isVisible { + terminalWindow.orderOut(nil) + } else { + primaryWindow.makeKeyAndOrderFront(nil) + primaryWindow.addTabbedWindow(terminalWindow, ordered: .above) + } + } + + // If our parent tab group already has this window, macOS added it and + // we need to remove it so we can set the correct order in the next line. + // If we don't do this, macOS gets really confused and the tabbedWindows + // state becomes incorrect. + if let tg = parent.tabGroup, tg.windows.firstIndex(of: parent) != nil { + tg.removeWindow(parent) } } - + // After bringing them all to front we make sure our app is active too. if !isVisible { NSApp.activate(ignoringOtherApps: true) } - + isVisible.toggle() } diff --git a/macos/Sources/Features/Terminal/TerminalManager.swift b/macos/Sources/Features/Terminal/TerminalManager.swift index 42e35b90e..82a5978c7 100644 --- a/macos/Sources/Features/Terminal/TerminalManager.swift +++ b/macos/Sources/Features/Terminal/TerminalManager.swift @@ -26,7 +26,7 @@ class TerminalManager { /// Returns the main window of the managed window stack. If there is no window /// then an arbitrary window will be chosen. - private var mainWindow: Window? { + var mainWindow: Window? { for window in windows { if (window.controller.window?.isMainWindow ?? false) { return window