From 4b25356625396e9da914e7225bc2ccec0fd4a749 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 7 Dec 2023 22:44:47 -0800 Subject: [PATCH] macos: manually show window to handle mission control behavior Fixes #1018 Fixes #1020 This disables the "visibleAtLaunch" configuration in the xib and manually shows the window when it loads. This lets us carefully control what happens particularly when a window is full screen (native) and part of Mission Control. Previously, the behavior depended on the Settings.app "Prefer tabs when opening documents" setting, but we didn't handle every behavior correctly (see #1018 and #1020). I couldn't find a way to robustly handle all cases because there are no published macOS APIs for interacting with Mission Control... Plus, terminals aren't really "documents" so it did confuse at least one user that Ghostty would follow this configuration. We just incidently did because we use native tabbing. This PR takes full control into our own hands. Our behavior is now: - If a new window is created from a native fullscreen window, the new window is created into native fullscreen. - If a new tab is created from a native fullscreen window, the tab is added to the existing window and does not create a new space. - If a window or tab is created from a non-fullscreen window, the existing behaviors remain. --- macos/Sources/Features/Terminal/Terminal.xib | 2 +- .../Features/Terminal/TerminalManager.swift | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/macos/Sources/Features/Terminal/Terminal.xib b/macos/Sources/Features/Terminal/Terminal.xib index b9e9a12b6..a086ffe89 100644 --- a/macos/Sources/Features/Terminal/Terminal.xib +++ b/macos/Sources/Features/Terminal/Terminal.xib @@ -13,7 +13,7 @@ - + diff --git a/macos/Sources/Features/Terminal/TerminalManager.swift b/macos/Sources/Features/Terminal/TerminalManager.swift index 6fce98e41..feefa9da9 100644 --- a/macos/Sources/Features/Terminal/TerminalManager.swift +++ b/macos/Sources/Features/Terminal/TerminalManager.swift @@ -63,16 +63,30 @@ class TerminalManager { /// Create a new terminal window. func newWindow(withBaseConfig base: Ghostty.SurfaceConfiguration? = nil) { let c = createWindow(withBaseConfig: base) - if let window = c.window { - Self.lastCascadePoint = window.cascadeTopLeft(from: Self.lastCascadePoint) + let window = c.window! + + // We want to go fullscreen if we're configured for new windows to go fullscreen + var toggleFullScreen = ghostty.windowFullscreen + + // If the previous focused window prior to creating this window is fullscreen, + // then this window also becomes fullscreen. + if let parent = focusedSurface?.window, parent.styleMask.contains(.fullScreen) { + toggleFullScreen = true } - if (ghostty.windowFullscreen) { - // NOTE: this doesn't properly handle non-native fullscreen yet - c.window?.toggleFullScreen(nil) + if (toggleFullScreen && !window.styleMask.contains(.fullScreen)) { + window.toggleFullScreen(nil) } c.showWindow(self) + + // Only cascade if we aren't fullscreen. This has to be dispatched async + // because it takes one event loop tick for showWindow to work. + if (!window.styleMask.contains(.fullScreen)) { + DispatchQueue.main.async { + Self.lastCascadePoint = window.cascadeTopLeft(from: Self.lastCascadePoint) + } + } } /// Creates a new tab in the current main window. If there are no windows, a window