Implement drag & drop with the locality model
The second half: system drag sessions over phase 1's model, per DRAG-REORDER.md and 04-interactions.md § Drag & drop. - Card faces, lane headers, and trash rows drag as NSItemProvider sessions (two exported UTTypes, JSON payload in flatten order, plain-text titles as the secondary representation) — replacing m4's custom lane-reorder gesture and trash drag-out wholesale; the app-wide DragSession carries the members, the frozen dragged sizes, the live proposal, and the effective operation. - Three drop delegates (lane masonry, strip, window fallback), each accepting both types and routing internally per the single-target-dispatch rule; the cursor is the physical mouse converted to strip space; proposals come from DropSlotMath with hysteresis threaded through, and the lane-strip proposal clamps in front of the shown trash. - Locality picks the default — move within a board, copy across, the badge tracking live; ⌥ forces copy (ignored on within-board lane drags), ⌘ forces move; trash rows restore within their board (positional), copy out across boards by default, ⌘ forcing the true restore-move. - N contiguous shadows with reflow keyed on the proposal; the committed-overlay hold renders the dropped arrangement until the reload echo lands (1.5 s dissolution deadline for refused writes); the re-grounding trio: geometry re-derives per render, proposals re-validate by liveness at release, an emptied drag cancels itself. - Edge autoscroll (ticking driver over DragAutoScrollMath, re-targeting per step), the mouse-up-gated late-event cleanup, and the polling watchdog — the pathfinder's lifecycle traps, ported. - Store: moveLanes and multi-card restoreByDrag join the one-bracket drop commits. 784 unit tests. Claude-Session: https://claude.ai/code/session_01SR4XGjmBE16ZUYWpfFHXwY
This commit is contained in:
+107
-116
@@ -2,70 +2,6 @@ import AppKit
|
||||
import Observation
|
||||
import SwiftUI
|
||||
|
||||
// MARK: - The strip's half of a trash row's drag
|
||||
|
||||
/// What the strip lends the trash so a row can be dragged back onto the board (03-board-ui.md §
|
||||
/// Trash ▸ Drag-to-restore).
|
||||
///
|
||||
/// `LaneHeaderDrag`'s sibling, and for its reason: the gesture lives on the row, but the one thing it
|
||||
/// needs — *which lane is under the cursor* — is the strip's geometry, and it must be read at gesture
|
||||
/// time rather than at body-evaluation time.
|
||||
@MainActor
|
||||
struct TrashRowDrag {
|
||||
/// The live lane under an x in strip coordinates, or `nil` when the point is not over one — a
|
||||
/// gap, the outer margin, or the trash itself (`LaneLayoutMath.laneIndex`).
|
||||
let laneUnder: (CGFloat) -> ItemID?
|
||||
}
|
||||
|
||||
// MARK: - TrashDragSession
|
||||
|
||||
/// Window-local state for an in-flight drag out of the trash — `LaneReorderSession`'s sibling, and
|
||||
/// deliberately as small.
|
||||
///
|
||||
/// It holds only what the pointer contributes: which card, and which lane is currently under it.
|
||||
/// Everything else — the strip's geometry, the lanes themselves — is read fresh at render time, so a
|
||||
/// foreign reload mid-drag cannot leave this holding a stale board (04-interactions.md ▸ Drag and
|
||||
/// drop's re-grounding rule).
|
||||
@MainActor
|
||||
@Observable
|
||||
final class TrashDragSession {
|
||||
|
||||
/// The card being dragged out of the trash; `nil` when idle.
|
||||
private(set) var cardID: ItemID?
|
||||
|
||||
/// The live lane the pointer is over, or `nil` when it is over nothing droppable. Observed: the
|
||||
/// lanes read it to draw the drop highlight, which is this milestone's whole visual feedback.
|
||||
private(set) var targetLaneID: ItemID?
|
||||
|
||||
/// How far the pointer must travel before a click on a row becomes a drag — the same threshold
|
||||
/// the lane header uses, so the two gestures feel alike.
|
||||
static let threshold: CGFloat = 4
|
||||
|
||||
var isActive: Bool { cardID != nil }
|
||||
|
||||
func isDragging(_ id: ItemID) -> Bool { cardID == id }
|
||||
|
||||
func isTarget(_ id: ItemID) -> Bool { targetLaneID == id }
|
||||
|
||||
func begin(cardID: ItemID) {
|
||||
self.cardID = cardID
|
||||
targetLaneID = nil
|
||||
}
|
||||
|
||||
func update(targetLaneID: ItemID?) {
|
||||
guard isActive else { return }
|
||||
self.targetLaneID = targetLaneID
|
||||
}
|
||||
|
||||
/// Ends the drag, handing the caller nothing — the *commit* needs the current snapshot, which
|
||||
/// the view has and this session deliberately does not. Idempotent, because a gesture can end
|
||||
/// after the card it was carrying has already vanished.
|
||||
func end() {
|
||||
cardID = nil
|
||||
targetLaneID = nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - TrashLaneView
|
||||
|
||||
/// The trash quasi-lane: the trailing, visually distinct column tombstoned items live in
|
||||
@@ -81,8 +17,11 @@ final class TrashDragSession {
|
||||
/// the edge drag never reaches it (`LaneLayoutMath.totalUnits(of:trashUnits:)` supplies the unit,
|
||||
/// so there is no `Lane` value for any of those to act on);
|
||||
/// - it is **not draggable and not reorderable** — the header carries no gesture, and it is absent
|
||||
/// from the reorder proposal's `unitCounts` by construction, since `BoardView` builds that from
|
||||
/// the snapshot's live lanes;
|
||||
/// from the drop proposal's slot list by construction, since `BoardView` builds that from the
|
||||
/// snapshot's live lanes;
|
||||
/// - it is **never a drop target** — "no move or paste ever targets the trash" (04-interactions.md ▸
|
||||
/// The trash), so nothing here declares an `onDrop` at all and a session over the column falls
|
||||
/// through to the strip's own target, where a cursor over no lane simply holds the proposal;
|
||||
/// - it has **no new-card button**: nothing is created in the trash.
|
||||
///
|
||||
/// ### No editing in the trash
|
||||
@@ -110,12 +49,9 @@ struct TrashLaneView: View {
|
||||
/// does.
|
||||
let confirmations: TrashConfirmations
|
||||
|
||||
/// The strip's drop resolution — see `TrashRowDrag`.
|
||||
let drag: TrashRowDrag
|
||||
|
||||
/// The window's one drag-out session, owned by `BoardView` for the same lifetime the reorder
|
||||
/// session has.
|
||||
let dragSession: TrashDragSession
|
||||
/// The board window's drop machinery — a row's drag is an ordinary card session on the
|
||||
/// **trashed** side (`DragSession`, 04-interactions.md ▸ The trash).
|
||||
let drops: BoardDropContext
|
||||
|
||||
/// The strip's rubber band. The column's empty space is its third surface, on the **trashed**
|
||||
/// side — "a rubber-band stays on the side of the boundary it started on" (04-interactions.md ▸
|
||||
@@ -234,8 +170,7 @@ struct TrashLaneView: View {
|
||||
store: store,
|
||||
entry: entry,
|
||||
confirmations: confirmations,
|
||||
drag: drag,
|
||||
dragSession: dragSession,
|
||||
drops: drops,
|
||||
registry: marquee.registry
|
||||
)
|
||||
// A row is a tombstoned item, so it arrives and leaves in the card's dialect —
|
||||
@@ -295,8 +230,7 @@ private struct TrashEntryRow: View {
|
||||
let store: BoardStore
|
||||
let entry: TrashEntry
|
||||
let confirmations: TrashConfirmations
|
||||
let drag: TrashRowDrag
|
||||
let dragSession: TrashDragSession
|
||||
let drops: BoardDropContext
|
||||
|
||||
/// Where the rubber band looks up what it is sweeping — the card face's rule, on the trashed
|
||||
/// side (`View.marqueeTarget`).
|
||||
@@ -304,7 +238,38 @@ private struct TrashEntryRow: View {
|
||||
|
||||
private let cornerRadius: CGFloat = 6
|
||||
|
||||
/// **Lane entries are not draggable** (03-board-ui.md § Trash: "a lane entry is not draggable —
|
||||
/// its entry is a compact row, not the lane; its move-out is Put Back"), so the drag half is
|
||||
/// simply *absent* for them rather than refused — no session, no image, no snap-back. A click
|
||||
/// still selects either way.
|
||||
@ViewBuilder
|
||||
var body: some View {
|
||||
if entry.isLaneEntry {
|
||||
plate
|
||||
} else {
|
||||
plate.onDrag(startRowDrag, preview: { dragReplica })
|
||||
}
|
||||
}
|
||||
|
||||
private var plate: some View {
|
||||
rowFace
|
||||
// The row being dragged out dims in place — the source stays visible in the trash,
|
||||
// because a restore is not a removal until the write lands.
|
||||
.opacity(drops.session.isDragging(entry.id) ? 0.45 : 1)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture { select() }
|
||||
.marqueeTarget(entry.id, kind: entry.isLaneEntry ? .lane : .card, side: .trashed, in: registry)
|
||||
.contextMenu { menu }
|
||||
}
|
||||
|
||||
/// The plate's *appearance*, with none of its behaviour — no gesture, no context menu, and
|
||||
/// crucially no marquee registration.
|
||||
///
|
||||
/// The split exists for the drag replica, which renders this and nothing else: a drag image is a
|
||||
/// snapshot, and one built out of the live plate would re-register this row's frame from inside
|
||||
/// the preview's own geometry and then *deregister* it when the image went away, quietly
|
||||
/// stealing the row from the rubber band and the arrow keys.
|
||||
private var rowFace: some View {
|
||||
HStack(alignment: .firstTextBaseline, spacing: 6) {
|
||||
Image(systemName: ItemSymbol.name(entry.icon, fallback: symbolFallback))
|
||||
.foregroundStyle(.secondary)
|
||||
@@ -331,12 +296,6 @@ private struct TrashEntryRow: View {
|
||||
RoundedRectangle(cornerRadius: cornerRadius)
|
||||
.strokeBorder(isSelected ? AnyShapeStyle(Color.accentColor) : AnyShapeStyle(.clear), lineWidth: 1.5)
|
||||
)
|
||||
// The row being dragged out dims further, so the gesture reads even without a replica.
|
||||
.opacity(dragSession.isDragging(entry.id) ? 0.45 : 1)
|
||||
.contentShape(Rectangle())
|
||||
.gesture(rowGesture)
|
||||
.marqueeTarget(entry.id, kind: entry.isLaneEntry ? .lane : .card, side: .trashed, in: registry)
|
||||
.contextMenu { menu }
|
||||
}
|
||||
|
||||
private var symbolFallback: String {
|
||||
@@ -367,46 +326,78 @@ private struct TrashEntryRow: View {
|
||||
|
||||
// MARK: - Drag out
|
||||
|
||||
/// One gesture recognising the same click-versus-drag split the lane header uses: a plain click
|
||||
/// selects, and only movement past the threshold begins a drag out of the trash.
|
||||
/// Begins the row's drag out of the trash — an ordinary **card session on the trashed side**,
|
||||
/// which is the whole of what makes it a restore rather than a move (04-interactions.md ▸ The
|
||||
/// trash; `DragLocality.operation`).
|
||||
///
|
||||
/// **Lane entries are not draggable** (03-board-ui.md § Trash: "a lane entry is not draggable —
|
||||
/// its entry is a compact row, not the lane; its move-out is Put Back"), so the drag half is
|
||||
/// simply absent for them and the release still selects.
|
||||
/// Where it lands is the ordinary drop model's answer: `DropSlotMath.cardSlot` over the
|
||||
/// destination lane's masonry, so "restores it at the drop position" is the same arithmetic every
|
||||
/// other card drop uses. What a release *means* differs by locality and modifier, and that lives
|
||||
/// in one place (`BoardDropContext.commitDrop`): within the board a restore, ⌥ a live copy-out,
|
||||
/// across boards a live copy with ⌘ forcing the true restore-move.
|
||||
///
|
||||
/// The coordinate space is the strip's, because what the release needs is a *position* over the
|
||||
/// board, not a translation.
|
||||
private var rowGesture: some Gesture {
|
||||
DragGesture(minimumDistance: 0, coordinateSpace: .named(BoardView.stripSpace))
|
||||
.onChanged { value in
|
||||
guard isDraggable, !store.isReadOnly, !store.isEditingInline else { return }
|
||||
if !dragSession.isDragging(entry.id) {
|
||||
let travelled = max(abs(value.translation.width), abs(value.translation.height))
|
||||
guard travelled > TrashDragSession.threshold else { return }
|
||||
dragSession.begin(cardID: entry.id)
|
||||
}
|
||||
dragSession.update(targetLaneID: drag.laneUnder(value.location.x))
|
||||
/// **Multi-drag carries the whole trashed selection**, in the trash's own sorted order — the
|
||||
/// order the rows are drawn in, which is the only relative order a set of tombstones has.
|
||||
///
|
||||
/// Refused under the read-only lock and while an inline editor is focused, like every other
|
||||
/// mutating gesture.
|
||||
private func startRowDrag() -> NSItemProvider {
|
||||
guard !store.isReadOnly, !store.isEditingInline else { return NSItemProvider() }
|
||||
let selection = store.selection
|
||||
let ids: Set<ItemID> = selection.liveness == .trashed
|
||||
&& selection.ids.contains(entry.id)
|
||||
&& selection.ids.count > 1
|
||||
? selection.ids
|
||||
: [entry.id]
|
||||
|
||||
// Card entries only: a lane entry cannot be dragged at all, so one caught up in a mixed
|
||||
// selection is simply not carried. (The selection is homogeneous by kind anyway — this is
|
||||
// belt over braces.)
|
||||
let rows: [(id: ItemID, laneID: ItemID, title: String?)] = TrashModel.entries(of: store.snapshot)
|
||||
.compactMap { candidate in
|
||||
guard ids.contains(candidate.id), case let .card(card, laneID) = candidate else { return nil }
|
||||
return (card.id, laneID, card.title.value)
|
||||
}
|
||||
.onEnded { value in
|
||||
guard dragSession.isDragging(entry.id) else {
|
||||
select()
|
||||
return
|
||||
}
|
||||
dragSession.end()
|
||||
// A drop over anything but a live lane — the trash itself, a gap, the outer margin —
|
||||
// writes nothing. There is no replica to snap back; the row never left.
|
||||
guard let lane = drag.laneUnder(value.location.x) else { return }
|
||||
// m5-drag phase 2: the drop position comes from `DropSlotMath.cardSlot` once this
|
||||
// gesture is replaced by the real drag session. Until then the interim is the
|
||||
// destination lane's bottom, which is the index past its last rendered card.
|
||||
let bottom = store.snapshot.lanes
|
||||
.first { $0.id == lane }?
|
||||
.cards.filter { !$0.isDeleted }.count ?? 0
|
||||
store.restoreByDrag(cardID: entry.id, intoLane: lane, at: bottom)
|
||||
guard !rows.isEmpty else { return NSItemProvider() }
|
||||
|
||||
let root = store.rootURL
|
||||
let payload = DragPayload(
|
||||
boardRoot: root,
|
||||
kind: .cards,
|
||||
side: .trashed,
|
||||
items: rows.map {
|
||||
DragPayload.Item(
|
||||
id: $0.id.rawValue,
|
||||
folder: TrashModel.ItemPath(laneID: $0.laneID, cardID: $0.id).folder(under: root).path,
|
||||
title: $0.title
|
||||
)
|
||||
}
|
||||
)
|
||||
drops.session.beginCards(
|
||||
rows.map(\.id),
|
||||
folders: payload.folders,
|
||||
heights: rows.map { _ in LaneDropRegistry.nominalCardHeight },
|
||||
side: .trashed,
|
||||
source: store
|
||||
)
|
||||
return payload.itemProvider()
|
||||
}
|
||||
|
||||
private var isDraggable: Bool { !entry.isLaneEntry }
|
||||
/// The image under the cursor: the row as it is drawn, fanned with a count badge for a
|
||||
/// multi-drag — the card replica's treatment, at a trash row's size.
|
||||
private var dragReplica: some View {
|
||||
let count = store.selection.liveness == .trashed && store.selection.ids.contains(entry.id)
|
||||
? max(1, store.selection.ids.count)
|
||||
: 1
|
||||
return ZStack {
|
||||
if count > 2 { rowFace.offset(x: 10, y: 10).opacity(0.45) }
|
||||
if count > 1 { rowFace.offset(x: 5, y: 5).opacity(0.7) }
|
||||
rowFace
|
||||
}
|
||||
.frame(width: 200)
|
||||
.overlay(alignment: .topTrailing) { DragCountBadge(count: count) }
|
||||
.padding(12)
|
||||
}
|
||||
|
||||
// MARK: - The trash entry's context menu
|
||||
|
||||
|
||||
Reference in New Issue
Block a user