From 8838ebf02a3e40b1fbf4d06db361516620ef5ec4 Mon Sep 17 00:00:00 2001 From: Mikhail Borisov Date: Tue, 25 Feb 2025 21:47:33 +0100 Subject: [PATCH] Refactor to use height/width from ghostty configuration --- macos/Sources/App/macOS/AppDelegate.swift | 34 -------------- .../Terminal/TerminalController.swift | 46 +++++++++++++++++++ macos/Sources/Ghostty/Ghostty.Config.swift | 16 ++++++- 3 files changed, 61 insertions(+), 35 deletions(-) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index ad48e24de..7caa04659 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -753,16 +753,6 @@ class AppDelegate: NSObject, quickController.toggle() } - @IBAction func returnToDefaultSize(_ sender: Any) { - guard let window = NSApp.keyWindow else { return } - let currentOrigin = window.frame.origin - let newFrame = NSRect( - origin: currentOrigin, - size: WindowConfig.defaultSize - ) - window.setFrame(newFrame, display: true) - } - /// Toggles visibility of all Ghosty Terminal windows. When hidden, activates Ghostty as the frontmost application @IBAction func toggleVisibility(_ sender: Any) { // If we have focus, then we hide all windows. @@ -814,10 +804,6 @@ class AppDelegate: NSObject, } } - private enum WindowConfig { - static let defaultSize = CGSize(width: 800, height: 600) - } - private struct ToggleVisibilityState { let hiddenWindows: [Weak] let keyWindow: Weak? @@ -847,23 +833,3 @@ class AppDelegate: NSObject, } } } - -extension AppDelegate: NSMenuItemValidation { - func validateMenuItem(_ item: NSMenuItem) -> Bool { - switch item.action { - case #selector(returnToDefaultSize): - guard let focusedWindow = NSApp.keyWindow else { - return false - } - if focusedWindow.styleMask.contains(.fullScreen) { - return false - } - if focusedWindow.frame.size == WindowConfig.defaultSize { - return false - } - return true - default: - return true - } - } -} diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index d7ddb793e..19fc0abfc 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -578,6 +578,23 @@ class TerminalController: BaseTerminalController { window.close() } + @IBAction func returnToDefaultSize(_ sender: Any) { + guard + let window = NSApp.keyWindow, + let initialWidth = ghostty.config.windowWidth, + let initialHeight = ghostty.config.windowHeight + else { return } + let currentOrigin = window.frame.origin + let newFrame = NSRect( + origin: currentOrigin, + size: .init( + width: CGFloat(initialWidth), + height: CGFloat(initialHeight) + ) + ) + window.setFrame(newFrame, display: true) + } + @IBAction override func closeWindow(_ sender: Any?) { guard let window = window else { return } guard let tabGroup = window.tabGroup else { @@ -823,3 +840,32 @@ class TerminalController: BaseTerminalController { } } } + + +extension TerminalController: NSMenuItemValidation { + func validateMenuItem(_ item: NSMenuItem) -> Bool { + switch item.action { + case #selector(returnToDefaultSize): + guard let focusedWindow = NSApp.keyWindow else { + return false + } + if focusedWindow.styleMask.contains(.fullScreen) { + return false + } + guard + let windowWidth = ghostty.config.windowWidth, + let windowHeight = ghostty.config.windowHeight, + focusedWindow.frame.size != .init( + width: CGFloat(windowWidth), + height: CGFloat(windowHeight) + ) + else { + return false + } + return true + default: + return true + } + } +} + diff --git a/macos/Sources/Ghostty/Ghostty.Config.swift b/macos/Sources/Ghostty/Ghostty.Config.swift index f30174b8e..70f21995f 100644 --- a/macos/Sources/Ghostty/Ghostty.Config.swift +++ b/macos/Sources/Ghostty/Ghostty.Config.swift @@ -140,7 +140,21 @@ extension Ghostty { guard let ptr = v else { return "" } return String(cString: ptr) } - + + var windowHeight: Int16? { + guard let config = self.config else { return nil } + var v: Int16 = 0 + let key = "window-height" + return ghostty_config_get(config, &v, key, UInt(key.count)) ? v : nil + } + + var windowWidth: Int16? { + guard let config = self.config else { return nil } + var v: Int16 = 0 + let key = "window-width" + return ghostty_config_get(config, &v, key, UInt(key.count)) ? v : nil + } + var windowPositionX: Int16? { guard let config = self.config else { return nil } var v: Int16 = 0