| | |
| | | |
| | | @main |
| | | struct TagLauncherApp: App { |
| | | private static let singletonLockFile = TagLauncherProcessSingleton.acquireOrHandOffAndExit() |
| | | @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate |
| | | |
| | | init() { |
| | | _ = Self.singletonLockFile |
| | | } |
| | | |
| | | var body: some Scene { |
| | | Settings { |
| | | PreferencesView() |
| | | } |
| | | .defaultSize(width: 880, height: 460) |
| | | .defaultSize(width: 1000, height: 480) |
| | | .commands { |
| | | CommandGroup(replacing: .systemServices) { } |
| | | CommandGroup(replacing: .appVisibility) { } |
| | | CommandGroup(replacing: .help) { } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // MARK: - App Delegate (menubar + overlay window + hotkey) |
| | | |
| | | final class OverlayPanel: NSPanel { |
| | | override var canBecomeKey: Bool { true } |
| | | override var canBecomeMain: Bool { true } |
| | | } |
| | | |
| | | final class AppDelegate: NSObject, NSApplicationDelegate { |
| | | final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { |
| | | private static let showDockIconKey = "showDockIcon" |
| | | private static let statusItemAutosaveName = AppIdentity.statusItemAutosaveName |
| | | private static let statusItemButtonIdentifier = NSUserInterfaceItemIdentifier("TagLauncherStatusItemButton") |
| | | private static let statusItemAccessibilityLabel = AppIdentity.displayName |
| | | private static let showAppListMenuItemIdentifier = NSUserInterfaceItemIdentifier("TagLauncherShowAppListMenuItem") |
| | | private static let overlayDefaultLevel = NSWindow.Level.normal |
| | | private static let overlayTextInputLevel = NSWindow.Level.normal |
| | | private static let helpMenuItemIdentifier = NSUserInterfaceItemIdentifier("TagLauncherHelpMenu") |
| | | private static let downloadHelpMenuItemIdentifier = NSUserInterfaceItemIdentifier("TagLauncherDownloadHelpMenuItem") |
| | | private static let externalActivationNotification = Notification.Name("TagLauncherExternalActivationRequested") |
| | | private static let externalActivationObject = AppIdentity.bundleIdentifier |
| | | private static let duplicateLaunchSuppressReopenKey = "duplicateLaunchSuppressReopenAt" |
| | | private static let launcherOverlayLevel = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue - 1) |
| | | private static let overlayDefaultLevel = launcherOverlayLevel |
| | | private static let overlayTextInputLevel = launcherOverlayLevel |
| | | |
| | | private var statusItem: NSStatusItem? |
| | | private var overlayWindow: NSWindow? |
| | | private var overlayKeyMonitor: Any? |
| | | private var quickSearchLocalMouseMonitor: Any? |
| | | private var quickSearchExternalMouseMonitor: Any? |
| | | private var settingsWindow: NSWindow? // Track Settings window to keep it above overlay |
| | | private var mainHotkeyRef: EventHotKeyRef? |
| | | private var quickSearchHotkeyRef: EventHotKeyRef? |
| | | private var hotkeyEventHandlerInstalled = false |
| | | private var isQuickSearchOpen = false |
| | | private var quickSearchShouldHideOverlayOnClose = false |
| | | private var quickSearchOnlyOverlaySession = false |
| | | private var lastQuickSearchHotkeyAt = Date.distantPast |
| | | private var isModalInteractionActive = false |
| | | private var isInEditMode = false // Suppress auto-dismiss during editing |
| | | private var isEditingAppNote = false |
| | | private var isConfiguringApplicationMenu = false |
| | | private var lastShowDockIcon: Bool? |
| | | private var statusMenuScreenForNextOverlay: NSScreen? |
| | | private var suppressReopenUntil = Date.distantPast |
| | | |
| | | private lazy var overlayController = OverlayWindowController( |
| | | dependencies: OverlayWindowController.Dependencies( |
| | | shouldStageAsAccessory: { [weak self] in |
| | | self?.shouldStageOverlayAsAccessory ?? false |
| | | }, |
| | | isSettingsVisible: { [weak self] in |
| | | self?.isSettingsVisible ?? false |
| | | }, |
| | | settingsWindow: { [weak self] in |
| | | self?.settingsWindow |
| | | }, |
| | | canHideOverlay: { [weak self] in |
| | | self?.isInEditMode == false |
| | | }, |
| | | currentOverlayLevel: { [weak self] in |
| | | self?.currentOverlayLevel ?? Self.overlayDefaultLevel |
| | | }, |
| | | overlayLevel: { [weak self] initialQuickSearchSource in |
| | | self?.overlayLevel(initialQuickSearchSource: initialQuickSearchSource) ?? Self.overlayDefaultLevel |
| | | }, |
| | | makeContentView: { [weak self] initialQuickSearchSource in |
| | | DismissibleHostingView( |
| | | rootView: ContentView( |
| | | hideOverlay: { [weak self] in |
| | | self?.hideOverlay(force: true) |
| | | }, |
| | | initialQuickSearchSource: initialQuickSearchSource |
| | | ), |
| | | onBackdropTap: { [weak self] in |
| | | self?.hideOverlay() |
| | | } |
| | | ) |
| | | }, |
| | | handleOverlayKeyEvent: { [weak self] event in |
| | | self?.handleOverlayKeyEvent(event) ?? false |
| | | }, |
| | | installOverlayKeyMonitor: { [weak self] in |
| | | self?.installOverlayKeyMonitor() |
| | | }, |
| | | removeOverlayKeyMonitor: { [weak self] in |
| | | self?.removeOverlayKeyMonitor() |
| | | }, |
| | | removeQuickSearchMouseMonitor: { [weak self] in |
| | | self?.removeQuickSearchExternalMouseMonitor() |
| | | self?.quickSearchShouldHideOverlayOnClose = false |
| | | self?.quickSearchOnlyOverlaySession = false |
| | | }, |
| | | detachSettingsWindow: { [weak self] window in |
| | | self?.detachSettingsWindow(window) |
| | | }, |
| | | prepareSettingsWindow: { [weak self] window in |
| | | self?.prepareSettingsWindow(window) |
| | | }, |
| | | refreshChromeState: { [weak self] activate, avoidSpaceSwitch in |
| | | self?.refreshLauncherChromeState(activate: activate, avoidSpaceSwitch: avoidSpaceSwitch) |
| | | }, |
| | | onWillHide: { |
| | | TagDatabase.flushPendingCategorySchemeBackupBatch() |
| | | }, |
| | | onDidHide: { |
| | | NotificationCenter.default.post(name: .tagLauncherOverlayDidHide, object: nil) |
| | | }, |
| | | onDidShow: { |
| | | NotificationCenter.default.post(name: .tagLauncherOverlayDidShow, object: nil) |
| | | } |
| | | ) |
| | | ) |
| | | |
| | | private var overlayWindow: NSWindow? { |
| | | overlayController.window |
| | | } |
| | | |
| | | private var overlayGeneration: Int { |
| | | overlayController.generation |
| | | } |
| | | |
| | | private var overlayAvoidsSpaceSwitch: Bool { |
| | | get { overlayController.avoidsSpaceSwitch } |
| | | set { overlayController.avoidsSpaceSwitch = newValue } |
| | | } |
| | | |
| | | private var isOverlayVisible: Bool { |
| | | overlayWindow?.isVisible == true |
| | |
| | | |
| | | private var requiresForegroundOwnership: Bool { |
| | | isOverlayVisible || isSettingsVisible |
| | | } |
| | | |
| | | private var shouldStageOverlayAsAccessory: Bool { |
| | | !isSettingsVisible && !UserDefaults.standard.bool(forKey: Self.showDockIconKey) |
| | | } |
| | | |
| | | private var currentOverlayLevel: NSWindow.Level { |
| | |
| | | observeAppNoteEditing() |
| | | observeQuickSearch() |
| | | observePreferencesRequests() |
| | | observeExternalActivationRequests() |
| | | observeApplicationMenuChanges() |
| | | observeChromeSettings() |
| | | observeLanguageChanges() |
| | | setupLaunchAtLogin() |
| | | configureApplicationMenuWhenAvailable() |
| | | suppressReopenUntil = Date().addingTimeInterval(1.0) |
| | | configureApplicationMenuWhenAvailable(retries: 200) |
| | | } |
| | | |
| | | /// Dock icon click → show overlay (same as menubar "Show TagLauncher") |
| | | func applicationDidBecomeActive(_ notification: Notification) { |
| | | configureApplicationMenuWhenAvailable(retries: 12) |
| | | } |
| | | |
| | | /// Dock icon reopen is an explicit App Grid entry only when the user chooses to show the Dock icon. |
| | | /// Duplicate-instance handoff suppresses this path so repeated launches do not show App Grid. |
| | | func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { |
| | | showOverlay() |
| | | guard Date() >= suppressReopenUntil else { return false } |
| | | let lastDuplicateLaunch = UserDefaults.standard.double(forKey: Self.duplicateLaunchSuppressReopenKey) |
| | | if lastDuplicateLaunch > 0, |
| | | Date().timeIntervalSince1970 - lastDuplicateLaunch < 2.0 { |
| | | suppressReopenUntil = Date().addingTimeInterval(1.0) |
| | | return false |
| | | } |
| | | guard UserDefaults.standard.bool(forKey: Self.showDockIconKey) else { return false } |
| | | guard isPointerNearDockArea() else { return false } |
| | | showOrFocusOverlay() |
| | | return false // Suppress default "unhide all windows" behavior |
| | | } |
| | | |
| | | private func isPointerNearDockArea() -> Bool { |
| | | let mouse = NSEvent.mouseLocation |
| | | return NSScreen.screens.contains { screen in |
| | | let frame = screen.frame |
| | | let visible = screen.visibleFrame |
| | | guard NSMouseInRect(mouse, frame, false) else { return false } |
| | | let bottomDock = visible.minY > frame.minY |
| | | && mouse.y >= frame.minY |
| | | && mouse.y <= visible.minY + 24 |
| | | let leftDock = visible.minX > frame.minX |
| | | && mouse.x >= frame.minX |
| | | && mouse.x <= visible.minX + 24 |
| | | let rightDock = visible.maxX < frame.maxX |
| | | && mouse.x <= frame.maxX |
| | | && mouse.x >= visible.maxX - 24 |
| | | return bottomDock || leftDock || rightDock |
| | | } |
| | | } |
| | | |
| | | func applicationWillTerminate(_ notification: Notification) { |
| | |
| | | TagDatabase.flushPendingCategorySchemeBackupBatch() |
| | | removeOverlayKeyMonitor() |
| | | removeQuickSearchExternalMouseMonitor() |
| | | DistributedNotificationCenter.default().removeObserver( |
| | | self, |
| | | name: Self.externalActivationNotification, |
| | | object: Self.externalActivationObject |
| | | ) |
| | | } |
| | | |
| | | /// Ensure defaultGroupName is always the language-neutral key "Other". |
| | |
| | | } |
| | | } |
| | | |
| | | private func beginLauncherForegroundOwnership(activate: Bool = true) { |
| | | if NSApp.activationPolicy() != .regular { |
| | | NSApp.setActivationPolicy(.regular) |
| | | private func beginLauncherForegroundOwnership(activate: Bool = true, keyWindow: NSWindow? = nil) { |
| | | let showDock = UserDefaults.standard.bool(forKey: Self.showDockIconKey) |
| | | let desiredPolicy: NSApplication.ActivationPolicy = showDock ? .regular : .accessory |
| | | if NSApp.activationPolicy() != desiredPolicy { |
| | | NSApp.setActivationPolicy(desiredPolicy) |
| | | } |
| | | if activate { |
| | | if activate, showDock { |
| | | claimLauncherForeground(keyWindow: keyWindow) |
| | | } else if activate { |
| | | NSApp.activate(ignoringOtherApps: true) |
| | | configureApplicationMenuWhenAvailable(retries: 4) |
| | | keyWindow?.makeKeyAndOrderFront(nil) |
| | | keyWindow?.orderFrontRegardless() |
| | | } |
| | | } |
| | | |
| | | private func refreshLauncherChromeState(activate: Bool = false) { |
| | | private func claimLauncherForeground( |
| | | keyWindow: NSWindow? = nil, |
| | | retries: Int = 0, |
| | | overlayGeneration expectedOverlayGeneration: Int? = nil |
| | | ) { |
| | | if NSApp.activationPolicy() != .regular { |
| | | NSApp.setActivationPolicy(.regular) |
| | | } |
| | | |
| | | if isOverlayVisible && !NSApp.presentationOptions.contains(.hideDock) { |
| | | NSApp.presentationOptions = [.hideDock] |
| | | } |
| | | |
| | | NSApp.unhide(nil) |
| | | NSApp.activate(ignoringOtherApps: true) |
| | | |
| | | if let keyWindow, keyWindow.isVisible { |
| | | keyWindow.makeKeyAndOrderFront(nil) |
| | | keyWindow.makeMain() |
| | | keyWindow.orderFrontRegardless() |
| | | } |
| | | configureApplicationMenuWhenAvailable(retries: 4) |
| | | |
| | | let overlayShouldYieldToSettings = keyWindow == overlayWindow && isSettingsVisible |
| | | let keyWindowStillNeedsFocus = !overlayShouldYieldToSettings |
| | | && keyWindow?.isVisible == true |
| | | && keyWindow?.isKeyWindow == false |
| | | let shouldRetry = !NSApp.isActive |
| | | || !NSApp.presentationOptions.contains(.hideDock) |
| | | || keyWindowStillNeedsFocus |
| | | guard retries > 0, isOverlayVisible else { return } |
| | | guard shouldRetry else { return } |
| | | |
| | | let retryWindow = keyWindow |
| | | DispatchQueue.main.asyncAfter(deadline: .now() + 0.08) { [weak self] in |
| | | guard let self, self.isOverlayVisible else { return } |
| | | if let expectedOverlayGeneration, |
| | | expectedOverlayGeneration != self.overlayGeneration { |
| | | return |
| | | } |
| | | self.refreshLauncherChromeState() |
| | | self.claimLauncherForeground( |
| | | keyWindow: retryWindow, |
| | | retries: retries - 1, |
| | | overlayGeneration: expectedOverlayGeneration |
| | | ) |
| | | } |
| | | } |
| | | |
| | | private func refreshLauncherChromeState(activate: Bool = false, avoidSpaceSwitch: Bool = false) { |
| | | let showDock = UserDefaults.standard.bool(forKey: Self.showDockIconKey) |
| | | lastShowDockIcon = showDock |
| | | |
| | | let desiredPolicy: NSApplication.ActivationPolicy = requiresForegroundOwnership |
| | | let shouldStayAccessoryForCurrentFullscreenSpace = isOverlayVisible |
| | | && (avoidSpaceSwitch || overlayAvoidsSpaceSwitch) |
| | | let shouldStayAccessoryForHiddenDockChrome = requiresForegroundOwnership |
| | | && !showDock |
| | | let shouldStayAccessoryForQuickOnlySearch = isOverlayVisible |
| | | && quickSearchOnlyOverlaySession |
| | | && !showDock |
| | | let desiredPolicy: NSApplication.ActivationPolicy = shouldStayAccessoryForCurrentFullscreenSpace |
| | | ? .accessory |
| | | : (shouldStayAccessoryForHiddenDockChrome ? .accessory |
| | | : (shouldStayAccessoryForQuickOnlySearch ? .accessory |
| | | : (requiresForegroundOwnership |
| | | ? .regular |
| | | : (showDock ? .regular : .accessory) |
| | | : (showDock ? .regular : .accessory)))) |
| | | if NSApp.activationPolicy() != desiredPolicy { |
| | | NSApp.setActivationPolicy(desiredPolicy) |
| | | } |
| | |
| | | NSApp.presentationOptions = desiredPresentation |
| | | } |
| | | |
| | | if activate && requiresForegroundOwnership { |
| | | NSApp.activate(ignoringOtherApps: true) |
| | | configureApplicationMenuWhenAvailable(retries: 4) |
| | | if activate && requiresForegroundOwnership |
| | | && !shouldStayAccessoryForCurrentFullscreenSpace |
| | | && !shouldStayAccessoryForHiddenDockChrome |
| | | && !shouldStayAccessoryForQuickOnlySearch { |
| | | let keyWindow = isSettingsVisible ? settingsWindow : (isOverlayVisible ? overlayWindow : nil) |
| | | claimLauncherForeground( |
| | | keyWindow: keyWindow, |
| | | retries: isOverlayVisible && !isSettingsVisible ? 5 : 0, |
| | | overlayGeneration: isOverlayVisible ? overlayGeneration : nil |
| | | ) |
| | | } |
| | | } |
| | | |
| | | private func handleApplicationDidResignActive() { |
| | | guard !isOverlayVisible else { |
| | | return |
| | | } |
| | | |
| | | if isQuickSearchOpen { |
| | | NotificationCenter.default.post(name: .tagLauncherQuickSearchDismissRequested, object: nil) |
| | | } |
| | | |
| | | guard isOverlayVisible else { |
| | | if !requiresForegroundOwnership { |
| | | refreshLauncherChromeState() |
| | | return |
| | | } |
| | | |
| | | // Once another app takes the menu bar, don't leave the overlay stranded onscreen. |
| | | hideOverlay(force: true) |
| | | } |
| | | |
| | | /// Keep app chrome in sync when language changes from any entry point. |
| | |
| | | guard supportsLaunchAtLogin else { return } |
| | | let plist: [String: Any] = [ |
| | | "Label": Self.launchAgentLabel, |
| | | "ProgramArguments": ["open", Bundle.main.bundlePath, "--hide"], |
| | | "ProgramArguments": Self.expectedLaunchAgentProgramArguments(), |
| | | "RunAtLoad": true, |
| | | ] |
| | | let dir = Self.launchAgentURL.deletingLastPathComponent() |
| | |
| | | (plist as NSDictionary).write(to: Self.launchAgentURL, atomically: true) |
| | | |
| | | let uid = getuid() |
| | | let bootoutTask = Process() |
| | | bootoutTask.launchPath = "/bin/launchctl" |
| | | bootoutTask.arguments = ["bootout", "gui/\(uid)/\(Self.launchAgentLabel)"] |
| | | try? bootoutTask.run() |
| | | bootoutTask.waitUntilExit() |
| | | |
| | | let task = Process() |
| | | task.launchPath = "/bin/launchctl" |
| | | task.arguments = ["bootstrap", "gui/\(uid)", Self.launchAgentURL.path] |
| | | task.launch() |
| | | try? task.run() |
| | | } |
| | | |
| | | static func disableLaunchAtLogin() { |
| | |
| | | let task = Process() |
| | | task.launchPath = "/bin/launchctl" |
| | | task.arguments = ["bootout", "gui/\(uid)/\(Self.launchAgentLabel)"] |
| | | task.launch() |
| | | try? task.run() |
| | | try? FileManager.default.removeItem(at: Self.launchAgentURL) |
| | | } |
| | | |
| | | private static func launchAgentProgramArguments() -> [String]? { |
| | | guard let plist = NSDictionary(contentsOf: Self.launchAgentURL) as? [String: Any] else { |
| | | return nil |
| | | } |
| | | return plist["ProgramArguments"] as? [String] |
| | | } |
| | | |
| | | private static func expectedLaunchAgentProgramArguments() -> [String] { |
| | | let executablePath = Bundle.main.executablePath |
| | | ?? Bundle.main.bundleURL |
| | | .appendingPathComponent("Contents/MacOS/\(AppIdentity.displayName)") |
| | | .path |
| | | return [executablePath, "--hide"] |
| | | } |
| | | |
| | | /// On first launch, enable login item by default via LaunchAgent. |
| | |
| | | return |
| | | } |
| | | let key = "launchAtLogin" |
| | | if !AppDefaults.hasStoredValue(for: key) && UserDefaults.standard.bool(forKey: key) { |
| | | guard UserDefaults.standard.bool(forKey: key) else { |
| | | Self.disableLaunchAtLogin() |
| | | return |
| | | } |
| | | |
| | | let expectedArguments = Self.expectedLaunchAgentProgramArguments() |
| | | if !AppDefaults.hasStoredValue(for: key) |
| | | || Self.launchAgentProgramArguments() != expectedArguments { |
| | | Self.enableLaunchAtLogin() |
| | | } |
| | | } |
| | |
| | | button.imageScaling = .scaleProportionallyDown |
| | | button.imagePosition = .imageOnly |
| | | button.toolTip = "TagLauncher — Tag-based app launcher" |
| | | button.action = #selector(toggleOverlay) |
| | | button.action = #selector(toggleOverlay(_:)) |
| | | button.target = self |
| | | } |
| | | |
| | | let menu = NSMenu() |
| | | menu.delegate = self |
| | | let showItem = NSMenuItem( |
| | | title: showAppListMenuTitle, |
| | | action: #selector(toggleOverlay), |
| | | action: #selector(toggleOverlayFromStatusMenu(_:)), |
| | | keyEquivalent: "" |
| | | ) |
| | | showItem.target = self |
| | |
| | | menu.addItem(.separator()) |
| | | let prefsItem = NSMenuItem( |
| | | title: tr("menu.preferences"), |
| | | action: #selector(openPreferences), |
| | | action: #selector(openPreferences(_:)), |
| | | keyEquivalent: "," |
| | | ) |
| | | prefsItem.target = self |
| | |
| | | ) |
| | | ) |
| | | statusItem.menu = menu |
| | | } |
| | | |
| | | func menuWillOpen(_ menu: NSMenu) { |
| | | statusMenuScreenForNextOverlay = overlayController.screenContainingCurrentPointer() |
| | | ?? statusItem?.button?.window?.screen |
| | | ?? NSScreen.main |
| | | ?? NSScreen.screens.first |
| | | } |
| | | |
| | | func menuDidClose(_ menu: NSMenu) { |
| | | DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in |
| | | self?.statusMenuScreenForNextOverlay = nil |
| | | } |
| | | } |
| | | |
| | | private func makeMenuBarIcon() -> NSImage { |
| | |
| | | } |
| | | |
| | | self.configureApplicationMenu() |
| | | self.configureHelpMenu() |
| | | if retries > 0 && self.applicationMenuNeedsCleanup() { |
| | | self.configureApplicationMenuWhenAvailable(retries: retries - 1) |
| | | } |
| | |
| | | |
| | | private func configureShowAppListItem(_ item: NSMenuItem) { |
| | | item.title = showAppListMenuTitle |
| | | item.action = #selector(toggleOverlay) |
| | | item.action = #selector(toggleOverlayFromStatusMenu(_:)) |
| | | item.target = self |
| | | item.keyEquivalent = "" |
| | | item.keyEquivalentModifierMask = [] |
| | |
| | | } |
| | | |
| | | item.title = tr("menu.preferences") |
| | | item.action = #selector(openPreferences) |
| | | item.action = #selector(openPreferences(_:)) |
| | | item.target = self |
| | | item.keyEquivalent = "," |
| | | item.keyEquivalentModifierMask = .command |
| | |
| | | |
| | | private func isSettingsMenuItem(_ item: NSMenuItem) -> Bool { |
| | | item.action == Selector(("showSettingsWindow:")) |
| | | || item.action == #selector(openPreferences) |
| | | || item.action == #selector(openPreferences(_:)) |
| | | || item.title == tr("menu.preferences") |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private func configureHelpMenu() { |
| | | guard let mainMenu = NSApp.mainMenu else { return } |
| | | NSApp.helpMenu = nil |
| | | |
| | | for item in mainMenu.items.reversed() { |
| | | let isHelpMenu = item.identifier == Self.helpMenuItemIdentifier |
| | | || item.title.localizedCaseInsensitiveContains("help") |
| | | || item.title == tr("menu.help") |
| | | || item.submenu?.title.localizedCaseInsensitiveContains("help") == true |
| | | || item.submenu?.title == tr("menu.help") |
| | | if isHelpMenu { |
| | | mainMenu.removeItem(item) |
| | | } |
| | | } |
| | | |
| | | let menuItem = NSMenuItem(title: tr("menu.help"), action: nil, keyEquivalent: "") |
| | | let helpMenu = NSMenu(title: tr("menu.help")) |
| | | menuItem.identifier = Self.helpMenuItemIdentifier |
| | | menuItem.submenu = helpMenu |
| | | |
| | | let downloadItem = NSMenuItem() |
| | | downloadItem.identifier = Self.downloadHelpMenuItemIdentifier |
| | | downloadItem.title = tr("help.downloadPDF") |
| | | downloadItem.action = #selector(openLocalizedHelp(_:)) |
| | | downloadItem.target = self |
| | | downloadItem.keyEquivalent = "" |
| | | downloadItem.keyEquivalentModifierMask = [] |
| | | downloadItem.isEnabled = true |
| | | helpMenu.addItem(downloadItem) |
| | | mainMenu.addItem(menuItem) |
| | | } |
| | | |
| | | @objc private func openLocalizedHelp(_ sender: Any? = nil) { |
| | | NSWorkspace.shared.open(HelpDocument.currentURL) |
| | | } |
| | | |
| | | private func removeMenuBarItem() { |
| | | guard let statusItem else { return } |
| | | NSStatusBar.system.removeStatusItem(statusItem) |
| | |
| | | |
| | | // MARK: - Overlay Window |
| | | |
| | | @objc private func toggleOverlay() { |
| | | if overlayWindow?.isVisible == true { |
| | | hideOverlay(force: true) |
| | | } else { |
| | | showOverlay() |
| | | @objc func toggleOverlay(_ sender: Any) { |
| | | performToggleOverlay(preferredScreen: nil) |
| | | } |
| | | |
| | | @objc func toggleOverlayFromStatusMenu(_ sender: Any) { |
| | | let preferredScreen = statusMenuScreenForNextOverlay |
| | | statusMenuScreenForNextOverlay = nil |
| | | DispatchQueue.main.async { [weak self] in |
| | | self?.performToggleOverlay(preferredScreen: preferredScreen) |
| | | } |
| | | } |
| | | |
| | | private func showOverlay(initialQuickSearchSource: String? = nil) { |
| | | // Use the screen under the mouse cursor — works in fullscreen spaces |
| | | let mousePoint = NSEvent.mouseLocation |
| | | guard let screen = NSScreen.screens.first(where: { |
| | | NSMouseInRect(mousePoint, $0.frame, false) |
| | | }) ?? NSScreen.main ?? NSScreen.screens.first else { return } |
| | | private func performToggleOverlay(preferredScreen: NSScreen?) { |
| | | overlayController.toggle(preferredScreen: preferredScreen) |
| | | } |
| | | |
| | | let window: NSWindow |
| | | let createdWindow: Bool |
| | | if let existingWindow = overlayWindow { |
| | | window = existingWindow |
| | | createdWindow = false |
| | | } else { |
| | | window = makeOverlayWindow(on: screen, initialQuickSearchSource: initialQuickSearchSource) |
| | | overlayWindow = window |
| | | createdWindow = true |
| | | private func showOrFocusOverlay(preferredScreen: NSScreen? = nil) { |
| | | overlayController.showOrFocus(preferredScreen: preferredScreen) |
| | | } |
| | | |
| | | private func showOverlay( |
| | | initialQuickSearchSource: String? = nil, |
| | | preferredScreen: NSScreen? = nil, |
| | | stagedForAllSpaces: Bool = false |
| | | ) { |
| | | if initialQuickSearchSource == QuickSearchOpenSource.globalHidden { |
| | | isQuickSearchOpen = true |
| | | quickSearchShouldHideOverlayOnClose = true |
| | | quickSearchOnlyOverlaySession = true |
| | | } |
| | | |
| | | installOverlayKeyMonitor() |
| | | beginLauncherForegroundOwnership() |
| | | |
| | | let targetFrame = screen.frame |
| | | let targetLevel = overlayLevel(initialQuickSearchSource: initialQuickSearchSource) |
| | | let canReuseVisibleWindow = !createdWindow |
| | | && window.isVisible |
| | | && NSEqualRects(window.frame, targetFrame) |
| | | && window.level == targetLevel |
| | | |
| | | if canReuseVisibleWindow { |
| | | if !window.isKeyWindow { |
| | | window.makeKeyAndOrderFront(nil) |
| | | } |
| | | refreshLauncherChromeState(activate: true) |
| | | if let initialQuickSearchSource { |
| | | NotificationCenter.default.post( |
| | | name: .tagLauncherQuickSearchRequested, |
| | | object: nil, |
| | | userInfo: ["source": initialQuickSearchSource] |
| | | ) |
| | | } |
| | | return |
| | | } |
| | | |
| | | window.setFrame(targetFrame, display: true) |
| | | window.level = targetLevel |
| | | |
| | | if let initialQuickSearchSource, !createdWindow { |
| | | NotificationCenter.default.post( |
| | | name: .tagLauncherQuickSearchRequested, |
| | | object: nil, |
| | | userInfo: ["source": initialQuickSearchSource] |
| | | ) |
| | | } |
| | | |
| | | window.makeKeyAndOrderFront(nil) |
| | | window.orderFrontRegardless() |
| | | refreshLauncherChromeState(activate: true) |
| | | NotificationCenter.default.post(name: .tagLauncherOverlayDidShow, object: nil) |
| | | overlayController.show( |
| | | initialQuickSearchSource: initialQuickSearchSource, |
| | | preferredScreen: preferredScreen, |
| | | stagedForAllSpaces: stagedForAllSpaces |
| | | ) |
| | | } |
| | | |
| | | private func installOverlayKeyMonitor() { |
| | | guard overlayKeyMonitor == nil else { return } |
| | | overlayKeyMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in |
| | | guard let self else { return event } |
| | | if event.keyCode == 53 { // Escape |
| | | if self.isQuickSearchOpen { |
| | | NotificationCenter.default.post(name: .tagLauncherQuickSearchDismissRequested, object: nil) |
| | | return nil |
| | | } |
| | | self.hideOverlay(force: true) |
| | | return nil |
| | | } |
| | | if self.shouldOpenQuickSearch(for: event) { |
| | | NotificationCenter.default.post( |
| | | name: .tagLauncherQuickSearchRequested, |
| | | object: nil, |
| | | userInfo: ["source": QuickSearchOpenSource.mainOverlay] |
| | | ) |
| | | if self.handleOverlayKeyEvent(event) { |
| | | return nil |
| | | } |
| | | return event |
| | | } |
| | | } |
| | | |
| | | @discardableResult |
| | | private func handleOverlayKeyEvent(_ event: NSEvent) -> Bool { |
| | | guard event.type == .keyDown else { return false } |
| | | |
| | | if event.keyCode == UInt16(kVK_Escape), |
| | | isQuickSearchOpen || quickSearchOnlyOverlaySession { |
| | | return handleOverlayEscapeKey() |
| | | } |
| | | |
| | | guard shouldHandleOverlayKeyEvent(event) else { return false } |
| | | |
| | | if event.keyCode == UInt16(kVK_Escape) { |
| | | return handleOverlayEscapeKey() |
| | | } |
| | | if shouldOpenQuickSearch(for: event) { |
| | | requestQuickSearch(source: QuickSearchOpenSource.mainOverlay) |
| | | return true |
| | | } |
| | | return false |
| | | } |
| | | |
| | | private func handleOverlayEscapeKey() -> Bool { |
| | | if isQuickSearchOpen || quickSearchOnlyOverlaySession { |
| | | dismissQuickSearchFromKeyboard() |
| | | return true |
| | | } |
| | | guard !isSettingsVisible, |
| | | !isEditingAppNote, |
| | | !isModalInteractionActive |
| | | else { return false } |
| | | hideOverlay(force: true) |
| | | return true |
| | | } |
| | | |
| | | private func dismissQuickSearchFromKeyboard() { |
| | | let shouldHideOverlayAfterDismiss = quickSearchShouldHideOverlayOnClose || quickSearchOnlyOverlaySession |
| | | isQuickSearchOpen = false |
| | | removeQuickSearchExternalMouseMonitor() |
| | | updateOverlayLevelForTextInput() |
| | | NotificationCenter.default.post(name: .tagLauncherQuickSearchDismissRequested, object: nil) |
| | | if shouldHideOverlayAfterDismiss { |
| | | quickSearchShouldHideOverlayOnClose = false |
| | | quickSearchOnlyOverlaySession = false |
| | | DispatchQueue.main.async { [weak self] in |
| | | self?.hideOverlay(force: true) |
| | | } |
| | | } |
| | | } |
| | | |
| | | private func requestQuickSearch(source: String) { |
| | | isQuickSearchOpen = true |
| | | quickSearchShouldHideOverlayOnClose = quickSearchShouldHideOverlayOnClose |
| | | || source == QuickSearchOpenSource.globalHidden |
| | | quickSearchOnlyOverlaySession = quickSearchOnlyOverlaySession |
| | | || source == QuickSearchOpenSource.globalHidden |
| | | promoteOverlayToForegroundInput() |
| | | updateOverlayLevelForTextInput() |
| | | installQuickSearchExternalMouseMonitor() |
| | | DispatchQueue.main.async { |
| | | NotificationCenter.default.post( |
| | | name: .tagLauncherQuickSearchRequested, |
| | | object: nil, |
| | | userInfo: ["source": source] |
| | | ) |
| | | } |
| | | } |
| | | |
| | | private func shouldHandleOverlayKeyEvent(_ event: NSEvent) -> Bool { |
| | | guard overlayWindow?.isVisible == true else { return false } |
| | | if !isSettingsVisible && NSApp.isActive { |
| | | return true |
| | | } |
| | | if event.window == overlayWindow { return true } |
| | | return event.window == nil && NSApp.keyWindow == overlayWindow |
| | | } |
| | | |
| | | private func shouldOpenQuickSearch(for event: NSEvent) -> Bool { |
| | |
| | | !isModalInteractionActive |
| | | else { return false } |
| | | |
| | | guard let firstResponder = overlayWindow?.firstResponder else { return true } |
| | | if firstResponder is NSText || firstResponder is NSTextField { |
| | | return false |
| | | } |
| | | if let responderView = firstResponder as? NSView, |
| | | viewOrAncestorHandlesSpace(responderView) { |
| | | return false |
| | | } |
| | | return true |
| | | } |
| | | |
| | | private func viewOrAncestorHandlesSpace(_ view: NSView) -> Bool { |
| | | var current: NSView? = view |
| | | while let candidate = current { |
| | | if candidate is NSButton || candidate is NSSegmentedControl || candidate is NSSlider { |
| | | return true |
| | | } |
| | | current = candidate.superview |
| | | } |
| | | return false |
| | | } |
| | | |
| | | private func removeOverlayKeyMonitor() { |
| | |
| | | } |
| | | } |
| | | |
| | | private func makeOverlayWindow(on screen: NSScreen, initialQuickSearchSource: String? = nil) -> NSWindow { |
| | | let panel = OverlayPanel( |
| | | contentRect: screen.frame, |
| | | styleMask: [.borderless, .fullSizeContentView], |
| | | backing: .buffered, |
| | | defer: false |
| | | ) |
| | | panel.isFloatingPanel = true |
| | | panel.hidesOnDeactivate = false |
| | | panel.collectionBehavior = [ |
| | | .moveToActiveSpace, |
| | | .fullScreenAuxiliary, |
| | | .stationary, |
| | | .transient, |
| | | .ignoresCycle |
| | | ] |
| | | panel.isOpaque = false |
| | | panel.backgroundColor = .clear |
| | | panel.hasShadow = false |
| | | panel.titlebarAppearsTransparent = true |
| | | panel.titleVisibility = .hidden |
| | | panel.isReleasedWhenClosed = false |
| | | panel.contentView = DismissibleHostingView( |
| | | rootView: ContentView( |
| | | hideOverlay: { [weak self] in |
| | | self?.hideOverlay(force: true) |
| | | }, |
| | | initialQuickSearchSource: initialQuickSearchSource |
| | | ), |
| | | onBackdropTap: { [weak self] in |
| | | self?.hideOverlay() |
| | | } |
| | | ) |
| | | return panel |
| | | } |
| | | |
| | | private func hideOverlay(force: Bool = false, discardWindow: Bool = false) { |
| | | guard force || !isInEditMode else { return } |
| | | TagDatabase.flushPendingCategorySchemeBackupBatch() |
| | | if let settingsWindow, settingsWindow.parent == overlayWindow { |
| | | detachSettingsWindow(settingsWindow) |
| | | } |
| | | overlayWindow?.orderOut(nil) |
| | | removeOverlayKeyMonitor() |
| | | removeQuickSearchExternalMouseMonitor() |
| | | refreshLauncherChromeState() |
| | | NotificationCenter.default.post(name: .tagLauncherOverlayDidHide, object: nil) |
| | | if discardWindow { |
| | | overlayWindow = nil |
| | | } |
| | | overlayController.hide(force: force, discardWindow: discardWindow) |
| | | } |
| | | |
| | | // MARK: - Global Hotkeys |
| | |
| | | if id == LauncherHotkeyKind.quickSearch.eventID { |
| | | showQuickSearchFromGlobalHotkey() |
| | | } else { |
| | | toggleOverlay() |
| | | performToggleOverlay(preferredScreen: nil) |
| | | } |
| | | } |
| | | |
| | | private func showQuickSearchFromGlobalHotkey() { |
| | | guard overlayWindow?.isVisible != true else { return } |
| | | let now = Date() |
| | | if overlayWindow?.isVisible == true, isQuickSearchOpen || quickSearchOnlyOverlaySession { |
| | | lastQuickSearchHotkeyAt = now |
| | | dismissQuickSearchFromKeyboard() |
| | | return |
| | | } |
| | | |
| | | if now.timeIntervalSince(lastQuickSearchHotkeyAt) < 0.28 { |
| | | promoteOverlayToForegroundInput() |
| | | updateOverlayLevelForTextInput() |
| | | return |
| | | } |
| | | lastQuickSearchHotkeyAt = now |
| | | |
| | | if overlayWindow?.isVisible == true { |
| | | refreshLauncherChromeState( |
| | | activate: !overlayAvoidsSpaceSwitch, |
| | | avoidSpaceSwitch: overlayAvoidsSpaceSwitch |
| | | ) |
| | | requestQuickSearch(source: QuickSearchOpenSource.globalVisible) |
| | | return |
| | | } |
| | | isQuickSearchOpen = true |
| | | quickSearchShouldHideOverlayOnClose = true |
| | | quickSearchOnlyOverlaySession = true |
| | | showOverlay(initialQuickSearchSource: QuickSearchOpenSource.globalHidden) |
| | | } |
| | | |
| | |
| | | !self.isInEditMode |
| | | else { return } |
| | | |
| | | if self.isSettingsOwnedPanel(keyWindow) { |
| | | return |
| | | } |
| | | |
| | | if self.isMenuTrackingWindow(keyWindow) { |
| | | return |
| | | } |
| | | |
| | | if self.isAppOwnedDocumentWindow(keyWindow) { |
| | | self.prepareSettingsWindow(keyWindow) |
| | | return |
| | | } |
| | | |
| | | if NSApp.windows.contains(keyWindow) { |
| | | return |
| | | } |
| | | |
| | | // Settings/Preferences window -> float it above overlay for real-time preview. |
| | | if self.isSettingsWindowCandidate(keyWindow) { |
| | | self.prepareSettingsWindow(keyWindow) |
| | |
| | | |
| | | /// Settings must always appear centered over the current overlay view and float above it. |
| | | private func prepareSettingsWindow(_ window: NSWindow) { |
| | | let settingsSize = NSSize(width: 880, height: 460) |
| | | dismissQuickSearchIfNeeded() |
| | | let settingsSize = NSSize(width: 1000, height: 480) |
| | | window.identifier = NSUserInterfaceItemIdentifier("TagLauncherPreferencesWindow") |
| | | window.minSize = settingsSize |
| | | window.maxSize = settingsSize |
| | |
| | | |
| | | var behavior = window.collectionBehavior |
| | | behavior.remove(.canJoinAllSpaces) |
| | | behavior.formUnion([.fullScreenAuxiliary, .moveToActiveSpace]) |
| | | if overlayAvoidsSpaceSwitch { |
| | | behavior.remove(.moveToActiveSpace) |
| | | behavior.formUnion([.fullScreenAuxiliary, .stationary, .transient, .ignoresCycle]) |
| | | } else { |
| | | behavior.formUnion([.fullScreenAuxiliary, .moveToActiveSpace]) |
| | | } |
| | | window.collectionBehavior = behavior |
| | | window.makeKeyAndOrderFront(nil) |
| | | window.orderFrontRegardless() |
| | | settingsWindow = window |
| | | refreshLauncherChromeState(activate: true) |
| | | refreshLauncherChromeState( |
| | | activate: !overlayAvoidsSpaceSwitch, |
| | | avoidSpaceSwitch: overlayAvoidsSpaceSwitch |
| | | ) |
| | | } |
| | | |
| | | private func attachSettingsWindow(_ window: NSWindow, to overlayWindow: NSWindow) { |
| | |
| | | private func isSettingsWindowCandidate(_ window: NSWindow) -> Bool { |
| | | if window == settingsWindow { return true } |
| | | if window.identifier?.rawValue == "TagLauncherPreferencesWindow" { return true } |
| | | if isAppOwnedDocumentWindow(window) { return true } |
| | | guard NSApp.windows.contains(window), |
| | | window != overlayWindow, |
| | | window.isVisible, |
| | | !(window is NSPanel) |
| | | else { return false } |
| | | return true |
| | | return settingsWindowTitleCandidates().contains(normalizedWindowTitle(window.title)) |
| | | } |
| | | |
| | | private func isAppOwnedDocumentWindow(_ window: NSWindow) -> Bool { |
| | | NSApp.windows.contains(window) |
| | | && window != overlayWindow |
| | | && window.isVisible |
| | | && !(window is NSPanel) |
| | | && !isMenuTrackingWindow(window) |
| | | && window.styleMask.contains(.titled) |
| | | } |
| | | |
| | | private func isMenuTrackingWindow(_ window: NSWindow) -> Bool { |
| | | let className = NSStringFromClass(type(of: window)) |
| | | return className.localizedCaseInsensitiveContains("Menu") |
| | | || className.localizedCaseInsensitiveContains("Popup") |
| | | } |
| | | |
| | | private func settingsWindowTitleCandidates() -> Set<String> { |
| | | let keys = [ |
| | | "menu.preferences", |
| | | "settings.language", |
| | | "settings.general", |
| | | "quickSearch.hotkeys", |
| | | "settings.tags", |
| | | "settings.data", |
| | | "settings.about" |
| | | ] |
| | | return Set(keys.map { normalizedWindowTitle(tr($0)) }) |
| | | } |
| | | |
| | | private func normalizedWindowTitle(_ title: String) -> String { |
| | | title |
| | | .replacingOccurrences(of: "…", with: "") |
| | | .trimmingCharacters(in: .whitespacesAndNewlines) |
| | | } |
| | | |
| | | private func isSettingsOwnedPanel(_ window: NSWindow) -> Bool { |
| | | guard window is NSPanel else { return false } |
| | | if window is NSSavePanel { return true } |
| | | guard let settingsWindow else { return false } |
| | | return window.sheetParent == settingsWindow |
| | | || settingsWindow.attachedSheet == window |
| | | || window.parent == settingsWindow |
| | | } |
| | | |
| | | private func center(_ window: NSWindow, over rect: NSRect) { |
| | |
| | | } |
| | | |
| | | private func screenUnderMouse() -> NSScreen? { |
| | | let mousePoint = NSEvent.mouseLocation |
| | | return NSScreen.screens.first(where: { |
| | | NSMouseInRect(mousePoint, $0.frame, false) |
| | | }) ?? NSScreen.main ?? NSScreen.screens.first |
| | | overlayController.screenUnderMouse() |
| | | } |
| | | |
| | | /// Clean up settingsWindow reference when the Settings window closes. |
| | |
| | | let closingWindow = notification.object as? NSWindow, |
| | | closingWindow == self.settingsWindow |
| | | else { return } |
| | | let shouldRefocusOverlay = self.isOverlayVisible |
| | | let preferredScreen = self.overlayWindow?.screen |
| | | self.detachSettingsWindow(closingWindow) |
| | | self.settingsWindow = nil |
| | | self.refreshLauncherChromeState() |
| | | self.refreshLauncherChromeState( |
| | | activate: shouldRefocusOverlay && !self.overlayAvoidsSpaceSwitch, |
| | | avoidSpaceSwitch: self.overlayAvoidsSpaceSwitch |
| | | ) |
| | | guard shouldRefocusOverlay else { return } |
| | | DispatchQueue.main.asyncAfter(deadline: .now() + 0.12) { [weak self] in |
| | | self?.showOrFocusOverlay(preferredScreen: preferredScreen) |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /// Lower the overlay while text input is active so IME and cursor services are not hidden behind it. |
| | | /// Keep text-input overlays at the launcher level so Quick Search stays visible in fullscreen Spaces. |
| | | private func observeAppNoteEditing() { |
| | | NotificationCenter.default.addObserver( |
| | | forName: .tagLauncherAppNoteEditingChanged, |
| | |
| | | ) { [weak self] notification in |
| | | guard let self else { return } |
| | | self.isEditingAppNote = (notification.userInfo?["active"] as? Bool) ?? false |
| | | if self.isEditingAppNote { |
| | | self.promoteOverlayToForegroundInput() |
| | | } |
| | | self.updateOverlayLevelForTextInput() |
| | | } |
| | | } |
| | | |
| | | private func promoteOverlayToForegroundInput() { |
| | | if overlayAvoidsSpaceSwitch { |
| | | refreshLauncherChromeState(activate: false, avoidSpaceSwitch: true) |
| | | guard let overlayWindow else { return } |
| | | overlayWindow.makeKeyAndOrderFront(nil) |
| | | overlayWindow.orderFrontRegardless() |
| | | return |
| | | } |
| | | if quickSearchOnlyOverlaySession && !UserDefaults.standard.bool(forKey: Self.showDockIconKey) { |
| | | refreshLauncherChromeState(activate: false) |
| | | guard let overlayWindow else { return } |
| | | NSApp.activate(ignoringOtherApps: true) |
| | | overlayWindow.makeKeyAndOrderFront(nil) |
| | | overlayWindow.orderFrontRegardless() |
| | | return |
| | | } |
| | | beginLauncherForegroundOwnership() |
| | | guard let overlayWindow else { return } |
| | | overlayWindow.makeKeyAndOrderFront(nil) |
| | | overlayWindow.orderFrontRegardless() |
| | | } |
| | | |
| | | private func observeQuickSearch() { |
| | |
| | | queue: .main |
| | | ) { [weak self] notification in |
| | | guard let self else { return } |
| | | self.isQuickSearchOpen = (notification.userInfo?["active"] as? Bool) ?? false |
| | | let active = (notification.userInfo?["active"] as? Bool) ?? false |
| | | let hideOverlayOnClose = (notification.userInfo?["hideOverlayOnClose"] as? Bool) ?? false |
| | | self.isQuickSearchOpen = active |
| | | if active, hideOverlayOnClose { |
| | | self.quickSearchShouldHideOverlayOnClose = true |
| | | self.quickSearchOnlyOverlaySession = true |
| | | } |
| | | if self.isQuickSearchOpen { |
| | | self.promoteOverlayToForegroundInput() |
| | | } |
| | | self.updateOverlayLevelForTextInput() |
| | | if self.isQuickSearchOpen { |
| | | self.installQuickSearchExternalMouseMonitor() |
| | | } else { |
| | | self.removeQuickSearchExternalMouseMonitor() |
| | | if hideOverlayOnClose || self.quickSearchShouldHideOverlayOnClose { |
| | | self.quickSearchShouldHideOverlayOnClose = false |
| | | self.quickSearchOnlyOverlaySession = false |
| | | self.hideOverlay(force: true) |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | private func installQuickSearchExternalMouseMonitor() { |
| | | installQuickSearchLocalMouseMonitor() |
| | | guard quickSearchExternalMouseMonitor == nil else { return } |
| | | quickSearchExternalMouseMonitor = NSEvent.addGlobalMonitorForEvents( |
| | | matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown] |
| | |
| | | } |
| | | } |
| | | |
| | | private func installQuickSearchLocalMouseMonitor() { |
| | | guard quickSearchLocalMouseMonitor == nil else { return } |
| | | quickSearchLocalMouseMonitor = NSEvent.addLocalMonitorForEvents( |
| | | matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown] |
| | | ) { [weak self] event in |
| | | guard let self, self.isQuickSearchOpen else { return event } |
| | | if event.window === self.overlayWindow { |
| | | NotificationCenter.default.post(name: .tagLauncherQuickSearchDismissRequested, object: nil) |
| | | return nil |
| | | } |
| | | return event |
| | | } |
| | | } |
| | | |
| | | private func removeQuickSearchExternalMouseMonitor() { |
| | | if let quickSearchLocalMouseMonitor { |
| | | NSEvent.removeMonitor(quickSearchLocalMouseMonitor) |
| | | self.quickSearchLocalMouseMonitor = nil |
| | | } |
| | | if let quickSearchExternalMouseMonitor { |
| | | NSEvent.removeMonitor(quickSearchExternalMouseMonitor) |
| | | self.quickSearchExternalMouseMonitor = nil |
| | |
| | | } |
| | | } |
| | | |
| | | @objc private func openPreferences() { |
| | | private func observeExternalActivationRequests() { |
| | | DistributedNotificationCenter.default().addObserver( |
| | | self, |
| | | selector: #selector(handleExternalActivationRequest(_:)), |
| | | name: Self.externalActivationNotification, |
| | | object: Self.externalActivationObject |
| | | ) |
| | | } |
| | | |
| | | @objc private func handleExternalActivationRequest(_ notification: Notification) { |
| | | let shouldShowOverlay = notification.userInfo?["showOverlay"] as? Bool ?? false |
| | | guard shouldShowOverlay else { |
| | | suppressReopenUntil = Date().addingTimeInterval(1.0) |
| | | return |
| | | } |
| | | showOrFocusOverlay() |
| | | } |
| | | |
| | | @objc private func openPreferences(_ sender: Any? = nil) { |
| | | dismissQuickSearchIfNeeded() |
| | | TagDatabase.flushPendingCategorySchemeBackupBatch() |
| | | beginLauncherForegroundOwnership() |
| | | if overlayAvoidsSpaceSwitch { |
| | | refreshLauncherChromeState(activate: false, avoidSpaceSwitch: true) |
| | | } else { |
| | | beginLauncherForegroundOwnership() |
| | | } |
| | | // Don't hide overlay — keep it visible for real-time setting preview. |
| | | if let overlayWindow, overlayWindow.isVisible { |
| | | overlayWindow.makeKeyAndOrderFront(nil) |
| | |
| | | return |
| | | } |
| | | |
| | | let settingsSize = NSSize(width: 880, height: 460) |
| | | let settingsSize = NSSize(width: 1000, height: 480) |
| | | let window = NSWindow( |
| | | contentRect: NSRect(origin: .zero, size: settingsSize), |
| | | styleMask: [.titled, .closable], |
| | |
| | | prepareSettingsWindow(window) |
| | | } |
| | | |
| | | private func dismissQuickSearchIfNeeded() { |
| | | guard isQuickSearchOpen else { return } |
| | | isQuickSearchOpen = false |
| | | removeQuickSearchExternalMouseMonitor() |
| | | updateOverlayLevelForTextInput() |
| | | quickSearchShouldHideOverlayOnClose = false |
| | | quickSearchOnlyOverlaySession = false |
| | | NotificationCenter.default.post(name: .tagLauncherQuickSearchDismissRequested, object: nil) |
| | | } |
| | | |
| | | @objc private func switchLanguage(_ sender: NSMenuItem) { |
| | | guard let code = sender.representedObject as? String else { return } |
| | | L10n.switchTo(code) |
| | |
| | | |
| | | final class DismissibleHostingView<Content: View>: NSHostingView<Content> { |
| | | private let onBackdropTap: () -> Void |
| | | private var suppressBackdropDismiss = false |
| | | private var modalInteractionSuppressesBackdropDismiss = false |
| | | private var quickSearchSuppressesBackdropDismiss = false |
| | | private var modalInteractionObserver: NSObjectProtocol? |
| | | private var quickSearchVisibilityObserver: NSObjectProtocol? |
| | | |
| | | private var suppressBackdropDismiss: Bool { |
| | | modalInteractionSuppressesBackdropDismiss || quickSearchSuppressesBackdropDismiss |
| | | } |
| | | |
| | | @MainActor required init(rootView: Content) { |
| | | self.onBackdropTap = {} |
| | | super.init(rootView: rootView) |
| | | observeModalInteractionChanges() |
| | | installWindowServerAnchorLayer() |
| | | observeBackdropDismissSuppressionChanges() |
| | | } |
| | | |
| | | init(rootView: Content, onBackdropTap: @escaping () -> Void) { |
| | | self.onBackdropTap = onBackdropTap |
| | | super.init(rootView: rootView) |
| | | observeModalInteractionChanges() |
| | | installWindowServerAnchorLayer() |
| | | observeBackdropDismissSuppressionChanges() |
| | | } |
| | | |
| | | deinit { |
| | | if let modalInteractionObserver { |
| | | NotificationCenter.default.removeObserver(modalInteractionObserver) |
| | | } |
| | | if let quickSearchVisibilityObserver { |
| | | NotificationCenter.default.removeObserver(quickSearchVisibilityObserver) |
| | | } |
| | | } |
| | | |
| | | @available(*, unavailable) |
| | | required init?(coder: NSCoder) { fatalError() } |
| | | |
| | | private func installWindowServerAnchorLayer() { |
| | | wantsLayer = true |
| | | // A near-transparent backing pixel makes the WindowServer publish the panel immediately. |
| | | layer?.backgroundColor = NSColor.black.withAlphaComponent(0.001).cgColor |
| | | } |
| | | |
| | | override func mouseDown(with event: NSEvent) { |
| | | let location = convert(event.locationInWindow, from: nil) |
| | |
| | | return |
| | | } |
| | | if hit == self { |
| | | if quickSearchSuppressesBackdropDismiss { |
| | | NotificationCenter.default.post(name: .tagLauncherQuickSearchDismissRequested, object: nil) |
| | | return |
| | | } |
| | | if suppressBackdropDismiss { |
| | | super.mouseDown(with: event) |
| | | return |
| | |
| | | super.mouseDown(with: event) |
| | | } |
| | | |
| | | private func observeModalInteractionChanges() { |
| | | private func observeBackdropDismissSuppressionChanges() { |
| | | modalInteractionObserver = NotificationCenter.default.addObserver( |
| | | forName: .tagLauncherModalInteractionChanged, |
| | | object: nil, |
| | | queue: .main |
| | | ) { [weak self] notification in |
| | | self?.suppressBackdropDismiss = (notification.userInfo?["active"] as? Bool) ?? false |
| | | self?.modalInteractionSuppressesBackdropDismiss = (notification.userInfo?["active"] as? Bool) ?? false |
| | | } |
| | | |
| | | quickSearchVisibilityObserver = NotificationCenter.default.addObserver( |
| | | forName: .tagLauncherQuickSearchVisibilityChanged, |
| | | object: nil, |
| | | queue: .main |
| | | ) { [weak self] notification in |
| | | self?.quickSearchSuppressesBackdropDismiss = (notification.userInfo?["active"] as? Bool) ?? false |
| | | } |
| | | } |
| | | |