tweaks to window position

This commit is contained in:
Mitchell Hashimoto
2025-01-02 13:04:30 -08:00
parent f49a029c49
commit 29b96be84f
4 changed files with 61 additions and 46 deletions

View File

@@ -273,6 +273,28 @@ class TerminalController: BaseTerminalController {
}
}
private func setInitialWindowPosition(x: Int16?, y: Int16?, windowDecorations: Bool) {
guard let window else { return }
// If we don't have both an X and Y we center.
guard let x, let y else {
window.center()
return
}
// Prefer the screen our window is being placed on otherwise our primary screen.
guard let screen = window.screen ?? NSScreen.screens.first else {
window.center()
return
}
// Orient based on the top left of the primary monitor
let frame = screen.visibleFrame
window.setFrameOrigin(.init(
x: frame.minX + CGFloat(x),
y: frame.maxY - (CGFloat(y) + window.frame.height)))
}
//MARK: - NSWindowController
override func windowWillLoad() {
@@ -370,8 +392,11 @@ class TerminalController: BaseTerminalController {
// Set our window positioning to coordinates if config value exists, otherwise
// fallback to original centering behavior
setInitialWindowPosition(window, x: config.windowInitialPositionX, y: config.windowInitialPositionY, windowDecorations: config.windowDecorations)
setInitialWindowPosition(
x: config.windowPositionX,
y: config.windowPositionY,
windowDecorations: config.windowDecorations)
// Make sure our theme is set on the window so styling is correct.
if let windowTheme = config.windowTheme {
window.windowTheme = .init(rawValue: windowTheme)
@@ -468,31 +493,6 @@ class TerminalController: BaseTerminalController {
let data = TerminalRestorableState(from: self)
data.encode(with: state)
}
func setInitialWindowPosition(_ window: NSWindow, x: Int16?, y: Int16?, windowDecorations: Bool) {
if let primaryScreen = NSScreen.screens.first {
let frame = primaryScreen.visibleFrame
if let windowPositionX = x, let windowPositionY = y {
// Offset titlebar if needed, otherwise use default padding of 12
// NOTE: Not 100% certain where this extra padding comes from but I'd love
// to calculate it dynamically if possible
let titlebarHeight = windowDecorations ? window.frame.height - (window.contentView?.frame.height ?? 0) : 12
// Orient based on the top left of the primary monitor
let startPositionX = frame.origin.x + CGFloat(windowPositionX)
let startPositionY = (frame.origin.y + frame.height) - (CGFloat(windowPositionY) + window.frame.height) + titlebarHeight
window.setFrameOrigin(NSPoint(x: startPositionX, y: startPositionY))
} else {
// Fallback to original centering behavior
window.center()
}
} else {
// Fallback to original centering behavior
window.center()
}
}
// MARK: First Responder

View File

@@ -150,17 +150,17 @@ extension Ghostty {
return String(cString: ptr)
}
var windowInitialPositionX: Int16? {
var windowPositionX: Int16? {
guard let config = self.config else { return nil }
var v: Int16 = 0
let key = "window-initial-position-x"
let key = "window-position-x"
return ghostty_config_get(config, &v, key, UInt(key.count)) ? v : nil
}
var windowInitialPositionY: Int16? {
var windowPositionY: Int16? {
guard let config = self.config else { return nil }
var v: Int16 = 0
let key = "window-initial-position-y"
let key = "window-position-y"
return ghostty_config_get(config, &v, key, UInt(key.count)) ? v : nil
}