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. The application-level shortcuts (new project, switch project, settings, assistant) are registered and advertised only when the shell marks the document with `data-desktop-chrome`. Chromium inside the 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; the page therefore leaves them unhandled and shows no hint for them.

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 共用工程存储、命令校验和编辑会话规则。桌

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

## 快捷键由页面处理

快捷键的处理、提示与帮助面板都读渲染进程内同一份注册表,安装包不额外注册加速键。应用级快捷键(新建工程、切换工程、设置、助手)只在文档带有 `data-desktop-chrome` 标记、即运行在桌面壳中时注册并提示。桌面壳里的 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 @@ -34,6 +34,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: "canvas composer placement", script: "tests/browser/canvas_composer_placement.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 } from "./shortcuts";
import type { CanvasNodeKind } from "./model";
import { useT } from "../i18n/I18nProvider";
import "./canvas-creation-menu.css";
Expand Down Expand Up @@ -228,9 +229,9 @@ export function CanvasCreationMenu({
? () => run(actions.onImportUrl!)
: undefined,
};
const shortcuts = navigator.platform.includes("Mac")
? ["⌘V", "⌘⇧Z", "⌘Z"]
: ["Ctrl+V", "Ctrl+Shift+Z", "Ctrl+Z"];
const shortcuts = (["paste", "redo", "undo"] as const).map((id) =>
shortcutKeys(id),
);
const shell = (content: ReactNode) => (
<div ref={root} onPointerLeave={scheduleClose} onPointerEnter={cancelClose}>
<CanvasContextMenu
Expand Down
11 changes: 6 additions & 5 deletions src/canvas/CanvasToolRail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useT } from "../i18n/I18nProvider";
import { CanvasIcon } from "./CanvasIcon";
import { CANVAS_CREATION_TOOLS } from "./creationTools";
import type { CanvasNodeKind } from "./model";
import { shortcutAriaKeys, shortcutKeys } from "./shortcuts";
import { TooltipButton } from "./TooltipButton";

const nodeTools = CANVAS_CREATION_TOOLS.filter((tool) => !tool.unavailable);
Expand Down Expand Up @@ -63,8 +64,10 @@ export function CanvasToolRail({
className="cw-rail-button"
label={t("canvas.rail.add", { label: t(tool.labelKey) })}
tooltipSide="right"
shortcut={tool.kind === "text" ? "T" : undefined}
aria-keyshortcuts={tool.kind === "text" ? "T" : undefined}
shortcut={tool.kind === "text" ? shortcutKeys("add-text") : undefined}
aria-keyshortcuts={
tool.kind === "text" ? shortcutAriaKeys("add-text") : undefined
}
tabIndex={tabStop === index ? 0 : -1}
disabled={readOnly}
onFocus={() => setTabStop(index)}
Expand All @@ -91,9 +94,7 @@ export function CanvasToolRail({
type="button"
className="cw-rail-button"
aria-label={t("canvas.rail.uploadLocal")}
aria-keyshortcuts={
navigator.platform.includes("Mac") ? "Meta+O" : "Control+O"
}
aria-keyshortcuts={shortcutAriaKeys("upload")}
disabled={readOnly}
tabIndex={tabStop === nodeTools.length + 1 ? 0 : -1}
onFocus={() => setTabStop(nodeTools.length + 1)}
Expand Down
7 changes: 6 additions & 1 deletion src/canvas/ChatReferenceEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
type MentionOption,
type ReferenceCategory,
} from "./referenceMentions.ts";
import { APP_SHORTCUTS, matchesShortcut } from "./shortcuts";
import "./reference-mentions.css";
import { assetDisplayName } from "./assetNames.ts";
import { useT } from "../i18n/I18nProvider.tsx";
Expand Down Expand Up @@ -1132,7 +1133,11 @@ export function ChatReferenceEditor({
event.nativeEvent.keyCode === 229
)
return;
if (event.key !== "Escape") event.stopPropagation();
if (
event.key !== "Escape" &&
!APP_SHORTCUTS.some((id) => matchesShortcut(event, id))
)
event.stopPropagation();
if (disabled || busy) {
event.preventDefault();
return;
Expand Down
15 changes: 14 additions & 1 deletion src/canvas/ProjectSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
} from "../backend/workspace";
import { errorMessage } from "../backend/api";
import { useT } from "../i18n/I18nProvider";
import {
appShortcutsAvailable,
shortcutAriaKeys,
shortcutKeys,
} from "./shortcuts";

const ACTION_MENU_HEIGHT = 92;

Expand Down Expand Up @@ -48,6 +53,7 @@ export function ProjectSelector({
onDeleteProject: (project: WorkspaceProject) => Promise<void>;
}) {
const t = useT();
const appHints = appShortcutsAvailable();
const canOpenInNewTab = !window.opendirectorDesktop;
const [open, setOpen] = useState(false);
const [query, setQuery] = useState("");
Expand Down Expand Up @@ -352,7 +358,12 @@ export function ProjectSelector({
ref={trigger}
disabled={disabled || busy}
aria-label={t("canvas.project.switch")}
title={t("canvas.project.switch")}
aria-keyshortcuts={appHints ? shortcutAriaKeys("switch-project") : undefined}
title={
appHints
? `${t("canvas.project.switch")} (${shortcutKeys("switch-project")})`
: t("canvas.project.switch")
}
aria-haspopup="menu"
aria-expanded={open}
aria-controls={open ? menuId : undefined}
Expand Down Expand Up @@ -533,13 +544,15 @@ export function ProjectSelector({
<button
className="cw-project-create"
role="menuitem"
aria-keyshortcuts={appHints ? shortcutAriaKeys("new-project") : undefined}
disabled={readOnly || busy || loading || disabled}
onClick={() => {
setOpen(false);
if (!readOnly) onCreate();
}}
>
<span>{t("canvas.project.create")}</span>
{appHints && <kbd aria-hidden="true">{shortcutKeys("new-project")}</kbd>}
<CanvasIcon name="plus" size={20} />
</button>
{actions && (
Expand Down
153 changes: 153 additions & 0 deletions src/canvas/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import type { MessageKey } from "../i18n/types";

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;

export const APP_SHORTCUTS = [
"new-project",
"switch-project",
"settings",
"assistant",
] as const satisfies readonly ShortcutId[];

export type AppShortcutId = (typeof APP_SHORTCUTS)[number];

export function appShortcutsAvailable(): boolean {
return (
typeof document !== "undefined" &&
document.documentElement.hasAttribute("data-desktop-chrome")
);
}

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();
}

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("+");
}

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}`;
}

const EVENT_KEYS: Record<string, string> = { comma: ",", slash: "/" };

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);
}

export function helpShortcutRows(
t: (key: MessageKey) => string,
mac = isMacPlatform(),
): Array<[string, string]> {
const rows: Array<[ShortcutId, MessageKey]> = [
...(appShortcutsAvailable()
? APP_SHORTCUTS.map(
(id) => [id, SHORTCUT_LABELS[id]!] as [ShortcutId, MessageKey],
)
: []),
["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)]);
}
10 changes: 10 additions & 0 deletions src/canvas/workspace.css
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,16 @@
.cw-project-cleanup:hover {
color: #fff;
}
.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
Loading
Loading