From 76004542ab08a93c96434256e54a338debaef0a0 Mon Sep 17 00:00:00 2001 From: Factor Date: Thu, 19 Nov 2026 16:03:09 +0100 Subject: [PATCH] feat: right-click menus for dock, star and panel rows Co-Authored-By: Ovis Fast (Claude) --- .../mods/herdr-composer/main.js | 317 ++++++++++++++++-- .../mods/herdr-composer/panel.js | 7 +- 2 files changed, 299 insertions(+), 25 deletions(-) diff --git a/herdr/herdr-composer/mods/herdr-composer/main.js b/herdr/herdr-composer/mods/herdr-composer/main.js index f2680f2..cea84d6 100644 --- a/herdr/herdr-composer/mods/herdr-composer/main.js +++ b/herdr/herdr-composer/mods/herdr-composer/main.js @@ -24,7 +24,7 @@ // from the agent's status. import { View, div, image } from "gpui-kit"; -import { v_flex, h_flex, TextareaState, Textarea, InputState, VirtualListScrollHandle } from "gpui-base"; +import { v_flex, h_flex, TextareaState, Textarea, InputState, Input, Popover, VirtualListScrollHandle } from "gpui-base"; import { agents as liveAgents, panes as livePanes, @@ -67,7 +67,20 @@ import { import * as storage from "./storage.js"; import { record, panelState, moveIndex, otherTab, toggleFavorite, isFavorite } from "./history.js"; import { renderPanel, renderPreview } from "./panel.js"; -import { dockAction } from "./dock.js"; +import { + dockAction, + dockMenu, + rowMenu, + starMenu, + isPinned, + pin as pinText, + unpin as unpinText, + remove as removeButton, + move as moveButton, + rename as renameButton, + toggleMode, + DOCK_LIMIT, +} from "./dock.js"; import { agentKey, agentName, @@ -250,6 +263,23 @@ export default class Composer extends View { this.favorites = storage.loadFavorites(this.store); // The buttons of the dock - one dock for every agent (dock.js). this.dock = storage.loadDock(this.store); + // 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.renaming = null; + this.renameStartedAt = 0; + // Esc that an overlay (menu, name edit) took must not reach the agent a + // moment later by another road (the action and the key arrive separately). + this.escSwallowUntil = 0; + this.renameInput = InputState.new({ placeholder: "Nazwa" }); + this.renameInput.on("submit", (event, cx) => this.renameCommit(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 && Date.now() - this.renameStartedAt > 400) { + this.renaming = null; + cx.notify(); + } + }); // The preview of an image (null when closed). this.preview = null; @@ -654,6 +684,11 @@ export default class Composer extends View { if (this.lastAction && this.lastAction.command === command && now - this.lastAction.at < 40) return; this.lastAction = { command, at: now }; + // An open menu or a name being edited has the keys first: Esc closes it + // (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; + // 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. @@ -716,6 +751,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; this.panelSearchFocused = false; if (this.panelRestorePx !== null) { this.resizeBlock(this.panelRestorePx); @@ -878,7 +914,8 @@ export default class Composer extends View { const view = this.panelView(); if (key === "escape") { - this.panelCommand("escape", cx); + // An open row menu takes the Esc first. + if (!this.overlayEscape(cx)) this.panelCommand("escape", cx); } else if (key === "arrowdown" || key === "down") { this.panelCommand("down", cx); } else if (key === "arrowup" || key === "up") { @@ -974,11 +1011,150 @@ export default class Composer extends View { cx.notify(); } - /** A row was right-clicked: its text is a favourite, or no longer. */ - panelToggleKey(key, cx) { + /** A row was right-clicked: its menu opens (Insert, pin, favourite). */ + panelRowMenu(key, event, cx) { if (!this.panel) return; + if (!this.panelRowText(key)) return; + this.contextMenu = { kind: "row", id: key }; + cx.notify(); + } + + /** The text of the row with this key, or null. */ + panelRowText(key) { + if (!this.panel) return null; const hit = this.panelView().state.hits.find((entry) => String(entry.item.id) === key); - if (hit) this.toggleFavoriteText(hit.item.text, cx); + return hit ? hit.item.text : null; + } + + // --- right-click menus (dock, star, rows of the panel) ---------------------- + + /** + * Esc while a menu is open or a name is being edited: it closes that and + * nothing else. True when the Esc was taken - also for the second delivery + * of the same press (the action and the key arrive separately), so it never + * reaches the panel or the agent. + */ + overlayEscape(cx) { + const now = Date.now(); + if (this.renaming !== null) { + this.renaming = null; + } else if (this.contextMenu) { + this.contextMenu = null; + } else if (now >= this.escSwallowUntil) { + return false; + } + this.escSwallowUntil = now + 80; + cx.notify(); + return true; + } + + /** The items of the menu that is open. */ + contextItems() { + const menu = this.contextMenu; + if (!menu) return []; + if (menu.kind === "dock") return dockMenu(this.dock, menu.id); + if (menu.kind === "star") return starMenu({ text: this.value, pinned: isPinned(this.dock, this.value) }); + const text = this.panelRowText(menu.id) ?? ""; + return rowMenu({ + tab: this.panel?.tab ?? "history", + favorite: isFavorite(this.favorites, text), + pinned: isPinned(this.dock, text), + }); + } + + /** 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; + // The Popover took Esc itself: the same press must not reach the agent. + this.escSwallowUntil = Date.now() + 80; + } + cx.notify(); + } + + /** A command of the menu that is open was chosen. */ + menuCommand(command, cx) { + const menu = this.contextMenu; + if (!menu) return; + this.contextMenu = null; + this.escSwallowUntil = Date.now() + 80; + + if (menu.kind === "dock") this.dockCommand(menu.id, command, cx); + else if (menu.kind === "star") { + if (command === "pin") this.togglePin(this.value, cx); + } else this.rowCommand(menu.id, command, cx); + cx.notify(); + } + + dockCommand(id, command, cx) { + const button = this.dock.find((entry) => entry.id === id); + if (!button) return; + + if (command === "rename") { + 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)); + } + + rowCommand(key, command, cx) { + const text = this.panelRowText(key); + if (text === null) return; + + if (command === "insert") this.panelPickKey(key, cx); + else if (command === "favorite") this.toggleFavoriteText(text, cx); + else if (command === "pin") this.togglePin(text, cx); + } + + /** 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)); + 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())); + } + + /** The dock is another list: it is kept and drawn. */ + setDock(next) { + this.dock = next; + try { + storage.saveDock(this.store, this.dock); + } catch (err) { + this.notice(`Nie udało się zapisać docka: ${errorText(err)}`, "problem"); + } + } + + /** The button becomes a small input with its name. */ + startRename(button, cx) { + this.renaming = button.id; + this.renameStartedAt = Date.now(); + 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(); + }); + } + + /** Enter in the name: it is saved, nothing is sent. */ + renameCommit(cx) { + 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(); } /** The star next to the field: the text of the field is a favourite, or no longer. */ @@ -1034,6 +1210,12 @@ export default class Composer extends View { /** The keyboard of the composer. */ key(event, cx) { + if (String(event.key).toLowerCase() === "escape" && this.overlayEscape(cx)) { + cx.stop_propagation(); + return; + } + // The name of a dock button is being typed: the keys are the input's. + if (this.renaming !== null) return; if (this.preview && String(event.key).toLowerCase() === "escape") { this.closePreview(cx); cx.stop_propagation(); @@ -2195,7 +2377,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), - toggle: (key, cx) => this.panelToggleKey(key, cx), + rowMenu: (key, event, cx) => this.panelRowMenu(key, event, 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. @@ -2204,6 +2386,57 @@ export default class Composer extends View { cx.notify(); }, }), + ) + // The menu of a row: a layer over the panel, at its top right corner. + .when(this.contextMenu?.kind === "row", (el) => + el.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(); + }) + .child(this.renderContextMenu(t, this.contextItems(), (command, cx) => this.menuCommand(command, cx))), + ), + ); + } + + /** + * One menu for everything that opens with a right click: a column of items. + * `danger` is drawn in the error colour, `disabled` is dimmed and does nothing. + */ + renderContextMenu(t, items, onCommand) { + return v_flex() + .id("composer-context-menu") + .min_w(210) + .p(4) + .rounded(8) + .border(1) + .border_color(t.border) + .bg(t.raised) + .children( + items.map((item) => { + let row = div() + .id(`composer-menu-${item.command}`) + .h(28) + .px(10) + .flex() + .items_center() + .rounded(6) + .text_size(12.5) + .text_color(item.disabled ? t.dim : item.danger ? t.blocked : t.foreground); + if (!item.disabled) { + row = row + .cursor_pointer() + .hover((s) => s.bg(t.hover)) + .on_click((event, cx) => onCommand(item.command, cx)); + } + return row.child(item.label); + }), ); } @@ -2488,16 +2721,7 @@ export default class Composer extends View { cx.notify(); }), ) - .child( - this.renderIcon( - t, - "composer-star", - isFavorite(this.favorites, this.value) ? "★" : "☆", - isFavorite(this.favorites, this.value), - active, - (event, cx) => this.toggleCurrentFavorite(cx), - ), - ) + .child(this.renderStar(t, active)) .child(div().w(1).h(18).flex_none().bg(t.border)) .child(this.renderDock(t, active)) .child(this.renderChip(t, agent, status, files.length)) @@ -2531,6 +2755,16 @@ export default class Composer extends View { /** One button of the dock: `send` has a fill, `insert` a frame and the ✎ mark. */ renderDockButton(t, button, active) { + // The name is being edited: a small input in place of the button + // (Enter saves, Esc or losing the focus cancels). + if (this.renaming === button.id) { + return div() + .id(`dock-rename-${button.id}`) + .flex_none() + .w(110) + .h(28) + .child(Input.new(this.renameInput).w_full().h(28).text_size(12)); + } const insert = button.mode === "insert"; let el = h_flex() .id(`dock-${button.id}`) @@ -2552,7 +2786,18 @@ export default class Composer extends View { .on_click((event, cx) => this.dockPress(button, cx)); } if (insert) el = el.child(div().flex_none().text_color(t.dim).child("✎ ")); - return el.child(div().min_w(0).truncate().child(button.label)); + el = el.child(div().min_w(0).truncate().child(button.label)); + if (!active) return el; + + // Right click: the menu of the button, anchored to it. + const open = this.contextMenu?.kind === "dock" && this.contextMenu.id === button.id; + return Popover.new(`dock-menu-${button.id}`) + .mouse_button("right") + .anchor("bottom_left") + .open(open) + .on_open_change((value, cx) => this.popoverChanged("dock", button.id, value, cx)) + .trigger(el) + .content(this.renderContextMenu(t, open ? this.contextItems() : [], (command, cx) => this.menuCommand(command, cx))); } /** @@ -2568,6 +2813,13 @@ export default class Composer extends View { const action = dockAction(button, { hasAgent }); if (action === "none" || !hasAgent) return; + // Right after the focus moved to another agent the composer may still + // show the previous one: neither the text nor the message goes anywhere. + if (!this.syncOk(cx)) { + this.notice("Agent się zmienił — wiadomość nie została wysłana.", "problem", cx); + return; + } + if (action === "insert") { this.textarea.replace(button.text); this.syncFieldAfterReplace(); @@ -2576,10 +2828,6 @@ export default class Composer extends View { return; } - if (!this.syncOk(cx)) { - this.notice("Agent się zmienił — wiadomość nie została wysłana.", "problem", cx); - return; - } const agent = this.target(); if (!agent) { this.notice("Composer nie ma do kogo pisać — brak agenta.", "problem", cx); @@ -2593,6 +2841,31 @@ export default class Composer extends View { this.finish(agent, button.text.trim(), cx); } + /** + * The star: a left click toggles the favourite as before; a right click + * opens the menu that pins the text of the field to the dock. + */ + renderStar(t, active) { + const icon = this.renderIcon( + t, + "composer-star", + isFavorite(this.favorites, this.value) ? "★" : "☆", + isFavorite(this.favorites, this.value), + active, + (event, cx) => this.toggleCurrentFavorite(cx), + ); + if (!active) return icon; + + const open = this.contextMenu?.kind === "star"; + return Popover.new("composer-star-menu") + .mouse_button("right") + .anchor("bottom_left") + .open(open) + .on_open_change((value, cx) => this.popoverChanged("star", "star", value, cx)) + .trigger(icon) + .content(this.renderContextMenu(t, open ? this.contextItems() : [], (command, cx) => this.menuCommand(command, cx))); + } + /** A round icon button next to the field (history, star). */ renderIcon(t, id, glyph, lit, enabled, action) { return div() diff --git a/herdr/herdr-composer/mods/herdr-composer/panel.js b/herdr/herdr-composer/mods/herdr-composer/panel.js index dd29b53..4ad47e6 100644 --- a/herdr/herdr-composer/mods/herdr-composer/panel.js +++ b/herdr/herdr-composer/mods/herdr-composer/panel.js @@ -10,7 +10,8 @@ // handlers (the host refuses a handler inside the item renderer) - one // `on_item_click` serves the whole list and names the row by its key. So a row // click inserts the text; the star of a row is a sign, not a button (the -// favourites are toggled with Ctrl+D or a right click - `on_item_secondary_click`). +// favourites are toggled with Ctrl+D or from the menu a right click opens - +// `on_item_secondary_click`). import { div, image } from "gpui-kit"; import { v_flex, h_flex, Input, Scrollbar, v_virtual_list } from "gpui-base"; @@ -47,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, - * toggle: (key:string, cx:any) => void, + * rowMenu: (key:string, event:any, cx:any) => void, * clear: (cx:any) => void, * close: (cx:any) => void, * }} on @@ -175,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.toggle(key, cx)), + .on_item_secondary_click((key, event, cx) => on.rowMenu(key, event, cx)), ) .child(Scrollbar.vertical("composer-panel-list").absolute().inset_0()); }