feat(outline): drag-reorder + zoom — S6 (#112)
CI / ci (push) Canceled after 0s

Pointer drag of a block run into any sibling seam or onto a block to
nest as its child (wf113 variant A: fat-gap 30/40/30 bands, seam
line at the future bullet column); the block selection rides the
drag, the moved run becomes the selection, and Esc cancels mid-gesture.
Double-click any bullet zooms to its children with breadcrumbs back
out. New OutlineTree extract/insertRun splice subtrees id-preserving;
the live row's NSTextView moved to OutlineRowTextView.

Audit round: the gesture is self-contained from arm time (strong
window + callback captures — a mid-drag scroll that drops the origin
row cannot strand drag state), a second arming cancels a stuck
predecessor, rowFrames prefers the grip frame, hitTestDrop is y-up
only, and the rails read DropGeometry. Pinned in OutlineDragTests
(monitors fire on sendEvent with a real window number).

[Generated by AI]
This commit is contained in:
AlexKucera committed 2026-10-16 14:49:57 +02:00
1 parent d722820fc8
commit 21e2f27450
14 files changed
+2196 -256

No files matched your search

+197
View File
@@ -7,6 +7,7 @@
// through deep trees, and setText mutation by node identity.
//
import CoreGraphics
import Foundation
@testable import Museion
import Testing
@@ -170,4 +171,200 @@ struct OutlinePresenterTests {
#expect(outNodes.map(\.text) == ["a", "a1", "a2", "a3"])
#expect(outNodes[1...].allSatisfy { node in node.children.isEmpty })
}
// #112 (S6): drag-reorder + zoom over the presenter.
@Test func `draggedIDs rides the selection deduped or goes solo`() {
let tree = [node("a", [node("a1", [node("a1x")]), node("a2")]), node("b"), node("c")]
// grab a selected row → the whole selection rides, preorder order,
// a selected ancestor carries its selected descendants
let sel = [tree[0].id, tree[0].children[0].id, tree[2].id]
#expect(
OutlinePresenter.draggedIDs(grabbing: tree[0].id, selected: sel, in: tree)
== [tree[0].id, tree[2].id],
)
// grab an UNselected row while a selection exists → solo
#expect(
OutlinePresenter.draggedIDs(grabbing: tree[1].id, selected: sel, in: tree)
== [tree[1].id],
)
// no selection at all → solo, works at any depth
#expect(
OutlinePresenter.draggedIDs(grabbing: tree[0].children[1].id, selected: nil, in: tree)
== [tree[0].children[1].id],
)
}
@Test func `isValidDrop rejects refs at or inside the moving run`() {
let tree = [node("a", [node("a1", [node("a1x")]), node("a2")]), node("b")]
// the ref itself moves
#expect(!OutlinePresenter.isValidDrop(moving: [tree[1].id], target: .before(tree[1].id), in: tree))
// the ref sits inside a moving subtree (any depth)
#expect(!OutlinePresenter.isValidDrop(moving: [tree[0].id], target: .lastChild(tree[0].children[0].id), in: tree))
#expect(!OutlinePresenter.isValidDrop(
moving: [tree[0].children[0].id],
target: .before(tree[0].children[0].children[0].id),
in: tree,
))
// dropping back into one's own PARENT as its last child is legal —
// extraction happens first, the target never ends up inside the run
#expect(OutlinePresenter.isValidDrop(moving: [tree[0].children[0].id], target: .lastChild(tree[0].id), in: tree))
// an unrelated row is fine
#expect(OutlinePresenter.isValidDrop(moving: [tree[0].id], target: .before(tree[1].id), in: tree))
// unknown ref → invalid
#expect(!OutlinePresenter.isValidDrop(moving: [tree[0].id], target: .before(UUID()), in: tree))
}
@Test func `applyDrop lands before afterLast and lastChild precisely`() {
// before: b moves up, directly before a
var beforeNodes = OutlineTree.parse("- a\n- b\n- c").nodes
#expect(OutlinePresenter.applyDrop(.before(beforeNodes[0].id), moving: [beforeNodes[1].id], in: &beforeNodes))
#expect(beforeNodes.map(\.text) == ["b", "a", "c"])
// afterLast: the end-of-list seam — lands after the LAST row at
// its depth, subtree pulled whole
var afterNodes = OutlineTree.parse("- a\n- b\n - b1\n- c").nodes
let movingID = afterNodes[0].id
#expect(OutlinePresenter.applyDrop(.afterLast(afterNodes[2].id), moving: [movingID], in: &afterNodes))
#expect(afterNodes.map(\.text) == ["b", "c", "a"])
// lastChild: onto — appended AFTER existing children (never first)
var ontoNodes = OutlineTree.parse("- a\n - a1\n- b").nodes
let bID = ontoNodes[1].id
#expect(OutlinePresenter.applyDrop(.lastChild(ontoNodes[0].id), moving: [bID], in: &ontoNodes))
#expect(ontoNodes.map(\.text) == ["a"])
#expect(ontoNodes[0].children.map(\.text) == ["a1", "b"])
// a seam below a deeper follower nests: dropping before a's first
// child (the seam between a and a1) makes it the FIRST child
var nestNodes = OutlineTree.parse("- a\n - a1\n- b").nodes
let a1ID = nestNodes[0].children[0].id
#expect(OutlinePresenter.applyDrop(.before(a1ID), moving: [nestNodes[1].id], in: &nestNodes))
#expect(nestNodes.map(\.text) == ["a"])
#expect(nestNodes[0].children.map(\.text) == ["b", "a1"])
}
@Test func `applyDrop moves a deduped batch as one contiguous run`() {
// a (with a1) and c selected together → they land adjacent, a
// before c, a's subtree riding inside
var nodes = OutlineTree.parse("- a\n - a1\n- b\n- c").nodes
let aID = nodes[0].id
let cID = nodes[2].id
#expect(OutlinePresenter.applyDrop(.afterLast(nodes[1].id), moving: [aID, cID], in: &nodes))
#expect(nodes.map(\.text) == ["b", "a", "c"])
#expect(nodes[1].children.map(\.text) == ["a1"])
// ids travel — focus/selection can follow the run
#expect(nodes[1].id == aID && nodes[2].id == cID)
}
@Test func `applyDrop no-ops a drop back at origin and refuses invalid targets`() {
// b is ALREADY immediately before c — extract + insert-before-c
// reproduces the tree → no-op, storage untouched
var nodes = OutlineTree.parse("- a\n- b\n- c").nodes
let snapshot = nodes
#expect(!OutlinePresenter.applyDrop(.before(nodes[2].id), moving: [nodes[1].id], in: &nodes))
#expect(nodes == snapshot) // storage untouched
// onto own subtree — invalid, dashed-indicator case
var selfNodes = OutlineTree.parse("- a\n - a1").nodes
let selfSnapshot = selfNodes
#expect(!OutlinePresenter.applyDrop(.lastChild(selfNodes[0].children[0].id), moving: [selfNodes[0].id], in: &selfNodes))
#expect(selfNodes == selfSnapshot)
// dropping onto one's own parent when ALREADY its last child → no-op
var tailNodes = OutlineTree.parse("- a\n - a1\n - a2").nodes
let tailSnapshot = tailNodes
#expect(!OutlinePresenter.applyDrop(.lastChild(tailNodes[0].id), moving: [tailNodes[0].children[1].id], in: &tailNodes))
#expect(tailNodes == tailSnapshot)
}
@Test func `applyDrop crosses parents through a drag`() {
// keyboard batch moves stay sibling-only; a drag is atomic
// extract+splice, so a1 can leave a and land among the roots
var nodes = OutlineTree.parse("- a\n - a1\n- b").nodes
let a1ID = nodes[0].children[0].id
#expect(OutlinePresenter.applyDrop(.before(nodes[1].id), moving: [a1ID], in: &nodes))
#expect(nodes.map(\.text) == ["a", "a1", "b"])
#expect(nodes[0].children.isEmpty)
}
@Test func `visibleRows zoom roots at the node with depths rebased`() {
let tree = [node("a", [node("a1", [node("a1x")]), node("a2")]), node("b")]
let rows = OutlinePresenter.visibleRows(tree, folded: [], zoomRootID: tree[0].id)
#expect(rows.map(\.text) == ["a1", "a1x", "a2"])
#expect(rows.map(\.depth) == [0, 1, 0]) // depth 2 re-based to 0
// the zoom root's own fold is ignored while it IS the root —
// its children are the document now
let folded = OutlinePresenter.visibleRows(tree, folded: [tree[0].id], zoomRootID: tree[0].id)
#expect(folded.map(\.text) == ["a1", "a1x", "a2"])
// folding INSIDE the zoom still hides
let inner = OutlinePresenter.visibleRows(tree, folded: [tree[0].children[0].id], zoomRootID: tree[0].id)
#expect(inner.map(\.text) == ["a1", "a2"])
#expect(inner[0].foldedDescendantCount == 1)
// unknown zoom id → the whole document
#expect(
OutlinePresenter.visibleRows(tree, folded: [], zoomRootID: UUID()).map(\.text)
== ["a", "a1", "a1x", "a2", "b"],
)
}
@Test func `rangeIDs under zoom is nil across the zoom boundary`() {
let tree = [node("a", [node("a1")]), node("b")]
// a is outside the zoom rooted at a — invisible → nil
#expect(OutlinePresenter.rangeIDs(
from: tree[0].id,
to: tree[1].id,
in: tree,
folded: [],
zoomRootID: tree[0].id,
) == nil)
// both ends inside → the span still works
#expect(OutlinePresenter.rangeIDs(
from: tree[0].children[0].id,
to: tree[0].children[0].id,
in: tree,
folded: [],
zoomRootID: tree[0].id,
) == [tree[0].children[0].id])
}
@Test func `hitTestDrop follows the fat-gap bands`() {
// window-space y-up frames (the only orientation both callers
// feed): a highest at y 60–90, c lowest at 0–30
let rowA = OutlineNode(text: "a")
let rowB = OutlineNode(text: "b")
let rowC = OutlineNode(text: "c")
let rows = OutlinePresenter.visibleRows([rowA, rowB, rowC], folded: [])
let frames: [UUID: CGRect] = [
rowA.id: CGRect(x: 0, y: 60, width: 400, height: 30),
rowB.id: CGRect(x: 0, y: 30, width: 400, height: 30),
rowC.id: CGRect(x: 0, y: 0, width: 400, height: 30),
]
let pairs = rows.compactMap { row in frames[row.id].map { (row, $0) } }
// pointer visually above a (y 95) → before a
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 95), rows: pairs) == .before(rowA.id))
// top 30% of b (y 55, rel 1/6 from the top) → the seam above b
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 55), rows: pairs) == .before(rowB.id))
// middle 40% of b (y 45) → onto (last child, ring)
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 45), rows: pairs) == .lastChild(rowB.id))
// bottom 30% of b (y 35) → the seam below → before the FOLLOWING row
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 35), rows: pairs) == .before(rowC.id))
// below c (y -10) → after c (end-of-list seam)
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: -10), rows: pairs) == .afterLast(rowC.id))
// rows without frames are simply unreachable
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 45), rows: []) == nil)
}
}