| | |
| | | |
| | | // MARK: - App Delegate (menubar + overlay window + hotkey) |
| | | |
| | | private enum HotkeyRegistrationAttempt { |
| | | case success(EventHotKeyRef) |
| | | case failure(OSStatus) |
| | | } |
| | | |
| | | final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { |
| | | private static let showDockIconKey = "showDockIcon" |
| | | private static let statusItemAutosaveName = AppIdentity.statusItemAutosaveName |
| | |
| | | observeApplicationMenuChanges() |
| | | observeChromeSettings() |
| | | observeLanguageChanges() |
| | | observeProEntitlementChanges() |
| | | setupLaunchAtLogin() |
| | | suppressReopenUntil = Date().addingTimeInterval(1.0) |
| | | didFinishLaunching = true |
| | |
| | | if LauncherHotkeyRegistrationStore.state(for: .main) == .failed { |
| | | return tr("menu.showShortcutUnavailable") |
| | | } |
| | | return "\(tr("menu.showAppList")) \(LauncherHotkey.main.displayString)" |
| | | return "\(tr("menu.showAppList")) \(LauncherHotkeySettings.effectiveHotkey(for: .main).displayString)" |
| | | } |
| | | |
| | | private func observeApplicationMenuChanges() { |
| | |
| | | |
| | | private func registerConfiguredHotkeys() { |
| | | installHotkeyEventHandlerIfNeeded() |
| | | registerFixedHotkey(.main) |
| | | registerFixedHotkey(.quickSearch) |
| | | registerEffectiveHotkey(.main) |
| | | registerEffectiveHotkey(.quickSearch) |
| | | } |
| | | |
| | | private func registerFixedHotkey(_ kind: LauncherHotkeyKind) { |
| | | private func registerEffectiveHotkey(_ kind: LauncherHotkeyKind) { |
| | | unregisterHotkey(for: kind) |
| | | let hotkey = kind.hotkey |
| | | let hotkey = LauncherHotkeySettings.effectiveHotkey(for: kind) |
| | | registerAndStoreHotkey(hotkey, for: kind, markFailure: true) |
| | | } |
| | | |
| | | @discardableResult |
| | | func applyCustomHotkey(_ hotkey: LauncherHotkey, for kind: LauncherHotkeyKind) -> LauncherHotkeyCustomizationResult { |
| | | guard ProEntitlementPolicy.isUnlocked(.customHotkeys) else { |
| | | return .locked |
| | | } |
| | | if let error = LauncherHotkeySettings.validationError(for: hotkey, kind: kind) { |
| | | return .invalid(error) |
| | | } |
| | | return activateHotkey(hotkey, for: kind, persistCustom: true) |
| | | } |
| | | |
| | | @discardableResult |
| | | func restoreDefaultHotkey(for kind: LauncherHotkeyKind) -> LauncherHotkeyCustomizationResult { |
| | | guard ProEntitlementPolicy.isUnlocked(.customHotkeys) else { |
| | | return .locked |
| | | } |
| | | return activateHotkey(LauncherHotkeySettings.defaultHotkey(for: kind), for: kind, persistCustom: false) |
| | | } |
| | | |
| | | private func activateHotkey( |
| | | _ hotkey: LauncherHotkey, |
| | | for kind: LauncherHotkeyKind, |
| | | persistCustom: Bool |
| | | ) -> LauncherHotkeyCustomizationResult { |
| | | installHotkeyEventHandlerIfNeeded() |
| | | |
| | | if LauncherHotkeySettings.effectiveHotkey(for: kind) == hotkey, |
| | | hotkeyRef(for: kind) != nil { |
| | | if persistCustom { |
| | | LauncherHotkeySettings.save(hotkey, for: kind) |
| | | LauncherHotkeyRegistrationStore.setActive(for: kind) |
| | | return .saved |
| | | } |
| | | LauncherHotkeySettings.clearCustomHotkey(for: kind) |
| | | LauncherHotkeyRegistrationStore.setActive(for: kind) |
| | | return .restored |
| | | } |
| | | |
| | | switch tryRegisterHotkey(hotkey, for: kind, markFailure: false) { |
| | | case .success(let newRef): |
| | | if let oldRef = hotkeyRef(for: kind) { |
| | | UnregisterEventHotKey(oldRef) |
| | | } |
| | | setHotkeyRef(newRef, for: kind) |
| | | if persistCustom { |
| | | LauncherHotkeySettings.save(hotkey, for: kind) |
| | | LauncherHotkeyRegistrationStore.setActive(for: kind) |
| | | return .saved |
| | | } |
| | | LauncherHotkeySettings.clearCustomHotkey(for: kind) |
| | | LauncherHotkeyRegistrationStore.setActive(for: kind) |
| | | return .restored |
| | | case .failure(let status): |
| | | return .registrationFailed(status) |
| | | } |
| | | } |
| | | |
| | | private func registerAndStoreHotkey( |
| | | _ hotkey: LauncherHotkey, |
| | | for kind: LauncherHotkeyKind, |
| | | markFailure: Bool |
| | | ) { |
| | | switch tryRegisterHotkey(hotkey, for: kind, markFailure: markFailure) { |
| | | case .success(let newRef): |
| | | setHotkeyRef(newRef, for: kind) |
| | | case .failure: |
| | | setHotkeyRef(nil, for: kind) |
| | | } |
| | | } |
| | | |
| | | private func tryRegisterHotkey( |
| | | _ hotkey: LauncherHotkey, |
| | | for kind: LauncherHotkeyKind, |
| | | markFailure: Bool |
| | | ) -> HotkeyRegistrationAttempt { |
| | | |
| | | var hotkeyID = EventHotKeyID() |
| | | hotkeyID.signature = OSType(0x41505447) // 'APTG' |
| | |
| | | ) |
| | | |
| | | if status == noErr, let newRef { |
| | | setHotkeyRef(newRef, for: kind) |
| | | LauncherHotkeyRegistrationStore.setActive(for: kind) |
| | | if markFailure { |
| | | LauncherHotkeyRegistrationStore.setActive(for: kind) |
| | | } |
| | | return .success(newRef) |
| | | } else { |
| | | setHotkeyRef(nil, for: kind) |
| | | LauncherHotkeyRegistrationStore.setFailed(status, for: kind) |
| | | if markFailure { |
| | | LauncherHotkeyRegistrationStore.setFailed(status, for: kind) |
| | | } |
| | | print("[TagLauncher] Fixed hotkey registration failed for \(kind.rawValue): \(status)") |
| | | return .failure(status) |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private func observeProEntitlementChanges() { |
| | | NotificationCenter.default.addObserver( |
| | | forName: .tagLauncherProEntitlementChanged, |
| | | object: nil, |
| | | queue: .main |
| | | ) { [weak self] _ in |
| | | self?.registerConfiguredHotkeys() |
| | | self?.setupMenuBar() |
| | | self?.configureApplicationMenuWhenAvailable(retries: 2) |
| | | } |
| | | } |
| | | |
| | | private func installHotkeyEventHandlerIfNeeded() { |
| | | guard !hotkeyEventHandlerInstalled else { return } |
| | | hotkeyEventHandlerInstalled = true |