From 3c04a4ce1597fad88a8775f24b5eb3b4e3debd4a Mon Sep 17 00:00:00 2001 From: lwouis Date: Tue, 23 Dec 2025 19:34:39 +0100 Subject: [PATCH] feat: avoid occasional delays in showing the switcher (closes #5109) --- src/ui/App.swift | 1 - src/ui/MainMenu.swift | 23 +++++++++++++++++------ src/ui/main-window/ThumbnailsPanel.swift | 9 +++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/ui/App.swift b/src/ui/App.swift index 0033f10f..62ee9aed 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -220,7 +220,6 @@ class App: AppCenterApplication { Logger.debug(shortcutIndex, self.shortcutIndex, isFirstSummon) App.app.appIsBeingUsed = true if isFirstSummon || shortcutIndex != self.shortcutIndex { - MainMenu.toggle(enabled: false) NSScreen.updatePreferred() if isVeryFirstSummon { Windows.sortByLevel() diff --git a/src/ui/MainMenu.swift b/src/ui/MainMenu.swift index a80eebe1..85a967b6 100644 --- a/src/ui/MainMenu.swift +++ b/src/ui/MainMenu.swift @@ -1,5 +1,6 @@ class MainMenu { private static var mainMenu: NSMenu! + private static var menuItemsWithShortcut = [NSMenuItem: String]() /// classic keyboard shortcuts like copy-and-paste are missing without a MainMenu. We generated the default menu from XCode and load it /// see https://stackoverflow.com/a/3746058/2249756 @@ -8,16 +9,26 @@ class MainMenu { Bundle.main.loadNibNamed("MainMenu", owner: self, topLevelObjects: &menuObjects) mainMenu = menuObjects?.first { $0 is NSMenu } as? NSMenu App.shared.mainMenu = mainMenu + rememberMenuItemsWithShortcut() } - /// we toggle the mainMenu off when showing the main window, and on when showing secondary windows static func toggle(enabled: Bool) { - if enabled { - if App.shared.mainMenu == nil { - App.shared.mainMenu = mainMenu + for (item, keyEquivalent) in menuItemsWithShortcut { + item.keyEquivalent = enabled ? keyEquivalent : "" + } + } + + private static func rememberMenuItemsWithShortcut() { + var stack: [NSMenu] = [mainMenu] + while let menu = stack.popLast() { + for item in menu.items { + if !item.keyEquivalent.isEmpty { + menuItemsWithShortcut[item] = item.keyEquivalent + } + if let submenu = item.submenu { + stack.append(submenu) + } } - } else { - App.shared.mainMenu = nil } } } diff --git a/src/ui/main-window/ThumbnailsPanel.swift b/src/ui/main-window/ThumbnailsPanel.swift index 741a3b32..13559ddc 100644 --- a/src/ui/main-window/ThumbnailsPanel.swift +++ b/src/ui/main-window/ThumbnailsPanel.swift @@ -99,6 +99,15 @@ extension ThumbnailsPanel: NSWindowDelegate { if App.app.appIsBeingUsed { App.app.thumbnailsPanel.makeKeyAndOrderFront(nil) } + MainMenu.toggle(enabled: true) + } + } + + func windowDidBecomeKey(_ notification: Notification) { + // we toggle the mainMenu off when showing the main window + // this avoids command+q from quitting AltTab itself, or command+p from printing + DispatchQueue.main.async { + MainMenu.toggle(enabled: false) } } } \ No newline at end of file