mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-06 07:38:21 +00:00
macOS: restore window frame under certain conditions
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user