From 596d502a756ce6454093b5d0782bc17d700804ab Mon Sep 17 00:00:00 2001 From: Lukas <134181853+bo2themax@users.noreply.github.com> Date: Wed, 11 Mar 2026 17:37:16 +0100 Subject: [PATCH] macOS: restore window frame under certain conditions --- .../Terminal/TerminalController.swift | 7 ++++++ .../Window Styles/TerminalWindow.swift | 5 +---- .../Sources/Helpers/LastWindowPosition.swift | 22 +++++++++++++++---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index 749e5b472..74b73ea00 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -1071,6 +1071,13 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr y: derivedConfig.windowPositionY, ) } + + LastWindowPosition.shared.restore( + window, + origin: derivedConfig.windowPositionX == nil && derivedConfig.windowPositionY == nil, + size: defaultSize == nil, + ) + // Store our initial frame so we can know our default later. This MUST // be after the defaultSize call above so that we don't re-apply our frame. // Note: we probably want to set this on the first frame change or something diff --git a/macos/Sources/Features/Terminal/Window Styles/TerminalWindow.swift b/macos/Sources/Features/Terminal/Window Styles/TerminalWindow.swift index b9dd3b10b..560f45207 100644 --- a/macos/Sources/Features/Terminal/Window Styles/TerminalWindow.swift +++ b/macos/Sources/Features/Terminal/Window Styles/TerminalWindow.swift @@ -539,10 +539,7 @@ class TerminalWindow: NSWindow { func setInitialWindowPosition(x: Int16?, y: Int16?) { // If we don't have an X/Y then we try to use the previously saved window pos. guard let x = x, let y = y else { - if !LastWindowPosition.shared.restore(self) { - center() - } - + center() return } diff --git a/macos/Sources/Helpers/LastWindowPosition.swift b/macos/Sources/Helpers/LastWindowPosition.swift index 3395e07f0..933eba394 100644 --- a/macos/Sources/Helpers/LastWindowPosition.swift +++ b/macos/Sources/Helpers/LastWindowPosition.swift @@ -19,7 +19,19 @@ class LastWindowPosition { return true } - func restore(_ window: NSWindow) -> Bool { + /// Restores a previously saved window frame (or parts of it) onto the given window. + /// + /// - Parameters: + /// - window: The window whose frame should be updated. + /// - restoreOrigin: Whether to restore the saved position. Pass `false` when the + /// config specifies an explicit `window-position-x`/`window-position-y`. + /// - restoreSize: Whether to restore the saved size. Pass `false` when the config + /// specifies an explicit `window-width`/`window-height`. + /// - Returns: `true` if the frame was modified, `false` if there was nothing to restore. + @discardableResult + func restore(_ window: NSWindow, origin restoreOrigin: Bool = true, size restoreSize: Bool = true) -> Bool { + guard restoreOrigin || restoreSize else { return false } + guard let values = UserDefaults.standard.array(forKey: positionKey) as? [Double], values.count >= 2 else { return false } @@ -29,14 +41,16 @@ class LastWindowPosition { let visibleFrame = screen.visibleFrame var newFrame = window.frame - newFrame.origin = lastPosition + if restoreOrigin { + newFrame.origin = lastPosition + } - if values.count >= 4 { + if restoreSize, values.count >= 4 { newFrame.size.width = min(values[2], visibleFrame.width) newFrame.size.height = min(values[3], visibleFrame.height) } - if !visibleFrame.contains(newFrame.origin) { + if restoreOrigin, !visibleFrame.contains(newFrame.origin) { newFrame.origin.x = max(visibleFrame.minX, min(visibleFrame.maxX - newFrame.width, newFrame.origin.x)) newFrame.origin.y = max(visibleFrame.minY, min(visibleFrame.maxY - newFrame.height, newFrame.origin.y)) }