From 5b8204116094cefd998ce575209c2afebac0320f Mon Sep 17 00:00:00 2001 From: Achieve Date: Wed, 25 Mar 2026 15:26:30 +0800 Subject: [PATCH] Handle Cmd+O in handleCustomShortcut to prevent Documents folder open (#2034) * Handle Cmd+O in handleCustomShortcut to prevent Documents folder open Cmd+O for "Open Folder" was only handled in SwiftUI menu, which can fail due to focus bugs when terminal is focused. This caused AppKit's default NSDocumentController to open the Documents folder instead. Now Cmd+O is intercepted in handleCustomShortcut like other shortcuts. Fixes #2010 Co-Authored-By: Claude Opus 4.6 (1M context) * Fix fallback directory loss and deduplicate Open Folder logic Address review feedback: 1. Pass selected directory URL to fallback window creation so the user's folder choice is not silently discarded 2. Replace inline NSOpenPanel code in cmuxApp.swift menu action with a call to AppDelegate.showOpenFolderPanel() to avoid future divergence between the two code paths Co-Authored-By: Claude Opus 4.6 (1M context) * Set NSOpenPanel directoryURL to current terminal working directory Address review feedback: set panel.directoryURL to the focused terminal's working directory so Open Folder starts in a contextually relevant location instead of AppKit's default. Co-Authored-By: Claude Opus 4.6 (1M context) * Use shared main-window resolver and openWorkspaceForExternalDirectory in showOpenFolderPanel Address review feedback: use preferredMainWindowContextForWorkspaceCreation for directory seeding (works when auxiliary windows are key) and openWorkspaceForExternalDirectory for workspace creation (ensures shouldBringToFront and consistent fallback behavior). --------- Co-authored-by: Claude Opus 4.6 (1M context) Co-authored-by: Lawrence Chen --- Sources/AppDelegate.swift | 32 ++++++++++++++++++++++++++++++++ Sources/cmuxApp.swift | 19 +------------------ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Sources/AppDelegate.swift b/Sources/AppDelegate.swift index d86e8c48..34aa3bc6 100644 --- a/Sources/AppDelegate.swift +++ b/Sources/AppDelegate.swift @@ -5385,6 +5385,30 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent _ = createMainWindow() } + /// Shows the "Open Folder" panel and creates a workspace for the selected directory. + /// Called from both the SwiftUI menu and `handleCustomShortcut`. + func showOpenFolderPanel() { + let panel = NSOpenPanel() + panel.canChooseFiles = false + panel.canChooseDirectories = true + panel.allowsMultipleSelection = false + panel.title = String(localized: "menu.file.openFolder.panelTitle", defaultValue: "Open Folder") + panel.prompt = String(localized: "menu.file.openFolder.panelPrompt", defaultValue: "Open") + // Seed the panel with the active workspace's directory. Use the shared + // main-window resolver so this works even when an auxiliary window is key. + if let context = preferredMainWindowContextForWorkspaceCreation(debugSource: "openFolderPanel.seed"), + let cwd = context.tabManager.selectedWorkspace?.currentDirectory, + !cwd.isEmpty { + panel.directoryURL = URL(fileURLWithPath: cwd) + } + if panel.runModal() == .OK, let url = panel.url { + openWorkspaceForExternalDirectory( + workingDirectory: url.path, + debugSource: "shortcut.openFolder" + ) + } + } + @objc func openWindow( _ pasteboard: NSPasteboard, userData: String?, @@ -9336,6 +9360,14 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent return true } + // Open Folder: Cmd+O + // Handled here to prevent AppKit's default NSDocumentController from opening + // the Documents folder when SwiftUI menu dispatch fails due to focus bugs. + if matchShortcut(event: event, shortcut: KeyboardShortcutSettings.shortcut(for: .openFolder)) { + showOpenFolderPanel() + return true + } + // Check Show Notifications shortcut if matchShortcut(event: event, shortcut: KeyboardShortcutSettings.shortcut(for: .showNotifications)) { toggleNotificationsPopover(animated: false, anchorView: fullscreenControlsViewModel?.notificationsAnchorView) diff --git a/Sources/cmuxApp.swift b/Sources/cmuxApp.swift index 28616f3e..f0de3cf0 100644 --- a/Sources/cmuxApp.swift +++ b/Sources/cmuxApp.swift @@ -591,24 +591,7 @@ struct cmuxApp: App { } splitCommandButton(title: String(localized: "menu.file.openFolder", defaultValue: "Open Folder…"), shortcut: openFolderMenuShortcut) { - let panel = NSOpenPanel() - panel.canChooseFiles = false - panel.canChooseDirectories = true - panel.allowsMultipleSelection = false - panel.title = String(localized: "menu.file.openFolder.panelTitle", defaultValue: "Open Folder") - panel.prompt = String(localized: "menu.file.openFolder.panelPrompt", defaultValue: "Open") - if panel.runModal() == .OK, let url = panel.url { - if let appDelegate = AppDelegate.shared { - if appDelegate.addWorkspaceInPreferredMainWindow( - workingDirectory: url.path, - debugSource: "menu.openFolder" - ) == nil { - appDelegate.openNewMainWindow(nil) - } - } else { - activeTabManager.addWorkspace(workingDirectory: url.path) - } - } + AppDelegate.shared?.showOpenFolderPanel() } }