From 30957260ceb25c9e96b9b11784311ae82becfc63 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Mon, 16 Nov 2026 16:05:54 -0500 Subject: [PATCH] Branch colours in the forest: a first child keeps its parent's, each further child starts the next A pane's colour (an index into a palette of HUES) is fixed when it opens and stored with the view; the first tree's root trail has none. A view stored before colours is coloured as if opened in tree order. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab --- src/ui/forks.ts | 89 ++++++++++++++++++++++++++++--------- src/ui/store.ts | 4 +- tests/forks.test.ts | 105 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 175 insertions(+), 23 deletions(-) diff --git a/src/ui/forks.ts b/src/ui/forks.ts index 03c4149..3b07ad0 100644 --- a/src/ui/forks.ts +++ b/src/ui/forks.ts @@ -12,8 +12,15 @@ * so a forest that did not change is the same object (the store memoises on identity). A node's * `id` names the pane for as long as it is open (keys, curves, the hosts of article panes off the * branch). + * + * Each pane has its branch's colour (`hue`), fixed when it opens: a pane opened as its parent's first + * child takes its parent's, any further child starts the next colour of the palette, so a plain trail + * is one colour and each fork starts a new one. The first tree's root trail has none (plain ink). */ +/** How many colours branches cycle through (style.css `--hue-0` …). */ +export const HUES = 7; + export interface Tree { ref: R; kids: Tree[]; @@ -28,12 +35,16 @@ export interface Tree { col?: number; /** Its place in its column, once the panes there have been put in an order by hand (dragged); unranked panes follow, in tree order. */ rank?: number; + /** Its branch's colour, 0 to HUES − 1; none on the first tree's root trail. Kept for the pane's life. */ + hue?: number; } /** The trees, the first starting at the top node; `at` is the one the focused branch is in. */ export interface Forest { trees: Tree[]; at: number; + /** How many branch colours have been started: the next is this one, round the palette. */ + hues?: number; } export type Same = (a: R, b: R) => boolean; @@ -54,6 +65,25 @@ export const plain = (refs: R[]): Forest => { return { trees: t ? [t] : [], at: 0 }; }; +/** A tree with this colour on every pane (none: as it is). */ +const tint = (t: Tree, hue: number | undefined): Tree => (hue === undefined ? t : { ...t, hue, kids: t.kids.map((k) => tint(k, hue)) }); + +/** Colours started by one operation: the next of the palette each time, never the parent's own. */ +function palette(f: Forest) { + let n = f.hues ?? 0; + return { + next(parent?: number): number { + let h = n++ % HUES; + if (h === parent) h = n++ % HUES; + return h; + }, + /** The forest, counting the colours started. */ + done(g: Forest): Forest { + return n === (g.hues ?? 0) ? g : { ...g, hues: n }; + }, + }; +} + /** The column a tree's root is in. */ export const colOf = (t: Tree) => t.col ?? 0; @@ -185,7 +215,7 @@ export function focus(f: Forest, id: number): Forest { if (!hit) return f; const t = select(f.trees[hit.ti], id)!; if (t === f.trees[hit.ti] && hit.ti === f.at) return f; - return { trees: t === f.trees[hit.ti] ? f.trees : f.trees.map((x, j) => (j === hit.ti ? t : x)), at: hit.ti }; + return { ...f, trees: t === f.trees[hit.ti] ? f.trees : f.trees.map((x, j) => (j === hit.ti ? t : x)), at: hit.ti }; } /** @@ -203,7 +233,7 @@ function replace(f: Forest, ti: number, t: Tree | null, orphan const trees = [...f.trees.slice(0, ti), ...(t ? [t] : []), ...orphans, ...f.trees.slice(ti + 1)]; const was = f.trees[f.at]; const at = trees.indexOf(was); - return { trees, at: at >= 0 ? at : Math.min(ti, trees.length - 1) }; + return { ...f, trees, at: at >= 0 ? at : Math.min(ti, trees.length - 1) }; } /** Remove a child of a node, picking the one that takes its place (the one below, else the one above). */ @@ -224,18 +254,22 @@ export function open(f: Forest, i: number, ref: R, fork: boolean, sa const t = focused(f); if (!t) return plain([ref]); const orphans: Tree[] = []; + const hues = palette(f); const next = at(t, i, (n) => { const k = n.kids.findIndex((c) => same(c.ref, ref)); if (k >= 0) return k === n.pick ? n : { ...n, pick: k }; - if (!n.kids.length) return { ...n, kids: [leaf(ref)], pick: 0 }; - if (fork || n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, leaf(ref)], pick: n.kids.length }; - orphans.push(...survivors(n.kids[n.pick], colOf(t) + i + 1, true)); + // The first child goes on in its parent's colour; another starts the next one. + if (!n.kids.length) return { ...n, kids: [tint(leaf(ref), n.hue)], pick: 0 }; + if (fork || n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, tint(leaf(ref), hues.next(n.hue))], pick: n.kids.length }; + const was = n.kids[n.pick]; + orphans.push(...survivors(was, colOf(t) + i + 1, true)); const kids = n.kids.slice(); - // In its place, in its column too. - kids[n.pick] = n.kids[n.pick].rank === undefined ? leaf(ref) : { ...leaf(ref), rank: n.kids[n.pick].rank }; + // In its place, in its column and its colour too. + const put = tint(leaf(ref), was.hue); + kids[n.pick] = was.rank === undefined ? put : { ...put, rank: was.rank }; return { ...n, kids }; }); - return replace(f, f.at, next, orphans); + return hues.done(replace(f, f.at, next, orphans)); } /** @@ -330,8 +364,9 @@ export function withBranch(f: Forest, refs: R[], same: Same): For const old = f.trees[0] && !f.trees[0].col ? f.trees[0] : null; if (old) orphans.push(...survivors(old, 0, false)); const rest = old ? f.trees.slice(1) : f.trees; - return { trees: [chain(refs)!, ...orphans, ...rest], at: 0 }; + return { ...f, trees: [chain(refs)!, ...orphans, ...rest], at: 0 }; } + const hues = palette(f); const go = (n: Tree, i: number): Tree => { if (i === refs.length) { const kept = n.kids.filter((k) => k.pinned); @@ -342,20 +377,20 @@ export function withBranch(f: Forest, refs: R[], same: Same): For const k = n.kids.findIndex((c) => same(c.ref, refs[i])); if (k < 0) { const rest = chain(refs.slice(i))!; - if (!n.kids.length) return { ...n, kids: [rest], pick: 0 }; - if (n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, rest], pick: n.kids.length }; + if (!n.kids.length) return { ...n, kids: [tint(rest, n.hue)], pick: 0 }; + if (n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, tint(rest, hues.next(n.hue))], pick: n.kids.length }; orphans.push(...survivors(n.kids[n.pick], i, true)); - return { ...n, kids: n.kids.map((c, j) => (j === n.pick ? rest : c)) }; + return { ...n, kids: n.kids.map((c, j) => (j === n.pick ? tint(rest, c.hue) : c)) }; } const sub = go(n.kids[k], i + 1); return sub === n.kids[k] && k === n.pick ? n : { ...n, kids: n.kids.map((c, j) => (j === k ? sub : c)), pick: k }; }; const t = go(f.trees[0], 1); - const g = replace(f, 0, t, orphans); + const g = hues.done(replace(f, 0, t, orphans)); return g.at === 0 ? g : { ...g, at: 0 }; } -/** As stored: refs (made storable by the caller), children, picks, pins and an orphan's column; no ids, nothing remembered. */ +/** As stored: refs (made storable by the caller), children, picks, pins, an orphan's column, places and colours; no ids, nothing remembered. */ export interface Saved { ref: R; kids?: Saved[]; @@ -363,6 +398,7 @@ export interface Saved { pin?: 1; col?: number; rank?: number; + hue?: number; } export function save(t: Tree, ref: (r: R) => O): Saved { @@ -372,6 +408,7 @@ export function save(t: Tree, ref: (r: R) => O): Saved { if (t.pinned) out.pin = 1; if (t.col) out.col = t.col; if (t.rank !== undefined) out.rank = t.rank; + if (t.hue !== undefined) out.hue = t.hue; return out; } @@ -385,32 +422,44 @@ export function load(o: unknown, ok: (r: unknown) => r is R): Tree | if (s.pin) t.pinned = true; if (typeof s.col === "number" && s.col >= 1) t.col = Math.floor(s.col); if (typeof s.rank === "number" && Number.isFinite(s.rank)) t.rank = s.rank; + if (typeof s.hue === "number" && Number.isInteger(s.hue) && s.hue >= 0 && s.hue < HUES) t.hue = s.hue; return t; } /** * What was stored: the forest; from before orphans, one tree; from before forks, the trail as a - * tree with one branch. An orphan stored first (its tree's root gone) is dropped. + * tree with one branch. An orphan stored first (its tree's root gone) is dropped. Stored from + * before colours (no count of them), the panes are coloured as if opened in tree order. */ -export function restore(saved: { forest?: unknown; at?: unknown; forks?: unknown; trail?: unknown[] }, ok: (r: unknown) => r is R): Forest { +export function restore(saved: { forest?: unknown; at?: unknown; forks?: unknown; trail?: unknown[]; hues?: unknown }, ok: (r: unknown) => r is R): Forest { + let f: Forest; if (Array.isArray(saved.forest)) { const trees = saved.forest.map((o) => load(o, ok)).filter((t): t is Tree => !!t); const first = trees.findIndex((t) => !t.col); const kept = first < 0 ? trees.filter((t) => t.col) : [trees[first], ...trees.filter((t, j) => j !== first && t.col)]; const at = typeof saved.at === "number" ? Math.floor(saved.at) : 0; - return { trees: kept, at: at >= 0 && at < kept.length ? at : 0 }; + f = { trees: kept, at: at >= 0 && at < kept.length ? at : 0 }; + } else { + const t = saved.forks ? load(saved.forks, ok) : chain((saved.trail ?? []).filter(ok)); + f = { trees: t ? [t] : [], at: 0 }; } - const t = saved.forks ? load(saved.forks, ok) : chain((saved.trail ?? []).filter(ok)); - return { trees: t ? [t] : [], at: 0 }; + if (typeof saved.hues === "number" && Number.isInteger(saved.hues) && saved.hues >= 0) return { ...f, hues: saved.hues }; + const hues = palette(f); + const paint = (t: Tree, hue: number | undefined): Tree => { + const kids = t.kids.map((k, j) => paint(k, j === 0 ? hue : hues.next(hue))); + return hue === undefined ? { ...t, kids } : { ...t, kids, hue }; + }; + return hues.done({ ...f, trees: f.trees.map((t) => paint(t, t.col ? hues.next() : undefined)) }); } -/** Whether a forest is well formed: picks in range, no pane twice among siblings, ids unique, orphans in a column right of the first, the focused tree there. */ +/** Whether a forest is well formed: picks in range, colours in the palette, no pane twice among siblings, ids unique, orphans in a column right of the first, the focused tree there. */ export function valid(f: Forest, same: Same): boolean { const seen = new Set(); const ok = (n: Tree): boolean => { if (seen.has(n.id)) return false; seen.add(n.id); if (n.pick < 0 || n.pick >= Math.max(1, n.kids.length) || !Number.isInteger(n.pick)) return false; + if (n.hue !== undefined && !(Number.isInteger(n.hue) && n.hue >= 0 && n.hue < HUES)) return false; for (let a = 0; a < n.kids.length; a++) for (let b = a + 1; b < n.kids.length; b++) if (same(n.kids[a].ref, n.kids[b].ref)) return false; return n.kids.every((k) => k.col === undefined && ok(k)); }; diff --git a/src/ui/store.ts b/src/ui/store.ts index a608127..3617e7d 100644 --- a/src/ui/store.ts +++ b/src/ui/store.ts @@ -1206,7 +1206,7 @@ class Store { } private restoreView() { const s = this.doc.snap(); - const v = lsGet<{ open: NodeId[]; focus: Focus | null; hoist: NodeId | null; forest?: unknown; at?: number; forks?: unknown; trail?: PaneRef[]; paneAt?: number }>(this.viewKey(), { open: [], focus: null, hoist: null }); + const v = lsGet<{ open: NodeId[]; focus: Focus | null; hoist: NodeId | null; forest?: unknown; at?: number; hues?: number; forks?: unknown; trail?: PaneRef[]; paneAt?: number }>(this.viewKey(), { open: [], focus: null, hoist: null }); this.open = new Set(v.open.filter((id) => s.nodes[id])); this.hoist = v.hoist && s.nodes[v.hoist] ? v.hoist : null; this.focus = v.focus && s.nodes[v.focus.node] ? v.focus : { node: this.top, grain: "node" }; @@ -1219,7 +1219,7 @@ class Store { saveView() { const bare = (p: PaneRef): PaneRef => ({ ...p, from: undefined }); // The trail too, for a build from before forks. - lsSet(this.viewKey(), { open: [...this.open], focus: this.focus, hoist: this.hoist, forest: this.forest.trees.map((t) => saveTree(t, bare)), at: this.forest.at, trail: this.trail.map(bare), paneAt: this.paneAt }); + lsSet(this.viewKey(), { open: [...this.open], focus: this.focus, hoist: this.hoist, forest: this.forest.trees.map((t) => saveTree(t, bare)), at: this.forest.at, hues: this.forest.hues ?? 0, trail: this.trail.map(bare), paneAt: this.paneAt }); } /** Keep focus on something that exists and is on screen. */ private repairView() { diff --git a/tests/forks.test.ts b/tests/forks.test.ts index f84468d..39b8ae7 100644 --- a/tests/forks.test.ts +++ b/tests/forks.test.ts @@ -1,7 +1,7 @@ /** The panes view's forest of forks (src/ui/forks.ts): each operation, and any sequence of them leaving a well-formed forest with one focused branch and one active pane. */ import fc from "fast-check"; import { describe, expect, it } from "vitest"; -import { all, besideInColumn, close, columns, find, focus, focused, load, open, path, pin, plain, prune, reorder, restore, save, valid, withBranch, type Forest, type Tree } from "../src/ui/forks"; +import { HUES, all, besideInColumn, close, columns, find, focus, focused, load, open, path, pin, plain, prune, reorder, restore, save, valid, withBranch, type Forest, type Tree } from "../src/ui/forks"; type F = Forest; type T = Tree; @@ -201,6 +201,8 @@ describe("forks", () => { let f: F = plain(["root"]); // The active pane, by id: on the focused branch. let active = f.trees[0].id; + // Each pane's colour, as first seen: it never changes while the pane is open. + const hueOf = new Map(); for (const o of ops) { const p = path(focused(f)); const i = Math.max(0, p.findIndex((n) => n.id === active)); @@ -256,6 +258,11 @@ describe("forks", () => { // Exactly one active pane: it is open, once, and on the focused branch. if (all(f).filter((x) => x.node.id === active).length !== 1 || !b.some((n) => n.id === active)) return false; if (!find(f, active) || f.trees[0].ref !== "root") return false; + if (f.trees[0].hue !== undefined) return false; + for (const x of all(f)) { + if (hueOf.has(x.node.id) && hueOf.get(x.node.id) !== x.node.hue) return false; + hueOf.set(x.node.id, x.node.hue); + } } return true; }), @@ -263,3 +270,99 @@ describe("forks", () => { ); }); }); + +describe("branch colours", () => { + const isStr = (r: unknown): r is string => typeof r === "string"; + /** Each pane's colour by its ref, "-" for none. */ + const hues = (f: F) => Object.fromEntries(all(f).map((x) => [x.node.ref, x.node.hue ?? "-"])); + const roundTrip = (f: F) => restore(JSON.parse(JSON.stringify({ forest: f.trees.map((t) => save(t, (r) => r)), at: f.at, hues: f.hues ?? 0 })), isStr); + + it("a plain trail is one colour, the first tree's none; each further child of a pane starts the next colour", () => { + let f: F = plain(["a", "b", "c"]); + expect(hues(f)).toEqual({ a: "-", b: "-", c: "-" }); + f = open(f, 0, "x", true, same); // a second child of a: a new colour + f = open(f, 1, "x1", true, same); // x's first child: x's colour + f = open(f, 2, "x2", false, same); + f = open(f, 1, "y", true, same); // a second child of x + f = open(f, 0, "z", true, same); // a third child of a + expect(hues(f)).toEqual({ a: "-", b: "-", c: "-", x: 0, x1: 0, x2: 0, y: 1, z: 2 }); + expect(f.hues).toBe(3); + }); + + it("the palette goes round, never giving a fork its parent's colour", () => { + let f: F = plain(["a"]); + for (let i = 0; i < HUES + 2; i++) f = open(focus(f, f.trees[0].id), 0, `k${i}`, true, same); + // k0 is a's first child (no colour); k1… take 0, 1, … round the palette. + expect(all(f).slice(1).map((x) => x.node.hue ?? "-")).toEqual(["-", ...Array.from({ length: HUES + 1 }, (_, i) => i % HUES)]); + // Forks of a coloured pane skip its colour when the palette comes round to it. + let g: F = plain(["a"]); + g = open(g, 0, "b", true, same); + g = open(g, 0, "c", true, same); // 0 + for (let i = 0; i < HUES - 1; i++) g = open(focus(g, g.trees[0].id), 0, `p${i}`, true, same); // 1 … HUES − 1 + g = open(focus(g, idOf(g, "c")), 1, "c1", true, same); // c's first: 0 + g = open(focus(g, idOf(g, "c")), 1, "c2", true, same); // next would be 0, c's own + expect(all(g).find((x) => x.node.ref === "c2")!.node.hue).toBe(1); + }); + + it("a pane opened in place of another takes its colour; one beside a pinned pane starts a new one; so does withBranch", () => { + let f: F = plain(["a", "b"]); + f = open(f, 0, "x", true, same); + f = open(f, 0, "y", true, same); + f = open(focus(f, idOf(f, "x")), 0, "x'", false, same); // in x's place + expect(hues(f)).toEqual({ a: "-", b: "-", "x'": 0, y: 1 }); + f = pin(f, idOf(f, "x'"), true); + f = open(f, 0, "w", false, same); // beside a pinned pane: a fork + expect(hues(f).w).toBe(2); + let g: F = plain(["a", "b"]); + g = open(g, 0, "x", true, same); + g = withBranch(g, ["a", "x", "x1", "x2"], same); + expect(hues(g)).toMatchObject({ x: 0, x1: 0, x2: 0 }); + g = withBranch(g, ["a", "x", "q"], same); // in x1's place + expect(hues(g).q).toBe(0); + expect(hues(withBranch(g, ["r", "s"], same))).toMatchObject({ r: "-", s: "-" }); + }); + + it("colours stay through a reorder, a close, a pin and a prune; an orphan keeps its own", () => { + let f: F = plain(["a", "b", "b1"]); + f = open(f, 1, "c", true, same); + f = open(f, 2, "c1", true, same); + f = open(f, 0, "d", true, same); + f = open(f, 1, "d1", true, same); + const was = hues(f); + f = reorder(f, idOf(f, "d1"), idOf(f, "b1")); + expect(hues(f)).toEqual(was); + f = pin(f, idOf(f, "c1"), true); + f = close(f, idOf(f, "c"), idOf(f, "d1")).forest; + expect(f.trees[1].ref).toBe("c1"); + const { c: _c, ...rest } = was; + expect(hues(f)).toEqual(rest); + const { d1: _d1, ...left } = rest; + expect(hues(prune(f, (r) => r !== "d1"))).toEqual(left); + // A new fork after all that still takes the next colour, not one used already. + expect(hues(open(focus(f, idOf(f, "a")), 0, "e", true, same)).e).toBe(f.hues); + }); + + it("colours are stored with the view; a view stored before colours is coloured as if opened in tree order", () => { + let f: F = plain(["a", "b", "c"]); + f = open(f, 0, "x", true, same); + f = open(f, 1, "x1", true, same); + f = open(f, 0, "y", true, same); + f = pin(f, idOf(f, "x1"), true); + f = close(f, idOf(f, "x"), idOf(f, "y")).forest; + expect(hues(f)).toEqual({ a: "-", b: "-", c: "-", y: 1, x1: 0 }); + const back = roundTrip(f); + expect(hues(back)).toEqual(hues(f)); + expect(back.hues).toBe(f.hues); + // From before colours: the root trail plain, each further child and each orphan a new colour. + const old = restore({ forest: [{ ref: "a", kids: [{ ref: "b", kids: [{ ref: "c" }, { ref: "c2" }] }, { ref: "x", kids: [{ ref: "x1" }] }] }, { ref: "o", col: 2, kids: [{ ref: "o1" }] }], at: 0 }, isStr); + expect(hues(old)).toEqual({ a: "-", b: "-", c: "-", c2: 0, x: 1, x1: 1, o: 2, o1: 2 }); + expect(old.hues).toBe(3); + expect(valid(old, same)).toBe(true); + // Older still: one tree, or a trail. + expect(hues(restore({ forks: { ref: "a", kids: [{ ref: "b" }, { ref: "c" }], pick: 1 } }, isStr))).toEqual({ a: "-", b: "-", c: 0 }); + expect(hues(restore({ trail: ["a", "b"] }, isStr))).toEqual({ a: "-", b: "-" }); + // A colour out of the palette is not trusted. + expect(load({ ref: "a", hue: HUES }, isStr)!.hue).toBeUndefined(); + expect(load({ ref: "a", hue: 1.5 }, isStr)!.hue).toBeUndefined(); + }); +});