Refactor toggleMaximize to use notifications

This commit is contained in:
Aaron Ruan
2025-04-27 08:48:06 +08:00
parent 334093a9ea
commit 1ec3e331de
4 changed files with 30 additions and 8 deletions

View File

@@ -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(

View File

@@ -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)

View File

@@ -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(

View File

@@ -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