fix: dock menu opens on the first right click after a command
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
52d2259060
commit
3c65a4f577
3 files changed
+61
-7
No files matched your search
@@ -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;
|
||||||
|
}
|
||||||
@@ -79,6 +79,7 @@ import {
|
|||||||
move as moveButton,
|
move as moveButton,
|
||||||
rename as renameButton,
|
rename as renameButton,
|
||||||
toggleMode,
|
toggleMode,
|
||||||
|
menuTransition,
|
||||||
DOCK_LIMIT,
|
DOCK_LIMIT,
|
||||||
} from "./dock.js";
|
} from "./dock.js";
|
||||||
import {
|
import {
|
||||||
@@ -267,6 +268,7 @@ export default class Composer extends View {
|
|||||||
// The right-click menu that is open: { kind: "dock"|"row"|"star", id } or
|
// 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.
|
// null. `renaming` is the id of the button whose name is being edited.
|
||||||
this.contextMenu = null;
|
this.contextMenu = null;
|
||||||
|
this.menuOpenedAt = 0;
|
||||||
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
|
||||||
@@ -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. */
|
/** What the open/close signal of a `Popover` does to the state of the menu. */
|
||||||
popoverChanged(kind, id, open, cx) {
|
popoverChanged(kind, id, open, cx) {
|
||||||
if (open) {
|
const now = Date.now();
|
||||||
this.contextMenu = { kind, id };
|
// A "closed" that comes in the moment the menu opened is the old menu's
|
||||||
} else if (this.contextMenu && this.contextMenu.kind === kind && this.contextMenu.id === id) {
|
// dismissal landing on the new press: the menu stays.
|
||||||
this.contextMenu = null;
|
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.
|
// The Popover took Esc itself: the same press must not reach the agent.
|
||||||
this.escSwallowUntil = Date.now() + 80;
|
this.escSwallowUntil = now + 80;
|
||||||
}
|
}
|
||||||
cx.notify();
|
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. */
|
/** A command of the menu that is open was chosen. */
|
||||||
menuCommand(command, cx) {
|
menuCommand(command, cx) {
|
||||||
const menu = this.contextMenu;
|
const menu = this.contextMenu;
|
||||||
@@ -2872,6 +2890,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));
|
||||||
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")
|
||||||
@@ -2927,7 +2946,7 @@ export default class Composer extends View {
|
|||||||
* opens the menu that pins the text of the field to the dock.
|
* opens the menu that pins the text of the field to the dock.
|
||||||
*/
|
*/
|
||||||
renderStar(t, active) {
|
renderStar(t, active) {
|
||||||
const icon = this.renderIcon(
|
let icon = this.renderIcon(
|
||||||
t,
|
t,
|
||||||
"composer-star",
|
"composer-star",
|
||||||
isFavorite(this.favorites, this.value) ? "★" : "☆",
|
isFavorite(this.favorites, this.value) ? "★" : "☆",
|
||||||
@@ -2938,6 +2957,7 @@ export default class Composer extends View {
|
|||||||
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));
|
||||||
return Popover.new("composer-star-menu")
|
return Popover.new("composer-star-menu")
|
||||||
.mouse_button("right")
|
.mouse_button("right")
|
||||||
.anchor("bottom_left")
|
.anchor("bottom_left")
|
||||||
|
|||||||
+19
-1
@@ -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,
|
rename, toggleMode, dockAction, dockMenu, rowMenu, starMenu, menuTransition,
|
||||||
} 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 });
|
||||||
@@ -131,3 +131,21 @@ test("normalize: duplicate ids dropped (first wins), labels trimmed", () => {
|
|||||||
{ id: "2", label: "c", text: "c", mode: "send" },
|
{ 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);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user