Replace prompt dialog with inline rename in sidebar
Thread rename now uses an inline input directly in the sidebar row instead of opening a separate Electron BrowserWindow or browser window.prompt. Enter commits, Escape cancels, blur saves. Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
1 parent
fd4ff8ca46
commit
500a800336
8 files changed
+576
-34
No files matched your search
+117
-17
@@ -5,16 +5,18 @@ import net from "node:net";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { app, BrowserWindow, dialog, ipcMain, Menu, protocol, shell } from "electron";
|
||||
import { app, BrowserWindow, dialog, ipcMain, Menu, nativeImage, protocol, shell } from "electron";
|
||||
import type { MenuItemConstructorOptions } from "electron";
|
||||
|
||||
import { showDesktopConfirmDialog } from "./confirmDialog";
|
||||
import { fixPath } from "./fixPath";
|
||||
import { showDesktopPromptDialog } from "./promptDialog";
|
||||
|
||||
fixPath();
|
||||
|
||||
const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
||||
const CONFIRM_CHANNEL = "desktop:confirm";
|
||||
const PROMPT_CHANNEL = "desktop:prompt";
|
||||
const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
|
||||
const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
|
||||
const MENU_ACTION_CHANNEL = "desktop:menu-action";
|
||||
@@ -36,6 +38,44 @@ let restartTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let isQuitting = false;
|
||||
let desktopProtocolRegistered = false;
|
||||
|
||||
interface DesktopContextMenuItem {
|
||||
id: string;
|
||||
label: string;
|
||||
destructive?: boolean;
|
||||
}
|
||||
|
||||
let destructiveMenuIconCache: Electron.NativeImage | null | undefined;
|
||||
|
||||
function getDestructiveMenuIcon(): Electron.NativeImage | undefined {
|
||||
if (destructiveMenuIconCache !== undefined) {
|
||||
return destructiveMenuIconCache ?? undefined;
|
||||
}
|
||||
try {
|
||||
const icon =
|
||||
process.platform === "darwin"
|
||||
? nativeImage.createFromNamedImage("trash").resize({
|
||||
width: 14,
|
||||
height: 14,
|
||||
})
|
||||
: nativeImage.createFromPath(resolveIconPath("png") ?? "").resize({
|
||||
width: 14,
|
||||
height: 14,
|
||||
});
|
||||
if (icon.isEmpty()) {
|
||||
destructiveMenuIconCache = null;
|
||||
return undefined;
|
||||
}
|
||||
if (process.platform === "darwin") {
|
||||
icon.setTemplateImage(true);
|
||||
}
|
||||
destructiveMenuIconCache = icon;
|
||||
return icon;
|
||||
} catch {
|
||||
destructiveMenuIconCache = null;
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
protocol.registerSchemesAsPrivileged([
|
||||
{
|
||||
scheme: DESKTOP_SCHEME,
|
||||
@@ -396,25 +436,85 @@ function registerIpcHandlers(): void {
|
||||
return showDesktopConfirmDialog(message, owner);
|
||||
});
|
||||
|
||||
ipcMain.removeHandler(CONTEXT_MENU_CHANNEL);
|
||||
ipcMain.handle(CONTEXT_MENU_CHANNEL, async (_event, items: { id: string; label: string }[]) => {
|
||||
const window = BrowserWindow.getFocusedWindow() ?? mainWindow;
|
||||
if (!window) return null;
|
||||
ipcMain.removeHandler(PROMPT_CHANNEL);
|
||||
ipcMain.handle(PROMPT_CHANNEL, async (_event, message: unknown, defaultValue: unknown) => {
|
||||
if (typeof message !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Promise<string | null>((resolve) => {
|
||||
const menu = Menu.buildFromTemplate(
|
||||
items.map((item) => ({
|
||||
label: item.label,
|
||||
click: () => resolve(item.id),
|
||||
})),
|
||||
);
|
||||
menu.popup({
|
||||
window,
|
||||
callback: () => resolve(null),
|
||||
});
|
||||
});
|
||||
const owner = BrowserWindow.getFocusedWindow() ?? mainWindow;
|
||||
return showDesktopPromptDialog(
|
||||
message,
|
||||
typeof defaultValue === "string" ? defaultValue : "",
|
||||
owner,
|
||||
);
|
||||
});
|
||||
|
||||
ipcMain.removeHandler(CONTEXT_MENU_CHANNEL);
|
||||
ipcMain.handle(
|
||||
CONTEXT_MENU_CHANNEL,
|
||||
async (
|
||||
_event,
|
||||
items: DesktopContextMenuItem[],
|
||||
position?: { x: number; y: number },
|
||||
) => {
|
||||
const normalizedItems = items
|
||||
.filter((item) => typeof item.id === "string" && typeof item.label === "string")
|
||||
.map((item) => ({
|
||||
id: item.id,
|
||||
label: item.label,
|
||||
destructive: item.destructive === true,
|
||||
}));
|
||||
if (normalizedItems.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const popupPosition =
|
||||
position &&
|
||||
Number.isFinite(position.x) &&
|
||||
Number.isFinite(position.y) &&
|
||||
position.x >= 0 &&
|
||||
position.y >= 0
|
||||
? {
|
||||
x: Math.floor(position.x),
|
||||
y: Math.floor(position.y),
|
||||
}
|
||||
: null;
|
||||
|
||||
const window = BrowserWindow.getFocusedWindow() ?? mainWindow;
|
||||
if (!window) return null;
|
||||
|
||||
return new Promise<string | null>((resolve) => {
|
||||
const template: MenuItemConstructorOptions[] = [];
|
||||
let hasInsertedDestructiveSeparator = false;
|
||||
for (const item of normalizedItems) {
|
||||
if (item.destructive && !hasInsertedDestructiveSeparator && template.length > 0) {
|
||||
template.push({ type: "separator" });
|
||||
hasInsertedDestructiveSeparator = true;
|
||||
}
|
||||
const itemOption: MenuItemConstructorOptions = {
|
||||
label: item.label,
|
||||
click: () => resolve(item.id),
|
||||
};
|
||||
if (item.destructive) {
|
||||
const destructiveIcon = getDestructiveMenuIcon();
|
||||
if (destructiveIcon) {
|
||||
itemOption.icon = destructiveIcon;
|
||||
}
|
||||
}
|
||||
template.push(itemOption);
|
||||
}
|
||||
|
||||
const menu = Menu.buildFromTemplate(template);
|
||||
menu.popup({
|
||||
window,
|
||||
...(popupPosition ?? {}),
|
||||
callback: () => resolve(null),
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
ipcMain.removeHandler(OPEN_EXTERNAL_CHANNEL);
|
||||
ipcMain.handle(OPEN_EXTERNAL_CHANNEL, async (_event, rawUrl: unknown) => {
|
||||
if (typeof rawUrl !== "string" || rawUrl.length === 0) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { DesktopBridge } from "@t3tools/contracts";
|
||||
|
||||
const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
||||
const CONFIRM_CHANNEL = "desktop:confirm";
|
||||
const PROMPT_CHANNEL = "desktop:prompt";
|
||||
const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
|
||||
const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
|
||||
const MENU_ACTION_CHANNEL = "desktop:menu-action";
|
||||
@@ -12,7 +13,8 @@ contextBridge.exposeInMainWorld("desktopBridge", {
|
||||
getWsUrl: () => wsUrl,
|
||||
pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL),
|
||||
confirm: (message) => ipcRenderer.invoke(CONFIRM_CHANNEL, message),
|
||||
showContextMenu: (items) => ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items),
|
||||
prompt: (message, defaultValue) => ipcRenderer.invoke(PROMPT_CHANNEL, message, defaultValue),
|
||||
showContextMenu: (items, position) => ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items, position),
|
||||
openExternal: (url: string) => ipcRenderer.invoke(OPEN_EXTERNAL_CHANNEL, url),
|
||||
onMenuAction: (listener) => {
|
||||
const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { BrowserWindow, ipcMain } from "electron";
|
||||
|
||||
const PROMPT_WIDTH = 420;
|
||||
const PROMPT_HEIGHT = 188;
|
||||
|
||||
function createPromptHtml(input: {
|
||||
message: string;
|
||||
defaultValue: string;
|
||||
submitChannel: string;
|
||||
cancelChannel: string;
|
||||
}): string {
|
||||
const scriptConfig = JSON.stringify(input);
|
||||
return `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
|
||||
<title>${input.message}</title>
|
||||
<style>
|
||||
:root {
|
||||
color-scheme: light dark;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
||||
--bg: #fff;
|
||||
--fg: #1a1a1a;
|
||||
--muted: #6b6b6b;
|
||||
--border: #d4d4d4;
|
||||
--input-bg: #fafafa;
|
||||
--focus-ring: rgba(59, 130, 246, 0.5);
|
||||
--btn-bg: #f5f5f5;
|
||||
--btn-hover: #e8e8e8;
|
||||
--btn-primary-bg: #171717;
|
||||
--btn-primary-fg: #fff;
|
||||
--btn-primary-hover: #333;
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--bg: #1e1e1e;
|
||||
--fg: #e4e4e4;
|
||||
--muted: #999;
|
||||
--border: #3a3a3a;
|
||||
--input-bg: #2a2a2a;
|
||||
--focus-ring: rgba(96, 165, 250, 0.5);
|
||||
--btn-bg: #333;
|
||||
--btn-hover: #404040;
|
||||
--btn-primary-bg: #e4e4e4;
|
||||
--btn-primary-fg: #1a1a1a;
|
||||
--btn-primary-hover: #d0d0d0;
|
||||
}
|
||||
}
|
||||
* { box-sizing: border-box; margin: 0; }
|
||||
body {
|
||||
padding: 16px;
|
||||
background: var(--bg);
|
||||
color: var(--fg);
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
p {
|
||||
margin-bottom: 10px;
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
}
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 7px 10px;
|
||||
font-size: 13px;
|
||||
font-family: inherit;
|
||||
color: var(--fg);
|
||||
background: var(--input-bg);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
outline: none;
|
||||
transition: border-color 0.15s, box-shadow 0.15s;
|
||||
}
|
||||
input:focus {
|
||||
border-color: var(--focus-ring);
|
||||
box-shadow: 0 0 0 2px var(--focus-ring);
|
||||
}
|
||||
.buttons {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
margin-top: 14px;
|
||||
}
|
||||
button {
|
||||
min-width: 72px;
|
||||
padding: 6px 14px;
|
||||
font-size: 12px;
|
||||
font-family: inherit;
|
||||
font-weight: 500;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
background: var(--btn-bg);
|
||||
color: var(--fg);
|
||||
transition: background 0.15s;
|
||||
}
|
||||
button:hover { background: var(--btn-hover); }
|
||||
button:active { opacity: 0.85; }
|
||||
button[type="submit"] {
|
||||
background: var(--btn-primary-bg);
|
||||
color: var(--btn-primary-fg);
|
||||
border-color: var(--btn-primary-bg);
|
||||
}
|
||||
button[type="submit"]:hover { background: var(--btn-primary-hover); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p id="message"></p>
|
||||
<form id="form">
|
||||
<input id="value" autocomplete="off" />
|
||||
<div class="buttons">
|
||||
<button id="cancel" type="button">Cancel</button>
|
||||
<button type="submit">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
<script>
|
||||
const { ipcRenderer } = require("electron");
|
||||
const config = ${scriptConfig};
|
||||
const message = document.getElementById("message");
|
||||
const form = document.getElementById("form");
|
||||
const valueInput = document.getElementById("value");
|
||||
const cancelButton = document.getElementById("cancel");
|
||||
|
||||
message.textContent = config.message;
|
||||
valueInput.value = config.defaultValue;
|
||||
|
||||
function cancel() {
|
||||
ipcRenderer.send(config.cancelChannel);
|
||||
window.close();
|
||||
}
|
||||
|
||||
form.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
ipcRenderer.send(config.submitChannel, valueInput.value);
|
||||
window.close();
|
||||
});
|
||||
cancelButton.addEventListener("click", cancel);
|
||||
window.addEventListener("keydown", (event) => {
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
cancel();
|
||||
}
|
||||
});
|
||||
window.addEventListener("DOMContentLoaded", () => {
|
||||
valueInput.focus();
|
||||
valueInput.select();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
export async function showDesktopPromptDialog(
|
||||
message: string,
|
||||
defaultValue: string,
|
||||
ownerWindow: BrowserWindow | null,
|
||||
): Promise<string | null> {
|
||||
const normalizedMessage = message.trim();
|
||||
if (normalizedMessage.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const token = randomBytes(12).toString("hex");
|
||||
const submitChannel = `desktop:prompt-dialog:submit:${token}`;
|
||||
const cancelChannel = `desktop:prompt-dialog:cancel:${token}`;
|
||||
|
||||
const promptWindow = new BrowserWindow({
|
||||
width: PROMPT_WIDTH,
|
||||
height: PROMPT_HEIGHT,
|
||||
useContentSize: true,
|
||||
show: false,
|
||||
resizable: false,
|
||||
minimizable: false,
|
||||
maximizable: false,
|
||||
fullscreenable: false,
|
||||
autoHideMenuBar: true,
|
||||
title: normalizedMessage,
|
||||
...(ownerWindow ? { parent: ownerWindow } : {}),
|
||||
modal: ownerWindow !== null,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
},
|
||||
});
|
||||
promptWindow.removeMenu();
|
||||
|
||||
return new Promise<string | null>((resolve) => {
|
||||
let settled = false;
|
||||
|
||||
const settle = (value: string | null) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
cleanup();
|
||||
resolve(value);
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
ipcMain.removeAllListeners(submitChannel);
|
||||
ipcMain.removeAllListeners(cancelChannel);
|
||||
if (!promptWindow.isDestroyed()) {
|
||||
promptWindow.destroy();
|
||||
}
|
||||
};
|
||||
|
||||
ipcMain.once(submitChannel, (_event, value: unknown) => {
|
||||
if (typeof value !== "string") {
|
||||
settle(null);
|
||||
return;
|
||||
}
|
||||
settle(value);
|
||||
});
|
||||
ipcMain.once(cancelChannel, () => settle(null));
|
||||
promptWindow.once("closed", () => settle(null));
|
||||
|
||||
const html = createPromptHtml({
|
||||
message: normalizedMessage,
|
||||
defaultValue,
|
||||
submitChannel,
|
||||
cancelChannel,
|
||||
});
|
||||
void promptWindow
|
||||
.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(html)}`)
|
||||
.then(() => {
|
||||
if (!promptWindow.isDestroyed()) {
|
||||
promptWindow.show();
|
||||
}
|
||||
})
|
||||
.catch(() => settle(null));
|
||||
});
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ChevronRightIcon, FolderIcon, GitPullRequestIcon, TerminalIcon } from "lucide-react";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import {
|
||||
DEFAULT_MODEL,
|
||||
ProjectId,
|
||||
@@ -266,6 +266,10 @@ export default function Sidebar() {
|
||||
const [newCwd, setNewCwd] = useState("");
|
||||
const [isPickingFolder, setIsPickingFolder] = useState(false);
|
||||
const [isAddingProject, setIsAddingProject] = useState(false);
|
||||
const [renamingThreadId, setRenamingThreadId] = useState<ThreadId | null>(null);
|
||||
const [renamingTitle, setRenamingTitle] = useState("");
|
||||
const renamingCommittedRef = useRef(false);
|
||||
const renamingInputRef = useRef<HTMLInputElement | null>(null);
|
||||
const pendingApprovalByThreadId = useMemo(() => {
|
||||
const map = new Map<ThreadId, boolean>();
|
||||
for (const thread of threads) {
|
||||
@@ -503,25 +507,74 @@ export default function Sidebar() {
|
||||
setIsPickingFolder(false);
|
||||
};
|
||||
|
||||
const cancelRename = useCallback(() => {
|
||||
setRenamingThreadId(null);
|
||||
renamingInputRef.current = null;
|
||||
}, []);
|
||||
|
||||
const commitRename = useCallback(
|
||||
async (threadId: ThreadId, newTitle: string, originalTitle: string) => {
|
||||
const trimmed = newTitle.trim();
|
||||
if (trimmed.length === 0) {
|
||||
toastManager.add({ type: "warning", title: "Thread title cannot be empty" });
|
||||
cancelRename();
|
||||
return;
|
||||
}
|
||||
if (trimmed === originalTitle) {
|
||||
cancelRename();
|
||||
return;
|
||||
}
|
||||
const api = readNativeApi();
|
||||
if (!api) {
|
||||
cancelRename();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await api.orchestration.dispatchCommand({
|
||||
type: "thread.meta.update",
|
||||
commandId: newCommandId(),
|
||||
threadId,
|
||||
title: trimmed,
|
||||
});
|
||||
} catch (error) {
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Failed to rename thread",
|
||||
description: error instanceof Error ? error.message : "An error occurred.",
|
||||
});
|
||||
}
|
||||
cancelRename();
|
||||
},
|
||||
[cancelRename],
|
||||
);
|
||||
|
||||
const handleThreadContextMenu = useCallback(
|
||||
async (threadId: ThreadId, position: { x: number; y: number }) => {
|
||||
const api = readNativeApi();
|
||||
if (!api) return;
|
||||
const clicked = await api.contextMenu.show(
|
||||
[
|
||||
{ id: "rename", label: "Rename thread" },
|
||||
{ id: "mark-unread", label: "Mark unread" },
|
||||
{ id: "delete", label: "Delete" },
|
||||
{ id: "delete", label: "Delete", destructive: true },
|
||||
],
|
||||
position,
|
||||
);
|
||||
const thread = threads.find((t) => t.id === threadId);
|
||||
if (!thread) return;
|
||||
|
||||
if (clicked === "rename") {
|
||||
setRenamingThreadId(threadId);
|
||||
setRenamingTitle(thread.title);
|
||||
renamingCommittedRef.current = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (clicked === "mark-unread") {
|
||||
markThreadUnread(threadId);
|
||||
return;
|
||||
}
|
||||
if (clicked !== "delete") return;
|
||||
|
||||
const thread = threads.find((t) => t.id === threadId);
|
||||
if (!thread) return;
|
||||
if (appSettings.confirmThreadDelete) {
|
||||
const confirmed = await api.dialogs.confirm(
|
||||
[
|
||||
@@ -635,7 +688,10 @@ export default function Sidebar() {
|
||||
async (projectId: ProjectId, position: { x: number; y: number }) => {
|
||||
const api = readNativeApi();
|
||||
if (!api) return;
|
||||
const clicked = await api.contextMenu.show([{ id: "delete", label: "Delete" }], position);
|
||||
const clicked = await api.contextMenu.show(
|
||||
[{ id: "delete", label: "Delete", destructive: true }],
|
||||
position,
|
||||
);
|
||||
if (clicked !== "delete") return;
|
||||
|
||||
const project = projects.find((entry) => entry.id === projectId);
|
||||
@@ -901,9 +957,41 @@ export default function Sidebar() {
|
||||
<span className="hidden md:inline">{threadStatus.label}</span>
|
||||
</span>
|
||||
)}
|
||||
<span className="min-w-0 flex-1 truncate text-xs">
|
||||
{thread.title}
|
||||
</span>
|
||||
{renamingThreadId === thread.id ? (
|
||||
<input
|
||||
ref={(el) => {
|
||||
if (el && renamingInputRef.current !== el) {
|
||||
renamingInputRef.current = el;
|
||||
el.focus();
|
||||
el.select();
|
||||
}
|
||||
}}
|
||||
className="min-w-0 flex-1 truncate text-xs bg-transparent outline-none border border-ring rounded px-0.5"
|
||||
value={renamingTitle}
|
||||
onChange={(e) => setRenamingTitle(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
renamingCommittedRef.current = true;
|
||||
void commitRename(thread.id, renamingTitle, thread.title);
|
||||
} else if (e.key === "Escape") {
|
||||
e.preventDefault();
|
||||
renamingCommittedRef.current = true;
|
||||
cancelRename();
|
||||
}
|
||||
}}
|
||||
onBlur={() => {
|
||||
if (!renamingCommittedRef.current) {
|
||||
void commitRename(thread.id, renamingTitle, thread.title);
|
||||
}
|
||||
}}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
) : (
|
||||
<span className="min-w-0 flex-1 truncate text-xs">
|
||||
{thread.title}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="ml-auto flex shrink-0 items-center gap-1.5">
|
||||
{terminalStatus && (
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* with the clicked item id, or null if dismissed.
|
||||
*/
|
||||
export function showContextMenuFallback<T extends string>(
|
||||
items: readonly { id: T; label: string }[],
|
||||
items: readonly { id: T; label: string; destructive?: boolean }[],
|
||||
position?: { x: number; y: number },
|
||||
): Promise<T | null> {
|
||||
return new Promise<T | null>((resolve) => {
|
||||
@@ -41,8 +41,8 @@ export function showContextMenuFallback<T extends string>(
|
||||
const btn = document.createElement("button");
|
||||
btn.type = "button";
|
||||
btn.textContent = item.label;
|
||||
const isDeleteAction = item.id === "delete";
|
||||
btn.className = isDeleteAction
|
||||
const isDestructiveAction = item.destructive === true || item.id === "delete";
|
||||
btn.className = isDestructiveAction
|
||||
? "flex w-full items-center gap-2 px-3 py-1.5 text-left text-[11px] text-destructive hover:bg-accent cursor-default"
|
||||
: "flex w-full items-center gap-2 px-3 py-1.5 text-left text-[11px] text-popover-foreground hover:bg-accent cursor-default";
|
||||
btn.addEventListener("click", () => cleanup(item.id));
|
||||
|
||||
@@ -9,6 +9,12 @@ import {
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const requestMock = vi.fn<(...args: Array<unknown>) => Promise<unknown>>();
|
||||
const showContextMenuFallbackMock = vi.fn<
|
||||
<T extends string>(
|
||||
items: readonly { id: T; label: string; destructive?: boolean }[],
|
||||
position?: { x: number; y: number },
|
||||
) => Promise<T | null>
|
||||
>();
|
||||
const channelListeners = new Map<string, Set<(data: unknown) => void>>();
|
||||
const subscribeMock = vi.fn<(channel: string, listener: (data: unknown) => void) => () => void>(
|
||||
(channel, listener) => {
|
||||
@@ -33,6 +39,10 @@ vi.mock("./wsTransport", () => {
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("./contextMenuFallback", () => ({
|
||||
showContextMenuFallback: showContextMenuFallbackMock,
|
||||
}));
|
||||
|
||||
function emitPush(channel: string, data: unknown): void {
|
||||
const listeners = channelListeners.get(channel);
|
||||
if (!listeners) return;
|
||||
@@ -41,11 +51,24 @@ function emitPush(channel: string, data: unknown): void {
|
||||
}
|
||||
}
|
||||
|
||||
function getWindowForTest(): Window & typeof globalThis & { desktopBridge?: unknown } {
|
||||
const testGlobal = globalThis as typeof globalThis & {
|
||||
window?: Window & typeof globalThis & { desktopBridge?: unknown };
|
||||
};
|
||||
if (!testGlobal.window) {
|
||||
testGlobal.window = {} as Window & typeof globalThis & { desktopBridge?: unknown };
|
||||
}
|
||||
return testGlobal.window;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
requestMock.mockReset();
|
||||
showContextMenuFallbackMock.mockReset();
|
||||
subscribeMock.mockClear();
|
||||
channelListeners.clear();
|
||||
Reflect.deleteProperty(getWindowForTest(), "desktopBridge");
|
||||
Reflect.deleteProperty(getWindowForTest(), "prompt");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -307,4 +330,84 @@ describe("wsNativeApi", () => {
|
||||
toTurnCount: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("forwards context menu metadata to desktop bridge", async () => {
|
||||
const showContextMenu = vi.fn().mockResolvedValue("delete");
|
||||
Object.defineProperty(getWindowForTest(), "desktopBridge", {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: {
|
||||
showContextMenu,
|
||||
},
|
||||
});
|
||||
|
||||
const { createWsNativeApi } = await import("./wsNativeApi");
|
||||
const api = createWsNativeApi();
|
||||
await api.contextMenu.show(
|
||||
[
|
||||
{ id: "rename", label: "Rename thread" },
|
||||
{ id: "delete", label: "Delete", destructive: true },
|
||||
],
|
||||
{ x: 200, y: 300 },
|
||||
);
|
||||
|
||||
expect(showContextMenu).toHaveBeenCalledWith(
|
||||
[
|
||||
{ id: "rename", label: "Rename thread" },
|
||||
{ id: "delete", label: "Delete", destructive: true },
|
||||
],
|
||||
{ x: 200, y: 300 },
|
||||
);
|
||||
});
|
||||
|
||||
it("uses fallback context menu when desktop bridge is unavailable", async () => {
|
||||
showContextMenuFallbackMock.mockResolvedValue("delete");
|
||||
Reflect.deleteProperty(getWindowForTest(), "desktopBridge");
|
||||
|
||||
const { createWsNativeApi } = await import("./wsNativeApi");
|
||||
const api = createWsNativeApi();
|
||||
await api.contextMenu.show(
|
||||
[{ id: "delete", label: "Delete", destructive: true }],
|
||||
{ x: 20, y: 30 },
|
||||
);
|
||||
|
||||
expect(showContextMenuFallbackMock).toHaveBeenCalledWith(
|
||||
[{ id: "delete", label: "Delete", destructive: true }],
|
||||
{ x: 20, y: 30 },
|
||||
);
|
||||
});
|
||||
|
||||
it("forwards prompt calls to desktop bridge", async () => {
|
||||
const prompt = vi.fn().mockResolvedValue("renamed");
|
||||
Object.defineProperty(getWindowForTest(), "desktopBridge", {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: {
|
||||
prompt,
|
||||
},
|
||||
});
|
||||
|
||||
const { createWsNativeApi } = await import("./wsNativeApi");
|
||||
const api = createWsNativeApi();
|
||||
const value = await api.dialogs.prompt("Rename thread", "Old title");
|
||||
|
||||
expect(value).toBe("renamed");
|
||||
expect(prompt).toHaveBeenCalledWith("Rename thread", "Old title");
|
||||
});
|
||||
|
||||
it("uses browser prompt fallback when desktop bridge is unavailable", async () => {
|
||||
const prompt = vi.fn().mockReturnValue("renamed");
|
||||
Object.defineProperty(getWindowForTest(), "prompt", {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: prompt,
|
||||
});
|
||||
|
||||
const { createWsNativeApi } = await import("./wsNativeApi");
|
||||
const api = createWsNativeApi();
|
||||
const value = await api.dialogs.prompt("Rename thread", "Old title");
|
||||
|
||||
expect(value).toBe("renamed");
|
||||
expect(prompt).toHaveBeenCalledWith("Rename thread", "Old title");
|
||||
});
|
||||
});
|
||||
@@ -124,6 +124,16 @@ export function createWsNativeApi(): NativeApi {
|
||||
}
|
||||
return window.confirm(message);
|
||||
},
|
||||
prompt: async (message, defaultValue) => {
|
||||
if (window.desktopBridge) {
|
||||
return window.desktopBridge.prompt(message, defaultValue);
|
||||
}
|
||||
try {
|
||||
return window.prompt(message, defaultValue ?? "");
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
},
|
||||
terminal: {
|
||||
open: (input) => transport.request(WS_METHODS.terminalOpen, input),
|
||||
@@ -171,11 +181,11 @@ export function createWsNativeApi(): NativeApi {
|
||||
},
|
||||
contextMenu: {
|
||||
show: async <T extends string>(
|
||||
items: readonly { id: T; label: string }[],
|
||||
items: readonly { id: T; label: string; destructive?: boolean }[],
|
||||
position?: { x: number; y: number },
|
||||
): Promise<T | null> => {
|
||||
if (window.desktopBridge) {
|
||||
return window.desktopBridge.showContextMenu(items) as Promise<T | null>;
|
||||
return window.desktopBridge.showContextMenu(items, position) as Promise<T | null>;
|
||||
}
|
||||
return showContextMenuFallback(items, position);
|
||||
},
|
||||
|
||||
@@ -37,12 +37,19 @@ import type {
|
||||
} from "./orchestration";
|
||||
import { EditorId } from "./editor";
|
||||
|
||||
export interface ContextMenuItem<T extends string = string> {
|
||||
id: T;
|
||||
label: string;
|
||||
destructive?: boolean;
|
||||
}
|
||||
|
||||
export interface DesktopBridge {
|
||||
getWsUrl: () => string | null;
|
||||
pickFolder: () => Promise<string | null>;
|
||||
confirm: (message: string) => Promise<boolean>;
|
||||
prompt: (message: string, defaultValue?: string) => Promise<string | null>;
|
||||
showContextMenu: <T extends string>(
|
||||
items: readonly { id: T; label: string }[],
|
||||
items: readonly ContextMenuItem<T>[],
|
||||
position?: { x: number; y: number },
|
||||
) => Promise<T | null>;
|
||||
openExternal: (url: string) => Promise<boolean>;
|
||||
@@ -53,6 +60,7 @@ export interface NativeApi {
|
||||
dialogs: {
|
||||
pickFolder: () => Promise<string | null>;
|
||||
confirm: (message: string) => Promise<boolean>;
|
||||
prompt: (message: string, defaultValue?: string) => Promise<string | null>;
|
||||
};
|
||||
terminal: {
|
||||
open: (input: TerminalOpenInput) => Promise<TerminalSessionSnapshot>;
|
||||
@@ -85,7 +93,7 @@ export interface NativeApi {
|
||||
};
|
||||
contextMenu: {
|
||||
show: <T extends string>(
|
||||
items: readonly { id: T; label: string }[],
|
||||
items: readonly ContextMenuItem<T>[],
|
||||
position?: { x: number; y: number },
|
||||
) => Promise<T | null>;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user