Skip to content

The design language that makes your AI harness better at design.

JavaScript

Preserve Tune values when reopening the panel (#1108)

Mageraafea3cff014
diff --git a/skill/scripts/live-browser.js b/skill/scripts/live-browser.js
index 504e75b9c..a63f1abe2 100644
--- a/skill/scripts/live-browser.js
+++ b/skill/scripts/live-browser.js
@@ -3271,6 +3271,14 @@
const P = paramsPanelPalette || barPaletteForTheme(detectPageTheme());
paramsPanelBody.innerHTML = '';
for (const p of params) {
+ // A knob the panel shows must be in the live values and on the variant,
+ // or accept drops it and the preview ignores it. Seed only missing keys
+ // so a reopen keeps edits.
+ if (!Object.hasOwn(paramsCurrentValues, p.id)) {
+ paramsCurrentValues[p.id] = p.default;
+ applyParamValue(variantEl, p, p.default);
+ }
+ const value = paramsCurrentValues[p.id];
const row = el('div', { display: 'flex', flexDirection: 'column', gap: '6px' });
const labelRow = el('div', {
display: 'flex', justifyContent: 'space-between',
@@ -3295,7 +3303,7 @@
input.min = String(p.min != null ? p.min : 0);
input.max = String(p.max != null ? p.max : 1);
input.step = String(p.step != null ? p.step : 0.05);
- input.value = String(p.default);
+ input.value = String(value);
Object.assign(input.style, {
width: '100%', accentColor: C.brand, cursor: 'pointer',
});
@@ -3310,7 +3318,7 @@
});
row.appendChild(input);
} else if (p.kind === 'toggle') {
- const initial = !!p.default;
+ const initial = !!value;
readout.textContent = initial ? 'On' : 'Off';
const track = el('button', {
position: 'relative', width: '36px', height: '20px',
@@ -3344,8 +3352,8 @@
const opts = (p.options || []).map(o =>
typeof o === 'string' ? { value: o, label: o } : o
);
- const activeOpt = opts.find(o => o.value === p.default) || opts[0];
- readout.textContent = activeOpt ? activeOpt.label : String(p.default);
+ const activeOpt = opts.find(o => o.value === value) || opts[0];
+ readout.textContent = activeOpt ? activeOpt.label : String(value);
const segRow = el('div', {
display: 'grid',
gridTemplateColumns: 'repeat(' + opts.length + ', 1fr)',
@@ -3354,7 +3362,7 @@
});
const segBtns = [];
opts.forEach(o => {
- const active = o.value === p.default;
+ const active = o.value === value;
const b = el('button', {
padding: '5px 4px', border: 'none', borderRadius: '3px',
background: active ? C.brand : 'transparent',
@@ -4988,8 +4996,8 @@
const variantEl = getVisibleVariantEl();
const params = parseVariantParams(variantEl);
if (!variantEl || params.length === 0) return;
- // Build fresh to ensure the current variant's controls are shown.
- applyParamDefaults(variantEl, params);
+ // Rebuild the controls from the current values. Defaults are applied
+ // when the variant is initialized, not when its Tune panel is reopened.
buildParamsPanel(variantEl, params);
tuneOpen = true;
showParamsPanel();
diff --git a/tests/live-browser-source.test.mjs b/tests/live-browser-source.test.mjs
index 3b1b1df64..b2294201b 100644
--- a/tests/live-browser-source.test.mjs
+++ b/tests/live-browser-source.test.mjs
@@ -1287,3 +1287,135 @@ describe('live-browser source contracts', () => {
});
});
+// Exercise Tune's real control listeners and open/close lifecycle without a server.
+function tuneHarness(params) {
+ function node(tag) {
+ return {
+ tag, style: {}, dataset: {}, children: [], listeners: {},
+ appendChild(child) { this.children.push(child); },
+ addEventListener(type, fn) { this.listeners[type] = fn; },
+ set innerHTML(value) { this.children = []; },
+ };
+ }
+ const variant = { values: {} };
+ const body = node('div');
+ const context = {
+ state: 'CYCLING', paramsCurrentValues: {}, tuneOpen: false, currentSessionId: null,
+ paramsPanelBody: body, paramsPanelEl: node('div'), paramsPanelPalette: {},
+ barEl: null, C: { brand: 'gold', ink: 'black' }, FONT: 'sans-serif', EASE: 'ease',
+ document: { createElement: node }, el: (tag, style) => Object.assign(node(tag), { style }),
+ getVisibleVariantEl: () => variant, parseVariantParams: () => params,
+ applyParamValue: (target, p, value) => { target.values[p.id] = value; },
+ showParamsPanel() {}, hideParamsPanel() {}, showOrUpdateCyclingBar() {}, queueCheckpoint() {},
+ };
+ const names = ['applyParamDefaults', 'formatRangeValue', 'buildParamsPanel', 'openTunePopover', 'closeTunePopover', 'refreshParamsPanel'];
+ const functions = names.map(name => {
+ const source = SOURCE.match(new RegExp(`function ${name}\\([^\\n]*\\) \\{[\\s\\S]*?\\n \\}`))?.[0];
+ assert.ok(source, name);
+ return source;
+ }).join('\n');
+ const api = runInNewContext(`${functions}\n({refreshParamsPanel, openTunePopover, closeTunePopover})`, context);
+ const fire = (control, event) => control.listeners[event]({ stopPropagation() {} });
+ return { api, context, variant, body, fire };
+}
+
+describe('Tune reopening', () => {
+ for (const value of [0, 0.65]) {
+ it(`preserves a range value of ${value} through repeated reopening`, () => {
+ const h = tuneHarness([{ id: 'lightness', kind: 'range', min: 0, max: 1, default: 0.5 }]);
+ h.api.refreshParamsPanel();
+ h.api.openTunePopover();
+ const input = h.body.children[0].children[1];
+ input.value = String(value);
+ h.fire(input, 'input');
+ for (let i = 0; i < 2; i++) {
+ h.api.closeTunePopover();
+ h.api.openTunePopover();
+ assert.equal(h.variant.values.lightness, value, 'preview retains edit');
+ assert.equal(h.context.paramsCurrentValues.lightness, value, 'accept/checkpoint state retains edit');
+ assert.equal(h.body.children[0].children[1].value, String(value), 'slider agrees');
+ assert.equal(h.body.children[0].children[0].children[1].textContent, value.toFixed(2));
+ }
+ });
+ }
+ for (const initial of [false, true]) {
+ it(`preserves a toggle changed from ${initial} to ${!initial}`, () => {
+ const h = tuneHarness([{ id: 'italic', kind: 'toggle', default: initial }]);
+ h.api.refreshParamsPanel();
+ h.api.openTunePopover();
+ h.fire(h.body.children[0].children[1], 'click');
+ h.api.closeTunePopover();
+ h.api.openTunePopover();
+ assert.equal(h.context.paramsCurrentValues.italic, !initial);
+ assert.equal(h.variant.values.italic, !initial);
+ assert.equal(h.body.children[0].children[0].children[1].textContent, initial ? 'Off' : 'On');
+ assert.equal(h.body.children[0].children[1].children[0].style.left, initial ? '2px' : '18px');
+ });
+ }
+ it('preserves steps and still resets to the next variant defaults on refresh', () => {
+ const params = [{ id: 'face', kind: 'steps', default: 'sans', options: ['sans', 'serif'] }];
+ const h = tuneHarness(params);
+ h.api.refreshParamsPanel();
+ h.api.openTunePopover();
+ h.fire(h.body.children[0].children[1].children[1], 'click');
+ h.api.closeTunePopover();
+ h.api.openTunePopover();
+ assert.equal(h.variant.values.face, 'serif');
+ assert.equal(h.context.paramsCurrentValues.face, 'serif');
+ assert.equal(h.body.children[0].children[0].children[1].textContent, 'serif');
+ assert.equal(h.body.children[0].children[1].children[1].style.background, 'gold');
+ const nextVariant = { values: {} };
+ h.context.getVisibleVariantEl = () => nextVariant;
+ h.api.refreshParamsPanel();
+ assert.equal(h.context.paramsCurrentValues.face, 'sans');
+ assert.equal(nextVariant.values.face, 'sans');
+ });
+});
+
+
+describe('Tune session boundaries', () => {
+ it('clears old values when leaving the cycling state', () => {
+ const h = tuneHarness([{ id: 'size', kind: 'range', default: 0.5 }]);
+ h.context.paramsCurrentValues = { size: 0.8 };
+ h.context.state = 'IDLE';
+ h.api.refreshParamsPanel();
+ assert.equal(Object.keys(h.context.paramsCurrentValues).length, 0);
+ assert.equal(h.context.tuneOpen, false);
+ h.context.state = 'CYCLING';
+ h.api.refreshParamsPanel();
+ h.api.openTunePopover();
+ assert.equal(h.context.paramsCurrentValues.size, 0.5);
+ assert.equal(h.body.children[0].children[1].value, '0.5');
+ });
+
+ it('clears old values when a variant has no Tune parameters', () => {
+ const h = tuneHarness([]);
+ h.context.paramsCurrentValues = { size: 0.8 };
+ h.context.tuneOpen = true;
+ h.api.refreshParamsPanel();
+ h.api.openTunePopover();
+ assert.equal(Object.keys(h.context.paramsCurrentValues).length, 0);
+ assert.equal(h.context.tuneOpen, false);
+ });
+});
+
+describe('Tune first open without a refresh', () => {
+ it('seeds and applies displayed defaults so the preview, accept and toggles agree', () => {
+ const h = tuneHarness([
+ { id: 'size', kind: 'range', min: 0, max: 1, default: 0.4 },
+ { id: 'italic', kind: 'toggle', default: true },
+ { id: 'face', kind: 'steps', default: 'serif', options: ['sans', 'serif'] },
+ ]);
+ // Tune parameters can land on the visible variant after its last refresh.
+ h.api.openTunePopover();
+ assert.deepEqual({ ...h.context.paramsCurrentValues }, { size: 0.4, italic: true, face: 'serif' });
+ assert.deepEqual({ ...h.variant.values }, { size: 0.4, italic: true, face: 'serif' });
+ h.fire(h.body.children[1].children[1], 'click');
+ assert.equal(h.context.paramsCurrentValues.italic, false, 'one click turns a default-on toggle off');
+ assert.equal(h.variant.values.italic, false);
+ h.api.closeTunePopover();
+ h.api.openTunePopover();
+ assert.equal(h.context.paramsCurrentValues.italic, false, 'reopen keeps the edit');
+ assert.equal(h.context.paramsCurrentValues.size, 0.4);
+ });
+});
diff --git a/tests/live-e2e.test.mjs b/tests/live-e2e.test.mjs
index 29722f5e8..ccb34b3d6 100644
--- a/tests/live-e2e.test.mjs
+++ b/tests/live-e2e.test.mjs
@@ -56,6 +56,7 @@ import {
installLiveQueryHelpers,
isMountErrorCardVisible,
openTunePanel,
+ closeTunePanel,
pickElement,
readComputedFontWeight,
runLiveChromeBottomBarSmoke,
@@ -785,6 +786,10 @@ for (const { name, fixture } of fixtures) {
assert.equal(applied, scenario.rangeValue, 'range knob took the requested value');
await chooseTuneStep(page, scenario.stepsLabel, scenario.stepsOptionLabel);
+ t.diagnostic('Closing and reopening Tune before accepting the edited values');
+ await closeTunePanel(page);
+ await openTunePanel(page);
+
t.diagnostic(`Accepting variant ${targetVariant} with tuned params`);
await clickAccept(page, { expectedVariant: targetVariant });
await waitForBarHidden(page);
diff --git a/tests/live-e2e/ui.mjs b/tests/live-e2e/ui.mjs
index 00cc8f680..f7eaa115b 100644
--- a/tests/live-e2e/ui.mjs
+++ b/tests/live-e2e/ui.mjs
@@ -909,6 +909,22 @@ export async function openTunePanel(page, { timeout = 5_000 } = {}) {
);
}
+/** Close Tune without changing variants or accepting the current choice. */
+export async function closeTunePanel(page) {
+ await installLiveQueryHelpers(page);
+ const alreadyClosed = await page.evaluate(
+ (sel) => window.__impeccableLiveQuery(sel)?.style.pointerEvents === 'none',
+ PARAMS_PANEL_ID,
+ );
+ if (alreadyClosed) return;
+ await clickLiveControl(page, TUNE_BUTTON);
+ await page.waitForFunction(
+ (sel) => window.__impeccableLiveQuery(sel)?.style.pointerEvents === 'none',
+ PARAMS_PANEL_ID,
+ { timeout: 5_000 },
+ );
+}
+
/**
* Drag a `range` knob to `value`. Setting `.value` + dispatching `input` is
* what a real drag produces; the panel's listener reads the input, not the