From 334093a9ea1ea91d6ae3c562cb182969fe5576fa Mon Sep 17 00:00:00 2001 From: Aaron Ruan Date: Fri, 25 Apr 2025 15:59:44 +0800 Subject: [PATCH 1/3] feat: implement toggleMaximize for macOS --- macos/Sources/App/macOS/AppDelegate.swift | 6 ++++++ .../Command Palette/TerminalCommandPalette.swift | 1 - macos/Sources/Ghostty/Ghostty.App.swift | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index 682099e92..8849ddb75 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -807,6 +807,12 @@ class AppDelegate: NSObject, setSecureInput(.toggle) } + @IBAction func toggleMaximize(_ sender: Any) { + if NSApp.isActive, let keyWindow = NSApp.keyWindow { + keyWindow.zoom(self) + } + } + @IBAction func toggleQuickTerminal(_ sender: Any) { if quickController == nil { quickController = QuickTerminalController( diff --git a/macos/Sources/Features/Command Palette/TerminalCommandPalette.swift b/macos/Sources/Features/Command Palette/TerminalCommandPalette.swift index 2e895d4d9..7820e45fa 100644 --- a/macos/Sources/Features/Command Palette/TerminalCommandPalette.swift +++ b/macos/Sources/Features/Command Palette/TerminalCommandPalette.swift @@ -29,7 +29,6 @@ struct TerminalCommandPaletteView: View { let key = String(cString: c.action_key) switch (key) { case "toggle_tab_overview", - "toggle_maximize", "toggle_window_decorations": return false default: diff --git a/macos/Sources/Ghostty/Ghostty.App.swift b/macos/Sources/Ghostty/Ghostty.App.swift index 677129960..8f2624df4 100644 --- a/macos/Sources/Ghostty/Ghostty.App.swift +++ b/macos/Sources/Ghostty/Ghostty.App.swift @@ -523,6 +523,9 @@ extension Ghostty { case GHOSTTY_ACTION_TOGGLE_COMMAND_PALETTE: toggleCommandPalette(app, target: target) + case GHOSTTY_ACTION_TOGGLE_MAXIMIZE: + toggleMaximize(app, target: target) + case GHOSTTY_ACTION_TOGGLE_QUICK_TERMINAL: toggleQuickTerminal(app, target: target) @@ -767,6 +770,14 @@ extension Ghostty { } } + private static func toggleMaximize( + _ app: ghostty_app_t, + target: ghostty_target_s + ) { + guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return } + appDelegate.toggleMaximize(self) + } + private static func toggleVisibility( _ app: ghostty_app_t, target: ghostty_target_s From 1ec3e331dedaa4db92d5556e1eec0a31fac17bd5 Mon Sep 17 00:00:00 2001 From: Aaron Ruan Date: Sun, 27 Apr 2025 08:48:06 +0800 Subject: [PATCH 2/3] Refactor toggleMaximize to use notifications --- macos/Sources/App/macOS/AppDelegate.swift | 6 ------ .../Terminal/BaseTerminalController.swift | 10 ++++++++++ macos/Sources/Ghostty/Ghostty.App.swift | 19 +++++++++++++++++-- macos/Sources/Ghostty/Package.swift | 3 +++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index 8849ddb75..682099e92 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -807,12 +807,6 @@ class AppDelegate: NSObject, setSecureInput(.toggle) } - @IBAction func toggleMaximize(_ sender: Any) { - if NSApp.isActive, let keyWindow = NSApp.keyWindow { - keyWindow.zoom(self) - } - } - @IBAction func toggleQuickTerminal(_ sender: Any) { if quickController == nil { quickController = QuickTerminalController( diff --git a/macos/Sources/Features/Terminal/BaseTerminalController.swift b/macos/Sources/Features/Terminal/BaseTerminalController.swift index b502e56e0..59cef503d 100644 --- a/macos/Sources/Features/Terminal/BaseTerminalController.swift +++ b/macos/Sources/Features/Terminal/BaseTerminalController.swift @@ -115,6 +115,11 @@ class BaseTerminalController: NSWindowController, selector: #selector(ghosttyCommandPaletteDidToggle(_:)), name: .ghosttyCommandPaletteDidToggle, object: nil) + center.addObserver( + self, + selector: #selector(toggleMaximize), + name: .ghosttyToggleMaximize, + object: nil) // Listen for local events that we need to know of outside of // single surface handlers. @@ -548,6 +553,11 @@ class BaseTerminalController: NSWindowController, window.performClose(sender) } + @IBAction func toggleMaximize(_ sender: Any) { + guard let window = window else { return } + window.zoom(self) + } + @IBAction func splitRight(_ sender: Any) { guard let surface = focusedSurface?.surface else { return } ghostty.split(surface: surface, direction: GHOSTTY_SPLIT_DIRECTION_RIGHT) diff --git a/macos/Sources/Ghostty/Ghostty.App.swift b/macos/Sources/Ghostty/Ghostty.App.swift index 8f2624df4..e72acc05e 100644 --- a/macos/Sources/Ghostty/Ghostty.App.swift +++ b/macos/Sources/Ghostty/Ghostty.App.swift @@ -774,8 +774,23 @@ extension Ghostty { _ app: ghostty_app_t, target: ghostty_target_s ) { - guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return } - appDelegate.toggleMaximize(self) + switch (target.tag) { + case GHOSTTY_TARGET_APP: + Ghostty.logger.warning("toggle maximize 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 } + NotificationCenter.default.post( + name: .ghosttyToggleMaximize, + object: surfaceView + ) + + + default: + assertionFailure() + } } private static func toggleVisibility( diff --git a/macos/Sources/Ghostty/Package.swift b/macos/Sources/Ghostty/Package.swift index e2c770899..ee0073aa1 100644 --- a/macos/Sources/Ghostty/Package.swift +++ b/macos/Sources/Ghostty/Package.swift @@ -257,6 +257,9 @@ extension Notification.Name { /// Ring the bell static let ghosttyBellDidRing = Notification.Name("com.mitchellh.ghostty.ghosttyBellDidRing") static let ghosttyCommandPaletteDidToggle = Notification.Name("com.mitchellh.ghostty.commandPaletteDidToggle") + + /// Toggle maximize of current window + static let ghosttyToggleMaximize = Notification.Name("com.mitchellh.ghostty.toggleMaximize") } // NOTE: I am moving all of these to Notification.Name extensions over time. This From 13f776d483d5120e6b7fab7b04ed2fd5894e77e6 Mon Sep 17 00:00:00 2001 From: Aaron Ruan Date: Mon, 28 Apr 2025 10:20:29 +0800 Subject: [PATCH 3/3] Rename maximize notification and refine handler --- .../Terminal/BaseTerminalController.swift | 16 +++++++++------- macos/Sources/Ghostty/Ghostty.App.swift | 2 +- macos/Sources/Ghostty/Package.swift | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/macos/Sources/Features/Terminal/BaseTerminalController.swift b/macos/Sources/Features/Terminal/BaseTerminalController.swift index 59cef503d..d6b0433ac 100644 --- a/macos/Sources/Features/Terminal/BaseTerminalController.swift +++ b/macos/Sources/Features/Terminal/BaseTerminalController.swift @@ -117,8 +117,8 @@ class BaseTerminalController: NSWindowController, object: nil) center.addObserver( self, - selector: #selector(toggleMaximize), - name: .ghosttyToggleMaximize, + selector: #selector(ghosttyMaximizeDidToggle(_:)), + name: .ghosttyMaximizeDidToggle, object: nil) // Listen for local events that we need to know of outside of @@ -239,6 +239,13 @@ class BaseTerminalController: NSWindowController, toggleCommandPalette(nil) } + @objc private func ghosttyMaximizeDidToggle(_ notification: Notification) { + guard let window else { return } + guard let surfaceView = notification.object as? Ghostty.SurfaceView else { return } + guard surfaceTree?.contains(view: surfaceView) ?? false else { return } + window.zoom(nil) + } + // MARK: Local Events private func localEventHandler(_ event: NSEvent) -> NSEvent? { @@ -553,11 +560,6 @@ class BaseTerminalController: NSWindowController, window.performClose(sender) } - @IBAction func toggleMaximize(_ sender: Any) { - guard let window = window else { return } - window.zoom(self) - } - @IBAction func splitRight(_ sender: Any) { guard let surface = focusedSurface?.surface else { return } ghostty.split(surface: surface, direction: GHOSTTY_SPLIT_DIRECTION_RIGHT) diff --git a/macos/Sources/Ghostty/Ghostty.App.swift b/macos/Sources/Ghostty/Ghostty.App.swift index e72acc05e..f7f5f475c 100644 --- a/macos/Sources/Ghostty/Ghostty.App.swift +++ b/macos/Sources/Ghostty/Ghostty.App.swift @@ -783,7 +783,7 @@ extension Ghostty { guard let surface = target.target.surface else { return } guard let surfaceView = self.surfaceView(from: surface) else { return } NotificationCenter.default.post( - name: .ghosttyToggleMaximize, + name: .ghosttyMaximizeDidToggle, object: surfaceView ) diff --git a/macos/Sources/Ghostty/Package.swift b/macos/Sources/Ghostty/Package.swift index ee0073aa1..80223776c 100644 --- a/macos/Sources/Ghostty/Package.swift +++ b/macos/Sources/Ghostty/Package.swift @@ -259,7 +259,7 @@ extension Notification.Name { static let ghosttyCommandPaletteDidToggle = Notification.Name("com.mitchellh.ghostty.commandPaletteDidToggle") /// Toggle maximize of current window - static let ghosttyToggleMaximize = Notification.Name("com.mitchellh.ghostty.toggleMaximize") + static let ghosttyMaximizeDidToggle = Notification.Name("com.mitchellh.ghostty.maximizeDidToggle") } // NOTE: I am moving all of these to Notification.Name extensions over time. This