Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
117 changes: 99 additions & 18 deletions Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16279,23 +16279,32 @@ struct TabItemView: View, Equatable {
}

private func updateSelection() {
#if DEBUG
let mods = NSEvent.modifierFlags
var modStr = ""
if mods.contains(.command) { modStr += "cmd " }
if mods.contains(.shift) { modStr += "shift " }
if mods.contains(.option) { modStr += "opt " }
if mods.contains(.control) { modStr += "ctrl " }
cmuxDebugLog("sidebar.select workspace=\(tab.id.uuidString.prefix(5)) modifiers=\(modStr.isEmpty ? "none" : modStr.trimmingCharacters(in: .whitespaces))")
#endif
let modifiers = NSEvent.modifierFlags
let isCommand = modifiers.contains(.command)
let isShift = modifiers.contains(.shift)
let wasSelected = tabManager.selectedTabId == tab.id
#if DEBUG
var modStr = ""
if modifiers.contains(.command) { modStr += "cmd " }
if modifiers.contains(.shift) { modStr += "shift " }
if modifiers.contains(.option) { modStr += "opt " }
if modifiers.contains(.control) { modStr += "ctrl " }
cmuxDebugLog("sidebar.select workspace=\(tab.id.uuidString.prefix(5)) modifiers=\(modStr.isEmpty ? "none" : modStr.trimmingCharacters(in: .whitespaces))")
#endif

let workspaceIds = tabManager.tabs.map(\.id)
let shiftAnchorIndex = isShift
? SidebarWorkspaceSelectionSyncPolicy.shiftClickAnchorIndex(
existingAnchorIndex: lastSidebarSelectionIndex,
selectedWorkspaceIds: selectedTabIds,
focusedWorkspaceId: tabManager.selectedTabId,
liveWorkspaceIds: workspaceIds
)
: nil

if isShift, let lastIndex = lastSidebarSelectionIndex {
let lower = min(lastIndex, index)
let upper = max(lastIndex, index)
if isShift, let anchorIndex = shiftAnchorIndex {
let lower = min(anchorIndex, index)
let upper = max(anchorIndex, index)
// Filter out workspaces hidden inside collapsed groups so a
// Shift-click range never silently includes rows the user
// can't see (e.g. clicking a collapsed group's anchor and
Expand Down Expand Up @@ -16332,7 +16341,11 @@ struct TabItemView: View, Equatable {
selectedTabIds = [tab.id]
}

lastSidebarSelectionIndex = index
lastSidebarSelectionIndex = SidebarWorkspaceSelectionSyncPolicy.anchorIndexAfterWorkspaceClick(
isShiftClick: isShift,
resolvedShiftAnchorIndex: shiftAnchorIndex,
clickedIndex: index
)
tabManager.selectTab(tab)
if wasSelected, !isCommand, !isShift {
tabManager.dismissNotificationOnDirectInteraction(
Expand Down Expand Up @@ -17633,6 +17646,64 @@ enum SidebarWorkspaceSelectionSyncPolicy {
}
return liveWorkspaceIds.firstIndex { selectedWorkspaceIds.contains($0) }
}

static func anchorWorkspaceId(
existingAnchorIndex: Int?,
liveWorkspaceIds: [UUID]
) -> UUID? {
guard let existingAnchorIndex,
liveWorkspaceIds.indices.contains(existingAnchorIndex) else {
return nil
}
return liveWorkspaceIds[existingAnchorIndex]
}

static func shiftClickAnchorIndex(
existingAnchorIndex: Int?,
selectedWorkspaceIds: Set<UUID>,
focusedWorkspaceId: UUID?,
liveWorkspaceIds: [UUID]
) -> Int? {
if let existingAnchorIndex,
liveWorkspaceIds.indices.contains(existingAnchorIndex) {
return existingAnchorIndex
}
if selectedWorkspaceIds.count == 1,
let selectedWorkspaceId = selectedWorkspaceIds.first,
let selectedIndex = liveWorkspaceIds.firstIndex(of: selectedWorkspaceId) {
return selectedIndex
}
if let focusedWorkspaceId {
return liveWorkspaceIds.firstIndex(of: focusedWorkspaceId)
}
return nil
}
Comment on lines +17661 to +17680

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Anchor still tracked by index, not workspace identity, for non-drag mutations

shiftClickAnchorIndex returns existingAnchorIndex whenever it falls in-bounds, without verifying the workspace at that position is the one originally anchored. The drag-reorder path is now correctly protected by the pre-reorder UUID snapshot, but any mutation that shifts the list without going through reorderSidebarWorkspace — workspace close, workspace create, or group collapse that renumbers rows — can silently move the anchor to a different workspace. If workspace B at index 0 is closed while the anchor sits at index 2, the next Shift-click anchors to whatever is now at index 2 rather than the original anchor workspace. Converting lastSidebarSelectionIndex from Int? to UUID? would make the anchor invariant to all mutations without special-casing each mutating path.

Rule Used: Flag Swift fixes that patch symptoms while leaving... (source)


static func anchorIndexAfterWorkspaceClick(
isShiftClick: Bool,
resolvedShiftAnchorIndex: Int?,
clickedIndex: Int
) -> Int {
isShiftClick ? (resolvedShiftAnchorIndex ?? clickedIndex) : clickedIndex
}

static func anchorIndexAfterWorkspaceReorder(
preferredAnchorWorkspaceId: UUID?,
selectedWorkspaceIds: Set<UUID>,
focusedWorkspaceId: UUID?,
liveWorkspaceIds: [UUID]
) -> Int? {
if let preferredAnchorWorkspaceId,
selectedWorkspaceIds.contains(preferredAnchorWorkspaceId),
let anchorIndex = liveWorkspaceIds.firstIndex(of: preferredAnchorWorkspaceId) {
return anchorIndex
}
return anchorIndex(
preferredWorkspaceId: focusedWorkspaceId,
selectedWorkspaceIds: selectedWorkspaceIds,
liveWorkspaceIds: liveWorkspaceIds
)
}
}

@MainActor
Expand Down Expand Up @@ -17869,21 +17940,27 @@ struct SidebarTabDropDelegate: DropDelegate {
#if DEBUG
cmuxDebugLog("sidebar.drop.noop from=\(fromIndex) to=\(targetIndex)")
#endif
syncSidebarSelection()
return true
}

#if DEBUG
cmuxDebugLog("sidebar.drop.commit tab=\(draggedTabId.uuidString.prefix(5)) from=\(fromIndex) to=\(targetIndex)")
#endif
let selectionBeforeReorder = selectedTabIds
let anchorWorkspaceIdBeforeReorder = SidebarWorkspaceSelectionSyncPolicy.anchorWorkspaceId(
existingAnchorIndex: lastSidebarSelectionIndex,
liveWorkspaceIds: tabManager.tabs.map(\.id)
)
let didReorder = tabManager.reorderSidebarWorkspace(
tabId: draggedTabId,
toIndex: targetIndex,
isDragOperation: true,
usesTopLevelRows: usesTopLevelRows
)
syncSidebarSelection(preserving: selectionBeforeReorder)
syncSidebarSelection(
preserving: selectionBeforeReorder,
preferredAnchorWorkspaceId: anchorWorkspaceIdBeforeReorder
)
return didReorder
}

Expand Down Expand Up @@ -18048,17 +18125,21 @@ struct SidebarTabDropDelegate: DropDelegate {
}
}

private func syncSidebarSelection(preserving previousSelectionIds: Set<UUID>) {
private func syncSidebarSelection(
preserving previousSelectionIds: Set<UUID>,
preferredAnchorWorkspaceId: UUID?
) {
let liveWorkspaceIds = tabManager.tabs.map(\.id)
let nextSelectionIds = SidebarWorkspaceSelectionSyncPolicy.reconciledSelection(
previousSelectionIds: previousSelectionIds,
liveWorkspaceIds: liveWorkspaceIds,
fallbackSelectedWorkspaceId: tabManager.selectedTabId
)
selectedTabIds = nextSelectionIds
lastSidebarSelectionIndex = SidebarWorkspaceSelectionSyncPolicy.anchorIndex(
preferredWorkspaceId: tabManager.selectedTabId,
lastSidebarSelectionIndex = SidebarWorkspaceSelectionSyncPolicy.anchorIndexAfterWorkspaceReorder(
preferredAnchorWorkspaceId: preferredAnchorWorkspaceId,
selectedWorkspaceIds: nextSelectionIds,
focusedWorkspaceId: tabManager.selectedTabId,
liveWorkspaceIds: liveWorkspaceIds
)
}
Expand Down
127 changes: 119 additions & 8 deletions cmuxTests/WorkspaceUnitTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import XCTest
import Testing
import AppKit
import SwiftUI
import UniformTypeIdentifiers
Expand Down Expand Up @@ -3852,9 +3853,11 @@ final class SidebarWorkspaceAuxiliaryDetailVisibilityTests: XCTestCase {
}


final class SidebarWorkspaceSelectionSyncPolicyTests: XCTestCase {
@Suite
struct SidebarWorkspaceSelectionSyncPolicyTests {
@MainActor
func testReconciledSelectionPreservesMultiSelectionAfterReorder() {
@Test
func reconciledSelectionPreservesMultiSelectionAfterReorder() {
let first = UUID()
let second = UUID()
let third = UUID()
Expand All @@ -3867,19 +3870,19 @@ final class SidebarWorkspaceSelectionSyncPolicyTests: XCTestCase {
fallbackSelectedWorkspaceId: second
)

XCTAssertEqual(result, previousSelection)
XCTAssertEqual(
#expect(result == previousSelection)
#expect(
SidebarWorkspaceSelectionSyncPolicy.anchorIndex(
preferredWorkspaceId: second,
selectedWorkspaceIds: result,
liveWorkspaceIds: [first, third, fourth, second]
),
3
) == 3
)
}

@MainActor
func testReconciledSelectionFallsBackToActiveWorkspaceWhenPreviousSelectionIsGone() {
@Test
func reconciledSelectionFallsBackToActiveWorkspaceWhenPreviousSelectionIsGone() {
let first = UUID()
let second = UUID()
let removed = UUID()
Expand All @@ -3890,7 +3893,115 @@ final class SidebarWorkspaceSelectionSyncPolicyTests: XCTestCase {
fallbackSelectedWorkspaceId: second
)

XCTAssertEqual(result, [second])
#expect(result == Set([second]))
}

@MainActor
@Test
func anchorWorkspaceIdReadsTheExistingAnchorIdentity() {
let first = UUID()
let second = UUID()
let third = UUID()

#expect(
SidebarWorkspaceSelectionSyncPolicy.anchorWorkspaceId(
existingAnchorIndex: 1,
liveWorkspaceIds: [first, second, third]
) == second
)
#expect(
SidebarWorkspaceSelectionSyncPolicy.anchorWorkspaceId(
existingAnchorIndex: 3,
liveWorkspaceIds: [first, second, third]
) == nil
)
}

@MainActor
@Test
func reorderKeepsRangeAnchorByWorkspaceIdentityInsteadOfFocus() {
let first = UUID()
let second = UUID()
let third = UUID()
let fourth = UUID()

let anchorIndex = SidebarWorkspaceSelectionSyncPolicy.anchorIndexAfterWorkspaceReorder(
preferredAnchorWorkspaceId: first,
selectedWorkspaceIds: [first, second, third],
focusedWorkspaceId: third,
liveWorkspaceIds: [second, third, first, fourth]
)

#expect(anchorIndex == 2)
}

@MainActor
@Test
func reorderFallsBackToFocusedWorkspaceWhenRangeAnchorIsNoLongerSelected() {
let first = UUID()
let second = UUID()
let third = UUID()

let anchorIndex = SidebarWorkspaceSelectionSyncPolicy.anchorIndexAfterWorkspaceReorder(
preferredAnchorWorkspaceId: first,
selectedWorkspaceIds: [second, third],
focusedWorkspaceId: third,
liveWorkspaceIds: [second, third, first]
)

#expect(anchorIndex == 1)
}

@MainActor
@Test
func shiftClickAnchorFallsBackToSingleSidebarSelection() {
let first = UUID()
let second = UUID()
let third = UUID()

let anchorIndex = SidebarWorkspaceSelectionSyncPolicy.shiftClickAnchorIndex(
existingAnchorIndex: nil,
selectedWorkspaceIds: [first],
focusedWorkspaceId: second,
liveWorkspaceIds: [first, second, third]
)

#expect(anchorIndex == 0)
}

@MainActor
@Test
func shiftClickKeepsExistingAnchorWhileFocusMoves() {
let first = UUID()
let second = UUID()
let third = UUID()

let anchorIndex = SidebarWorkspaceSelectionSyncPolicy.shiftClickAnchorIndex(
existingAnchorIndex: 0,
selectedWorkspaceIds: [first, second],
focusedWorkspaceId: second,
liveWorkspaceIds: [first, second, third]
)
let nextAnchorIndex = SidebarWorkspaceSelectionSyncPolicy.anchorIndexAfterWorkspaceClick(
isShiftClick: true,
resolvedShiftAnchorIndex: anchorIndex,
clickedIndex: 2
)

#expect(anchorIndex == 0)
#expect(nextAnchorIndex == 0)
}

@MainActor
@Test
func nonShiftClickMovesSidebarSelectionAnchor() {
let nextAnchorIndex = SidebarWorkspaceSelectionSyncPolicy.anchorIndexAfterWorkspaceClick(
isShiftClick: false,
resolvedShiftAnchorIndex: 0,
clickedIndex: 2
)

#expect(nextAnchorIndex == 2)
}
}

Expand Down
Loading