diff --git a/desktop/src/l10n/zh.ts b/desktop/src/l10n/zh.ts index 9005dbf2..42854ae0 100644 --- a/desktop/src/l10n/zh.ts +++ b/desktop/src/l10n/zh.ts @@ -210,6 +210,7 @@ export const zh: Record = { "Category": "类别", "Change": "更改", "Change look": "更改外观", + "Change Look…": "更改外观…", "Chat": "聊天", "Chat name": "聊天名称", "Chat names live inside the encrypted roster blob, never on the relay.": "聊天名称保存在加密的名册数据中,不会以明文出现在中继上。", diff --git a/desktop/src/ui/layout.css b/desktop/src/ui/layout.css index a096b012..b27c482a 100644 --- a/desktop/src/ui/layout.css +++ b/desktop/src/ui/layout.css @@ -274,6 +274,10 @@ body.resizing * { border-radius: 8px; } +.chat-row:hover:not(.selected) { + background: var(--hover); +} + .chat-row.selected { background: var(--selection-inactive); } @@ -381,6 +385,11 @@ body.resizing * { color: var(--label-2); } +.pane-row:hover:not(.selected), +.setting-row:hover:not(.selected) { + background: var(--hover); +} + .pane-row.selected, .setting-row.selected { background: var(--selection-inactive); diff --git a/desktop/src/ui/root.tsx b/desktop/src/ui/root.tsx index 24a7c6da..83ce9b7b 100644 --- a/desktop/src/ui/root.tsx +++ b/desktop/src/ui/root.tsx @@ -260,8 +260,7 @@ export function botsAvailableToAdd(chatID: string) { return store.bots.filter((bot) => !chat.botIDs.includes(bot.id)); } -export async function renameChat(): Promise { - const chatID = selectedChatID(); +export async function renameChat(chatID: string | null = selectedChatID()): Promise { const chat = chatID ? store.chat(chatID) : undefined; if (!chatID || !chat || !isGroup(chat)) { void host.beep(); @@ -277,13 +276,11 @@ export async function renameChat(): Promise { if (answer === 0) store.rename(chatID, value); } -export function togglePinChat(): void { - const chatID = selectedChatID(); +export function togglePinChat(chatID: string | null = selectedChatID()): void { if (chatID) store.togglePin(chatID); } -export async function deleteChat(): Promise { - const chatID = selectedChatID(); +export async function deleteChat(chatID: string | null = selectedChatID()): Promise { const chat = chatID ? store.chat(chatID) : undefined; if (!chatID || !chat) { void host.beep(); diff --git a/desktop/src/ui/sidebar.test.ts b/desktop/src/ui/sidebar.test.ts new file mode 100644 index 00000000..e7a189b8 --- /dev/null +++ b/desktop/src/ui/sidebar.test.ts @@ -0,0 +1,96 @@ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import type { chatContextMenuEntries as getChatContextMenuEntries, openChatContextMenu as getOpenChatContextMenu } from "./sidebar"; +import type { select as getSelect, selection as getSelection } from "./root"; +import type { store as getStore } from "../model/store"; + +let chatContextMenuEntries: typeof getChatContextMenuEntries; +let openChatContextMenu: typeof getOpenChatContextMenu; +let select: typeof getSelect; +let selection: typeof getSelection; +let store: typeof getStore; + +const locationDescriptor = Object.getOwnPropertyDescriptor(globalThis, "location"); + +beforeAll(async () => { + Object.defineProperty(globalThis, "location", { + configurable: true, + value: { search: "?platform=linux", pathname: "/" }, + }); + const sidebar = await import("./sidebar"); + chatContextMenuEntries = sidebar.chatContextMenuEntries; + openChatContextMenu = sidebar.openChatContextMenu; + const root = await import("./root"); + select = root.select; + selection = root.selection; + const storeModule = await import("../model/store"); + store = storeModule.store; +}); + +afterAll(() => { + if (locationDescriptor) Object.defineProperty(globalThis, "location", locationDescriptor); + else Reflect.deleteProperty(globalThis, "location"); +}); + +describe("sidebar chat context menu", () => { + it("leaves the current selection alone and operates on the clicked chat", async () => { + // Seed test selection to chat 1 + const originalSelection = { kind: "chat" as const, id: "c1" }; + select(originalSelection); + + // Mock store.chat + const c1 = { id: "c1", kind: "dm", isPinned: false, botIDs: ["b1"], messages: [] }; + const c2 = { id: "c2", kind: "dm", isPinned: false, botIDs: ["b2"], messages: [] }; + const originalChat = store.chat.bind(store); + const originalBotsIn = store.botsIn.bind(store); + const originalTogglePin = store.togglePin.bind(store); + + let pinnedChatID: string | null = null; + store.chat = ((id: string) => (id === "c1" ? c1 : id === "c2" ? c2 : undefined)) as typeof store.chat; + store.botsIn = ((chat: unknown) => [{ id: "b2", name: "Bot 2" }]) as typeof store.botsIn; + store.togglePin = ((id: string) => { + pinnedChatID = id; + }) as typeof store.togglePin; + + try { + // Right-clicking c2 while c1 is selected + let menuEntries: ReturnType = []; + await openChatContextMenu("c2", { x: 10, y: 10 }, async (entries) => { + menuEntries = entries; + return "pin"; + }); + + // The selection should remain c1 + expect(selection.get()).toEqual(originalSelection); + // But the action (pin) should have been performed on c2 + expect(pinnedChatID).toBe("c2"); + // Menu entries for bot DM should include Pin, Change Look…, separator, Delete Bot + const labels = menuEntries.map((e) => e.label ?? (e.separator ? "---" : "")); + expect(labels).toContain("Pin"); + expect(labels).toContain("Change Look…"); + expect(labels).toContain("Delete Bot"); + } finally { + store.chat = originalChat; + store.botsIn = originalBotsIn; + store.togglePin = originalTogglePin; + } + }); + + it("offers Change Look… for bot chats but not for groups", () => { + const dmChat = { id: "dm", kind: "dm", isPinned: false, botIDs: ["b1"], messages: [] } as any; + const groupChat = { id: "grp", kind: "group", isPinned: false, botIDs: ["b1", "b2"], messages: [] } as any; + + const originalBotsIn = store.botsIn.bind(store); + store.botsIn = ((chat: any) => (chat.id === "dm" ? [{ id: "b1", name: "Bot 1" }] : [])) as typeof store.botsIn; + + try { + const dmEntries = chatContextMenuEntries(dmChat); + const groupEntries = chatContextMenuEntries(groupChat); + + expect(dmEntries.some((e) => e.label === "Change Look…")).toBe(true); + expect(groupEntries.some((e) => e.label === "Change Look…")).toBe(false); + expect(groupEntries.some((e) => e.label === "Rename…")).toBe(true); + } finally { + store.botsIn = originalBotsIn; + } + }); +}); diff --git a/desktop/src/ui/sidebar.tsx b/desktop/src/ui/sidebar.tsx index 6c2d860e..d13ade7a 100644 --- a/desktop/src/ui/sidebar.tsx +++ b/desktop/src/ui/sidebar.tsx @@ -10,10 +10,12 @@ import { deviceSymbol, isDM, isGroup, paneSymbol, paneTitle, settingsPanes, type import { track } from "../model/reactive"; import { store } from "../model/store"; import { AvatarCluster, botAvatar, sameAvatar, type AvatarContent } from "./avatar"; +import { addBotToChat } from "./actions"; import { commands, shortcutText } from "./commands"; import { HoverButton, SearchField } from "./controls"; import { Icon } from "./icons"; -import { popupMenu, separator } from "./menu"; +import { popupMenu, separator, type MenuEntry } from "./menu"; +import { presentBotLook } from "./sheets/botLook"; import { botsAvailableToAdd, closeSettings, @@ -103,7 +105,41 @@ function watchShortcutKey(): () => void { }; } -function ChatRow(props: { id: string; index: number }) { +export function chatContextMenuEntries(chat: ReturnType): MenuEntry[] { + if (!chat) return []; + const bots = store.botsIn(chat); + return [ + { id: "pin", label: chat.isPinned ? L("Unpin") : L("Pin") }, + ...(isGroup(chat) + ? [ + { id: "rename", label: L("Rename…") }, + { id: "add", label: L("Add Bot…"), disabled: botsAvailableToAdd(chat.id).length === 0 }, + ] + : bots.length > 0 + ? [{ id: "look", label: L("Change Look…") }] + : []), + separator, + { id: "delete", label: isDM(chat) ? L("Delete Bot") : L("Delete Chat") }, + ]; +} + +export async function openChatContextMenu( + chatID: string, + at: { x: number; y: number }, + popup: typeof popupMenu = popupMenu, +): Promise { + const chat = store.chat(chatID); + if (!chat) return; + const bots = store.botsIn(chat); + const picked = await popup(chatContextMenuEntries(chat), at); + if (picked === "pin") togglePinChat(chatID); + else if (picked === "rename") void renameChat(chatID); + else if (picked === "add") addBotToChat(chatID); + else if (picked === "look" && bots.length > 0) presentBotLook(bots[0]!.id); + else if (picked === "delete") void deleteChat(chatID); +} + +export function ChatRow(props: { id: string; index: number }) { const content = createMemo( () => { track.chats(); @@ -119,23 +155,8 @@ function ChatRow(props: { id: string; index: number }) { const shortcut = () => (showsShortcuts() && props.index < 9 ? shortcutText(`CmdOrCtrl+${props.index + 1}`) : null); const menu = async (event: MouseEvent) => { event.preventDefault(); - // Acting on the clicked row means selecting it first; the commands read the selection. - select({ kind: "chat", id: props.id }); - const chat = store.chat(props.id); - if (!chat) return; - const picked = await popupMenu( - [ - { id: "pin", label: chat.isPinned ? L("Unpin") : L("Pin") }, - ...(isGroup(chat) ? [{ id: "rename", label: L("Rename…") }, { id: "add", label: L("Add Bot…"), disabled: botsAvailableToAdd(props.id).length === 0 }] : []), - separator, - { id: "delete", label: isDM(chat) ? L("Delete Bot") : L("Delete Chat") }, - ], - { x: event.clientX, y: event.clientY }, - ); - if (picked === "pin") togglePinChat(); - else if (picked === "rename") void renameChat(); - else if (picked === "add") commands.run("addBot"); - else if (picked === "delete") void deleteChat(); + // The menu acts on the clicked row and leaves the selection where it is. + await openChatContextMenu(props.id, { x: event.clientX, y: event.clientY }); }; return ( @@ -150,7 +171,7 @@ function ChatRow(props: { id: string; index: number }) { if (event.button === 0) select({ kind: "chat", id: props.id }); }} onDblClick={() => { - if (row().isGroup) void renameChat(); + if (row().isGroup) void renameChat(props.id); }} onContextMenu={menu} > diff --git a/docs/architecture/desktop-app.md b/docs/architecture/desktop-app.md index ca3c5671..01e9c30c 100644 --- a/docs/architecture/desktop-app.md +++ b/docs/architecture/desktop-app.md @@ -3,7 +3,7 @@ `desktop/` is the app for Windows and Linux, built with [MyGo](https://github.com/egoist/mygo): a Go process that owns the windows, the menu bars, the tray icon, notifications, and the CLI, and pages in the system webview (WebView2 on Windows, WebKitGTK on Linux) written with Solid 2 and TypeScript. It follows the macOS app screen for screen: the same windows, panes, sheets, menus, and words, with Lucide icons standing in for SF Symbols (`src/ui/icons.tsx` maps each symbol name the models and views use). - **The Go side** (`desktop/*.go`): `launcher.go` finds the CLI (`BEANS_CLI`, the app's `resources/bin`, `~/.local/bin`, `~/.cargo/bin`, then `PATH`), admits only this build's fixed port (4874 production, 4875 Dev) before probing, and starts `beans serve --port … --parent-pid … --ready-stdout` when nothing answers, restarting it with the macOS launcher's backoff; `client.go` holds the one websocket to the CLI and reconnects it. Saved preferences and explicit `BEANS_PORT` must independently match the build; invalid configuration stays offline before probing, dialing, preference writes or reconnect. This is app connection policy, not a restriction on standalone CLI operator commands. A page calls the CLI through the bound `CLI.request` and hears its events as `cli:event`. `app.go` picks the window as the macOS AppDelegate does: onboarding until the CLI reports an identity, then the main window, and while onboarding is up, Settings as a small window of General and Advanced. A computer without an identity waits for the CLI's answer past the 2.5-second loading deadline, which a CLI's first start (a new binary, a new database) can outlast; a startup failure or 10 seconds without an answer opens the main window on the offline recovery controls. With a tray icon, closing the windows leaves the app running, since the bots on this computer run with it. `files.go` serves attachments and bot images to the pages by token under the `beans-file` scheme, and `avatar.go` makes a bot's image a square PNG of at most 512 px; `prefs.go` keeps the preferences (selection, pane widths, appearance, language, send key, the CLI port) in `preferences-v2.json` in the app's data folder; `menus.go` builds each window's menu bar from what the page describes and brings clicks back as commands. `Host.OpenExternal` opens only http(s) links with a host and mailto links, as the macOS app's `NSWorkspace.openLink` does, since links from bots, plugins, and the marketplace reach it. -- **The page** (`desktop/src`): `model/` is the macOS app's AppStore, models, formatters, and demo reply engine (`BEANS_MOCK=1`, or `?mock=1` in a browser tab) in TypeScript, and message Markdown is markdown-it folded into the blocks `crates/markdown` produces, with bare links found by the crate's autolink rules (`model/autolink.ts`). The store is plain and synchronous; `model/reactive.ts` turns its events into signals the views read. Routes (`@solidjs/router`) name each window's page: the main window at `/`, a chat at `/chat/:id` and a settings pane at `/settings/:pane` inside it, onboarding at `/onboarding`, and the small settings window at `/settings-window`. A selection replaces the route, and back and forward walk the settings panes, as the macOS toolbar's do. A sheet (`src/ui/overlay.tsx`) is the key window, as on the Mac: the window behind it is `inert`, keys typed while nothing has focus go to the sheet in front, and closing one hands the keyboard back to what had it. The composer has no Dictate button and General no Dictation section: WebView2 and WebKitGTK give a page no speech recognizer. +- **The page** (`desktop/src`): `model/` is the macOS app's AppStore, models, formatters, and demo reply engine (`BEANS_MOCK=1`, or `?mock=1` in a browser tab) in TypeScript, and message Markdown is markdown-it folded into the blocks `crates/markdown` produces, with bare links found by the crate's autolink rules (`model/autolink.ts`). The store is plain and synchronous; `model/reactive.ts` turns its events into signals the views read. Routes (`@solidjs/router`) name each window's page: the main window at `/`, a chat at `/chat/:id` and a settings pane at `/settings/:pane` inside it, onboarding at `/onboarding`, and the small settings window at `/settings-window`. A selection replaces the route, and back and forward walk the settings panes, as the macOS toolbar's do. Sidebar navigation chooses a chat or settings row on pointer press; a right-click opens a row's menu without choosing the row, and the menu's items act on that row's chat, with a bot's chat offering Change Look…; chat and settings rows show a hover background. A sheet (`src/ui/overlay.tsx`) is the key window, as on the Mac: the window behind it is `inert`, keys typed while nothing has focus go to the sheet in front, and closing one hands the keyboard back to what had it. The composer has no Dictate button and General no Dictation section: WebView2 and WebKitGTK give a page no speech recognizer. - **Bot avatars** (`src/ui/avatar.tsx`): `@beans/blobatar` generates a self-contained SVG from the stable bot id, encoded as an image URL rather than injected as markup. This works offline and matches the Mac and phone look for the same id. An encrypted uploaded profile image overrides it after local fetch; working dots, group clusters, and separate you/system discs remain. Marketplace bot previews use template ids (or names when an id is absent). The Look sheet drafts generated base appearance and per-activity-state settings independently of the uploaded photo. Save awaits one `bots.update` for changed look/photo fields; failures keep the draft open and show the error. See [Bot avatars](avatars.md) for the shared appearance contract. Legacy `symbol_name` and `accent` remain on the wire for compatibility but never render as bot avatars. - **Provider models**: Settings › Providers offers Refresh Models for saved custom providers. It calls the CLI's `providers.refresh` and reports changed, unchanged, or error without exposing the key; newly discovered IDs appear in model pickers while the bot's selected model stays put. - **MCP servers**: Settings › Plugins lists the picked Runner's marketplace plugins separately from its `mcp.json` servers. The Solid sheet adds, edits, parses pasted server JSON, enables, reconnects, signs out, hides individual tools, and removes a server through the CLI's `mcp.*` requests; sign-in uses `plugins.connect` so the browser opens on the requesting Device. The Runner owns its file and credentials. Reload rereads an externally edited file; Open is local to that Runner. The inspector opens the same sheet for a Runner-owned server, while bot capability switches still limit each bot's plugin access.