fix: dock final review — shared state, menu guards, cleanup

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 16:18:25 +01:00
1 parent fa44ec0bf7
commit 989fa88345
4 files changed
+103 -47

No files matched your search

@@ -245,3 +245,16 @@ export function menuTransition(current, { kind, id, open }) {
if (open) return { kind, id }; if (open) return { kind, id };
return current && current.kind === kind && current.id === id ? null : current; return current && current.kind === kind && current.id === id ? null : current;
} }
/**
* Apply one change to the dock as it is stored now. The dock is shared by
* every copy of the composer, so a copy never writes back the list it holds:
* it reads the stored one, applies the operation to it and writes the result.
*
* @param {unknown} stored
* @param {(dock: DockButton[]) => DockButton[]} operation
* @returns {DockButton[]}
*/
export function applyDock(stored, operation) {
return normalize(operation(normalize(stored)));
}
@@ -80,6 +80,7 @@ import {
rename as renameButton, rename as renameButton,
toggleMode, toggleMode,
menuTransition, menuTransition,
applyDock,
DOCK_LIMIT, DOCK_LIMIT,
} from "./dock.js"; } from "./dock.js";
import { import {
@@ -245,7 +246,7 @@ export default class Composer extends View {
for (const input of Object.values(this.panelInputs)) { for (const input of Object.values(this.panelInputs)) {
input.on("change", (event, cx) => { input.on("change", (event, cx) => {
if (!this.panel) return; if (!this.panel) return;
if (this.contextMenu?.kind === "row") this.contextMenu = null; if (this.contextMenu?.kind === "row") this.dropContextMenu();
this.panel.index = 0; this.panel.index = 0;
this.scrollPanel(0); this.scrollPanel(0);
cx.notify(); cx.notify();
@@ -269,6 +270,8 @@ export default class Composer extends View {
// null. `renaming` is the id of the button whose name is being edited. // null. `renaming` is the id of the button whose name is being edited.
this.contextMenu = null; this.contextMenu = null;
this.menuOpenedAt = 0; this.menuOpenedAt = 0;
this.menuClosedAt = 0;
this.renameRefocused = false;
this.renaming = null; this.renaming = null;
this.renameStartedAt = 0; this.renameStartedAt = 0;
// Esc that an overlay (menu, name edit) took must not reach the agent a // Esc that an overlay (menu, name edit) took must not reach the agent a
@@ -284,8 +287,10 @@ export default class Composer extends View {
}); });
this.renameInput.on("blur", (event, cx) => { this.renameInput.on("blur", (event, cx) => {
if (this.renaming === null) return; if (this.renaming === null) return;
if (Date.now() - this.renameStartedAt <= 400) { if (!this.renameRefocused && Date.now() - this.renameStartedAt <= 400) {
// The menu handed the focus back a moment after the click: take it again. // The menu handed the focus back a moment after the click: take it
// again - once; the next blur cancels.
this.renameRefocused = true;
this.renameInput.focus(); this.renameInput.focus();
return; return;
} }
@@ -331,6 +336,7 @@ export default class Composer extends View {
// follow, so the composer watches who it talks to. // follow, so the composer watches who it talks to.
cx.timer.every(500, (cx) => { cx.timer.every(500, (cx) => {
this.syncAgent(cx); this.syncAgent(cx);
this.refreshDock(cx);
}); });
// Once a second: the end of a turn starts the countdown of the queue and // Once a second: the end of a turn starts the countdown of the queue and
// the countdown counts down. // the countdown counts down.
@@ -377,7 +383,7 @@ export default class Composer extends View {
this.saveDraftNow(); this.saveDraftNow();
this.confirmImage = null; this.confirmImage = null;
// A menu or a name being edited belongs to the agent we leave. // A menu or a name being edited belongs to the agent we leave.
this.contextMenu = null; this.dropContextMenu();
this.renaming = null; this.renaming = null;
this.currentKey = key; this.currentKey = key;
if (!key) return; if (!key) return;
@@ -705,12 +711,13 @@ export default class Composer extends View {
// (and never reaches the agent), the arrows and Tab belong to the name. // (and never reaches the agent), the arrows and Tab belong to the name.
if (command === "escape" && this.overlayEscape(cx)) return; if (command === "escape" && this.overlayEscape(cx)) return;
if (this.renaming !== null) return; if (this.renaming !== null) return;
if (this.justRenamed() && ["escape", "send_now", "hold"].includes(command)) return; if (this.justRenamed() && ["send_now", "hold"].includes(command)) return;
// While the panel is open the keys are the panel's - the keyboard is in // While the panel is open the keys are the panel's - the keyboard is in
// its search field, not in the composer's, and the bindings of the // its search field, not in the composer's, and the bindings of the
// composer (`herdr-composer > Input`) catch these keys as actions first. // composer (`herdr-composer > Input`) catch these keys as actions first.
if (this.panel) { if (this.panel) {
if (this.contextMenu?.kind === "row") this.dropContextMenu();
if (command === "up") this.panelCommand("up", cx); if (command === "up") this.panelCommand("up", cx);
else if (command === "down") this.panelCommand("down", cx); else if (command === "down") this.panelCommand("down", cx);
else if (command === "accept") this.panelCommand("tab", cx); else if (command === "accept") this.panelCommand("tab", cx);
@@ -769,7 +776,7 @@ export default class Composer extends View {
if (!this.panel) return; if (!this.panel) return;
const { caret } = this.panel; const { caret } = this.panel;
this.panel = null; this.panel = null;
if (this.contextMenu?.kind === "row") this.contextMenu = null; if (this.contextMenu?.kind === "row") this.dropContextMenu();
this.panelSearchFocused = false; this.panelSearchFocused = false;
if (this.panelRestorePx !== null) { if (this.panelRestorePx !== null) {
this.resizeBlock(this.panelRestorePx); this.resizeBlock(this.panelRestorePx);
@@ -932,7 +939,7 @@ export default class Composer extends View {
const view = this.panelView(); const view = this.panelView();
// Any key but Esc closes the menu of a row. // Any key but Esc closes the menu of a row.
if (key !== "escape" && this.contextMenu?.kind === "row") this.contextMenu = null; if (key !== "escape" && this.contextMenu?.kind === "row") this.dropContextMenu();
if (key === "escape") { if (key === "escape") {
// An open row menu takes the Esc first. // An open row menu takes the Esc first.
@@ -1035,7 +1042,7 @@ export default class Composer extends View {
} }
/** A row was right-clicked: its menu opens (Insert, pin, favourite). */ /** A row was right-clicked: its menu opens (Insert, pin, favourite). */
panelRowMenu(key, event, cx) { panelRowMenu(key, cx) {
if (!this.panel) return; if (!this.panel) return;
if (!this.panelRowText(key)) return; if (!this.panelRowText(key)) return;
this.contextMenu = { kind: "row", id: key }; this.contextMenu = { kind: "row", id: key };
@@ -1044,9 +1051,14 @@ export default class Composer extends View {
/** A click outside the menu of a row: it closes the menu and goes no further. */ /** A click outside the menu of a row: it closes the menu and goes no further. */
closeRowMenuByClick(cx) { closeRowMenuByClick(cx) {
this.rowMenuClosedAt = Date.now(); this.closeRowMenu(cx);
if (this.contextMenu?.kind === "row") this.contextMenu = null;
cx.stop_propagation(); cx.stop_propagation();
}
/** The menu of a row is closed (what closed it was a click outside it). */
closeRowMenu(cx) {
this.rowMenuClosedAt = Date.now();
if (this.contextMenu?.kind === "row") this.dropContextMenu();
cx.notify(); cx.notify();
} }
@@ -1070,7 +1082,7 @@ export default class Composer extends View {
if (this.renaming !== null) { if (this.renaming !== null) {
this.endRename(cx, true); this.endRename(cx, true);
} else if (this.contextMenu) { } else if (this.contextMenu) {
this.contextMenu = null; this.dropContextMenu();
} else if (now >= this.escSwallowUntil) { } else if (now >= this.escSwallowUntil) {
return false; return false;
} }
@@ -1104,28 +1116,34 @@ export default class Composer extends View {
} }
const before = this.contextMenu; const before = this.contextMenu;
this.contextMenu = menuTransition(before, { kind, id, open }); this.contextMenu = menuTransition(before, { kind, id, open });
if (open) this.menuOpenedAt = now; if (open) {
else if (before && !this.contextMenu) { this.menuOpenedAt = now;
// Another copy may have changed the dock: the menu shows the fresh one.
this.refreshDock(cx);
} else if (before && !this.contextMenu) {
this.menuClosedAt = now;
// The Popover took Esc itself: the same press must not reach the agent. // The Popover took Esc itself: the same press must not reach the agent.
this.escSwallowUntil = now + 80; this.escSwallowUntil = now + 80;
} }
cx.notify(); cx.notify();
} }
/** /** The menu is closed (or was a moment ago): a click now is the click that closed it. */
* A right press on a button of the dock or on the star opens its menu at menuBusy() {
* once, whatever the Popover makes of the press: the first right click after return this.contextMenu !== null || Date.now() - this.menuClosedAt < 150;
* a command or a click elsewhere always opens the menu. }
*/
openMenuByPress(kind, id, cx) { /** The menu is closed; the moment is kept (see `menuBusy`). */
this.popoverChanged(kind, id, true, cx); dropContextMenu() {
if (this.contextMenu !== null) this.menuClosedAt = Date.now();
this.contextMenu = null;
} }
/** A command of the menu that is open was chosen. */ /** A command of the menu that is open was chosen. */
menuCommand(command, cx) { menuCommand(command, cx) {
const menu = this.contextMenu; const menu = this.contextMenu;
if (!menu) return; if (!menu) return;
this.contextMenu = null; this.dropContextMenu();
this.escSwallowUntil = Date.now() + 80; this.escSwallowUntil = Date.now() + 80;
if (menu.kind === "dock") this.dockCommand(menu.id, command, cx); if (menu.kind === "dock") this.dockCommand(menu.id, command, cx);
@@ -1145,10 +1163,10 @@ export default class Composer extends View {
this.startRename(button, cx); this.startRename(button, cx);
return; return;
} }
if (command === "mode") this.setDock(toggleMode(this.dock, id), cx); if (command === "mode") this.setDock((dock) => toggleMode(dock, id), cx);
else if (command === "left") this.setDock(moveButton(this.dock, id, -1), cx); else if (command === "left") this.setDock((dock) => moveButton(dock, id, -1), cx);
else if (command === "right") this.setDock(moveButton(this.dock, id, 1), cx); else if (command === "right") this.setDock((dock) => moveButton(dock, id, 1), cx);
else if (command === "remove") this.setDock(removeButton(this.dock, id), cx); else if (command === "remove") this.setDock((dock) => removeButton(dock, id), cx);
} }
rowCommand(key, command, cx) { rowCommand(key, command, cx) {
@@ -1165,24 +1183,36 @@ export default class Composer extends View {
/** The text is pinned to the dock, or unpinned when it already is. */ /** The text is pinned to the dock, or unpinned when it already is. */
togglePin(text, cx) { togglePin(text, cx) {
if (isPinned(this.dock, text)) { const stored = storage.loadDock(this.store);
this.setDock(unpinText(this.dock, text), cx); if (isPinned(stored, text)) {
this.setDock((dock) => unpinText(dock, text), cx);
return; return;
} }
if (String(text).trim() === "") { if (String(text).trim() === "") {
this.notice("Wpisz tekst, aby przypiąć go do docka.", "problem", cx); this.notice("Wpisz tekst, aby przypiąć go do docka.", "problem", cx);
return; return;
} }
if (this.dock.length >= DOCK_LIMIT) { if (stored.length >= DOCK_LIMIT) {
this.notice(`Dock jest pełny (${DOCK_LIMIT} przycisków).`, "problem", cx); this.notice(`Dock jest pełny (${DOCK_LIMIT} przycisków).`, "problem", cx);
return; return;
} }
this.setDock(pinText(this.dock, text, this.newId()), cx); const id = this.newId();
this.setDock((dock) => pinText(dock, text, id), cx);
}
/** The dock is read again from the store: another copy of the composer may have changed it. */
refreshDock(cx) {
const stored = storage.loadDock(this.store);
if (JSON.stringify(stored) === JSON.stringify(this.dock)) return;
this.dock = stored;
cx.notify();
} }
/** The dock is another list: it is kept and drawn. */ /** The dock is another list: it is kept and drawn. */
setDock(next, cx) { setDock(operation, cx) {
this.dock = next; // The dock is shared with every copy of the composer: the stored list is
// read again, the change is applied to it and the result is written.
this.dock = applyDock(storage.loadDock(this.store), operation);
try { try {
storage.saveDock(this.store, this.dock); storage.saveDock(this.store, this.dock);
} catch (err) { } catch (err) {
@@ -1204,6 +1234,7 @@ export default class Composer extends View {
this.renaming = button.id; this.renaming = button.id;
this.renameStartedAt = Date.now(); this.renameStartedAt = Date.now();
this.renameFocused = false; this.renameFocused = false;
this.renameRefocused = false;
this.renameInput.set_value(button.label); this.renameInput.set_value(button.label);
// The input exists after the next paint; the menu hands the focus back // The input exists after the next paint; the menu hands the focus back
// just after the click, so the input takes it a moment later. // just after the click, so the input takes it a moment later.
@@ -1220,11 +1251,13 @@ export default class Composer extends View {
endRename(cx, focusField) { endRename(cx, focusField) {
this.renaming = null; this.renaming = null;
this.renameEndedAt = Date.now(); this.renameEndedAt = Date.now();
// The Esc that ended the edit may arrive a second time by another road.
this.escSwallowUntil = this.renameEndedAt + 80;
if (focusField) this.focusField(); if (focusField) this.focusField();
cx.notify(); cx.notify();
} }
/** Enter and Esc that arrive just after a name was settled are not the composer's. */ /** An Enter that arrives just after a name was settled is not the composer's. */
justRenamed() { justRenamed() {
return Date.now() - this.renameEndedAt < 150; return Date.now() - this.renameEndedAt < 150;
} }
@@ -1235,7 +1268,7 @@ export default class Composer extends View {
if (id === null) return; if (id === null) return;
const name = this.renameInput.value(); const name = this.renameInput.value();
this.endRename(cx, true); this.endRename(cx, true);
this.setDock(renameButton(this.dock, id, name), cx); this.setDock((dock) => renameButton(dock, id, name), cx);
} }
/** The star next to the field: the text of the field is a favourite, or no longer. */ /** The star next to the field: the text of the field is a favourite, or no longer. */
@@ -1298,7 +1331,7 @@ export default class Composer extends View {
// The name of a dock button is being typed: the keys are the input's. // The name of a dock button is being typed: the keys are the input's.
if (this.renaming !== null) return; if (this.renaming !== null) return;
const pressed = String(event.key).toLowerCase(); const pressed = String(event.key).toLowerCase();
if (this.justRenamed() && (pressed === "enter" || pressed === "escape")) { if (this.justRenamed() && pressed === "enter") {
cx.stop_propagation(); cx.stop_propagation();
return; return;
} }
@@ -2463,7 +2496,7 @@ export default class Composer extends View {
key: (event, cx) => this.panelKey(event, cx), key: (event, cx) => this.panelKey(event, cx),
tab: (tab, cx) => this.panelTab(tab, cx), tab: (tab, cx) => this.panelTab(tab, cx),
pick: (key, cx) => this.panelPickKey(key, cx), pick: (key, cx) => this.panelPickKey(key, cx),
rowMenu: (key, event, cx) => this.panelRowMenu(key, event, cx), rowMenu: (key, cx) => this.panelRowMenu(key, cx),
clear: (cx) => this.panelClear(cx), clear: (cx) => this.panelClear(cx),
// The close button: what Esc does - the field gets the keyboard, the // The close button: what Esc does - the field gets the keyboard, the
// caret and the height of the block back. // caret and the height of the block back.
@@ -2485,7 +2518,7 @@ export default class Composer extends View {
.inset_0() .inset_0()
.on_mouse_down("left", (event, cx) => this.closeRowMenuByClick(cx)) .on_mouse_down("left", (event, cx) => this.closeRowMenuByClick(cx))
.on_mouse_down("right", (event, cx) => this.closeRowMenuByClick(cx)) .on_mouse_down("right", (event, cx) => this.closeRowMenuByClick(cx))
.on_click((event, cx) => this.closeRowMenuByClick(cx)), .on_click((event, cx) => cx.stop_propagation()),
) )
.child( .child(
div() div()
@@ -2493,7 +2526,7 @@ export default class Composer extends View {
.absolute() .absolute()
.top(52) .top(52)
.right(18) .right(18)
.on_mouse_down_out((event, cx) => this.closeRowMenuByClick(cx)) .on_mouse_down_out((event, cx) => this.closeRowMenu(cx))
// A press on the menu is the menu's: it must not reach the shield // A press on the menu is the menu's: it must not reach the shield
// under it (the shield would close the menu before the item's click). // under it (the shield would close the menu before the item's click).
.on_mouse_down("left", (event, cx) => cx.stop_propagation()) .on_mouse_down("left", (event, cx) => cx.stop_propagation())
@@ -2890,7 +2923,7 @@ export default class Composer extends View {
// Right click: the menu of the button, anchored to it. // Right click: the menu of the button, anchored to it.
const open = this.contextMenu?.kind === "dock" && this.contextMenu.id === button.id; const open = this.contextMenu?.kind === "dock" && this.contextMenu.id === button.id;
el = el.on_mouse_down("right", (event, cx) => this.openMenuByPress("dock", button.id, cx)); el = el.on_mouse_down("right", (event, cx) => this.popoverChanged("dock", button.id, true, cx));
return Popover.new(`dock-menu-${button.id}`) return Popover.new(`dock-menu-${button.id}`)
.mouse_button("right") .mouse_button("right")
.anchor("bottom_left") .anchor("bottom_left")
@@ -2909,6 +2942,8 @@ export default class Composer extends View {
* the attachments alone: only the text of the button goes out. * the attachments alone: only the text of the button goes out.
*/ */
dockPress(button, cx) { dockPress(button, cx) {
// The click that closed a menu (or lands while one is open) is not a press.
if (this.menuBusy()) return;
const hasAgent = Boolean(this.target()); const hasAgent = Boolean(this.target());
const action = dockAction(button, { hasAgent }); const action = dockAction(button, { hasAgent });
if (action === "none" || !hasAgent) return; if (action === "none" || !hasAgent) return;
@@ -2929,10 +2964,6 @@ export default class Composer extends View {
} }
const agent = this.target(); const agent = this.target();
if (!agent) {
this.notice("Composer nie ma do kogo pisać — brak agenta.", "problem", cx);
return;
}
const over = overLimit(button.text); const over = overLimit(button.text);
if (over > 0) { if (over > 0) {
this.notice(`Limit ${LIMITS.messageChars} znaków · ${over} za dużo`, "problem", cx); this.notice(`Limit ${LIMITS.messageChars} znaków · ${over} za dużo`, "problem", cx);
@@ -2952,12 +2983,15 @@ export default class Composer extends View {
isFavorite(this.favorites, this.value) ? "★" : "☆", isFavorite(this.favorites, this.value) ? "★" : "☆",
isFavorite(this.favorites, this.value), isFavorite(this.favorites, this.value),
active, active,
(event, cx) => this.toggleCurrentFavorite(cx), (event, cx) => {
if (this.menuBusy()) return;
this.toggleCurrentFavorite(cx);
},
); );
if (!active) return icon; if (!active) return icon;
const open = this.contextMenu?.kind === "star"; const open = this.contextMenu?.kind === "star";
icon = icon.on_mouse_down("right", (event, cx) => this.openMenuByPress("star", "star", cx)); icon = icon.on_mouse_down("right", (event, cx) => this.popoverChanged("star", "star", true, cx));
return Popover.new("composer-star-menu") return Popover.new("composer-star-menu")
.mouse_button("right") .mouse_button("right")
.anchor("bottom_left") .anchor("bottom_left")
@@ -48,7 +48,7 @@ const TEXT_HEIGHT = 40;
* key: (event:any, cx:any) => void, * key: (event:any, cx:any) => void,
* tab: (tab:string, cx:any) => void, * tab: (tab:string, cx:any) => void,
* pick: (key:string, cx:any) => void, * pick: (key:string, cx:any) => void,
* rowMenu: (key:string, event:any, cx:any) => void, * rowMenu: (key:string, cx:any) => void,
* clear: (cx:any) => void, * clear: (cx:any) => void,
* close: (cx:any) => void, * close: (cx:any) => void,
* }} on * }} on
@@ -176,7 +176,7 @@ function renderList(t, view, on) {
.track_scroll(view.scroll) .track_scroll(view.scroll)
.size_full() .size_full()
.on_item_click((key, cx) => on.pick(key, cx)) .on_item_click((key, cx) => on.pick(key, cx))
.on_item_secondary_click((key, event, cx) => on.rowMenu(key, event, cx)), .on_item_secondary_click((key, event, cx) => on.rowMenu(key, cx)),
) )
.child(Scrollbar.vertical("composer-panel-list").absolute().inset_0()); .child(Scrollbar.vertical("composer-panel-list").absolute().inset_0());
} }
+10 -1
View File
@@ -2,7 +2,7 @@ import test from "node:test";
import assert from "node:assert/strict"; import assert from "node:assert/strict";
import { import {
DOCK_LIMIT, LABEL_MAX, defaultLabel, normalize, isPinned, pin, unpin, remove, move, DOCK_LIMIT, LABEL_MAX, defaultLabel, normalize, isPinned, pin, unpin, remove, move,
rename, toggleMode, dockAction, dockMenu, rowMenu, starMenu, menuTransition, rename, toggleMode, dockAction, dockMenu, rowMenu, starMenu, menuTransition, applyDock,
} from "../herdr/herdr-composer/mods/herdr-composer/dock.js"; } from "../herdr/herdr-composer/mods/herdr-composer/dock.js";
const btn = (id, text, extra = {}) => ({ id, label: text, text, mode: "send", ...extra }); const btn = (id, text, extra = {}) => ({ id, label: text, text, mode: "send", ...extra });
@@ -149,3 +149,12 @@ test("menuTransition: open replaces, close only closes its own menu", () => {
// same id, other kind is another menu // same id, other kind is another menu
assert.deepEqual(menuTransition(B, { kind: "star", id: "B", open: false }), B); assert.deepEqual(menuTransition(B, { kind: "star", id: "B", open: false }), B);
}); });
test("applyDock: works on the stored list, so another copy's change survives", () => {
const stored = [btn("1", "a"), btn("2", "from-copy-A")];
// copy B held only ["a"]; it pins "b" - the result keeps A's pin
const out = applyDock(stored, (d) => pin(d, "b", "3"));
assert.deepEqual(out.map((b) => b.id), ["1", "2", "3"]);
assert.deepEqual(applyDock("rubbish", (d) => pin(d, "x", "9")).map((b) => b.id), ["9"]);
assert.deepEqual(applyDock(stored, (d) => remove(d, "1")).map((b) => b.id), ["2"]);
});