macOS: save frame only if the window is visible

This commit is contained in:
Lukas
2026-03-11 17:33:37 +01:00
parent 0af9938ad2
commit e8c82ca1af
2 changed files with 11 additions and 10 deletions

View File

@@ -1171,27 +1171,21 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr
self.fixTabBar()
// Whenever we move save our last position for the next start.
if let window {
LastWindowPosition.shared.save(window)
}
LastWindowPosition.shared.save(window)
}
override func windowDidResize(_ notification: Notification) {
super.windowDidResize(notification)
// Whenever we resize save our last position and size for the next start.
if let window {
LastWindowPosition.shared.save(window)
}
LastWindowPosition.shared.save(window)
}
func windowDidBecomeMain(_ notification: Notification) {
// Whenever we get focused, use that as our last window position for
// restart. This differs from Terminal.app but matches iTerm2 behavior
// and I think its sensible.
if let window {
LastWindowPosition.shared.save(window)
}
LastWindowPosition.shared.save(window)
// Remember our last main
Self.lastMain = self

View File

@@ -6,10 +6,17 @@ class LastWindowPosition {
private let positionKey = "NSWindowLastPosition"
func save(_ window: NSWindow) {
@discardableResult
func save(_ window: NSWindow?) -> Bool {
// We should only save the frame if the window is visible.
// This avoids overriding the previously saved one
// with the wrong one when window decorations change while creating,
// e.g. adding a toolbar affects the window's frame.
guard let window, window.isVisible else { return false }
let frame = window.frame
let rect = [frame.origin.x, frame.origin.y, frame.size.width, frame.size.height]
UserDefaults.standard.set(rect, forKey: positionKey)
return true
}
func restore(_ window: NSWindow) -> Bool {