Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions tests/unit/web-public.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
progressFields,
} from '../../web/public/facts.js';
import { diffGraph } from '../../web/public/diff.js';
import { clampDrawerWidth, DRAWER_MIN, GRAPH_MIN, parseStoredWidth } from '../../web/public/resize.js';
import { formatTokens, inputTotal, runsFor, stripText, totalTitle } from '../../web/public/usage.js';
import { parseTranscript } from '../../web/public/transcript.js';
import { connState, nextDelay, POLL_MS, STALE_MS } from '../../web/public/poll.js';
Expand Down Expand Up @@ -812,6 +813,21 @@ Deno.test('progressFields reads round, best result, established and open questio
assertEquals(progressFields('nothing'), { round: null, established: null, best: null, open: null });
});

Deno.test('clampDrawerWidth keeps the drawer wide enough and leaves room for the graph', () => {
assertEquals(clampDrawerWidth(500, 1400), 500);
assertEquals(clampDrawerWidth(100, 1400), DRAWER_MIN);
assertEquals(clampDrawerWidth(1390, 1400), 1400 - GRAPH_MIN);
assertEquals(clampDrawerWidth(512.6, 1400), 513);
// Too narrow for both: the drawer keeps its minimum, but never more than the page.
assertEquals(clampDrawerWidth(900, DRAWER_MIN + 10), DRAWER_MIN);
assertEquals(clampDrawerWidth(900, 200), 200);
});

Deno.test('parseStoredWidth reads only a positive whole number of pixels', () => {
assertEquals(parseStoredWidth('640'), 640);
for (const bad of [null, '', 'abc', '0', '-5', '12.5', '1e3', ' 640']) assertEquals(parseStoredWidth(bad), null);
});

Deno.test('formatTokens shortens with k and M, one decimal under ten', () => {
assertEquals(
[0, 999, 1000, 1234, 12345, 999_999, 1_234_567, 12_345_678].map(formatTokens),
Expand Down
94 changes: 93 additions & 1 deletion web/public/drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { builtBefore, citations, foundLine, latestMeasurement, measurementVerdic
import { writeSection } from './forms.js';
import { createLogView } from './logview.js';
import { kindOf, shortName, verdictOf, workerLabel } from './names.js';
import { clampDrawerWidth, DRAWER_MIN, KEY_STEP, parseStoredWidth, WIDTH_KEY } from './resize.js';
import { formatTokens, inputTotal, runsFor } from './usage.js';

/**
Expand Down Expand Up @@ -83,7 +84,7 @@ function build() {
}
bar.append(tabs.memory, tabs.links, tabs.log);
body.append(panels.memory, panels.links, panels.log);
root.append(head, bar, body);
root.append(resizer(root), head, bar, body);
(document.querySelector('main') ?? document.body).append(root);

document.addEventListener('keydown', (e) => {
Expand All @@ -101,6 +102,97 @@ function build() {
return { root, title, tabs, panels };
}

/** localStorage, or null where the browser refuses it (private mode, blocked storage). */
function storage() {
try {
return globalThis.localStorage;
} catch {
return null;
}
}

/**
* The drawer's left edge, which drags to change its width. Arrow keys move it too, a double-click goes back to the
* default width, and the chosen width is kept for the next visit.
* @param {HTMLElement} root the drawer
*/
function resizer(root) {
const grip = h('div', 'drawer-resize');
grip.setAttribute('role', 'separator');
grip.setAttribute('aria-orientation', 'vertical');
grip.setAttribute('aria-label', 'Resize the details panel');
grip.tabIndex = 0;
grip.title = 'Drag to resize. Double-click for the default width.';
/** The width the person chose, or null for the default. */
let chosen = parseStoredWidth(storage()?.getItem(WIDTH_KEY) ?? null);
const total = () => (root.parentElement ?? document.body).clientWidth;

function apply() {
if (chosen === null) {
root.style.removeProperty('width');
root.style.removeProperty('min-width');
} else {
// The stylesheet's minimum is for the default width; a chosen one has its own limits.
root.style.minWidth = '0';
root.style.width = `${clampDrawerWidth(chosen, total())}px`;
}
grip.setAttribute('aria-valuemin', String(DRAWER_MIN));
grip.setAttribute('aria-valuemax', String(clampDrawerWidth(Infinity, total())));
grip.setAttribute('aria-valuenow', String(Math.round(root.getBoundingClientRect().width)));
}

/** @param {number | null} px */
function choose(px) {
chosen = px === null ? null : clampDrawerWidth(px, total());
apply();
try {
if (chosen === null) storage()?.removeItem(WIDTH_KEY);
else storage()?.setItem(WIDTH_KEY, String(chosen));
} catch {
// A full or blocked store only means the width is not kept for next time.
}
}

grip.addEventListener('pointerdown', (e) => {
if (e.button !== 0) return;
e.preventDefault();
grip.setPointerCapture(e.pointerId);
document.body.classList.add('resizing');
});
grip.addEventListener('pointermove', (e) => {
if (!grip.hasPointerCapture(e.pointerId)) return;
chosen = clampDrawerWidth(root.getBoundingClientRect().right - e.clientX, total());
apply();
});
/** @param {PointerEvent} e */
const end = (e) => {
if (!grip.hasPointerCapture(e.pointerId)) return;
grip.releasePointerCapture(e.pointerId);
document.body.classList.remove('resizing');
choose(chosen);
};
grip.addEventListener('pointerup', end);
grip.addEventListener('pointercancel', end);
grip.addEventListener('lostpointercapture', () => document.body.classList.remove('resizing'));
grip.addEventListener('dblclick', () => choose(null));
grip.addEventListener('keydown', (e) => {
// The drawer is on the right, so moving its edge left makes it wider.
const step = { ArrowLeft: KEY_STEP, ArrowRight: -KEY_STEP }[e.key];
if (step === undefined) return;
e.preventDefault();
choose(root.getBoundingClientRect().width + step);
});
// A smaller window shrinks a chosen width to fit; the choice itself stays for a wider one.
globalThis.addEventListener('resize', () => {
if (!root.hidden) apply();
});
// Opening the drawer draws the chosen width and the separator's numbers.
new MutationObserver(() => {
if (!root.hidden) apply();
}).observe(root, { attributes: true, attributeFilter: ['hidden'] });
return grip;
}

/** @param {TabName} k */
function selectTab(k) {
if (!ui) return;
Expand Down
31 changes: 31 additions & 0 deletions web/public/resize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @ts-check
// Limits for the drawer's width when the person drags its edge. No DOM.

/** The narrowest the drawer goes, in pixels. */
export const DRAWER_MIN = 300;
/** How much of the page the drawer leaves uncovered for the graph or list, in pixels. */
export const GRAPH_MIN = 240;
/** How far one arrow key moves the edge, in pixels. */
export const KEY_STEP = 32;
/** Where the chosen width is kept between visits. */
export const WIDTH_KEY = 'agent-memory.drawer-width';

/**
* The drawer width to use for a wanted width, on a page `total` pixels wide.
* @param {number} px
* @param {number} total
* @returns {number}
*/
export function clampDrawerWidth(px, total) {
const upper = Math.max(DRAWER_MIN, total - GRAPH_MIN);
return Math.min(Math.round(Math.min(Math.max(px, DRAWER_MIN), upper)), Math.round(total));
}

/**
* A stored width, or null when there is none or it is not a positive whole number.
* @param {string | null} text
* @returns {number | null}
*/
export function parseStoredWidth(text) {
return text !== null && /^[1-9]\d*$/.test(text) ? Number(text) : null;
}
26 changes: 25 additions & 1 deletion web/public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ main {
bottom: 0;
z-index: 10;
width: 40%;
min-width: 380px;
min-width: min(380px, 100%);
max-width: 100%;
display: flex;
flex-direction: column;
Expand All @@ -375,6 +375,30 @@ main {
animation: drawer-in 0.15s ease-out;
}

/* The drawer's left edge: drag it, or focus it and use the arrow keys. */
.drawer-resize {
position: absolute;
top: 0;
bottom: 0;
left: -4px;
z-index: 1;
width: 8px;
cursor: col-resize;
touch-action: none;
}

.drawer-resize:hover,
.drawer-resize:focus-visible,
body.resizing .drawer-resize {
background: linear-gradient(to right, transparent 3px, var(--accent) 3px, var(--accent) 5px, transparent 5px);
outline: none;
}

body.resizing {
cursor: col-resize;
user-select: none;
}

@keyframes drawer-in {
from {
transform: translateX(24px);
Expand Down
Loading