diff --git a/herdr/herdr-composer/RECIPE.md b/herdr/herdr-composer/RECIPE.md index 6191a45..668b216 100644 --- a/herdr/herdr-composer/RECIPE.md +++ b/herdr/herdr-composer/RECIPE.md @@ -164,7 +164,7 @@ It makes no network request. to (`herdr-composer.userMin`), 120-520, resized after 120 ms and only for a difference over 4 px; the panel pauses it. - Row handlers cannot live inside a `v_virtual_list` renderer: a row click uses - `on_item_click`, so the star in a panel row is a sign; Ctrl+D or a right click toggle it. + `on_item_click`; the star in a panel row is found by where `on_mouse_down` on the list landed (STAR_ZONE, right edge). Ctrl+D or a right click toggle it too. - `image()` draws only files inside the app's folder (give it the path relative to `appHome()`): thumbnails exist for pasted images; an image from elsewhere gets a tile with its extension and no preview. diff --git a/herdr/herdr-composer/mods/herdr-composer/dock.js b/herdr/herdr-composer/mods/herdr-composer/dock.js index 6005ae5..78145c5 100644 --- a/herdr/herdr-composer/mods/herdr-composer/dock.js +++ b/herdr/herdr-composer/mods/herdr-composer/dock.js @@ -258,3 +258,22 @@ export function menuTransition(current, { kind, id, open }) { export function applyDock(stored, operation) { return normalize(operation(normalize(stored))); } + +/** + * Add a button made by hand (the "+" form): the text, a name (empty gives the + * default label) and a mode. The same dock comes back when the text is empty, + * already pinned, or the dock is full. + * + * @param {DockButton[]} dock + * @param {{id: string, text: string, label?: string, mode?: string}} button + * @returns {DockButton[]} + */ +export function add(dock, { id, text, label, mode }) { + const clean = String(text).trim(); + if (clean === "" || isPinned(dock, clean) || dock.length >= DOCK_LIMIT) return dock; + const name = String(label ?? "").trim(); + return [ + ...dock, + { id, label: name === "" ? defaultLabel(clean) : name, text: clean, mode: mode === "insert" ? "insert" : "send" }, + ]; +} diff --git a/herdr/herdr-composer/mods/herdr-composer/main.js b/herdr/herdr-composer/mods/herdr-composer/main.js index e2ebb55..8430a76 100644 --- a/herdr/herdr-composer/mods/herdr-composer/main.js +++ b/herdr/herdr-composer/mods/herdr-composer/main.js @@ -66,7 +66,7 @@ import { } from "./clipboard.js"; import * as storage from "./storage.js"; import { record, panelState, moveIndex, otherTab, toggleFavorite, isFavorite } from "./history.js"; -import { renderPanel, renderPreview } from "./panel.js"; +import { renderPanel, renderPreview, STAR_ZONE } from "./panel.js"; import { dockAction, dockMenu, @@ -81,6 +81,7 @@ import { toggleMode, menuTransition, applyDock, + add as addButton, DOCK_LIMIT, } from "./dock.js"; import { @@ -279,6 +280,11 @@ export default class Composer extends View { this.escSwallowUntil = 0; this.renameInput = InputState.new({ placeholder: "Nazwa" }); this.renameInput.on("submit", (event, cx) => this.renameCommit(cx)); + // The form of the "+" button: a name, the text and the mode of a new button. + this.addLabel = InputState.new({ placeholder: "Nazwa (puste = z tekstu)" }); + this.addText = TextareaState.new({ value: "", placeholder: "Tekst przycisku", rows: 3 }); + this.addMode = "send"; + this.addLabel.on("submit", (event, cx) => this.addCommit(cx)); this.renameEndedAt = 0; this.renameFocused = false; this.rowMenuClosedAt = 0; @@ -1015,11 +1021,29 @@ export default class Composer extends View { cx.notify(); } + /** + * A left press on the list, before the click of the row. A row cannot carry + * its own handlers, so where the press landed is kept here: the star is the + * right edge of the row (see STAR_ZONE in panel.js). + */ + panelPress(event) { + const x = event?.local_position?.x; + const width = event?.bounds?.width; + this.panelStarPress = typeof x === "number" && typeof width === "number" && x >= width - STAR_ZONE; + } + /** 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; + // The click on the star of a row toggles the favourite and inserts nothing. + if (this.panelStarPress) { + this.panelStarPress = false; + const text = this.panelRowText(key); + if (text) this.toggleFavoriteText(text, cx); + return; + } const index = this.panelView().state.hits.findIndex((hit) => String(hit.item.id) === key); if (index >= 0) this.panelPick(index, cx); } @@ -1120,6 +1144,7 @@ export default class Composer extends View { this.menuOpenedAt = now; // Another copy may have changed the dock: the menu shows the fresh one. this.refreshDock(cx); + if (kind === "add") this.startAdd(cx); } else if (before && !this.contextMenu) { this.menuClosedAt = now; // The Popover took Esc itself: the same press must not reach the agent. @@ -1229,6 +1254,141 @@ export default class Composer extends View { } } + /** The "+" form opens: the text of the field is the first guess for the text of the button. */ + startAdd(cx) { + this.addLabel.set_value(""); + this.addText.set_value(this.value.trim()); + this.addMode = "send"; + cx.timer.after(60, () => { + if (this.contextMenu?.kind !== "add") return; + try { + this.addText.focus(); + } catch { + // The form stays; the person clicks into it. + } + }); + } + + /** "Dodaj" in the form: the button goes to the end of the dock. */ + addCommit(cx) { + const text = this.addText.value(); + const label = this.addLabel.value(); + const stored = storage.loadDock(this.store); + if (text.trim() === "") { + this.notice("Wpisz tekst przycisku.", "problem", cx); + return; + } + if (isPinned(stored, text)) { + this.notice("Taki przycisk już jest w docku.", "problem", cx); + return; + } + if (stored.length >= DOCK_LIMIT) { + this.notice(`Dock jest pełny (${DOCK_LIMIT} przycisków).`, "problem", cx); + return; + } + const id = this.newId(); + const mode = this.addMode; + this.dropContextMenu(); + this.escSwallowUntil = Date.now() + 80; + this.setDock((dock) => addButton(dock, { id, text, label, mode }), cx); + this.focusField(); + cx.notify(); + } + + /** "Anuluj" in the form. */ + addCancel(cx) { + this.dropContextMenu(); + this.escSwallowUntil = Date.now() + 80; + this.focusField(); + cx.notify(); + } + + /** The form of the "+" button: name, text, mode, and the two buttons. */ + renderAddForm(t) { + const insert = this.addMode === "insert"; + const button = (id, label, fill, action) => + div() + .id(id) + .h(28) + .px(12) + .flex() + .items_center() + .rounded(6) + .text_size(12.5) + .text_color(fill ? t.foreground : t.muted) + .when(fill, (el) => el.bg(t.accent)) + .cursor_pointer() + .hover((s) => s.bg(t.hover)) + .on_click((event, cx) => action(cx)) + .child(label); + + return v_flex() + .id("composer-add-form") + .w(320) + .p(10) + .gap(8) + .rounded(8) + .border(1) + .border_color(t.border) + .bg(t.raised) + .child(div().text_size(12).text_color(t.dim).child("Nowy przycisk docka")) + .child(Input.new(this.addLabel).w_full().h(28).text_size(12.5)) + .child(Textarea.new(this.addText).w_full().px(8).py(6).rounded(6).bg(t.background).text_size(12.5)) + .child( + div() + .id("composer-add-mode") + .h(28) + .px(10) + .flex() + .items_center() + .rounded(6) + .bg(t.chrome) + .text_size(12.5) + .text_color(t.muted) + .cursor_pointer() + .hover((s) => s.bg(t.hover)) + .on_click((event, cx) => { + this.addMode = insert ? "send" : "insert"; + cx.notify(); + }) + .child(insert ? "Tryb: tylko wstawia (✎)" : "Tryb: wysyła od razu"), + ) + .child( + h_flex() + .gap(8) + .justify_end() + .child(button("composer-add-cancel", "Anuluj", false, (cx) => this.addCancel(cx))) + .child(button("composer-add-ok", "Dodaj", true, (cx) => this.addCommit(cx))), + ); + } + + /** The "+" of the dock: it opens the form. */ + renderAddButton(t, active) { + const icon = div() + .id("composer-dock-add") + .size(28) + .rounded(999) + .flex_none() + .flex() + .items_center() + .justify_center() + .bg(t.chrome) + .text_size(15) + .text_color(active ? t.muted : t.dim) + .cursor_pointer() + .hover((s) => s.bg(t.hover).text_color(t.foreground)) + .tooltip("Dodaj przycisk do docka") + .child("+"); + if (!active) return icon; + const open = this.contextMenu?.kind === "add"; + return Popover.new("composer-dock-add-menu") + .anchor("bottom_left") + .open(open) + .on_open_change((value, cx) => this.popoverChanged("add", "add", value, cx)) + .trigger(icon) + .content(open ? this.renderAddForm(t) : div()); + } + /** The button becomes a small input with its name. */ startRename(button, cx) { this.renaming = button.id; @@ -2497,6 +2657,7 @@ export default class Composer extends View { tab: (tab, cx) => this.panelTab(tab, cx), pick: (key, cx) => this.panelPickKey(key, cx), rowMenu: (key, cx) => this.panelRowMenu(key, cx), + press: (event) => this.panelPress(event), 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. @@ -2877,12 +3038,13 @@ export default class Composer extends View { .items_center() .overflow_x_scroll(); + const add = this.renderAddButton(t, active); if (this.dock.length === 0) { - return row.child( - div().flex_none().text_size(12).text_color(t.dim).child("Prawy klik w Ulubionych → Przypnij do docka"), - ); + return row + .child(add) + .child(div().flex_none().text_size(12).text_color(t.dim).child("Plus lub prawy klik w Ulubionych → Przypnij do docka")); } - return row.children(this.dock.map((button) => this.renderDockButton(t, button, active))); + return row.children([add, ...this.dock.map((button) => this.renderDockButton(t, button, active))]); } /** One button of the dock: `send` has a fill, `insert` a frame and the ✎ mark. */ diff --git a/herdr/herdr-composer/mods/herdr-composer/panel.js b/herdr/herdr-composer/mods/herdr-composer/panel.js index bcb3108..69f51b1 100644 --- a/herdr/herdr-composer/mods/herdr-composer/panel.js +++ b/herdr/herdr-composer/mods/herdr-composer/panel.js @@ -9,9 +9,10 @@ // Limit of the list: rows of a `v_virtual_list` cannot carry their own click // 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 from the menu a right click opens - -// `on_item_secondary_click`). +// click inserts the text. The star of a row is not an element with a handler +// either: `on_mouse_down` on the list says where the press landed, and a press +// in the STAR_ZONE at the right edge makes the click toggle the favourite +// (main.js). Ctrl+D and the right-click menu do the same. import { div, image } from "gpui-kit"; import { v_flex, h_flex, Input, Scrollbar, v_virtual_list } from "gpui-base"; @@ -26,6 +27,12 @@ import { segments, entryMeta } from "./history.js"; */ export const ROW_HEIGHT = 76; +/** + * The right edge of a row that counts as its star: 10 px of padding, the + * 24 px star and a little room (a click lands next to a small target). + */ +export const STAR_ZONE = 48; + /** Two lines of the text of a row, at about 20 px a line (the rest is cut). */ const TEXT_HEIGHT = 40; @@ -49,6 +56,7 @@ const TEXT_HEIGHT = 40; * tab: (tab:string, cx:any) => void, * pick: (key:string, cx:any) => void, * rowMenu: (key:string, cx:any) => void, + * press: (event:any) => void, * clear: (cx:any) => void, * close: (cx:any) => void, * }} on @@ -165,6 +173,7 @@ function renderList(t, view, on) { return v_flex() .size_full() .relative() + .on_mouse_down("left", (event, cx) => on.press(event)) .child( v_virtual_list( "composer-panel-list", diff --git a/tests/dock.test.mjs b/tests/dock.test.mjs index 5af6853..686aae2 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, menuTransition, applyDock, + rename, toggleMode, dockAction, dockMenu, rowMenu, starMenu, menuTransition, applyDock, add, } from "../herdr/herdr-composer/mods/herdr-composer/dock.js"; const btn = (id, text, extra = {}) => ({ id, label: text, text, mode: "send", ...extra }); @@ -158,3 +158,15 @@ test("applyDock: works on the stored list, so another copy's change survives", ( assert.deepEqual(applyDock("rubbish", (d) => pin(d, "x", "9")).map((b) => b.id), ["9"]); assert.deepEqual(applyDock(stored, (d) => remove(d, "1")).map((b) => b.id), ["2"]); }); + +test("add: label, mode, empty text, duplicate, limit", () => { + const a = add([], { id: "1", text: " /handoff ", label: "", mode: "send" }); + assert.deepEqual(a, [{ id: "1", label: "/handoff", text: "/handoff", mode: "send" }]); + const b = add(a, { id: "2", text: "npm test", label: " Testy ", mode: "insert" }); + assert.deepEqual(b[1], { id: "2", label: "Testy", text: "npm test", mode: "insert" }); + assert.equal(add(b, { id: "3", text: " ", label: "x", mode: "send" }), b); + assert.equal(add(b, { id: "3", text: "npm test", label: "x", mode: "send" }), b); + const full = Array.from({ length: DOCK_LIMIT }, (_, i) => btn(`f${i}`, `t${i}`)); + assert.equal(add(full, { id: "n", text: "new", label: "", mode: "send" }), full); + assert.equal(add([], { id: "1", text: "x", label: "", mode: "bogus" })[0].mode, "send"); +});