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() } }