From c8a40a7791ff121425b1696891174627563a7370 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 29 Sep 2024 14:04:48 -0700 Subject: [PATCH] macos: quick terminal close focuses next window on same screen/space Previously, we'd find the next Ghostty window anywhere. Now we find the one on the same screen/space to avoid moving the focus to a different screen. --- .../QuickTerminalController.swift | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift b/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift index 3f4444fd9..c7e833499 100644 --- a/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift +++ b/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift @@ -177,13 +177,24 @@ class QuickTerminalController: BaseTerminalController { position.setInitial(in: window.animator(), on: screen) }, completionHandler: { guard wasKey else { return } - self.focusNextWindow() + self.focusNextWindow(on: screen) }) } - private func focusNextWindow() { - // We only want to consider windows that are visible - let windows = NSApp.windows.filter { $0.isVisible } + private func focusNextWindow(on screen: NSScreen) { + let windows = NSApp.windows.filter { + // Visible, otherwise we'll make an invisible window visible. + guard $0.isVisible else { return false } + + // Same screen, just a preference... + guard $0.screen == screen else { return false } + + // Same space (virtual screen). Otherwise we'll force an animation to + // another space which is very jarring. + guard $0.isOnActiveSpace else { return false } + + return true + } // If we have no windows there is nothing to focus. guard !windows.isEmpty else { return }