fix: dock menus — row-menu click-through, rename focus, state reset

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 16:06:37 +01:00
1 parent 76004542ab
commit 68fa3677d1
1 file changed
+99 -24
@@ -244,6 +244,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;
this.panel.index = 0; this.panel.index = 0;
this.scrollPanel(0); this.scrollPanel(0);
cx.notify(); cx.notify();
@@ -273,12 +274,21 @@ export default class Composer extends View {
this.escSwallowUntil = 0; this.escSwallowUntil = 0;
this.renameInput = InputState.new({ placeholder: "Nazwa" }); this.renameInput = InputState.new({ placeholder: "Nazwa" });
this.renameInput.on("submit", (event, cx) => this.renameCommit(cx)); this.renameInput.on("submit", (event, cx) => this.renameCommit(cx));
this.renameEndedAt = 0;
this.renameFocused = false;
this.rowMenuClosedAt = 0;
this.renameInput.on("focus", () => {
this.renameFocused = true;
});
this.renameInput.on("blur", (event, cx) => { this.renameInput.on("blur", (event, cx) => {
// Losing the focus cancels - but not in the moment the menu hands it back. if (this.renaming === null) return;
if (this.renaming !== null && Date.now() - this.renameStartedAt > 400) { if (Date.now() - this.renameStartedAt <= 400) {
this.renaming = null; // The menu handed the focus back a moment after the click: take it again.
cx.notify(); this.renameInput.focus();
return;
} }
// Losing the focus later cancels the edit (the focus is where the person put it).
this.endRename(cx, false);
}); });
// The preview of an image (null when closed). // The preview of an image (null when closed).
@@ -364,6 +374,9 @@ export default class Composer extends View {
// The draft of the agent we leave goes to the disk at once. // The draft of the agent we leave goes to the disk at once.
this.saveDraftNow(); this.saveDraftNow();
this.confirmImage = null; this.confirmImage = null;
// A menu or a name being edited belongs to the agent we leave.
this.contextMenu = null;
this.renaming = null;
this.currentKey = key; this.currentKey = key;
if (!key) return; if (!key) return;
@@ -595,6 +608,8 @@ export default class Composer extends View {
/** Enter in the field: the message goes out, or the menu takes the key. */ /** Enter in the field: the message goes out, or the menu takes the key. */
submitted(event, cx) { submitted(event, cx) {
// The Enter that settled a name is not a message.
if (this.renaming !== null || this.justRenamed()) return;
// The newline that Enter put into the field is not part of the message - // The newline that Enter put into the field is not part of the message -
// the text of the field as it was before it. // the text of the field as it was before it.
const text = this.enterText ?? this.value; const text = this.enterText ?? this.value;
@@ -688,6 +703,7 @@ 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;
// 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
@@ -913,6 +929,9 @@ export default class Composer extends View {
const m = event.modifiers ?? {}; const m = event.modifiers ?? {};
const view = this.panelView(); 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") { if (key === "escape") {
// An open row menu takes the Esc first. // An open row menu takes the Esc first.
if (!this.overlayEscape(cx)) this.panelCommand("escape", cx); if (!this.overlayEscape(cx)) this.panelCommand("escape", cx);
@@ -990,6 +1009,8 @@ export default class Composer extends View {
/** A row was clicked: it is named by its key (the id of the entry). */ /** A row was clicked: it is named by its key (the id of the entry). */
panelPickKey(key, cx) { panelPickKey(key, cx) {
if (!this.panel) return; if (!this.panel) return;
// The click that closed the menu of a row is not a pick.
if (Date.now() - this.rowMenuClosedAt < 250) return;
const index = this.panelView().state.hits.findIndex((hit) => String(hit.item.id) === key); const index = this.panelView().state.hits.findIndex((hit) => String(hit.item.id) === key);
if (index >= 0) this.panelPick(index, cx); if (index >= 0) this.panelPick(index, cx);
} }
@@ -1019,6 +1040,14 @@ export default class Composer extends View {
cx.notify(); cx.notify();
} }
/** 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;
cx.stop_propagation();
cx.notify();
}
/** The text of the row with this key, or null. */ /** The text of the row with this key, or null. */
panelRowText(key) { panelRowText(key) {
if (!this.panel) return null; if (!this.panel) return null;
@@ -1037,7 +1066,7 @@ export default class Composer extends View {
overlayEscape(cx) { overlayEscape(cx) {
const now = Date.now(); const now = Date.now();
if (this.renaming !== null) { if (this.renaming !== null) {
this.renaming = null; this.endRename(cx, true);
} else if (this.contextMenu) { } else if (this.contextMenu) {
this.contextMenu = null; this.contextMenu = null;
} else if (now >= this.escSwallowUntil) { } else if (now >= this.escSwallowUntil) {
@@ -1085,6 +1114,8 @@ export default class Composer extends View {
else if (menu.kind === "star") { else if (menu.kind === "star") {
if (command === "pin") this.togglePin(this.value, cx); if (command === "pin") this.togglePin(this.value, cx);
} else this.rowCommand(menu.id, command, cx); } else this.rowCommand(menu.id, command, cx);
// The field gets the keyboard back (a name being edited has it).
if (this.renaming === null && !this.panel) this.focusField();
cx.notify(); cx.notify();
} }
@@ -1096,17 +1127,20 @@ 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)); if (command === "mode") this.setDock(toggleMode(this.dock, id), cx);
else if (command === "left") this.setDock(moveButton(this.dock, id, -1)); else if (command === "left") this.setDock(moveButton(this.dock, id, -1), cx);
else if (command === "right") this.setDock(moveButton(this.dock, id, 1)); else if (command === "right") this.setDock(moveButton(this.dock, id, 1), cx);
else if (command === "remove") this.setDock(removeButton(this.dock, id)); else if (command === "remove") this.setDock(removeButton(this.dock, id), cx);
} }
rowCommand(key, command, cx) { rowCommand(key, command, cx) {
const text = this.panelRowText(key); const text = this.panelRowText(key);
if (text === null) return; if (text === null) return;
if (command === "insert") this.panelPickKey(key, cx); if (command === "insert") {
this.rowMenuClosedAt = 0;
this.panelPickKey(key, cx);
}
else if (command === "favorite") this.toggleFavoriteText(text, cx); else if (command === "favorite") this.toggleFavoriteText(text, cx);
else if (command === "pin") this.togglePin(text, cx); else if (command === "pin") this.togglePin(text, cx);
} }
@@ -1114,24 +1148,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)) { if (isPinned(this.dock, text)) {
this.setDock(unpinText(this.dock, text)); this.setDock(unpinText(this.dock, text), cx);
return;
}
if (String(text).trim() === "") {
this.notice("Wpisz tekst, aby przypiąć go do docka.", "problem", cx);
return; return;
} }
if (String(text).trim() === "") return;
if (this.dock.length >= DOCK_LIMIT) { if (this.dock.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())); this.setDock(pinText(this.dock, text, this.newId()), cx);
} }
/** The dock is another list: it is kept and drawn. */ /** The dock is another list: it is kept and drawn. */
setDock(next) { setDock(next, cx) {
this.dock = next; this.dock = next;
try { try {
storage.saveDock(this.store, this.dock); storage.saveDock(this.store, this.dock);
} catch (err) { } catch (err) {
this.notice(`Nie udało się zapisać docka: ${errorText(err)}`, "problem"); this.notice(`Nie udało się zapisać docka: ${errorText(err)}`, "problem", cx);
}
}
/** The field of the composer gets the keyboard. */
focusField() {
try {
this.textarea.focus();
} catch {
// The field does not answer - the focus stays where it is.
} }
} }
@@ -1139,12 +1185,30 @@ export default class Composer extends View {
startRename(button, cx) { startRename(button, cx) {
this.renaming = button.id; this.renaming = button.id;
this.renameStartedAt = Date.now(); this.renameStartedAt = Date.now();
this.renameFocused = 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.
cx.timer.after(60, () => { cx.timer.after(60, () => {
if (this.renaming === button.id) this.renameInput.focus(); if (this.renaming === button.id) this.renameInput.focus();
}); });
// An input that never got the focus must not hold the keys for ever.
cx.timer.after(1000, (cx) => {
if (this.renaming === button.id && !this.renameFocused) this.endRename(cx, false);
});
}
/** The edit of a name is over (saved or not); the field may get the keyboard. */
endRename(cx, focusField) {
this.renaming = null;
this.renameEndedAt = Date.now();
if (focusField) this.focusField();
cx.notify();
}
/** Enter and Esc that arrive just after a name was settled are not the composer's. */
justRenamed() {
return Date.now() - this.renameEndedAt < 150;
} }
/** Enter in the name: it is saved, nothing is sent. */ /** Enter in the name: it is saved, nothing is sent. */
@@ -1152,9 +1216,8 @@ export default class Composer extends View {
const id = this.renaming; const id = this.renaming;
if (id === null) return; if (id === null) return;
const name = this.renameInput.value(); const name = this.renameInput.value();
this.renaming = null; this.endRename(cx, true);
this.setDock(renameButton(this.dock, id, name)); this.setDock(renameButton(this.dock, id, name), cx);
cx.notify();
} }
/** 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. */
@@ -1216,6 +1279,11 @@ 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();
if (this.justRenamed() && (pressed === "enter" || pressed === "escape")) {
cx.stop_propagation();
return;
}
if (this.preview && String(event.key).toLowerCase() === "escape") { if (this.preview && String(event.key).toLowerCase() === "escape") {
this.closePreview(cx); this.closePreview(cx);
cx.stop_propagation(); cx.stop_propagation();
@@ -2389,17 +2457,24 @@ export default class Composer extends View {
) )
// The menu of a row: a layer over the panel, at its top right corner. // The menu of a row: a layer over the panel, at its top right corner.
.when(this.contextMenu?.kind === "row", (el) => .when(this.contextMenu?.kind === "row", (el) =>
el.child( el
// A clear layer under the menu swallows the click outside it: that
// click only closes the menu and does not reach the list.
.child(
div()
.id("composer-row-menu-shield")
.absolute()
.inset_0()
.on_mouse_down((event, cx) => this.closeRowMenuByClick(cx))
.on_click((event, cx) => this.closeRowMenuByClick(cx)),
)
.child(
div() div()
.id("composer-row-menu") .id("composer-row-menu")
.absolute() .absolute()
.top(52) .top(52)
.right(18) .right(18)
.on_mouse_down_out((event, cx) => { .on_mouse_down_out((event, cx) => this.closeRowMenuByClick(cx))
if (this.contextMenu?.kind !== "row") return;
this.contextMenu = null;
cx.notify();
})
.child(this.renderContextMenu(t, this.contextItems(), (command, cx) => this.menuCommand(command, cx))), .child(this.renderContextMenu(t, this.contextItems(), (command, cx) => this.menuCommand(command, cx))),
), ),
); );