From df7120d13064bd74c29100c51f4baa6f1fe47353 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 23 Oct 2024 10:13:35 -0700 Subject: [PATCH] macos: use notification to detect when quick terminal shows/hides Fixes #2474 --- macos/Sources/App/macOS/AppDelegate.swift | 15 +++++++++++++-- .../QuickTerminal/QuickTerminalController.swift | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index 8c1f392d6..8179e1950 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -155,6 +155,14 @@ class AppDelegate: NSObject, matching: [.keyDown], handler: localEventHandler) + // Notifications + NotificationCenter.default.addObserver( + self, + selector: #selector(quickTerminalDidChangeVisibility), + name: .quickTerminalDidChangeVisibility, + object: nil + ) + // Configure user notifications let actions = [ UNNotificationAction(identifier: Ghostty.userNotificationActionShow, title: "Show") @@ -409,6 +417,11 @@ class AppDelegate: NSObject, return event } + @objc private func quickTerminalDidChangeVisibility(_ notification: Notification) { + guard let quickController = notification.object as? QuickTerminalController else { return } + self.menuQuickTerminal?.state = if (quickController.visible) { .on } else { .off } + } + //MARK: - Restorable State /// We support NSSecureCoding for restorable state. Required as of macOS Sonoma (14) but a good idea anyways. @@ -622,8 +635,6 @@ class AppDelegate: NSObject, guard let quickController = self.quickController else { return } quickController.toggle() - - self.menuQuickTerminal?.state = if (quickController.visible) { .on } else { .off } } /// Toggles visibility of all Ghosty Terminal windows. When hidden, activates Ghostty as the frontmost application diff --git a/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift b/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift index 13b69d000..959f17d4a 100644 --- a/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift +++ b/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift @@ -140,6 +140,12 @@ class QuickTerminalController: BaseTerminalController { guard !visible else { return } visible = true + // Notify the change + NotificationCenter.default.post( + name: .quickTerminalDidChangeVisibility, + object: self + ) + // If we have a previously focused application and it isn't us, then // we want to store it so we can restore state later. if !NSApp.isActive { @@ -170,6 +176,12 @@ class QuickTerminalController: BaseTerminalController { guard visible else { return } visible = false + // Notify the change + NotificationCenter.default.post( + name: .quickTerminalDidChangeVisibility, + object: self + ) + animateWindowOut(window: window, to: position) } @@ -350,3 +362,8 @@ class QuickTerminalController: BaseTerminalController { syncAppearance() } } + +extension Notification.Name { + /// The quick terminal did become hidden or visible. + static let quickTerminalDidChangeVisibility = Notification.Name("QuickTerminalDidChangeVisibility") +}