From 3c65a4f57700fc73a6d8ee84e3f2cb1bafb1fb96 Mon Sep 17 00:00:00 2001 From: Factor Date: Thu, 19 Nov 2026 16:12:38 +0100 Subject: [PATCH] fix: dock menu opens on the first right click after a command Co-Authored-By: Ovis Fast (Claude) --- .../mods/herdr-composer/dock.js | 16 ++++++++++ .../mods/herdr-composer/main.js | 32 +++++++++++++++---- tests/dock.test.mjs | 20 +++++++++++- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/herdr/herdr-composer/mods/herdr-composer/dock.js b/herdr/herdr-composer/mods/herdr-composer/dock.js index 5a63efb..c603566 100644 --- a/herdr/herdr-composer/mods/herdr-composer/dock.js +++ b/herdr/herdr-composer/mods/herdr-composer/dock.js @@ -229,3 +229,19 @@ export function starMenu({ text, pinned }) { }, ]; } + +/** + * The menu that is open after a `Popover` said it opened or closed. + * + * Opening always opens that menu (the one before it, if any, is replaced). + * Closing only closes the menu it is about: a late "closed" of a menu that + * another one has replaced must leave the new one open. + * + * @param {{kind: string, id: string}|null} current + * @param {{kind: string, id: string, open: boolean}} change + * @returns {{kind: string, id: string}|null} + */ +export function menuTransition(current, { kind, id, open }) { + if (open) return { kind, id }; + return current && current.kind === kind && current.id === id ? null : current; +} diff --git a/herdr/herdr-composer/mods/herdr-composer/main.js b/herdr/herdr-composer/mods/herdr-composer/main.js index 8828862..ab04f2a 100644 --- a/herdr/herdr-composer/mods/herdr-composer/main.js +++ b/herdr/herdr-composer/mods/herdr-composer/main.js @@ -79,6 +79,7 @@ import { move as moveButton, rename as renameButton, toggleMode, + menuTransition, DOCK_LIMIT, } from "./dock.js"; import { @@ -267,6 +268,7 @@ export default class Composer extends View { // The right-click menu that is open: { kind: "dock"|"row"|"star", id } or // null. `renaming` is the id of the button whose name is being edited. this.contextMenu = null; + this.menuOpenedAt = 0; this.renaming = null; this.renameStartedAt = 0; // Esc that an overlay (menu, name edit) took must not reach the agent a @@ -1093,16 +1095,32 @@ export default class Composer extends View { /** What the open/close signal of a `Popover` does to the state of the menu. */ popoverChanged(kind, id, open, cx) { - if (open) { - this.contextMenu = { kind, id }; - } else if (this.contextMenu && this.contextMenu.kind === kind && this.contextMenu.id === id) { - this.contextMenu = null; + const now = Date.now(); + // A "closed" that comes in the moment the menu opened is the old menu's + // dismissal landing on the new press: the menu stays. + if (!open && this.contextMenu && now - this.menuOpenedAt < 150) { + cx.notify(); + return; + } + const before = this.contextMenu; + this.contextMenu = menuTransition(before, { kind, id, open }); + if (open) this.menuOpenedAt = now; + else if (before && !this.contextMenu) { // The Popover took Esc itself: the same press must not reach the agent. - this.escSwallowUntil = Date.now() + 80; + 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); + } + /** A command of the menu that is open was chosen. */ menuCommand(command, cx) { const menu = this.contextMenu; @@ -2872,6 +2890,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)); return Popover.new(`dock-menu-${button.id}`) .mouse_button("right") .anchor("bottom_left") @@ -2927,7 +2946,7 @@ export default class Composer extends View { * opens the menu that pins the text of the field to the dock. */ renderStar(t, active) { - const icon = this.renderIcon( + let icon = this.renderIcon( t, "composer-star", isFavorite(this.favorites, this.value) ? "★" : "☆", @@ -2938,6 +2957,7 @@ export default class Composer extends View { if (!active) return icon; const open = this.contextMenu?.kind === "star"; + icon = icon.on_mouse_down("right", (event, cx) => this.openMenuByPress("star", "star", cx)); return Popover.new("composer-star-menu") .mouse_button("right") .anchor("bottom_left") diff --git a/tests/dock.test.mjs b/tests/dock.test.mjs index 92f0f91..b42b528 100644 --- a/tests/dock.test.mjs +++ b/tests/dock.test.mjs @@ -2,7 +2,7 @@ import test from "node:test"; import assert from "node:assert/strict"; import { DOCK_LIMIT, LABEL_MAX, defaultLabel, normalize, isPinned, pin, unpin, remove, move, - rename, toggleMode, dockAction, dockMenu, rowMenu, starMenu, + rename, toggleMode, dockAction, dockMenu, rowMenu, starMenu, menuTransition, } from "../herdr/herdr-composer/mods/herdr-composer/dock.js"; const btn = (id, text, extra = {}) => ({ id, label: text, text, mode: "send", ...extra }); @@ -131,3 +131,21 @@ test("normalize: duplicate ids dropped (first wins), labels trimmed", () => { { id: "2", label: "c", text: "c", mode: "send" }, ]); }); + +test("menuTransition: open replaces, close only closes its own menu", () => { + const A = { kind: "dock", id: "A" }; + const B = { kind: "dock", id: "B" }; + assert.deepEqual(menuTransition(null, { ...A, open: true }), A); + // open A, a command (menu closed), open B -> B is open + let state = menuTransition(null, { ...A, open: true }); + state = null; + assert.deepEqual(menuTransition(state, { ...B, open: true }), B); + // open A, then open B, then the late close of A -> B stays + state = menuTransition(A, { ...B, open: true }); + assert.deepEqual(menuTransition(state, { ...A, open: false }), B); + // close of the open one closes; a close with nothing open stays null + assert.equal(menuTransition(B, { ...B, open: false }), null); + assert.equal(menuTransition(null, { ...A, open: false }), null); + // same id, other kind is another menu + assert.deepEqual(menuTransition(B, { kind: "star", id: "B", open: false }), B); +});