Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5473,6 +5473,38 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
return true
}

@discardableResult
func showRightSidebarModeInActiveMainWindow(
mode: RightSidebarMode,
focusFirstItem: Bool = true,
preferredWindow: NSWindow? = nil
) -> Bool {
guard mode.isAvailable() else {
return false
}

let context = preferredRegisteredMainWindowContext(preferredWindow: preferredWindow)
if let context {
let window = context.window ?? windowForMainWindowId(context.windowId)
if let window {
mainWindowVisibilityController.focusForInWindowCommand(window, reason: .rightSidebarFocus)
}
return context.keyboardFocusCoordinator.focusRightSidebar(
mode: mode,
focusFirstItem: focusFirstItem
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sidebar state mutated before focus result causes false beep

Medium Severity

When showRightSidebarModeInActiveMainWindow delegates to context.keyboardFocusCoordinator.focusRightSidebar, the internal implementation of focusRightSidebar unconditionally calls state.setVisible(true) and sets state.mode before determining the focus result. If focus ultimately fails (endpoint and fallback both fail), focusRightSidebar returns false, which propagates back to callers in SidebarFooterButtons.showFileExplorer and handleCommandPaletteRightSidebarMode, causing NSSound.beep(). The user sees the sidebar open but also hears an error beep — contradicting the PR's goal of not partially applying sidebar state on failure.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 3ca32d9. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changed: this is not a real failure path in the current coordinator contract. Once context.fileExplorerState exists, focusRightSidebar sets the pending request and returns success even if the concrete endpoint is not mounted yet; the false-return paths are validated before any sidebar mutation in showRightSidebarModeInActiveMainWindow.

— Claude Code

}

guard let state = fileExplorerState else {
return false
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
if state.mode != mode {
state.mode = mode
}
state.setVisible(true)
return true
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}

func applyRightSidebarRemoteCommand(
_ command: RightSidebarRemoteCommand,
target: RightSidebarRemoteTarget = RightSidebarRemoteTarget()
Expand Down
14 changes: 4 additions & 10 deletions Sources/ContentView+RightSidebarCommandPalette.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,13 @@ extension ContentView {
}

func handleCommandPaletteRightSidebarMode(_ mode: RightSidebarMode, observedWindow: NSWindow?) {
guard mode.isAvailable() else {
NSSound.beep()
return
}
if AppDelegate.shared?.focusRightSidebarInActiveMainWindow(
guard AppDelegate.shared?.showRightSidebarModeInActiveMainWindow(
mode: mode,
focusFirstItem: true,
preferredWindow: observedWindow ?? NSApp.keyWindow ?? NSApp.mainWindow
) != true {
fileExplorerState.setVisible(true)
if fileExplorerState.mode != mode {
fileExplorerState.mode = mode
}
) == true else {
NSSound.beep()
return
}
}

Expand Down
48 changes: 48 additions & 0 deletions Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10504,15 +10504,63 @@ private struct SidebarFooter: View {
private struct SidebarFooterButtons: View {
@ObservedObject var updateViewModel: UpdateViewModel
@ObservedObject var fileExplorerState: FileExplorerState
@ObservedObject private var keyboardShortcutSettingsObserver = KeyboardShortcutSettingsObserver.shared
let onSendFeedback: () -> Void

var body: some View {
let _ = keyboardShortcutSettingsObserver.revision
HStack(spacing: 4) {
SidebarFileExplorerButton(
isSelected: fileExplorerState.isVisible && fileExplorerState.mode == .files,
title: KeyboardShortcutSettings.Action.switchRightSidebarToFiles.label,
helpText: KeyboardShortcutSettings.Action.switchRightSidebarToFiles.tooltip(
KeyboardShortcutSettings.Action.switchRightSidebarToFiles.label
),
action: showFileExplorer
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
SidebarHelpMenuButton(onSendFeedback: onSendFeedback)
UpdatePill(model: updateViewModel)
}
.frame(maxWidth: .infinity, alignment: .leading)
}

private func showFileExplorer() {
let preferredWindow = NSApp.keyWindow ?? NSApp.mainWindow
guard AppDelegate.shared?.showRightSidebarModeInActiveMainWindow(
mode: .files,
focusFirstItem: true,
preferredWindow: preferredWindow
) == true else {
NSSound.beep()
return
}
}
Comment thread
austinywang marked this conversation as resolved.
Resolved
}

private struct SidebarFileExplorerButton: View {
let isSelected: Bool
let title: String
let helpText: String
let action: () -> Void

private let buttonSize: CGFloat = 22
private let iconSize: CGFloat = 11

var body: some View {
Button(action: action) {
Image(systemName: "folder")
.symbolRenderingMode(.monochrome)
.font(.system(size: iconSize, weight: .medium))
.foregroundStyle(isSelected ? Color.accentColor : Color(nsColor: .secondaryLabelColor))
.frame(width: buttonSize, height: buttonSize, alignment: .center)
}
.buttonStyle(SidebarFooterIconButtonStyle())
.frame(width: buttonSize, height: buttonSize, alignment: .center)
.accessibilityElement(children: .ignore)
.safeHelp(helpText)
.accessibilityLabel(title)
.accessibilityIdentifier("SidebarFileExplorerButton")
}
}

private struct FeedbackComposerMessageEditor: NSViewRepresentable {
Expand Down
76 changes: 76 additions & 0 deletions cmuxTests/RightSidebarCommandPaletteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,71 @@ final class RightSidebarCommandPaletteTests: XCTestCase {
}
}

@MainActor
func testShowRightSidebarModeUsesGlobalStateWhenNoMainWindowContextExists() {
withSavedRightSidebarDefaults {
let previousAppDelegate = AppDelegate.shared
let appDelegate = AppDelegate()
let fileExplorerState = FileExplorerState()
defer { AppDelegate.shared = previousAppDelegate }

fileExplorerState.mode = .find
fileExplorerState.setVisible(false)
appDelegate.fileExplorerState = fileExplorerState

XCTAssertTrue(
appDelegate.showRightSidebarModeInActiveMainWindow(
mode: .files,
focusFirstItem: true,
preferredWindow: nil
)
)
XCTAssertTrue(fileExplorerState.isVisible)
XCTAssertEqual(fileExplorerState.mode, .files)
}
}

@MainActor
func testShowRightSidebarModeDoesNotMutateGlobalStateWhenRegisteredFocusFails() {
withSavedRightSidebarDefaults {
let previousAppDelegate = AppDelegate.shared
let appDelegate = AppDelegate()
let fileExplorerState = FileExplorerState()
defer { AppDelegate.shared = previousAppDelegate }

fileExplorerState.mode = .find
fileExplorerState.setVisible(false)
appDelegate.fileExplorerState = fileExplorerState
appDelegate.registerMainWindowContextForTesting(
tabManager: TabManager(),
fileExplorerState: nil
)
guard let context = appDelegate.mainWindowContexts.values.first else {
XCTFail("Expected a registered main-window context")
return
}

XCTAssertTrue(appDelegate.fileExplorerState === fileExplorerState)
XCTAssertNil(context.fileExplorerState)
XCTAssertFalse(
context.keyboardFocusCoordinator.focusRightSidebar(
mode: .files,
focusFirstItem: true
)
)

XCTAssertFalse(
appDelegate.showRightSidebarModeInActiveMainWindow(
mode: .files,
focusFirstItem: true,
preferredWindow: nil
)
)
XCTAssertFalse(fileExplorerState.isVisible)
XCTAssertEqual(fileExplorerState.mode, .find)
}
}

private func withSavedBetaFeatureDefaults(_ body: () throws -> Void) rethrows {
let defaults = UserDefaults.standard
let previousDock = defaults.object(forKey: RightSidebarBetaFeatureSettings.dockEnabledKey)
Expand All @@ -66,6 +131,17 @@ final class RightSidebarCommandPaletteTests: XCTestCase {
try body()
}

private func withSavedRightSidebarDefaults(_ body: () throws -> Void) rethrows {
let defaults = UserDefaults.standard
let previousVisibility = defaults.object(forKey: "fileExplorer.isVisible")
let previousMode = defaults.object(forKey: "rightSidebar.mode")
defer {
restore(previousVisibility, forKey: "fileExplorer.isVisible")
restore(previousMode, forKey: "rightSidebar.mode")
}
try body()
}

private func restore(_ value: Any?, forKey key: String) {
let defaults = UserDefaults.standard
if let value {
Expand Down