From 406f00f0e42556ba84232d226ffe4a717679d313 Mon Sep 17 00:00:00 2001 From: Factor Date: Thu, 19 Nov 2026 12:23:06 +0100 Subject: [PATCH] feat: composer block fits its content, keeps a height the person dragged to Co-Authored-By: Ovis Fast (Claude) --- layouts/ovis-composer.json | 2 +- mod/ovis-composer/main.js | 135 +++++++++++++++++++++++++++++++---- mod/ovis-composer/storage.js | 24 +++++++ mod/ovis-composer/view.js | 52 ++++++++++++++ tests/storage.test.mjs | 17 +++++ tests/view.test.mjs | 44 ++++++++++++ 6 files changed, 260 insertions(+), 14 deletions(-) diff --git a/layouts/ovis-composer.json b/layouts/ovis-composer.json index bfd7887..276879e 100644 --- a/layouts/ovis-composer.json +++ b/layouts/ovis-composer.json @@ -160,7 +160,7 @@ "id": "composer", "type": "block", "size": { - "px": 260 + "px": 150 }, "min": 120, "max": 520, diff --git a/mod/ovis-composer/main.js b/mod/ovis-composer/main.js index 88d91b0..71f4df1 100644 --- a/mod/ovis-composer/main.js +++ b/mod/ovis-composer/main.js @@ -94,6 +94,9 @@ import { shortPath, stillSameAgent, directSendBlocked, + blockTargetPx, + shouldResize, + isManualResize, statusText, targetAgent, } from "./view.js"; @@ -113,6 +116,16 @@ const PANEL_MIN_BLOCK = 320; /** ...to this. */ const PANEL_BLOCK_PX = 360; +/** The padding of the root, per side (px) - the block is the content plus two of them. */ +const ROOT_PADDING = 8; + +/** The range the layout gives the composer block (`layouts/ovis-composer.json`), px. */ +const BLOCK_MIN_PX = 120; +const BLOCK_MAX_PX = 520; + +/** The block is resized once the content has been quiet for this long, ms. */ +const FIT_DELAY_MS = 120; + /** How many rows the menu of the suggestions shows at most. */ const MENU_LIMIT = 20; @@ -199,6 +212,15 @@ export default class Composer extends View { // to give back when a panel that made the block taller closes. this.blockHeight = null; this.panelRestorePx = null; + // The block is as tall as its content: the height of the content, the + // height this copy ordered last (what it did not order is the person's + // drag), the floor the person dragged to, and the timer that waits for the + // content to settle before the block is resized. + this.contentHeight = null; + this.orderedPx = null; + this.ignoreSizeUntil = 0; + this.fitTimer = null; + this.userMin = storage.loadUserMin(this.store); this.panelInputs = { history: InputState.new({ placeholder: "Szukaj w historii…" }), favorites: InputState.new({ placeholder: "Szukaj w ulubionych…" }), @@ -700,18 +722,26 @@ export default class Composer extends View { */ layoutPx() { if (!Number.isFinite(this.blockHeight)) return null; - let zoom = 1; + return Math.round(this.blockHeight / this.zoomFactor()); + } + + /** The zoom of the app (1 when it is not known): logical px = layout px times this. */ + zoomFactor() { try { const value = Number(settings()?.zoom); - if (Number.isFinite(value) && value > 0) zoom = value; + if (Number.isFinite(value) && value > 0) return value; } catch { // Unknown zoom: 1, the reported height is taken as it is. } - return Math.round(this.blockHeight / zoom); + return 1; } /** The composer block gets another height (`layout.resize`); a refusal is said, not worked around. */ resizeBlock(px) { + // What is reported in the next moments is the answer to this order, not a + // drag of the border. + this.orderedPx = px; + this.ignoreSizeUntil = Date.now() + 400; try { run("layout.resize", { id: "composer", px }); } catch (err) { @@ -719,6 +749,75 @@ export default class Composer extends View { } } + /** + * The root reported its size (logical px). + * + * A height this copy did not order is the person dragging the border: it + * becomes the floor (`userMin`, kept across restarts). While the panel is + * open its own logic owns the height. + */ + rootSized(size, cx) { + this.blockHeight = size.height; + if (this.panel) return; + + const px = this.layoutPx(); + if (px === null) return; + if (this.orderedPx === null) { + // The first height is the layout's own - the point to start from. + this.orderedPx = px; + } else if (Date.now() >= this.ignoreSizeUntil && isManualResize({ reportedPx: px, orderedPx: this.orderedPx })) { + this.userMin = px; + this.orderedPx = px; + try { + storage.saveUserMin(this.store, px); + } catch { + // The floor is not kept across a restart; it still holds for now. + } + } + this.scheduleFit(cx); + } + + /** The content (everything but the layers) reported its size. */ + contentSized(size, cx) { + this.contentHeight = size.height; + this.scheduleFit(cx); + } + + /** The block is fitted once the content has been quiet for 120 ms. */ + scheduleFit(cx) { + if (this.fitTimer) { + this.fitTimer.cancel(); + this.fitTimer = null; + } + this.fitTimer = cx.timer.after(FIT_DELAY_MS, () => { + this.fitTimer = null; + this.fitBlock(); + }); + } + + /** + * The block takes the height of its content (never below what the person + * dragged it to), when that differs by more than 4 px from what it has. The + * height last ordered is not ordered again, so a resize that comes back as a + * size change does not start another one. + */ + fitBlock() { + if (this.panel || !Number.isFinite(this.contentHeight)) return; + const current = this.layoutPx(); + if (current === null) return; + + const target = blockTargetPx({ + contentHeight: this.contentHeight, + padding: ROOT_PADDING * 2, + zoom: this.zoomFactor(), + userMin: this.userMin, + min: BLOCK_MIN_PX, + max: BLOCK_MAX_PX, + }); + if (!shouldResize(current, target) || target === this.orderedPx) return; + this.resizeBlock(target); + } + /** The selection (or the caret) of the field. */ restoreSelection(range) { try { @@ -2012,9 +2111,7 @@ export default class Composer extends View { return v_flex() .id("ovis-composer") .relative() - .on_size_change((size) => { - this.blockHeight = size.height; - }) + .on_size_change((size, cx) => this.rootSized(size, cx)) .track_focus(this.handle) .key_context(CONTEXT) .on_key_down((event, cx) => this.key(event, cx)) @@ -2029,7 +2126,7 @@ export default class Composer extends View { .on_action("ovis-composer.hold", (e, cx) => this.runAction("hold", cx)) .on_action("ovis-composer.escape", (e, cx) => this.runAction("escape", cx)) .size_full() - .p(8) + .p(ROOT_PADDING) .gap(6) // The card and the context strip sit at the bottom edge of the block, // right above the status bar; the menu, the queue and the notices stay @@ -2039,12 +2136,24 @@ export default class Composer extends View { .text_color(t.foreground) .children( [ - this.renderMenu(t), - this.renderQueue(t, agent, key), - this.renderImageConfirm(t), - this.renderNotice(t), - this.renderCard(t, agent, key, status), - this.renderContext(t, agent), + // The content is measured as it is - `flex_none`, so a block that is + // too low does not squeeze it and hide how much room it needs. + v_flex() + .id("composer-content") + .flex_none() + .w_full() + .gap(6) + .on_size_change((size, cx) => this.contentSized(size, cx)) + .children( + [ + this.renderMenu(t), + this.renderQueue(t, agent, key), + this.renderImageConfirm(t), + this.renderNotice(t), + this.renderCard(t, agent, key, status), + this.renderContext(t, agent), + ].filter(Boolean), + ), this.renderPanelLayer(t), this.renderPreviewLayer(t), ].filter(Boolean), diff --git a/mod/ovis-composer/storage.js b/mod/ovis-composer/storage.js index 34006dd..01b51dc 100644 --- a/mod/ovis-composer/storage.js +++ b/mod/ovis-composer/storage.js @@ -64,6 +64,8 @@ export const KEYS = { history: "ovis-composer.history", favorites: "ovis-composer.favorites", usage: "ovis-composer.usage", + /** The height (layout px) the person dragged the composer to; 0 or none: not set. */ + userMin: "ovis-composer.userMin", /** The queue is per agent: `ovis-composer.queue:`. */ queue: "ovis-composer.queue", /** The draft is per agent: `ovis-composer.draft:`. */ @@ -247,6 +249,28 @@ function toDraft(value) { }; } +/** + * The height the person dragged the composer block to (layout px). 0 is "not + * set"; anything that is not a positive number is not a height. + * + * @param {Store} store + * @returns {number} + */ +export function loadUserMin(store) { + const value = read(store, KEYS.userMin); + return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0; +} + +/** + * @param {Store} store + * @param {number} px - 0 clears it; what is not a number is not written + * @returns {void} + */ +export function saveUserMin(store, px) { + if (typeof px !== "number" || !Number.isFinite(px) || px < 0) return; + write(store, KEYS.userMin, Math.round(px)); +} + /** * How often each command was used - the numbers the ranking of the * suggestions breaks its ties with (`suggest.rank` reads `item.uses`). diff --git a/mod/ovis-composer/view.js b/mod/ovis-composer/view.js index e97feed..36b0492 100644 --- a/mod/ovis-composer/view.js +++ b/mod/ovis-composer/view.js @@ -516,3 +516,55 @@ export function directSendBlocked({ draining, mode, status, queue }) { if (mode === "now" || status === "blocked") return false; return queueBlocksDirectSend(queue); } + +// --- the height of the composer block ---------------------------------------- +// +// The block is as tall as its content (menu, queue, notices, card, strip), +// unless the person dragged its border: then that height is the floor. + +/** + * The height the block should have, in the pixels of the layout. + * + * What the content measures is in logical pixels (layout px times the zoom of + * the app), plus the padding of the root; the floor is what the person dragged + * the block to (`userMin`, 0 for none); the result stays inside the range the + * layout allows. + * + * @param {{contentHeight:number, padding:number, zoom:number, userMin:number, min:number, max:number}} input + * @returns {number} + */ +export function blockTargetPx({ contentHeight, padding, zoom, userMin, min, max }) { + const factor = Number.isFinite(zoom) && zoom > 0 ? zoom : 1; + const need = Math.ceil((contentHeight + padding) / factor); + const wanted = Math.max(need, Number.isFinite(userMin) ? userMin : 0); + return Math.round(Math.min(max, Math.max(min, wanted))); +} + +/** + * Is the difference worth a `layout.resize`? Under 5 px it is not: typing and + * the growing of the field must not make the block twitch. + * + * @param {number|null|undefined} current + * @param {number|null|undefined} target + * @param {number} [threshold] + * @returns {boolean} + */ +export function shouldResize(current, target, threshold = 4) { + if (!Number.isFinite(current) || !Number.isFinite(target)) return false; + return Math.abs(current - target) > threshold; +} + +/** + * Is a reported height the person's drag? + * + * The composer keeps the height it ordered last (`orderedPx`); a height it did + * not order is somebody dragging the border. Before anything was ordered the + * first height is the layout's own, not a drag. + * + * @param {{reportedPx:number, orderedPx:number|null, tolerance?:number}} input + * @returns {boolean} + */ +export function isManualResize({ reportedPx, orderedPx, tolerance = 3 }) { + if (!Number.isFinite(reportedPx) || !Number.isFinite(orderedPx)) return false; + return Math.abs(reportedPx - orderedPx) > tolerance; +} diff --git a/tests/storage.test.mjs b/tests/storage.test.mjs index 7a07bf2..d15c6c8 100644 --- a/tests/storage.test.mjs +++ b/tests/storage.test.mjs @@ -132,3 +132,20 @@ test("queue roundtrip keeps a countdown entry (restart turns it into held)", () saveQueue(store, "a", q); assert.deepEqual(loadQueue(store, "a"), q); }); + +import { loadUserMin, saveUserMin } from "../mod/ovis-composer/storage.js"; + +test("userMin: the height the person dragged the composer to; 0 is none", () => { + const store = memStore(); + assert.equal(loadUserMin(store), 0); + saveUserMin(store, 300); + assert.equal(loadUserMin(store), 300); + saveUserMin(store, 0); + assert.equal(loadUserMin(store), 0); + store.set("ovis-composer.userMin", "x"); + assert.equal(loadUserMin(store), 0); + store.set("ovis-composer.userMin", -5); + assert.equal(loadUserMin(store), 0); + saveUserMin(store, NaN); + assert.equal(store.get("ovis-composer.userMin"), -5); +}); diff --git a/tests/view.test.mjs b/tests/view.test.mjs index dc7b1b8..fe72cb6 100644 --- a/tests/view.test.mjs +++ b/tests/view.test.mjs @@ -100,3 +100,47 @@ test("directSendBlocked: who must wait behind the queue", () => { assert.equal(directSendBlocked({ draining: false, mode: "auto", status: "idle", queue: [] }), false); assert.equal(directSendBlocked({ draining: false, mode: "auto", status: "idle", queue: [{ state: "held" }] }), false); }); + +import { blockTargetPx, shouldResize, isManualResize } from "../mod/ovis-composer/view.js"; + +const fit = (over = {}) => ({ contentHeight: 100, padding: 16, zoom: 1.2, userMin: 0, min: 120, max: 520, ...over }); + +test("blockTargetPx: the content (plus padding) in layout px, clamped to the layout's range", () => { + // (100 + 16) / 1.2 = 96.7 -> 97, under the minimum of 120 + assert.equal(blockTargetPx(fit()), 120); + // (300 + 16) / 1.2 = 263.3 -> 264 + assert.equal(blockTargetPx(fit({ contentHeight: 300 })), 264); + assert.equal(blockTargetPx(fit({ contentHeight: 5000 })), 520); + assert.equal(blockTargetPx(fit({ contentHeight: 300, zoom: 1 })), 316); +}); + +test("blockTargetPx: the height the person dragged to is kept, and content that needs more wins", () => { + assert.equal(blockTargetPx(fit({ contentHeight: 100, userMin: 300 })), 300); + assert.equal(blockTargetPx(fit({ contentHeight: 500, userMin: 300 })), 430); + assert.equal(blockTargetPx(fit({ contentHeight: 100, userMin: 9999 })), 520); + assert.equal(blockTargetPx(fit({ contentHeight: 100, userMin: 0 })), 120); +}); + +test("blockTargetPx: a zoom that is not a number counts as 1", () => { + assert.equal(blockTargetPx(fit({ contentHeight: 300, zoom: 0 })), 316); + assert.equal(blockTargetPx(fit({ contentHeight: 300, zoom: NaN })), 316); +}); + +test("shouldResize: only for a difference over 4 px", () => { + assert.equal(shouldResize(150, 154), false); + assert.equal(shouldResize(150, 155), true); + assert.equal(shouldResize(150, 145), true); + assert.equal(shouldResize(150, 146), false); + assert.equal(shouldResize(null, 150), false); + assert.equal(shouldResize(150, undefined), false); +}); + +test("isManualResize: a height nobody ordered is the person's drag", () => { + assert.equal(isManualResize({ reportedPx: 200, orderedPx: 200 }), false); + assert.equal(isManualResize({ reportedPx: 202, orderedPx: 200 }), false); + assert.equal(isManualResize({ reportedPx: 260, orderedPx: 200 }), true); + assert.equal(isManualResize({ reportedPx: 100, orderedPx: 200 }), true); + // nothing was ordered yet: the first size is the layout's, not a drag + assert.equal(isManualResize({ reportedPx: 150, orderedPx: null }), false); + assert.equal(isManualResize({ reportedPx: NaN, orderedPx: 200 }), false); +});