From 2d4ffcbfeeb6313c13081299a373c86886defd8f Mon Sep 17 00:00:00 2001 From: anthhub Date: Tue, 24 Mar 2026 12:11:00 +0800 Subject: [PATCH 1/4] 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) --- Sources/AppDelegate.swift | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Sources/AppDelegate.swift b/Sources/AppDelegate.swift index e17b02090a10..eae2652a8639 100644 --- a/Sources/AppDelegate.swift +++ b/Sources/AppDelegate.swift @@ -5363,6 +5363,25 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent _ = createMainWindow() } + /// Shows the "Open Folder" panel and creates a workspace for the selected directory. + /// Extracted so it can be 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") + if panel.runModal() == .OK, let url = panel.url { + if addWorkspaceInPreferredMainWindow( + workingDirectory: url.path, + debugSource: "shortcut.openFolder" + ) == nil { + openNewMainWindow(nil) + } + } + } + @objc func openWindow( _ pasteboard: NSPasteboard, userData: String?, @@ -9309,6 +9328,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) From 553d35cd2f14fa0dd769a94241a5101ba7c54b36 Mon Sep 17 00:00:00 2001 From: anthhub Date: Tue, 24 Mar 2026 12:25:06 +0800 Subject: [PATCH 2/4] 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) --- Sources/AppDelegate.swift | 2 +- Sources/cmuxApp.swift | 19 +------------------ 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/Sources/AppDelegate.swift b/Sources/AppDelegate.swift index eae2652a8639..6c02faef0bec 100644 --- a/Sources/AppDelegate.swift +++ b/Sources/AppDelegate.swift @@ -5377,7 +5377,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent workingDirectory: url.path, debugSource: "shortcut.openFolder" ) == nil { - openNewMainWindow(nil) + createMainWindow(initialWorkingDirectory: url.path) } } } diff --git a/Sources/cmuxApp.swift b/Sources/cmuxApp.swift index 1cc1f9fcf8fe..cb3437d1561a 100644 --- a/Sources/cmuxApp.swift +++ b/Sources/cmuxApp.swift @@ -587,24 +587,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() } } From d3538d4caad58a3c3adb5e28b3a092c30f4a53f6 Mon Sep 17 00:00:00 2001 From: anthhub Date: Tue, 24 Mar 2026 13:22:38 +0800 Subject: [PATCH 3/4] 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) --- Sources/AppDelegate.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/AppDelegate.swift b/Sources/AppDelegate.swift index 6c02faef0bec..3d1ecc27e435 100644 --- a/Sources/AppDelegate.swift +++ b/Sources/AppDelegate.swift @@ -5372,6 +5372,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent 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 let context = contextForMainWindow(NSApp.keyWindow), + let cwd = context.tabManager.selectedWorkspace?.currentDirectory, + !cwd.isEmpty { + panel.directoryURL = URL(fileURLWithPath: cwd) + } if panel.runModal() == .OK, let url = panel.url { if addWorkspaceInPreferredMainWindow( workingDirectory: url.path, From 43d8469c300e5d378e67bf51754aea22d4527131 Mon Sep 17 00:00:00 2001 From: Lawrence Chen Date: Wed, 25 Mar 2026 00:13:17 -0700 Subject: [PATCH 4/4] 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). --- Sources/AppDelegate.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/AppDelegate.swift b/Sources/AppDelegate.swift index 3d1ecc27e435..4eca83ff4aee 100644 --- a/Sources/AppDelegate.swift +++ b/Sources/AppDelegate.swift @@ -5364,7 +5364,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent } /// Shows the "Open Folder" panel and creates a workspace for the selected directory. - /// Extracted so it can be called from both the SwiftUI menu and `handleCustomShortcut`. + /// Called from both the SwiftUI menu and `handleCustomShortcut`. func showOpenFolderPanel() { let panel = NSOpenPanel() panel.canChooseFiles = false @@ -5372,18 +5372,18 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent 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 let context = contextForMainWindow(NSApp.keyWindow), + // 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 { - if addWorkspaceInPreferredMainWindow( + openWorkspaceForExternalDirectory( workingDirectory: url.path, debugSource: "shortcut.openFolder" - ) == nil { - createMainWindow(initialWorkingDirectory: url.path) - } + ) } }