macOS: restore window frame under certain conditions

This commit is contained in:
Lukas
2026-03-11 17:37:16 +01:00
parent 45d360dc68
commit 596d502a75
3 changed files with 26 additions and 8 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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))
}