Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2abe07b75 | ||
|
|
7edd7e8faf | ||
|
|
c0ad410467 | ||
|
|
57ad1aa7c2 | ||
|
|
5209192d22 | ||
|
|
61a9a235c9 | ||
|
|
81532cbb04 | ||
|
|
1094f679d9 | ||
|
|
500a800336 |
No files matched your search
+88
-16
@@ -5,9 +5,10 @@ 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 type { ContextMenuItem } from "@t3tools/contracts";
|
||||
import { showDesktopConfirmDialog } from "./confirmDialog";
|
||||
import { fixPath } from "./fixPath";
|
||||
|
||||
@@ -36,6 +37,31 @@ let restartTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let isQuitting = false;
|
||||
let desktopProtocolRegistered = false;
|
||||
|
||||
let destructiveMenuIconCache: Electron.NativeImage | null | undefined;
|
||||
|
||||
function getDestructiveMenuIcon(): Electron.NativeImage | undefined {
|
||||
if (process.platform !== "darwin") return undefined;
|
||||
if (destructiveMenuIconCache !== undefined) {
|
||||
return destructiveMenuIconCache ?? undefined;
|
||||
}
|
||||
try {
|
||||
const icon = nativeImage.createFromNamedImage("trash").resize({
|
||||
width: 14,
|
||||
height: 14,
|
||||
});
|
||||
if (icon.isEmpty()) {
|
||||
destructiveMenuIconCache = null;
|
||||
return undefined;
|
||||
}
|
||||
icon.setTemplateImage(true);
|
||||
destructiveMenuIconCache = icon;
|
||||
return icon;
|
||||
} catch {
|
||||
destructiveMenuIconCache = null;
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
protocol.registerSchemesAsPrivileged([
|
||||
{
|
||||
scheme: DESKTOP_SCHEME,
|
||||
@@ -397,23 +423,69 @@ function registerIpcHandlers(): void {
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
return new Promise<string | null>((resolve) => {
|
||||
const menu = Menu.buildFromTemplate(
|
||||
items.map((item) => ({
|
||||
ipcMain.handle(
|
||||
CONTEXT_MENU_CHANNEL,
|
||||
async (
|
||||
_event,
|
||||
items: ContextMenuItem[],
|
||||
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,
|
||||
click: () => resolve(item.id),
|
||||
})),
|
||||
);
|
||||
menu.popup({
|
||||
window,
|
||||
callback: () => resolve(null),
|
||||
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) => {
|
||||
|
||||
@@ -12,7 +12,7 @@ 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),
|
||||
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) => {
|
||||
|
||||
@@ -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,82 @@ 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 finishRename = () => {
|
||||
setRenamingThreadId((current) => {
|
||||
if (current !== threadId) return current;
|
||||
renamingInputRef.current = null;
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
const trimmed = newTitle.trim();
|
||||
if (trimmed.length === 0) {
|
||||
toastManager.add({ type: "warning", title: "Thread title cannot be empty" });
|
||||
finishRename();
|
||||
return;
|
||||
}
|
||||
if (trimmed === originalTitle) {
|
||||
finishRename();
|
||||
return;
|
||||
}
|
||||
const api = readNativeApi();
|
||||
if (!api) {
|
||||
finishRename();
|
||||
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.",
|
||||
});
|
||||
}
|
||||
finishRename();
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
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 +696,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 +965,42 @@ 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) => {
|
||||
e.stopPropagation();
|
||||
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 && (
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import type { ContextMenuItem } from "@t3tools/contracts";
|
||||
|
||||
/**
|
||||
* Imperative DOM-based context menu for non-Electron environments.
|
||||
* Shows a positioned dropdown and returns a promise that resolves
|
||||
* with the clicked item id, or null if dismissed.
|
||||
*/
|
||||
export function showContextMenuFallback<T extends string>(
|
||||
items: readonly { id: T; label: string }[],
|
||||
items: readonly ContextMenuItem<T>[],
|
||||
position?: { x: number; y: number },
|
||||
): Promise<T | null> {
|
||||
return new Promise<T | null>((resolve) => {
|
||||
@@ -41,8 +43,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));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
CommandId,
|
||||
type ContextMenuItem,
|
||||
ORCHESTRATION_WS_CHANNELS,
|
||||
ORCHESTRATION_WS_METHODS,
|
||||
ProjectId,
|
||||
@@ -9,6 +10,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 ContextMenuItem<T>[],
|
||||
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 +40,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 +52,23 @@ 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");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -307,4 +330,51 @@ 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 },
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
OrchestrationEvent,
|
||||
ORCHESTRATION_WS_CHANNELS,
|
||||
ORCHESTRATION_WS_METHODS,
|
||||
type ContextMenuItem,
|
||||
type NativeApi,
|
||||
ServerConfigUpdatedPayload,
|
||||
TerminalEvent,
|
||||
@@ -171,11 +172,11 @@ export function createWsNativeApi(): NativeApi {
|
||||
},
|
||||
contextMenu: {
|
||||
show: async <T extends string>(
|
||||
items: readonly { id: T; label: string }[],
|
||||
items: readonly ContextMenuItem<T>[],
|
||||
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,18 @@ 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>;
|
||||
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>;
|
||||
@@ -85,7 +91,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