Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/architecture/desktop.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ Closing the main window hides it so work in progress can continue. Quitting the

A provider can continue running a submitted job and charge for it after the local application quits. Browser export requires the renderer process.

## Keyboard shortcuts belong to the page

Shortcut handling, hints, and the help dialog read one registry in the renderer, and the packaged application adds no accelerators of its own. Chromium inside the desktop shell has no browser-level shortcut table, so `Ctrl+N`, `Ctrl+K` and `Ctrl+J` reach the page and run the registered action. In a normal browser those combinations belong to the browser (new window, address-bar search, downloads), which runs its own action instead and never delivers the key.

A menu accelerator would therefore duplicate work rather than enable it: the menu action and the page handler both run for one keypress. The shell keeps only window-level menus (fullscreen, developer tools, quit) and leaves every application shortcut to the renderer.

See [Desktop application development](../development/desktop.md) for build commands, CLI registration, and release checks.
6 changes: 6 additions & 0 deletions docs/architecture/desktop.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ Host 与独立 API 共用工程存储、命令校验和编辑会话规则。桌

退出本机应用后,供应商可能继续执行已提交的任务并产生费用。浏览器导出需要渲染进程保持运行。

## 快捷键由页面处理

快捷键的处理、提示与帮助面板都读渲染进程内同一份注册表,安装包不额外注册加速键。桌面壳里的 Chromium 没有浏览器级快捷键表,所以 `Ctrl+N`、`Ctrl+K`、`Ctrl+J` 会照常到达页面并执行注册的动作。在普通浏览器里这些组合属于浏览器(新建窗口、地址栏搜索、下载),浏览器会执行自己的动作而不把按键交给页面。

也正因如此,菜单加速键只会造成重复而不是启用:一次按键会同时触发菜单项和页面处理器。桌面壳只保留窗口级菜单(全屏、开发者工具、退出),业务快捷键全部留给渲染进程。

构建命令、CLI 注册和发布检查见[桌面应用开发指南](../development/desktop.zh-CN.md)。
1 change: 1 addition & 0 deletions scripts/run-browser-suite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const workflows = [
{ name: "minimax h3", script: "tests/browser/minimax_h3.mjs" },
{ name: "provider settings", script: "tests/browser/provider_settings.mjs" },
{ name: "new model discovery", script: "tests/browser/new_model_discovery.mjs" },
{ name: "shortcut hints", script: "tests/browser/shortcuts.mjs" },
{ name: "Zhipu model settings feedback", script: "tests/browser/zhipu_provider.mjs" },
{ name: "capability discovery", script: "tests/browser/capability_discovery.mjs" },
{ name: "created node focus", script: "tests/browser/created_node_focus.mjs" },
Expand Down
7 changes: 4 additions & 3 deletions src/canvas/CanvasCreationMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CanvasContextMenu } from "./CanvasContextMenu";
import { CanvasIcon } from "./CanvasIcon";
import { TooltipButton } from "./TooltipButton";
import { CANVAS_CREATION_TOOLS } from "./creationTools";
import { shortcutKeys, type ShortcutId } from "./shortcuts";
import type { CanvasNodeKind } from "./model";
import { useT } from "../i18n/I18nProvider";
import "./canvas-creation-menu.css";
Expand Down Expand Up @@ -217,9 +218,9 @@ export function CanvasCreationMenu({
onAssets: () => run(actions.onAssets),
onUpload: () => run(actions.onUpload),
};
const shortcuts = navigator.platform.includes("Mac")
? ["⌘V", "⌘⇧Z", "⌘Z"]
: ["Ctrl+V", "Ctrl+Shift+Z", "Ctrl+Z"];
const shortcuts = ["paste", "redo", "undo"].map((id) =>
shortcutKeys(id as ShortcutId),
);
const shell = (content: ReactNode) => (
<div ref={root} onPointerLeave={scheduleClose} onPointerEnter={cancelClose}>
<CanvasContextMenu
Expand Down
14 changes: 13 additions & 1 deletion src/canvas/ProjectSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "../backend/workspace";
import { errorMessage } from "../backend/api";
import { useT } from "../i18n/I18nProvider";
import { shortcutAriaKeys, shortcutKeys } from "./shortcuts";

const ACTION_MENU_HEIGHT = 92;

Expand All @@ -31,9 +32,12 @@ export function ProjectSelector({
onRename,
onRenameProject,
onDeleteProject,
openSignal,
}: {
currentId: string;
readOnly?: boolean;
/** Bumped by the app-wide shortcut to open the list without the trigger. */
openSignal?: number;
editNameForId?: string | null;
name: string;
disabled: boolean;
Expand Down Expand Up @@ -226,6 +230,10 @@ export function ProjectSelector({
if (open) searchInput.current?.focus();
else setQuery("");
}, [open]);
useEffect(() => {
if (!openSignal || disabled) return;
setOpen(true);
}, [openSignal, disabled]);
return (
<div
className="cw-project-selector"
Expand Down Expand Up @@ -340,7 +348,8 @@ export function ProjectSelector({
ref={trigger}
disabled={disabled || busy}
aria-label={t("canvas.project.switch")}
title={t("canvas.project.switch")}
aria-keyshortcuts={shortcutAriaKeys("switch-project")}
title={`${t("canvas.project.switch")} (${shortcutKeys("switch-project")})`}
aria-haspopup="menu"
aria-expanded={open}
aria-controls={open ? menuId : undefined}
Expand Down Expand Up @@ -521,13 +530,16 @@ export function ProjectSelector({
<button
className="cw-project-create"
role="menuitem"
aria-keyshortcuts={shortcutAriaKeys("new-project")}
disabled={readOnly || busy || loading || disabled}
onClick={() => {
setOpen(false);
if (!readOnly) onCreate();
}}
>
<span>{t("canvas.project.create")}</span>
{/* Visible hint only: the accessible name stays the action's. */}
<kbd aria-hidden="true">{shortcutKeys("new-project")}</kbd>
<CanvasIcon name="plus" size={20} />
</button>
{actions && (
Expand Down
158 changes: 158 additions & 0 deletions src/canvas/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import type { MessageKey } from "../i18n/types";

/**
* One registry for every keyboard shortcut the workspace handles.
*
* Combos are written once, in a modifier+key form: `mod` is ⌘ on macOS and
* Ctrl elsewhere. Tooltips, the help dialog and the key handlers all read from
* here, so a shortcut cannot be documented in one place and handled in another.
*/
export const SHORTCUT_COMBOS = {
"new-project": "mod+n",
"switch-project": "mod+k",
settings: "mod+comma",
assistant: "mod+j",
help: "?",
upload: "mod+o",
undo: "mod+z",
redo: "mod+shift+z",
"select-all": "mod+a",
copy: "mod+c",
cut: "mod+x",
paste: "mod+v",
ungroup: "mod+shift+g",
group: "mod+g",
duplicate: "mod+d",
fit: "shift+1",
"fit-selection": "shift+2",
delete: "delete",
"add-text": "t",
"hand-tool": "h",
"select-tool": "v",
} as const;

export type ShortcutId = keyof typeof SHORTCUT_COMBOS;

/** Action names reused from the toolbar so the hint never invents a new label. */
export const SHORTCUT_LABELS: Partial<Record<ShortcutId, MessageKey>> = {
"new-project": "canvas.project.create",
"switch-project": "canvas.project.switch",
settings: "canvas.workspace.toolbar.settings",
assistant: "canvas.workspace.toolbar.chat",
help: "canvas.workspace.toolbar.help",
};

const KEY_NAMES: Record<string, string> = {
comma: ",",
slash: "/",
space: "Space",
delete: "Delete",
enter: "Enter",
escape: "Esc",
arrowleft: "←",
arrowright: "→",
arrowup: "↑",
arrowdown: "↓",
};

export function isMacPlatform(): boolean {
if (typeof navigator === "undefined") return false;
return /mac/i.test(navigator.platform ?? "");
}

function parts(combo: string) {
const tokens = combo.toLowerCase().split("+");
return { key: tokens.at(-1) ?? "", mods: tokens.slice(0, -1) };
}

function keyName(key: string, mac: boolean) {
if (key === "delete") return mac ? "⌫" : "Delete";
return KEY_NAMES[key] ?? key.toUpperCase();
}

/**
* Display form for tooltips and the help dialog: `⌘N`, `⇧⌘Z`, `Ctrl+Shift+Z`.
* macOS uses the menu-bar symbols; other platforms spell the modifiers out.
*/
export function shortcutKeys(id: ShortcutId, mac = isMacPlatform()): string {
const { key, mods } = parts(SHORTCUT_COMBOS[id]);
if (!mods.length && key === "?") return "?";
if (mac) {
const symbols = mods
.map((mod) =>
mod === "mod" ? "⌘" : mod === "shift" ? "⇧" : mod === "alt" ? "⌥" : "⌃",
)
.sort((a, b) => "⌃⌥⇧⌘".indexOf(a) - "⌃⌥⇧⌘".indexOf(b))
.join("");
return `${symbols}${keyName(key, mac)}`;
}
const names = mods
.map((mod) => (mod === "mod" ? "Ctrl" : mod === "shift" ? "Shift" : "Alt"))
.sort((a, b) => ["Ctrl", "Alt", "Shift"].indexOf(a) - ["Ctrl", "Alt", "Shift"].indexOf(b));
return [...names, keyName(key, mac)].join("+");
}

/**
* `aria-keyshortcuts` spelling: "Control+N Meta+N" so both platforms match.
* Screen readers announce these, while tooltips show the local one.
*/
export function shortcutAriaKeys(id: ShortcutId): string {
const { key, mods } = parts(SHORTCUT_COMBOS[id]);
const name = (mod: string) =>
mod === "mod" ? "Control" : mod === "shift" ? "Shift" : mod === "alt" ? "Alt" : "Ctrl";
const control = [...mods.map(name), keyName(key, false)].join("+");
if (!mods.includes("mod")) return control;
const meta = [
...mods.map((mod) => (mod === "mod" ? "Meta" : name(mod))),
keyName(key, false),
].join("+");
return `${control} ${meta}`;
}

// Combos spell the key; native events report what the key prints.
const EVENT_KEYS: Record<string, string> = { comma: ",", slash: "/" };

/** True when a native keyboard event matches the registered combo. */
export function matchesShortcut(
event: {
key: string;
ctrlKey?: boolean;
metaKey?: boolean;
shiftKey?: boolean;
altKey?: boolean;
},
id: ShortcutId,
): boolean {
const { key, mods } = parts(SHORTCUT_COMBOS[id]);
const mod = Boolean(event.ctrlKey || event.metaKey);
if (mods.includes("mod") !== mod) return false;
if (mods.includes("shift") !== Boolean(event.shiftKey)) return false;
if (mods.includes("alt") !== Boolean(event.altKey)) return false;
if (mods.includes("ctrl") && !event.ctrlKey) return false;
return event.key.toLowerCase() === (EVENT_KEYS[key] ?? key);
}

/**
* Rows for the help dialog. Canvas gestures stay written as prose; the key
* combinations come from the registry so they cannot drift from the handlers.
*/
export function helpShortcutRows(
t: (key: MessageKey) => string,
mac = isMacPlatform(),
): Array<[string, string]> {
const rows: Array<[ShortcutId, MessageKey]> = [
["new-project", "canvas.project.create"],
["switch-project", "canvas.project.switch"],
["settings", "canvas.workspace.toolbar.settings"],
["assistant", "canvas.workspace.toolbar.chat"],
["upload", "canvas.rail.upload"],
["undo", "canvas.workspace.toolbar.undo"],
["redo", "canvas.workspace.toolbar.redo"],
["select-all", "canvas.workspace.help.selectAll"],
["fit", "canvas.workspace.help.fit"],
["delete", "canvas.workspace.help.delete"],
["add-text", "canvas.kind.text"],
["help", "canvas.workspace.toolbar.help"],
];
return rows.map(([id, key]) => [t(key), shortcutKeys(id, mac)]);
}
11 changes: 11 additions & 0 deletions src/canvas/workspace.css
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,17 @@
.cw-project-cleanup:hover {
color: #fff;
}
/* The new-project row carries its shortcut so the key is discoverable here. */
.cw-project-create kbd {
margin-left: auto;
padding: 1px 5px;
border: 1px solid #ffffff1f;
border-radius: 4px;
color: #9a9a9e;
font-family: inherit;
font-size: 11px;
line-height: 1.5;
}
.cw-project-cleanup {
color: #f0a3a3;
}
Expand Down
30 changes: 5 additions & 25 deletions src/canvas/workspace/DesktopSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useT } from "../../i18n/I18nProvider";
import { IconButton } from "./workspaceChrome";
import { CanvasIcon } from "../CanvasIcon";
import { ProjectDeleteDialog } from "../ProjectDeleteDialog";
import { shortcutAriaKeys, shortcutKeys } from "../shortcuts";
import logo from "../../assets/open_director_logo.png";

export function DesktopSidebar({ open, currentId, switchingStatus, disabled, onToggle, onCreate, onSelect, onDelete, onSettings }: {
Expand All @@ -14,8 +15,6 @@ export function DesktopSidebar({ open, currentId, switchingStatus, disabled, onT
onCreate: () => void; onSelect: (project: WorkspaceProject) => Promise<boolean>; onSettings: () => void;
}) {
const t = useT();
const mac = document.documentElement.dataset.desktopPlatform === "darwin";
const modifier = mac ? "⌘" : "Ctrl+";
const [projects, setProjects] = useState<WorkspaceProject[]>(() => peekWorkspaceProjects()?.projects ?? []);
const [actions, setActions] = useState<{ id: string; top: number; left: number } | null>(null);
const [deleteTarget, setDeleteTarget] = useState<WorkspaceProject | null>(null);
Expand Down Expand Up @@ -52,25 +51,6 @@ export function DesktopSidebar({ open, currentId, switchingStatus, disabled, onT
const timer = window.setTimeout(() => searchInput.current?.focus({ preventScroll: true }), 200);
return () => window.clearTimeout(timer);
}, [open, searching]);
useEffect(() => {
if (!document.documentElement.hasAttribute("data-desktop-chrome")) return;
const keydown = (event: KeyboardEvent) => {
if (!(event.metaKey || event.ctrlKey) || event.altKey || event.shiftKey || event.isComposing || document.querySelector("dialog[open]")) return;
const key = event.key.toLowerCase();
if (key === "n") {
event.preventDefault();
if (!disabled && !event.repeat) createProject();
return;
}
if (key !== "k") return;
event.preventDefault();
if (!open) onToggle();
setSearching(true);
searchInput.current?.focus({ preventScroll: true });
};
window.addEventListener("keydown", keydown);
return () => window.removeEventListener("keydown", keydown);
}, [open, onToggle, onCreate, disabled]);
useEffect(() => {
if (!searching || query.trim()) return;
const dismiss = (event: PointerEvent) => {
Expand Down Expand Up @@ -114,9 +94,9 @@ export function DesktopSidebar({ open, currentId, switchingStatus, disabled, onT
return <div className="od-desktop-navigation">
<aside id="od-project-sidebar" className="od-project-sidebar" inert={!open} aria-hidden={!open}>
<div className="od-sidebar-brand"><img src={logo} alt=""/>OpenDirector</div>
<button className="od-sidebar-create" type="button" disabled={disabled} onClick={createProject} aria-keyshortcuts="Meta+N Control+N">
<button className="od-sidebar-create" type="button" disabled={disabled} onClick={createProject} aria-keyshortcuts={shortcutAriaKeys("new-project")}>
<span className="od-sidebar-create-label"><CanvasIcon name="newProject" size={14} />{t("desktop.sidebar.newProject")}</span>
<span className="od-sidebar-create-shortcut" aria-hidden="true"><kbd>{modifier}</kbd><kbd>N</kbd></span>
<span className="od-sidebar-create-shortcut" aria-hidden="true"><kbd>{shortcutKeys("new-project")}</kbd></span>
</button>
<div className="od-sidebar-heading" ref={searchRoot}>
{searching ? <div className="od-sidebar-search-field">
Expand All @@ -129,7 +109,7 @@ export function DesktopSidebar({ open, currentId, switchingStatus, disabled, onT
</div> : <>
<span className="od-sidebar-label">{t("desktop.sidebar.projects")}</span>
<button ref={searchButton} className="od-sidebar-search-toggle" type="button" onClick={() => setSearching(true)}
aria-label={t("canvas.project.search")} aria-expanded={false} aria-keyshortcuts="Meta+K Control+K" title={`${t("canvas.project.search")} (${modifier}K)`}><CanvasIcon name="search" size={14} /></button>
aria-label={t("canvas.project.search")} aria-expanded={false} title={t("canvas.project.search")}><CanvasIcon name="search" size={14} /></button>
</>}
</div>
<span className="od-sidebar-switch-status" role="status">{switchingStatus}</span>
Expand Down Expand Up @@ -161,7 +141,7 @@ export function DesktopSidebar({ open, currentId, switchingStatus, disabled, onT
<button type="button" onClick={onToggle} aria-expanded={open} aria-controls="od-project-sidebar" title={t("desktop.sidebar.toggle")} aria-label={t("desktop.sidebar.toggle")}>
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M9 4v16"/></svg>
</button>
{!open && <button type="button" disabled={disabled} onClick={createProject} title={`${t("desktop.sidebar.newProject")} (${modifier}N)`} aria-label={t("desktop.sidebar.newProject")} aria-keyshortcuts="Meta+N Control+N"><CanvasIcon name="newProject" size={18} /></button>}
{!open && <button type="button" disabled={disabled} onClick={createProject} title={`${t("desktop.sidebar.newProject")} (${shortcutKeys("new-project")})`} aria-label={t("desktop.sidebar.newProject")} aria-keyshortcuts={shortcutAriaKeys("new-project")}><CanvasIcon name="newProject" size={18} /></button>}
</div>
</div>;
}
Loading
Loading