fix: dock final review — shared state, menu guards, cleanup
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
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 };
|
||||
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,
|
||||
toggleMode,
|
||||
menuTransition,
|
||||
applyDock,
|
||||
DOCK_LIMIT,
|
||||
} from "./dock.js";
|
||||
import {
|
||||
@@ -245,7 +246,7 @@ export default class Composer extends View {
|
||||
for (const input of Object.values(this.panelInputs)) {
|
||||
input.on("change", (event, cx) => {
|
||||
if (!this.panel) return;
|
||||
if (this.contextMenu?.kind === "row") this.contextMenu = null;
|
||||
if (this.contextMenu?.kind === "row") this.dropContextMenu();
|
||||
this.panel.index = 0;
|
||||
this.scrollPanel(0);
|
||||
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.
|
||||
this.contextMenu = null;
|
||||
this.menuOpenedAt = 0;
|
||||
this.menuClosedAt = 0;
|
||||
this.renameRefocused = false;
|
||||
this.renaming = null;
|
||||
this.renameStartedAt = 0;
|
||||
// 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) => {
|
||||
if (this.renaming === null) return;
|
||||
if (Date.now() - this.renameStartedAt <= 400) {
|
||||
// The menu handed the focus back a moment after the click: take it again.
|
||||
if (!this.renameRefocused && Date.now() - this.renameStartedAt <= 400) {
|
||||
// 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();
|
||||
return;
|
||||
}
|
||||
@@ -331,6 +336,7 @@ export default class Composer extends View {
|
||||
// follow, so the composer watches who it talks to.
|
||||
cx.timer.every(500, (cx) => {
|
||||
this.syncAgent(cx);
|
||||
this.refreshDock(cx);
|
||||
});
|
||||
// Once a second: the end of a turn starts the countdown of the queue and
|
||||
// the countdown counts down.
|
||||
@@ -377,7 +383,7 @@ export default class Composer extends View {
|
||||
this.saveDraftNow();
|
||||
this.confirmImage = null;
|
||||
// A menu or a name being edited belongs to the agent we leave.
|
||||
this.contextMenu = null;
|
||||
this.dropContextMenu();
|
||||
this.renaming = null;
|
||||
this.currentKey = key;
|
||||
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.
|
||||
if (command === "escape" && this.overlayEscape(cx)) 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
|
||||
// its search field, not in the composer's, and the bindings of the
|
||||
// composer (`herdr-composer > Input`) catch these keys as actions first.
|
||||
if (this.panel) {
|
||||
if (this.contextMenu?.kind === "row") this.dropContextMenu();
|
||||
if (command === "up") this.panelCommand("up", cx);
|
||||
else if (command === "down") this.panelCommand("down", cx);
|
||||
else if (command === "accept") this.panelCommand("tab", cx);
|
||||
@@ -769,7 +776,7 @@ export default class Composer extends View {
|
||||
if (!this.panel) return;
|
||||
const { caret } = this.panel;
|
||||
this.panel = null;
|
||||
if (this.contextMenu?.kind === "row") this.contextMenu = null;
|
||||
if (this.contextMenu?.kind === "row") this.dropContextMenu();
|
||||
this.panelSearchFocused = false;
|
||||
if (this.panelRestorePx !== null) {
|
||||
this.resizeBlock(this.panelRestorePx);
|
||||
@@ -932,7 +939,7 @@ export default class Composer extends View {
|
||||
const view = this.panelView();
|
||||
|
||||
// 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") {
|
||||
// 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). */
|
||||
panelRowMenu(key, event, cx) {
|
||||
panelRowMenu(key, cx) {
|
||||
if (!this.panel) return;
|
||||
if (!this.panelRowText(key)) return;
|
||||
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. */
|
||||
closeRowMenuByClick(cx) {
|
||||
this.rowMenuClosedAt = Date.now();
|
||||
if (this.contextMenu?.kind === "row") this.contextMenu = null;
|
||||
this.closeRowMenu(cx);
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -1070,7 +1082,7 @@ export default class Composer extends View {
|
||||
if (this.renaming !== null) {
|
||||
this.endRename(cx, true);
|
||||
} else if (this.contextMenu) {
|
||||
this.contextMenu = null;
|
||||
this.dropContextMenu();
|
||||
} else if (now >= this.escSwallowUntil) {
|
||||
return false;
|
||||
}
|
||||
@@ -1104,28 +1116,34 @@ export default class Composer extends View {
|
||||
}
|
||||
const before = this.contextMenu;
|
||||
this.contextMenu = menuTransition(before, { kind, id, open });
|
||||
if (open) this.menuOpenedAt = now;
|
||||
else if (before && !this.contextMenu) {
|
||||
if (open) {
|
||||
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.
|
||||
this.escSwallowUntil = now + 80;
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/**
|
||||
* A right press on a button of the dock or on the star opens its menu at
|
||||
* once, whatever the Popover makes of the press: the first right click after
|
||||
* a command or a click elsewhere always opens the menu.
|
||||
*/
|
||||
openMenuByPress(kind, id, cx) {
|
||||
this.popoverChanged(kind, id, true, cx);
|
||||
/** The menu is closed (or was a moment ago): a click now is the click that closed it. */
|
||||
menuBusy() {
|
||||
return this.contextMenu !== null || Date.now() - this.menuClosedAt < 150;
|
||||
}
|
||||
|
||||
/** The menu is closed; the moment is kept (see `menuBusy`). */
|
||||
dropContextMenu() {
|
||||
if (this.contextMenu !== null) this.menuClosedAt = Date.now();
|
||||
this.contextMenu = null;
|
||||
}
|
||||
|
||||
/** A command of the menu that is open was chosen. */
|
||||
menuCommand(command, cx) {
|
||||
const menu = this.contextMenu;
|
||||
if (!menu) return;
|
||||
this.contextMenu = null;
|
||||
this.dropContextMenu();
|
||||
this.escSwallowUntil = Date.now() + 80;
|
||||
|
||||
if (menu.kind === "dock") this.dockCommand(menu.id, command, cx);
|
||||
@@ -1145,10 +1163,10 @@ export default class Composer extends View {
|
||||
this.startRename(button, cx);
|
||||
return;
|
||||
}
|
||||
if (command === "mode") this.setDock(toggleMode(this.dock, id), cx);
|
||||
else if (command === "left") this.setDock(moveButton(this.dock, id, -1), cx);
|
||||
else if (command === "right") this.setDock(moveButton(this.dock, id, 1), cx);
|
||||
else if (command === "remove") this.setDock(removeButton(this.dock, id), cx);
|
||||
if (command === "mode") this.setDock((dock) => toggleMode(dock, id), cx);
|
||||
else if (command === "left") this.setDock((dock) => moveButton(dock, id, -1), cx);
|
||||
else if (command === "right") this.setDock((dock) => moveButton(dock, id, 1), cx);
|
||||
else if (command === "remove") this.setDock((dock) => removeButton(dock, id), 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. */
|
||||
togglePin(text, cx) {
|
||||
if (isPinned(this.dock, text)) {
|
||||
this.setDock(unpinText(this.dock, text), cx);
|
||||
const stored = storage.loadDock(this.store);
|
||||
if (isPinned(stored, text)) {
|
||||
this.setDock((dock) => unpinText(dock, text), cx);
|
||||
return;
|
||||
}
|
||||
if (String(text).trim() === "") {
|
||||
this.notice("Wpisz tekst, aby przypiąć go do docka.", "problem", cx);
|
||||
return;
|
||||
}
|
||||
if (this.dock.length >= DOCK_LIMIT) {
|
||||
if (stored.length >= DOCK_LIMIT) {
|
||||
this.notice(`Dock jest pełny (${DOCK_LIMIT} przycisków).`, "problem", cx);
|
||||
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. */
|
||||
setDock(next, cx) {
|
||||
this.dock = next;
|
||||
setDock(operation, cx) {
|
||||
// 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 {
|
||||
storage.saveDock(this.store, this.dock);
|
||||
} catch (err) {
|
||||
@@ -1204,6 +1234,7 @@ export default class Composer extends View {
|
||||
this.renaming = button.id;
|
||||
this.renameStartedAt = Date.now();
|
||||
this.renameFocused = false;
|
||||
this.renameRefocused = false;
|
||||
this.renameInput.set_value(button.label);
|
||||
// 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.
|
||||
@@ -1220,11 +1251,13 @@ export default class Composer extends View {
|
||||
endRename(cx, focusField) {
|
||||
this.renaming = null;
|
||||
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();
|
||||
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() {
|
||||
return Date.now() - this.renameEndedAt < 150;
|
||||
}
|
||||
@@ -1235,7 +1268,7 @@ export default class Composer extends View {
|
||||
if (id === null) return;
|
||||
const name = this.renameInput.value();
|
||||
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. */
|
||||
@@ -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.
|
||||
if (this.renaming !== null) return;
|
||||
const pressed = String(event.key).toLowerCase();
|
||||
if (this.justRenamed() && (pressed === "enter" || pressed === "escape")) {
|
||||
if (this.justRenamed() && pressed === "enter") {
|
||||
cx.stop_propagation();
|
||||
return;
|
||||
}
|
||||
@@ -2463,7 +2496,7 @@ export default class Composer extends View {
|
||||
key: (event, cx) => this.panelKey(event, cx),
|
||||
tab: (tab, cx) => this.panelTab(tab, 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),
|
||||
// The close button: what Esc does - the field gets the keyboard, the
|
||||
// caret and the height of the block back.
|
||||
@@ -2485,7 +2518,7 @@ export default class Composer extends View {
|
||||
.inset_0()
|
||||
.on_mouse_down("left", (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(
|
||||
div()
|
||||
@@ -2493,7 +2526,7 @@ export default class Composer extends View {
|
||||
.absolute()
|
||||
.top(52)
|
||||
.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
|
||||
// under it (the shield would close the menu before the item's click).
|
||||
.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.
|
||||
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}`)
|
||||
.mouse_button("right")
|
||||
.anchor("bottom_left")
|
||||
@@ -2909,6 +2942,8 @@ export default class Composer extends View {
|
||||
* the attachments alone: only the text of the button goes out.
|
||||
*/
|
||||
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 action = dockAction(button, { hasAgent });
|
||||
if (action === "none" || !hasAgent) return;
|
||||
@@ -2929,10 +2964,6 @@ export default class Composer extends View {
|
||||
}
|
||||
|
||||
const agent = this.target();
|
||||
if (!agent) {
|
||||
this.notice("Composer nie ma do kogo pisać — brak agenta.", "problem", cx);
|
||||
return;
|
||||
}
|
||||
const over = overLimit(button.text);
|
||||
if (over > 0) {
|
||||
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),
|
||||
active,
|
||||
(event, cx) => this.toggleCurrentFavorite(cx),
|
||||
(event, cx) => {
|
||||
if (this.menuBusy()) return;
|
||||
this.toggleCurrentFavorite(cx);
|
||||
},
|
||||
);
|
||||
if (!active) return icon;
|
||||
|
||||
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")
|
||||
.mouse_button("right")
|
||||
.anchor("bottom_left")
|
||||
|
||||
@@ -48,7 +48,7 @@ const TEXT_HEIGHT = 40;
|
||||
* key: (event:any, cx:any) => void,
|
||||
* tab: (tab: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,
|
||||
* close: (cx:any) => void,
|
||||
* }} on
|
||||
@@ -176,7 +176,7 @@ function renderList(t, view, on) {
|
||||
.track_scroll(view.scroll)
|
||||
.size_full()
|
||||
.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());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user