mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-25 13:16:11 +03:00
macos: handle setting initial window position when window is created
This commit is contained in:

committed by
Mitchell Hashimoto

parent
970e45559b
commit
200d0d642b
@ -368,9 +368,9 @@ class TerminalController: BaseTerminalController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Center the window to start, we'll move the window frame automatically
|
// Set our window positioning to coordinates if config value exists, otherwise
|
||||||
// when cascading.
|
// fallback to original centering behavior
|
||||||
window.center()
|
setInitialWindowPosition(window, x: config.windowPositionX, y: config.windowPositionY, windowDecorations: config.windowDecorations)
|
||||||
|
|
||||||
// Make sure our theme is set on the window so styling is correct.
|
// Make sure our theme is set on the window so styling is correct.
|
||||||
if let windowTheme = config.windowTheme {
|
if let windowTheme = config.windowTheme {
|
||||||
@ -469,6 +469,31 @@ class TerminalController: BaseTerminalController {
|
|||||||
data.encode(with: state)
|
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
|
// MARK: First Responder
|
||||||
|
|
||||||
@IBAction func newWindow(_ sender: Any?) {
|
@IBAction func newWindow(_ sender: Any?) {
|
||||||
|
@ -517,9 +517,6 @@ extension Ghostty {
|
|||||||
case GHOSTTY_ACTION_INITIAL_SIZE:
|
case GHOSTTY_ACTION_INITIAL_SIZE:
|
||||||
setInitialSize(app, target: target, v: action.action.initial_size)
|
setInitialSize(app, target: target, v: action.action.initial_size)
|
||||||
|
|
||||||
case GHOSTTY_ACTION_INITIAL_POSITION:
|
|
||||||
setInitialPosition(app, target: target, v: action.action.initial_position)
|
|
||||||
|
|
||||||
case GHOSTTY_ACTION_CELL_SIZE:
|
case GHOSTTY_ACTION_CELL_SIZE:
|
||||||
setCellSize(app, target: target, v: action.action.cell_size)
|
setCellSize(app, target: target, v: action.action.cell_size)
|
||||||
|
|
||||||
@ -1072,26 +1069,6 @@ extension Ghostty {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func setInitialPosition(
|
|
||||||
_ app: ghostty_app_t,
|
|
||||||
target: ghostty_target_s,
|
|
||||||
v: ghostty_action_initial_position_s) {
|
|
||||||
switch (target.tag) {
|
|
||||||
case GHOSTTY_TARGET_APP:
|
|
||||||
Ghostty.logger.warning("mouse over link does nothing with an app target")
|
|
||||||
return
|
|
||||||
|
|
||||||
case GHOSTTY_TARGET_SURFACE:
|
|
||||||
guard let surface = target.target.surface else { return }
|
|
||||||
guard let surfaceView = self.surfaceView(from: surface) else { return }
|
|
||||||
surfaceView.initialPosition = NSMakePoint(Double(v.x), Double(v.y))
|
|
||||||
|
|
||||||
|
|
||||||
default:
|
|
||||||
assertionFailure()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func setCellSize(
|
private static func setCellSize(
|
||||||
_ app: ghostty_app_t,
|
_ app: ghostty_app_t,
|
||||||
target: ghostty_target_s,
|
target: ghostty_target_s,
|
||||||
|
@ -150,6 +150,20 @@ extension Ghostty {
|
|||||||
return String(cString: ptr)
|
return String(cString: ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var windowPositionX: Int16? {
|
||||||
|
guard let config = self.config else { return nil }
|
||||||
|
var v: Int16 = 0
|
||||||
|
let key = "window-position-x"
|
||||||
|
return ghostty_config_get(config, &v, key, UInt(key.count)) ? v : nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var windowPositionY: Int16? {
|
||||||
|
guard let config = self.config else { return nil }
|
||||||
|
var v: Int16 = 0
|
||||||
|
let key = "window-position-y"
|
||||||
|
return ghostty_config_get(config, &v, key, UInt(key.count)) ? v : nil
|
||||||
|
}
|
||||||
|
|
||||||
var windowNewTabPosition: String {
|
var windowNewTabPosition: String {
|
||||||
guard let config = self.config else { return "" }
|
guard let config = self.config else { return "" }
|
||||||
var v: UnsafePointer<Int8>? = nil
|
var v: UnsafePointer<Int8>? = nil
|
||||||
|
@ -42,6 +42,11 @@ fn getValue(ptr_raw: *anyopaque, value: anytype) bool {
|
|||||||
ptr.* = @intCast(value);
|
ptr.* = @intCast(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
i16 => {
|
||||||
|
const ptr: *c_short = @ptrCast(@alignCast(ptr_raw));
|
||||||
|
ptr.* = @intCast(value);
|
||||||
|
},
|
||||||
|
|
||||||
f32, f64 => |Float| {
|
f32, f64 => |Float| {
|
||||||
const ptr: *Float = @ptrCast(@alignCast(ptr_raw));
|
const ptr: *Float = @ptrCast(@alignCast(ptr_raw));
|
||||||
ptr.* = @floatCast(value);
|
ptr.* = @floatCast(value);
|
||||||
|
Reference in New Issue
Block a user