Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
public import Foundation

/// One visible sidebar row as seen by the live-reorder preview.
public struct SidebarWorkspaceReorderPreviewRow: Equatable, Sendable {
public let workspaceId: UUID
public let groupId: UUID?
public let isGroupHeader: Bool

public init(workspaceId: UUID, groupId: UUID?, isGroupHeader: Bool) {
self.workspaceId = workspaceId
self.groupId = groupId
self.isGroupHeader = isGroupHeader
}
}

/// The row order the sidebar should show while a drag hovers a resolved plan,
/// expressed as source indices into the input rows.
public struct SidebarWorkspaceReorderPreviewOrder: Equatable, Sendable {
/// Indices into the input rows, in the order the preview should display.
public let order: [Int]

/// Indices into the input rows forming the dragged block (header first
/// for group drags, single element for workspace drags).
public let draggedBlock: [Int]

/// The group the dragged workspace would join when dropped here, `nil`
/// for a top-level slot. Drives the floating row's indent preview.
public let destinationGroupId: UUID?
}

/// Maps a resolved drop plan's indicator onto a concrete visible row order so
/// the table can animate rows apart while the drag is still in flight. The
/// commit stays plan-driven; this only decides what the preview shows, and it
/// is derived from the same indicator the resolver would render, so the
/// settled preview and the committed order agree.
public struct SidebarWorkspaceReorderPreviewPermutation: Sendable {
public init() {}

/// Resolves the preview order for a plan, or `nil` when the indicator
/// cannot be mapped onto the visible rows (the caller should keep the
/// last valid preview).
public func previewOrder(
rows: [SidebarWorkspaceReorderPreviewRow],
draggedWorkspaceId: UUID,
indicator: SidebarDropIndicator,
scope: SidebarWorkspaceReorderDropIndicatorScope
) -> SidebarWorkspaceReorderPreviewOrder? {
guard let block = draggedBlockIndices(rows: rows, draggedWorkspaceId: draggedWorkspaceId),
!block.isEmpty else {
return nil
}
// An indicator pointing at the dragged block itself is a no-op slot.
if let tabId = indicator.tabId, block.contains(where: { rows[$0].workspaceId == tabId }) {
return SidebarWorkspaceReorderPreviewOrder(
order: Array(rows.indices),
draggedBlock: block,
destinationGroupId: destinationGroupId(scope: scope)
)
}

let blockSet = Set(block)
let remaining = rows.indices.filter { !blockSet.contains($0) }
guard let insertionOffset = insertionOffset(
rows: rows,
remaining: remaining,
indicator: indicator,
scope: scope
) else {
return nil
}

var order: [Int] = []
order.reserveCapacity(rows.count)
order.append(contentsOf: remaining.prefix(insertionOffset))
order.append(contentsOf: block)
order.append(contentsOf: remaining.dropFirst(insertionOffset))
return SidebarWorkspaceReorderPreviewOrder(
order: order,
draggedBlock: block,
destinationGroupId: destinationGroupId(scope: scope)
)
}

/// The dragged rows as one unit: a workspace row alone, or a group header
/// plus its contiguous member rows when the dragged id is a group anchor.
private func draggedBlockIndices(
rows: [SidebarWorkspaceReorderPreviewRow],
draggedWorkspaceId: UUID
) -> [Int]? {
if let headerIndex = rows.firstIndex(where: { $0.isGroupHeader && $0.workspaceId == draggedWorkspaceId }) {
let groupId = rows[headerIndex].groupId
var block = [headerIndex]
var next = headerIndex + 1
while next < rows.count, !rows[next].isGroupHeader, rows[next].groupId == groupId, groupId != nil {
block.append(next)
next += 1
}
return block
}
guard let rowIndex = rows.firstIndex(where: { !$0.isGroupHeader && $0.workspaceId == draggedWorkspaceId }) else {
return nil
}
return [rowIndex]
}

/// Where in the remaining (block-removed) rows the block re-inserts.
private func insertionOffset(
rows: [SidebarWorkspaceReorderPreviewRow],
remaining: [Int],
indicator: SidebarDropIndicator,
scope: SidebarWorkspaceReorderDropIndicatorScope
) -> Int? {
guard let tabId = indicator.tabId else {
return remaining.count
}
if case .group(let groupId) = scope {
// Group scope: the indicator names a member row (or the anchor
// when the slot is directly below a header). Insert against that
// row's edge inside the group span.
if let memberOffset = remaining.firstIndex(where: { index in
let row = rows[index]
return !row.isGroupHeader && row.groupId == groupId && row.workspaceId == tabId
}) {
return indicator.edge == .top ? memberOffset : memberOffset + 1
}
if let headerOffset = remaining.firstIndex(where: { index in
let row = rows[index]
return row.isGroupHeader && row.groupId == groupId
}) {
return headerOffset + 1
}
return nil
}
// Top-level/raw scope: the indicator names a top-level row. When that
// row is a group header (anchor id), a bottom edge means below the
// whole group block, not between the header and its first member.
guard let targetOffset = remaining.firstIndex(where: { rows[$0].workspaceId == tabId }) else {
return nil
}
if indicator.edge == .top {
return targetOffset
}
let targetRow = rows[remaining[targetOffset]]
guard targetRow.isGroupHeader, let groupId = targetRow.groupId else {
return targetOffset + 1
}
var offset = targetOffset + 1
while offset < remaining.count {
let row = rows[remaining[offset]]
if row.isGroupHeader || row.groupId != groupId { break }
offset += 1
}
return offset
}

private func destinationGroupId(
scope: SidebarWorkspaceReorderDropIndicatorScope
) -> UUID? {
guard case .group(let groupId) = scope else { return nil }
return groupId

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 Badge Derive indent from commit action, not indicator scope

indicatorScope describes where to render the indicator, not necessarily whether the workspace will join that group: the resolver has root-lane group-boundary plans that render with .group(...) while committing usesTopLevelRows: true with explicitGroupId == nil. Returning the scope's group here makes the floating row indent as if it will become a group member even though the drop commits a top-level move, so root drops around expanded/collapsed groups preview the wrong hierarchy. Please derive this from the reorder action's explicit destination group instead of the render scope.

Useful? React with 👍 / 👎.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import Foundation
import Testing
@testable import CmuxFoundation

@Suite struct SidebarWorkspaceReorderPreviewPermutationTests {
private let a = UUID()
private let b = UUID()
private let c = UUID()
private let d = UUID()
private let groupAnchor = UUID()
private let member1 = UUID()
private let member2 = UUID()
private let groupId = UUID()

private func row(_ id: UUID, group: UUID? = nil, header: Bool = false) -> SidebarWorkspaceReorderPreviewRow {
SidebarWorkspaceReorderPreviewRow(workspaceId: id, groupId: group, isGroupHeader: header)
}

/// a b c d, drag a below c → b c a d
@Test func movesWorkspaceBelowTarget() throws {
let rows = [row(a), row(b), row(c), row(d)]
let preview = try #require(SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: a,
indicator: SidebarDropIndicator(tabId: c, edge: .bottom),
scope: .raw
))
#expect(preview.order == [1, 2, 0, 3])
#expect(preview.draggedBlock == [0])
#expect(preview.destinationGroupId == nil)
}

/// a b c d, drag d above b → a d b c
@Test func movesWorkspaceAboveTarget() throws {
let rows = [row(a), row(b), row(c), row(d)]
let preview = try #require(SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: d,
indicator: SidebarDropIndicator(tabId: b, edge: .top),
scope: .raw
))
#expect(preview.order == [0, 3, 1, 2])
}

/// nil tabId inserts at the end of the list.
@Test func nilTargetAppendsAtEnd() throws {
let rows = [row(a), row(b), row(c)]
let preview = try #require(SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: a,
indicator: SidebarDropIndicator(tabId: nil, edge: .bottom),
scope: .raw
))
#expect(preview.order == [1, 2, 0])
}

/// An indicator that points at the dragged row itself keeps the order.
@Test func selfTargetIsIdentity() throws {
let rows = [row(a), row(b), row(c)]
let preview = try #require(SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: b,
indicator: SidebarDropIndicator(tabId: b, edge: .top),
scope: .raw
))
#expect(preview.order == [0, 1, 2])
}

/// Dragging a group anchor picks up the header and its contiguous member
/// rows as one block.
@Test func anchorDragMovesWholeBlock() throws {
let rows = [
row(a),
row(groupAnchor, group: groupId, header: true),
row(member1, group: groupId),
row(member2, group: groupId),
row(b),
]
let preview = try #require(SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: groupAnchor,
indicator: SidebarDropIndicator(tabId: b, edge: .bottom),
scope: .topLevel
))
#expect(preview.draggedBlock == [1, 2, 3])
#expect(preview.order == [0, 4, 1, 2, 3])
}

/// Top-level scope: a bottom edge against a group header lands below the
/// whole expanded group, not between the header and its first member.
@Test func bottomOfHeaderSkipsGroupMembers() throws {
let rows = [
row(a),
row(groupAnchor, group: groupId, header: true),
row(member1, group: groupId),
row(member2, group: groupId),
row(b),
]
let preview = try #require(SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: a,
indicator: SidebarDropIndicator(tabId: groupAnchor, edge: .bottom),
scope: .topLevel
))
#expect(preview.order == [1, 2, 3, 0, 4])
#expect(preview.destinationGroupId == nil)
}

/// Group scope: inserting below a member row joins the group and reports
/// the destination group for the indent preview.
@Test func groupScopeInsertsAmongMembers() throws {
let rows = [
row(a),
row(groupAnchor, group: groupId, header: true),
row(member1, group: groupId),
row(member2, group: groupId),
row(b),
]
let preview = try #require(SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: a,
indicator: SidebarDropIndicator(tabId: member1, edge: .bottom),
scope: .group(groupId)
))
#expect(preview.order == [1, 2, 0, 3, 4])
#expect(preview.destinationGroupId == groupId)
}

/// Group scope with the anchor as target (slot directly below a header,
/// e.g. a collapsed group) inserts right after the header row.
@Test func groupScopeAnchorTargetInsertsBelowHeader() throws {
let rows = [
row(a),
row(groupAnchor, group: groupId, header: true),
row(b),
]
let preview = try #require(SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: a,
indicator: SidebarDropIndicator(tabId: groupAnchor, edge: .bottom),
scope: .group(groupId)
))
#expect(preview.order == [1, 0, 2])
#expect(preview.destinationGroupId == groupId)
}

/// Unknown indicator target refuses (caller keeps the last preview).
@Test func unknownTargetReturnsNil() {
let rows = [row(a), row(b)]
let preview = SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: a,
indicator: SidebarDropIndicator(tabId: UUID(), edge: .top),
scope: .raw
)
#expect(preview == nil)
}

/// Dragged id missing from the rows refuses.
@Test func missingDraggedRowReturnsNil() {
let rows = [row(a), row(b)]
let preview = SidebarWorkspaceReorderPreviewPermutation().previewOrder(
rows: rows,
draggedWorkspaceId: UUID(),
indicator: SidebarDropIndicator(tabId: a, edge: .top),
scope: .raw
)
#expect(preview == nil)
}
}
11 changes: 11 additions & 0 deletions Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11468,6 +11468,17 @@ struct VerticalTabsSidebar: View, Equatable {
performWorkspaceDrop: { point, targets in
performWorkspaceReorderDrop(point: point, targets: targets, renderContext: renderContext)
},
resolveWorkspaceReorderPlan: { point, targets in
guard activateSidebarWorkspaceDragIfNeeded() else { return nil }
return workspaceReorderPlan(point: point, targets: targets, renderContext: renderContext)
},
commitWorkspaceReorderPlan: { plan in
defer {
dragState.clearDrag()
dragAutoScrollController.stop()
}
return performWorkspaceReorderPlan(plan)
},
clearWorkspaceDropIndicator: {
dragState.clearDropIndicator()
dragAutoScrollController.stop()
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sidebar/AppKitList/SidebarWorkspaceTableActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ struct SidebarWorkspaceTableActions {
let isValidWorkspaceDrag: () -> Bool
let updateWorkspaceDrag: (CGPoint, [SidebarWorkspaceReorderDropOverlay.Target]) -> Bool
let performWorkspaceDrop: (CGPoint, [SidebarWorkspaceReorderDropOverlay.Target]) -> Bool
/// Plan resolution/commit for the local live-reorder preview. Both run
/// the same resolver as `performWorkspaceDrop`, split so the preview can
/// show a plan continuously and commit exactly what it last showed.
let resolveWorkspaceReorderPlan: (CGPoint, [SidebarWorkspaceReorderDropOverlay.Target]) -> SidebarWorkspaceReorderDropPlan?
let commitWorkspaceReorderPlan: (SidebarWorkspaceReorderDropPlan) -> Bool
let clearWorkspaceDropIndicator: () -> Void
let currentDropIndicator: () -> SidebarDropIndicator?
let currentDropIndicatorScope: () -> SidebarWorkspaceReorderDropIndicatorScope
Expand Down
Loading