diff --git a/herdr/herdr-composer/mods/herdr-composer/main.js b/herdr/herdr-composer/mods/herdr-composer/main.js index cea84d6..9de5261 100644 --- a/herdr/herdr-composer/mods/herdr-composer/main.js +++ b/herdr/herdr-composer/mods/herdr-composer/main.js @@ -244,6 +244,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; this.panel.index = 0; this.scrollPanel(0); cx.notify(); @@ -273,12 +274,21 @@ export default class Composer extends View { this.escSwallowUntil = 0; this.renameInput = InputState.new({ placeholder: "Nazwa" }); 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) => { - // Losing the focus cancels - but not in the moment the menu hands it back. - if (this.renaming !== null && Date.now() - this.renameStartedAt > 400) { - this.renaming = null; - cx.notify(); + 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. + 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). @@ -364,6 +374,9 @@ export default class Composer extends View { // The draft of the agent we leave goes to the disk at once. this.saveDraftNow(); 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; 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. */ 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 text of the field as it was before it. 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. if (command === "escape" && this.overlayEscape(cx)) 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 // 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 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") { // An open row menu takes the Esc first. 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). */ panelPickKey(key, cx) { 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); if (index >= 0) this.panelPick(index, cx); } @@ -1019,6 +1040,14 @@ export default class Composer extends View { 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. */ panelRowText(key) { if (!this.panel) return null; @@ -1037,7 +1066,7 @@ export default class Composer extends View { overlayEscape(cx) { const now = Date.now(); if (this.renaming !== null) { - this.renaming = null; + this.endRename(cx, true); } else if (this.contextMenu) { this.contextMenu = null; } else if (now >= this.escSwallowUntil) { @@ -1085,6 +1114,8 @@ export default class Composer extends View { else if (menu.kind === "star") { if (command === "pin") this.togglePin(this.value, 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(); } @@ -1096,17 +1127,20 @@ export default class Composer extends View { this.startRename(button, cx); return; } - if (command === "mode") this.setDock(toggleMode(this.dock, id)); - else if (command === "left") this.setDock(moveButton(this.dock, id, -1)); - else if (command === "right") this.setDock(moveButton(this.dock, id, 1)); - else if (command === "remove") this.setDock(removeButton(this.dock, id)); + 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); } rowCommand(key, command, cx) { const text = this.panelRowText(key); 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 === "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. */ togglePin(text, cx) { 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; } - if (String(text).trim() === "") return; if (this.dock.length >= DOCK_LIMIT) { this.notice(`Dock jest pełny (${DOCK_LIMIT} przycisków).`, "problem", cx); 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. */ - setDock(next) { + setDock(next, cx) { this.dock = next; try { storage.saveDock(this.store, this.dock); } 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) { this.renaming = button.id; this.renameStartedAt = Date.now(); + this.renameFocused = 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. cx.timer.after(60, () => { 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. */ @@ -1152,9 +1216,8 @@ export default class Composer extends View { const id = this.renaming; if (id === null) return; const name = this.renameInput.value(); - this.renaming = null; - this.setDock(renameButton(this.dock, id, name)); - cx.notify(); + this.endRename(cx, true); + this.setDock(renameButton(this.dock, id, name), cx); } /** 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. 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") { this.closePreview(cx); 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. .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() .id("composer-row-menu") .absolute() .top(52) .right(18) - .on_mouse_down_out((event, cx) => { - if (this.contextMenu?.kind !== "row") return; - this.contextMenu = null; - cx.notify(); - }) + .on_mouse_down_out((event, cx) => this.closeRowMenuByClick(cx)) .child(this.renderContextMenu(t, this.contextItems(), (command, cx) => this.menuCommand(command, cx))), ), );