UI: replace WebView browser right-click menu with Kiri's app editing menu (#964)

This commit is contained in:
Precious Okwu authored and GitHub committed 2026-11-25 20:47:39 +01:00
1 parent f1eb18c545
commit 2ccef3e96a
2 files changed
+209 -11

No files matched your search

+141 -4
View File
@@ -2371,6 +2371,15 @@ main { position: relative; }
.htctx .mi { display: flex; align-items: center; gap: 8px; width: 100%; height: 28px; padding: 0 8px; border: 0; border-radius: 6px; background: none; cursor: pointer; color: var(--text); font: var(--fs) var(--ui-font); text-align: var(--start); }
.htctx .mi:hover { background: var(--blue); color: #fff; }
.htctx .mi:disabled { color: var(--faint); background: none; cursor: default; }
.appctx { position: fixed; z-index: 120; min-width: 160px; padding: 5px; background: var(--raised); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 14px 40px rgb(0 0 0 / .45); }
.appctx[hidden] { display: none; }
.appctx .mi { display: flex; align-items: center; justify-content: space-between; gap: 16px; width: 100%; height: 28px; padding-block: 0; padding-inline: 8px; border: 0; border-radius: 6px; background: none; cursor: pointer; color: var(--text); font: var(--fs) var(--ui-font); text-align: var(--start); }
.appctx .mi:hover { background: var(--blue); color: #fff; }
.appctx .mi:hover .k { color: rgb(255 255 255 / .8); }
.appctx .mi:disabled { color: var(--faint); background: none; cursor: default; }
.appctx .mi:disabled .k { color: var(--faint); }
.appctx .mi .k { color: var(--faint); font-size: var(--fs-xs); }
.appctx hr { border: 0; border-top: 1px solid var(--line); margin: 4px 2px; }
.httab .x:hover { background: color-mix(in srgb, var(--text) 10%, transparent); color: var(--text); }
.hthead .grow { flex: 1; min-width: 10px; align-self: stretch; }
.hthead .sep { width: 1px; height: 16px; background: var(--line); margin: 0 4px; }
@@ -3017,12 +3026,129 @@ function applyPrefs() {
r.setProperty("--blue-light", `color-mix(in srgb, ${accent} 60%, white)`);
window.htApply?.();
}
/* A right-click gets no browser menu (Reload, Back, Inspect): this is an app,
not a web page. Text fields and selected text keep theirs, for Copy and Paste. */
/* Right-click never opens the WebView's browser menu (Safari's Search with
Google, Look Up, Translate, Copy Link with Highlight, Share…): Kiri draws
its own app menu with only editing actions (Copy on selected text, Cut /
Copy / Paste / Select All in text fields, Copy / Paste in the terminal). */
const appCtxEl = document.createElement("div");
appCtxEl.id = "app-ctx";
appCtxEl.className = "appctx";
appCtxEl.setAttribute("role", "menu");
appCtxEl.hidden = true;
document.body.append(appCtxEl);
let ctxTarget = null, ctxPasteField = null;
function hideAppCtx() { appCtxEl.hidden = true; ctxTarget = null; }
function isTextField(el) {
if (!el) return false;
if (el.tagName === "TEXTAREA") return true;
if (el.tagName === "INPUT") {
const t = (el.type || "text").toLowerCase();
return ["text", "search", "url", "tel", "password", "email", "number"].includes(t);
}
return false;
}
function fieldRange(el) {
try {
if (typeof el.selectionStart === "number" && typeof el.selectionEnd === "number") return [el.selectionStart, el.selectionEnd, true];
} catch {}
const len = (el.value || "").length;
return [0, len, false];
}
function replaceFieldText(el, text, start, end, hasRange) {
el.focus();
if (hasRange) {
try { el.setRangeText(text, start, end, "end"); }
catch { el.value = (el.value || "").slice(0, start) + text + (el.value || "").slice(end); }
} else {
el.value = text;
}
el.dispatchEvent(new Event("input", { bubbles: true }));
}
function ctxItem(act, label, key, disabled) {
return `<button type="button" class="mi" role="menuitem" data-ctx="${act}"${disabled ? " disabled" : ""}><span>${label}</span>${key ? `<span class="k">${esc(key)}</span>` : ""}</button>`;
}
function openAppCtx(ev, html) {
setHTML(appCtxEl, html);
appCtxEl.hidden = false;
const zoom = Number(prefs.scale) || 1, vw = (innerWidth || 800) / zoom, vh = (innerHeight || 600) / zoom;
const rect = appCtxEl.getBoundingClientRect(), mw = (rect.width || 168) / zoom, mh = (rect.height || 38) / zoom;
const x = Math.max(6, Math.min((ev.clientX || 0) / zoom, vw - mw - 6));
const y = Math.max(6, Math.min((ev.clientY || 0) / zoom, vh - mh - 6));
appCtxEl.style.left = `${x}px`;
appCtxEl.style.top = `${y}px`;
}
document.addEventListener("contextmenu", (ev) => {
const editable = ev.target.closest?.("input, textarea, [contenteditable]");
if (!editable && !String(getSelection()).trim()) ev.preventDefault();
ev.preventDefault();
const htMenuEl = document.querySelector(".htctx");
if (htMenuEl) htMenuEl.hidden = true;
const modKey = WIN || LINUX ? "Ctrl+" : "⌘";
const field = ev.target.closest?.("input, textarea");
if (isTextField(field) && !field.disabled) {
if (document.activeElement !== field) field.focus();
const [start, end, hasRange] = fieldRange(field);
const isPwd = (field.type || "").toLowerCase() === "password";
const writable = !field.readOnly;
const selected = !isPwd && hasRange && end > start ? (field.value || "").slice(start, end) : (!isPwd && !hasRange ? (field.value || "") : "");
const hasText = (field.value || "").length > 0;
ctxTarget = { kind: "field", el: field, start, end, hasRange, text: selected, writable };
const items = writable
? [
ctxItem("cut", tr("Cut"), `${modKey}X`, !selected),
ctxItem("copy", tr("Copy"), `${modKey}C`, !selected),
ctxItem("paste", tr("Paste"), `${modKey}V`, false),
"<hr>",
ctxItem("selectall", tr("Select All"), `${modKey}A`, !hasText),
]
: [
ctxItem("copy", tr("Copy"), `${modKey}C`, !selected),
ctxItem("selectall", tr("Select All"), `${modKey}A`, !hasText),
];
openAppCtx(ev, items.join(""));
return;
}
const paneEl = ev.target.closest?.(".htpane");
if (paneEl && ev.target.closest?.(".htx, .xterm") && typeof htPane === "function") {
const p = htPane(paneEl.dataset.id);
if (!p) { hideAppCtx(); return; }
const selected = p.term?.getSelection() || "";
ctxTarget = { kind: "term", pane: p, text: selected };
const tk = WIN || LINUX ? "Ctrl+Shift+" : "⌘";
openAppCtx(ev, [
ctxItem("copy", tr("Copy"), `${tk}C`, !selected),
ctxItem("paste", tr("Paste"), `${tk}V`, Boolean(p.exited)),
].join(""));
return;
}
const selected = String(getSelection());
if (!selected.trim()) { hideAppCtx(); return; }
ctxTarget = { kind: "selection", text: selected };
openAppCtx(ev, ctxItem("copy", tr("Copy"), `${modKey}C`, false));
});
appCtxEl.addEventListener("mousedown", (ev) => ev.preventDefault());
appCtxEl.addEventListener("click", (ev) => {
const btn = ev.target.closest?.("[data-ctx]");
if (!btn || btn.disabled) return;
const act = btn.dataset.ctx, t = ctxTarget;
hideAppCtx();
if (!t) return;
if (act === "copy") {
if (t.kind === "term") htCopy(t.pane);
else if (t.text) send("term", { name: "copy", value: t.text });
} else if (act === "cut" && t.kind === "field" && t.writable && t.text) {
send("term", { name: "copy", value: t.text });
replaceFieldText(t.el, "", t.start, t.end, t.hasRange);
} else if (act === "paste") {
if (t.kind === "term") send("term", { name: "paste", id: t.pane.id });
else if (t.kind === "field" && t.writable) { ctxPasteField = t.el; send("term", { name: "paste", id: "__field" }); }
} else if (act === "selectall" && t.kind === "field") {
t.el.focus();
try { t.el.select(); } catch {}
}
});
document.addEventListener("mousedown", (ev) => { if (!ev.target.closest?.("#app-ctx")) hideAppCtx(); });
document.addEventListener("keydown", (ev) => { if (ev.key === "Escape" && !appCtxEl.hidden) hideAppCtx(); });
document.addEventListener("scroll", hideAppCtx, true);
addEventListener("blur", hideAppCtx);
/* Copy with the keyboard: selected text (a log, a row's value) goes to the
clipboard the way the terminal's does. On the Mac, Edit → Copy does it.
Text fields and the terminal keep their own handling. */
@@ -12325,6 +12451,17 @@ function htOn(e) {
if (ht.wantTab) { const want = ht.wantTab; ht.wantTab = false; htNewTab(want === true ? undefined : want); }
return;
}
if (e.ev === "paste" && e.id === "__field") {
const el = ctxPasteField;
ctxPasteField = null;
if (el && el.isConnected && !el.readOnly && !el.disabled) {
const raw = String(e.text || "");
const text = el.tagName === "INPUT" ? raw.replace(/[\r\n]+/g, " ") : raw;
const [s, end, hasRange] = fieldRange(el);
replaceFieldText(el, text, s, end, hasRange);
}
return;
}
const p = htPane(e.id);
if (!p?.term) return;
if (e.ev === "data") p.term.write(htUnB64(e.b64));
+68 -7
View File
@@ -1,13 +1,74 @@
// Right-click: no browser menu (Reload…) except in text fields and on selected text.
// Right-click: never opens the browser's menu (Search with Google, Look Up,
// Translate, Reload…); Kiri draws its own app menu (#app-ctx) with only editing
// actions (Copy on selected text, Cut / Copy / Paste / Select All in fields).
kiri.on({ type: "status", version: "0.1.4", dev: true, app: "Kiri Dev", ours: "kiri-dev", context: "kiri-dev" });
const R = [];
const t = (name, fn) => { try { fn(); R.push("PASS " + name); } catch (e) { R.push("FAIL " + name + ": " + e.message); } };
const eq = (a, b, m) => { if (JSON.stringify(a) !== JSON.stringify(b)) throw new Error(m + ": got " + JSON.stringify(a) + " want " + JSON.stringify(b)); };
const menu = (el) => { const ev = new MouseEvent("contextmenu", { bubbles: true, cancelable: true }); el.dispatchEvent(ev); return !ev.defaultPrevented; };
t("no browser menu on the Images view", () => { getSelection().removeAllRanges(); show("images"); eq(menu($("content")), false, "menu shown"); });
t("a text field keeps its menu", () => { const i = document.createElement("input"); document.body.append(i); eq(menu(i), true, "menu shown"); i.remove(); });
t("selected text keeps its menu", () => {
const p = document.createElement("p"); p.textContent = "copy me"; p.style.userSelect = p.style.webkitUserSelect = "text"; document.body.append(p);
getSelection().selectAllChildren(p); eq(menu(p), true, "menu shown"); getSelection().removeAllRanges(); p.remove();
const menu = (el) => { const ev = new MouseEvent("contextmenu", { bubbles: true, cancelable: true, clientX: 120, clientY: 120 }); el.dispatchEvent(ev); return !ev.defaultPrevented; };
const ctxLabels = () => [...$("app-ctx").querySelectorAll("[data-ctx]")].map((b) => [b.dataset.ctx, b.disabled]);
t("no browser or app menu on unselected UI", () => {
getSelection().removeAllRanges();
show("images");
eq(menu($("content")), false, "browser menu prevented");
eq($("app-ctx").hidden, true, "app menu stays hidden");
});
t("selected log text opens Kiri's Copy-only menu (no browser Search with Google)", () => {
sent.length = 0;
const p = document.createElement("p");
p.textContent = "2026-11-20 10:14:34.716 UTC starting: time";
p.style.userSelect = p.style.webkitUserSelect = "text";
document.body.append(p);
getSelection().selectAllChildren(p);
eq(menu(p), false, "browser menu prevented");
eq($("app-ctx").hidden, false, "app menu shown");
eq(ctxLabels(), [["copy", false]], "only Copy shown");
eq($("app-ctx").textContent.includes("Google"), false, "never mentions Google");
$("app-ctx").querySelector('[data-ctx="copy"]').click();
eq($("app-ctx").hidden, true, "menu closes on click");
eq(sent.at(-1), { type: "term", cmd: "term", name: "copy", value: "2026-11-20 10:14:34.716 UTC starting: time" }, "copied through native bridge");
getSelection().removeAllRanges();
p.remove();
});
t("a text field gets Kiri's Cut, Copy, Paste and Select All menu", () => {
sent.length = 0;
const i = document.createElement("input");
i.value = "hello world";
document.body.append(i);
i.focus();
i.setSelectionRange(0, 5);
eq(menu(i), false, "browser menu prevented");
eq($("app-ctx").hidden, false, "app menu shown");
eq(ctxLabels(), [["cut", false], ["copy", false], ["paste", false], ["selectall", false]], "field actions");
$("app-ctx").querySelector('[data-ctx="cut"]').click();
eq(i.value, " world", "Cut removes selection");
eq(sent.at(-1), { type: "term", cmd: "term", name: "copy", value: "hello" }, "Cut copies selection");
menu(i);
$("app-ctx").querySelector('[data-ctx="paste"]').click();
eq(sent.at(-1), { type: "term", cmd: "term", name: "paste", id: "__field" }, "Paste asks native clipboard");
kiri.on({ type: "term", ev: "paste", id: "__field", text: "kiri\nlogs" });
eq(i.value, "kiri logs world", "Paste inserts sanitized single-line text");
i.remove();
});
t("number and email inputs support Cut, Copy and Paste without setRangeText InvalidStateError", () => {
sent.length = 0;
const n = document.createElement("input");
n.type = "number";
n.value = "8080";
document.body.append(n);
eq(menu(n), false, "browser menu prevented on number input");
$("app-ctx").querySelector('[data-ctx="cut"]').click();
eq(n.value, "", "Cut clears number input safely");
eq(sent.at(-1), { type: "term", cmd: "term", name: "copy", value: "8080" }, "Cut copied number value");
menu(n);
$("app-ctx").querySelector('[data-ctx="paste"]').click();
kiri.on({ type: "term", ev: "paste", id: "__field", text: "9090" });
eq(n.value, "9090", "Paste updates number input safely");
n.remove();
});
report(R);