Official
sidebar
Built-in sidebar UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/sidebar@0.2.7
Permissions in 0.2.7
Files
footer-geometry.test.ts9.8 KB
// The sidebar's last row keeps Options, the stand-in profile picture, in one
// place against the rest of the sidebar, whatever the theme's row padding or
// the window's rem size:
// - its centre is on the centre line of the chat rows' trailing marks (a
// running row's workspace mark) and of the headings' counts, which are
// centred in a box as wide as such a mark, and
// - the room under the last row is as much as the room to the left of what
// leads it: under Complete's folded heading's label when the row has it,
// and under Options when Options is alone, and
// - Complete's folded heading in the last row has the room above it that
// every section's heading has, so the headings at the sidebar's foot stand
// evenly apart, and Options alone has as much room above it as to its right.
// The test works these out from the tree the sidebar draws, by adding up the
// paddings and margins on the way from the sidebar's edge to Options and to
// the list's rows, so a change to any of them that moves Options off that
// line or off those margins fails here.
import assert from "node:assert/strict";
import { test } from "node:test";
import type { AppState, Chat } from "divergence";
import type { EffectViewContext } from "divergence/effect";
import { item } from "../ui/components/item.tsx";
import type { Sidebar } from "./controller.ts";
import { initialState, noteTask } from "./sections.ts";
import { renderSidebar } from "./views.tsx";
type Node = { id?: string; type?: string; children?: Node[]; [prop: string]: unknown };
// Every theme preset's row padding and one larger, so the rule must hold as a
// relation between the values, not by a coincidence of one. GPUI's rem is 16px.
const SIZES = [8, 9, 12].map((rowPaddingX) => ({ rowPaddingX, rem: 16 }));
type Size = (typeof SIZES)[number];
// A length the way the kernel reads one: a number of px, a spacing step, rems,
// or theme sizes, each optionally scaled, with an offset
// ("density.rowPaddingX + type.baseSize * 0.25 - 5").
function length(value: unknown, size: Size): number {
if (value === undefined || value === null) return 0;
if (typeof value === "number") return value;
const text = String(value).trim();
const steps: Record<string, number> = { "0": 0, none: 0, small: 0.25 * size.rem, medium: 0.5 * size.rem, large: size.rem };
if (text in steps) return steps[text]!;
if (text.endsWith("rem")) return Number(text.slice(0, -3)) * size.rem;
const sum = text.replace(/density\.rowPaddingX/g, String(size.rowPaddingX)).replace(/type\.baseSize/g, String(size.rem)).replace(/px/g, "");
assert.match(sum, /^[\d.\s+*-]+$/, `a length the test can read: ${text}`);
return sum.split(/(?=[+-])/).reduce((total, term) => {
const [value, scale = "1"] = term.replace(/\s/g, "").split("*");
return total + Number(value) * Number(scale);
}, 0);
}
// One side of a node's padding or margin, the more specific prop first.
const side = (node: Node, kind: "padding" | "margin", edge: "Right" | "Bottom" | "Top", size: Size) =>
length(node[`${kind}${edge}`] ?? node[`${kind}${edge === "Right" ? "X" : "Y"}`] ?? node[kind], size);
// The nodes from the root down to the one with `id`, the root first and that one last.
function pathTo(node: Node | null | undefined, id: string): Node[] | null {
if (!node) return null;
if (node.id === id) return [node];
for (const child of node.children ?? []) {
const path = pathTo(child, id);
if (path) return [node, ...path];
}
return null;
}
// The node with `id`, among a scroller's rows (`items`) too.
function find(node: Node | null | undefined, id: string): Node | undefined {
if (!node) return undefined;
if (node.id === id) return node;
for (const child of [...((node.items as Node[] | undefined) ?? []), ...(node.children ?? [])]) {
const found = find(child, id);
if (found) return found;
}
return undefined;
}
// How far in from the sidebar's edge a node's box starts on one side: the
// padding of every node above it and the margins of it and every node above.
function inset(path: Node[], edge: "Right" | "Bottom", size: Size): number {
return path.reduce((total, node, index) => total + (index < path.length - 1 ? side(node, "padding", edge, size) : 0) + side(node, "margin", edge, size), 0);
}
const chat = (id: string, minute: number): Chat => ({
id,
workspaceId: "ws",
agentId: "agent",
title: id,
sessionId: "s",
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: `2026-01-02T00:${String(minute).padStart(2, "0")}:00.000Z`,
archived: false,
options: {},
status: "idle",
});
// Open chats in the list and Complete folded, so its heading takes the last
// row; without a completed chat Options stands alone in it.
function render(complete = true): Node {
const state = initialState();
state.loaded = true;
state.workspaces = [{ id: "ws", name: "ws", path: "/ws" } as never];
const done = chat("done", 1);
// A draft, so that Open's heading is not the first and has a section's room above it.
state.chats = [{ ...chat("draft", 32), sessionId: undefined }, chat("open-a", 30), chat("open-b", 31), ...(complete ? [done] : [])];
noteTask(state, {
id: done.id,
layout: { root: { kind: "pane", id: "pane-done", tabs: [{ kind: "chat", chatId: done.id }], active: { kind: "chat", chatId: done.id } }, focused: "pane-done" },
completedAt: "2026-01-03T00:00:00.000Z",
createdAt: done.createdAt,
updatedAt: done.updatedAt,
});
state.collapsed = { settled: true };
const app: AppState = { route: "chat", chatId: "open-a" };
const ctx = { app, state: {}, plugin: "sidebar", update() {}, run() {} } as unknown as EffectViewContext<{ cmd?: boolean }>;
return renderSidebar(ctx, { state, ensurePreview() {} } as unknown as Sidebar["Service"]) as unknown as Node;
}
// The folded heading in Options' row: 18px tall (its 3px padding above and
// below a 12px line, `type.sublabelLine`), centred on the 22px row, so its
// label ends this far above the row's foot.
// How wide a row's trailing mark is: a small icon, a workspace's picture, a number's cap.
const TRAILING_MARK = 14;
const LABEL_ABOVE_FOOT = (22 - 18) / 2 + 3;
test("Options stands on the centre line of the rows' marks and the headings' counts, Complete's label has as much room below it as to its left, and its heading has the room above it that a heading in the list has", () => {
const tree = render();
const options = pathTo(tree, "sidebar-options-button");
const list = pathTo(tree, "chat-list");
assert.ok(options && list, "the sidebar draws Options and the chat list");
const button = options.at(-1)!;
assert.equal(button.width, button.height, "Options is a circle");
// Options' popover sits in the last row, and the last row ends the footer.
const popover = options.at(-2)!;
const lastRow = options.at(-3)!;
const footer = options.at(-4)!;
assert.equal(popover.id, "sidebar-options");
assert.equal(lastRow.children?.at(-1), popover, "Options ends the last row");
assert.equal(footer.children?.at(-1), lastRow, "the last row ends the footer");
assert.ok(options[0]!.children!.indexOf(footer) > options[0]!.children!.indexOf(list[1]!), "the footer comes after the list");
assert.ok(pathTo(lastRow, "heading-settled"), "Complete's folded heading leads the last row");
for (const size of SIZES) {
const right = inset(options, "Right", size);
// A row's trailing mark ends where the row's own padding starts, the row
// filling the list from edge to edge.
const markEnd: number = inset(list, "Right", size) + side(list.at(-1)!, "padding", "Right", size) + length((item.template as Node).paddingX, size);
assert.equal(right + length(button.width, size) / 2, markEnd + TRAILING_MARK / 2, `Options' centre is on the marks' centre line at ${JSON.stringify(size)}`);
const count = (find(tree, "section-open")!.children ?? []).at(-1)!;
assert.equal(count.width, TRAILING_MARK, "a count is centred in a box as wide as a mark");
assert.equal(count.justifyContent, "center");
const labelLeft: number = side(options[0]!, "padding", "Right", size) + side(footer, "padding", "Right", size) + length((item.template as Node).paddingX, size);
const labelBelow: number = inset(options.slice(0, -2), "Bottom", size) + side(lastRow, "padding", "Bottom", size) + LABEL_ABOVE_FOOT;
assert.equal(labelBelow, labelLeft, `Complete's label has as much room below it as to its left at ${JSON.stringify(size)}`);
// The scroller ends where the last row above ends (a row of the list, or
// the last docked heading or row), so the footer's top padding is the
// room above Complete's heading, which has none of its own there.
// In the list the room above a heading is a row of its own.
const room: number = length(find(tree, "gap-open")!.height, size);
const tucked = find(tree, "heading-settled")!;
assert.ok(room > 0, "Open's heading follows Drafts, with a section's room above it");
assert.equal(side(find(tree, "heading-open")!, "padding", "Top", size), 0, "and none of its own");
assert.equal(side(footer, "padding", "Top", size) + side(tucked, "padding", "Top", size), room, `Complete's heading has a section's room above it at ${JSON.stringify(size)}`);
}
});
test("Options alone in the last row has as much room below it and above it as to its right", () => {
const options = pathTo(render(false), "sidebar-options-button");
assert.ok(options, "the sidebar draws Options");
const footer = options.at(-4)!;
assert.ok(!pathTo(options[0], "heading-settled"), "no completed chat, so no Complete heading");
for (const size of SIZES) {
assert.equal(inset(options, "Bottom", size), inset(options, "Right", size), `Options has as much room below it as to its right at ${JSON.stringify(size)}`);
assert.equal(side(footer, "padding", "Top", size), inset(options, "Right", size), `Options has as much room above it as to its right at ${JSON.stringify(size)}`);
}
});
No comments yet.