From 1ec3e331dedaa4db92d5556e1eec0a31fac17bd5 Mon Sep 17 00:00:00 2001 From: Aaron Ruan Date: Sun, 27 Apr 2025 08:48:06 +0800 Subject: [PATCH] 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