Skip to content

Commit a5b6206

Browse files
committed
feat(desktop): add windows app menu
1 parent 66d409d commit a5b6206

14 files changed

Lines changed: 554 additions & 134 deletions

File tree

packages/app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"type": "module",
66
"exports": {
77
".": "./src/index.ts",
8+
"./desktop-menu": "./src/desktop-menu.ts",
89
"./vite": "./vite.js",
910
"./index.css": "./src/index.css"
1011
},

packages/app/src/components/titlebar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { usePlatform } from "@/context/platform"
1212
import { useCommand } from "@/context/command"
1313
import { useLanguage } from "@/context/language"
1414
import { useSettings } from "@/context/settings"
15+
import { WindowsAppMenu } from "./windows-app-menu"
1516
import { applyPath, backPath, forwardPath } from "./titlebar-history"
1617

1718
type TauriDesktopWindow = {
@@ -191,6 +192,9 @@ export function Titlebar() {
191192
"pl-2": !mac(),
192193
}}
193194
>
195+
<Show when={windows()}>
196+
<WindowsAppMenu command={command} platform={platform} />
197+
</Show>
194198
<Show when={mac()}>
195199
<div class="h-full shrink-0" style={{ width: `${72 / zoom()}px` }} />
196200
<div class="xl:hidden w-10 shrink-0 flex items-center justify-center">
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { Show, type JSX } from "solid-js"
2+
import { DropdownMenu } from "@opencode-ai/ui/dropdown-menu"
3+
import { Icon } from "@opencode-ai/ui/icon"
4+
import { IconButton } from "@opencode-ai/ui/icon-button"
5+
6+
import { useCommand } from "@/context/command"
7+
import { DESKTOP_MENU, desktopMenuVisible, type DesktopMenuAction, type DesktopMenuEntry } from "@/desktop-menu"
8+
import { usePlatform } from "@/context/platform"
9+
10+
export function WindowsAppMenu(props: { command: ReturnType<typeof useCommand>; platform: ReturnType<typeof usePlatform> }) {
11+
let lastFocused: HTMLElement | undefined
12+
13+
const rememberFocus = () => {
14+
const active = document.activeElement
15+
lastFocused = active instanceof HTMLElement ? active : undefined
16+
}
17+
const commandDisabled = (id: string) => {
18+
const option = props.command.options.find((option) => option.id === id)
19+
if (!option) return true
20+
return option.disabled ?? false
21+
}
22+
const runCommand = (id: string) => {
23+
if (commandDisabled(id)) return
24+
props.command.trigger(id)
25+
}
26+
const runAction = (action: DesktopMenuAction) => {
27+
if (action.startsWith("edit.") && lastFocused?.isConnected) lastFocused.focus({ preventScroll: true })
28+
void props.platform.runDesktopMenuAction?.(action)
29+
}
30+
const runEntry = (entry: DesktopMenuEntry) => {
31+
if (entry.type === "separator") return
32+
if (entry.command) {
33+
runCommand(entry.command)
34+
return
35+
}
36+
if (entry.action) {
37+
runAction(entry.action)
38+
return
39+
}
40+
if (entry.href) props.platform.openLink(entry.href)
41+
}
42+
43+
return (
44+
<DropdownMenu gutter={4} placement="bottom-start">
45+
<DropdownMenu.Trigger
46+
as={IconButton}
47+
icon="menu"
48+
variant="ghost"
49+
class="titlebar-icon rounded-md shrink-0"
50+
aria-label="OpenCode menu"
51+
onPointerDown={rememberFocus}
52+
onKeyDown={rememberFocus}
53+
/>
54+
<DropdownMenu.Portal>
55+
<DropdownMenu.Content class="desktop-app-menu">
56+
<DropdownMenu.Group>
57+
<DropdownMenu.GroupLabel class="desktop-app-menu-heading">OpenCode</DropdownMenu.GroupLabel>
58+
{DESKTOP_MENU.filter((menu) => desktopMenuVisible(menu, "windows")).map((menu) => (
59+
<DesktopMenuSubmenu label={menu.label}>
60+
{menu.items?.filter((entry) => desktopMenuVisible(entry, "windows")).map((entry) =>
61+
entry.type === "separator" ? (
62+
<DropdownMenu.Separator />
63+
) : (
64+
<DesktopMenuItem
65+
label={entry.label ?? ""}
66+
keybind={entry.command ? props.command.keybind(entry.command) : entry.accelerator?.windows}
67+
disabled={entry.command ? commandDisabled(entry.command) : false}
68+
onSelect={() => runEntry(entry)}
69+
/>
70+
),
71+
)}
72+
</DesktopMenuSubmenu>
73+
))}
74+
</DropdownMenu.Group>
75+
</DropdownMenu.Content>
76+
</DropdownMenu.Portal>
77+
</DropdownMenu>
78+
)
79+
}
80+
81+
function DesktopMenuSubmenu(props: { label: string; children: JSX.Element }) {
82+
return (
83+
<DropdownMenu.Sub>
84+
<DropdownMenu.SubTrigger>
85+
<span data-slot="dropdown-menu-item-label">{props.label}</span>
86+
<span data-slot="desktop-app-menu-chevron">
87+
<Icon name="chevron-right" size="small" />
88+
</span>
89+
</DropdownMenu.SubTrigger>
90+
<DropdownMenu.Portal>
91+
<DropdownMenu.SubContent class="desktop-app-menu">{props.children}</DropdownMenu.SubContent>
92+
</DropdownMenu.Portal>
93+
</DropdownMenu.Sub>
94+
)
95+
}
96+
97+
function DesktopMenuItem(props: { label: string; keybind?: string; disabled?: boolean; onSelect: () => void }) {
98+
return (
99+
<DropdownMenu.Item disabled={props.disabled} onSelect={props.onSelect}>
100+
<DropdownMenu.ItemLabel>{props.label}</DropdownMenu.ItemLabel>
101+
<Show when={props.keybind}>
102+
<span data-slot="desktop-app-menu-keybind">{props.keybind}</span>
103+
</Show>
104+
</DropdownMenu.Item>
105+
)
106+
}

packages/app/src/context/platform.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createSimpleContext } from "@opencode-ai/ui/context"
22
import type { AsyncStorage, SyncStorage } from "@solid-primitives/storage"
33
import type { Accessor } from "solid-js"
4+
import type { DesktopMenuAction } from "../desktop-menu"
45
import { ServerConnection } from "./server"
56

67
type PickerPaths = string | string[] | null
@@ -82,6 +83,9 @@ export type Platform = {
8283
/** Webview zoom level (desktop only) */
8384
webviewZoom?: Accessor<number>
8485

86+
/** Run a desktop-only menu action from the app chrome */
87+
runDesktopMenuAction?(action: DesktopMenuAction): Promise<void> | void
88+
8589
/** Check if an editor app exists (desktop only) */
8690
checkAppExists?(appName: string): Promise<boolean>
8791

packages/app/src/desktop-menu.ts

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
export type DesktopMenuPlatform = "macos" | "windows"
2+
3+
export type DesktopMenuAction =
4+
| "app.checkForUpdates"
5+
| "app.relaunch"
6+
| "edit.undo"
7+
| "edit.redo"
8+
| "edit.cut"
9+
| "edit.copy"
10+
| "edit.paste"
11+
| "edit.delete"
12+
| "edit.selectAll"
13+
| "view.reload"
14+
| "view.toggleDevTools"
15+
| "view.resetZoom"
16+
| "view.zoomIn"
17+
| "view.zoomOut"
18+
| "view.toggleFullscreen"
19+
| "window.new"
20+
| "window.close"
21+
| "window.minimize"
22+
| "window.toggleMaximize"
23+
24+
export type DesktopMenuRole =
25+
| "about"
26+
| "close"
27+
| "copy"
28+
| "cut"
29+
| "hide"
30+
| "hideOthers"
31+
| "paste"
32+
| "quit"
33+
| "redo"
34+
| "reload"
35+
| "resetZoom"
36+
| "selectAll"
37+
| "toggleDevTools"
38+
| "togglefullscreen"
39+
| "undo"
40+
| "unhide"
41+
| "windowMenu"
42+
| "zoomIn"
43+
| "zoomOut"
44+
45+
export type DesktopMenuItem = {
46+
type: "item"
47+
label?: string
48+
command?: string
49+
action?: DesktopMenuAction
50+
role?: DesktopMenuRole
51+
href?: string
52+
accelerator?: Partial<Record<DesktopMenuPlatform, string>>
53+
enabled?: "updater"
54+
platforms?: DesktopMenuPlatform[]
55+
}
56+
57+
export type DesktopMenuSeparator = {
58+
type: "separator"
59+
platforms?: DesktopMenuPlatform[]
60+
}
61+
62+
export type DesktopMenuEntry = DesktopMenuItem | DesktopMenuSeparator
63+
64+
export type DesktopMenu = {
65+
id: string
66+
label: string
67+
role?: DesktopMenuRole
68+
items?: DesktopMenuEntry[]
69+
platforms?: DesktopMenuPlatform[]
70+
}
71+
72+
export const DESKTOP_MENU: DesktopMenu[] = [
73+
{
74+
id: "app",
75+
label: "OpenCode",
76+
platforms: ["macos"],
77+
items: [
78+
{ type: "item", role: "about" },
79+
{ type: "item", label: "Check for Updates...", action: "app.checkForUpdates", enabled: "updater" },
80+
{ type: "item", label: "Settings", command: "settings.open", accelerator: { macos: "Cmd+," } },
81+
{ type: "item", label: "Reload Webview", action: "view.reload" },
82+
{ type: "item", label: "Restart", action: "app.relaunch" },
83+
{ type: "separator" },
84+
{ type: "item", role: "hide" },
85+
{ type: "item", role: "hideOthers" },
86+
{ type: "item", role: "unhide" },
87+
{ type: "separator" },
88+
{ type: "item", role: "quit" },
89+
],
90+
},
91+
{
92+
id: "file",
93+
label: "File",
94+
items: [
95+
{
96+
type: "item",
97+
label: "New Session",
98+
command: "session.new",
99+
accelerator: { macos: "Shift+Cmd+S" },
100+
},
101+
{ type: "item", label: "Open Project...", command: "project.open", accelerator: { macos: "Cmd+O" } },
102+
{
103+
type: "item",
104+
label: "Settings",
105+
command: "settings.open",
106+
accelerator: { windows: "Ctrl+," },
107+
platforms: ["windows"],
108+
},
109+
{
110+
type: "item",
111+
label: "New Window",
112+
action: "window.new",
113+
accelerator: { macos: "Cmd+Shift+N", windows: "Ctrl+Shift+N" },
114+
},
115+
{ type: "separator" },
116+
{ type: "item", label: "Close Window", action: "window.close", role: "close" },
117+
],
118+
},
119+
{
120+
id: "edit",
121+
label: "Edit",
122+
items: [
123+
{ type: "item", label: "Undo", action: "edit.undo", role: "undo", accelerator: { windows: "Ctrl+Z" } },
124+
{ type: "item", label: "Redo", action: "edit.redo", role: "redo", accelerator: { windows: "Ctrl+Y" } },
125+
{ type: "separator" },
126+
{ type: "item", label: "Cut", action: "edit.cut", role: "cut", accelerator: { windows: "Ctrl+X" } },
127+
{ type: "item", label: "Copy", action: "edit.copy", role: "copy", accelerator: { windows: "Ctrl+C" } },
128+
{ type: "item", label: "Paste", action: "edit.paste", role: "paste", accelerator: { windows: "Ctrl+V" } },
129+
{ type: "item", label: "Delete", action: "edit.delete" },
130+
{
131+
type: "item",
132+
label: "Select All",
133+
action: "edit.selectAll",
134+
role: "selectAll",
135+
accelerator: { windows: "Ctrl+A" },
136+
},
137+
],
138+
},
139+
{
140+
id: "view",
141+
label: "View",
142+
items: [
143+
{ type: "item", label: "Toggle Sidebar", command: "sidebar.toggle", accelerator: { macos: "Cmd+B" } },
144+
{ type: "item", label: "Toggle Terminal", command: "terminal.toggle", accelerator: { macos: "Ctrl+`" } },
145+
{ type: "item", label: "Toggle File Tree", command: "fileTree.toggle" },
146+
{ type: "separator" },
147+
{ type: "item", label: "Reload", action: "view.reload", role: "reload" },
148+
{ type: "item", label: "Toggle Developer Tools", action: "view.toggleDevTools", role: "toggleDevTools" },
149+
{ type: "separator" },
150+
{
151+
type: "item",
152+
label: "Actual Size",
153+
action: "view.resetZoom",
154+
role: "resetZoom",
155+
accelerator: { windows: "Ctrl+0" },
156+
},
157+
{ type: "item", label: "Zoom In", action: "view.zoomIn", role: "zoomIn", accelerator: { windows: "Ctrl++" } },
158+
{ type: "item", label: "Zoom Out", action: "view.zoomOut", role: "zoomOut", accelerator: { windows: "Ctrl+-" } },
159+
{ type: "separator" },
160+
{ type: "item", label: "Toggle Full Screen", action: "view.toggleFullscreen", role: "togglefullscreen" },
161+
],
162+
},
163+
{
164+
id: "go",
165+
label: "Go",
166+
items: [
167+
{ type: "item", label: "Back", command: "common.goBack", accelerator: { macos: "Cmd+[" } },
168+
{ type: "item", label: "Forward", command: "common.goForward", accelerator: { macos: "Cmd+]" } },
169+
{ type: "separator" },
170+
{ type: "item", label: "Previous Session", command: "session.previous", accelerator: { macos: "Option+Up" } },
171+
{ type: "item", label: "Next Session", command: "session.next", accelerator: { macos: "Option+Down" } },
172+
{ type: "separator" },
173+
{
174+
type: "item",
175+
label: "Previous Project",
176+
command: "project.previous",
177+
accelerator: { macos: "Cmd+Option+Up" },
178+
},
179+
{
180+
type: "item",
181+
label: "Next Project",
182+
command: "project.next",
183+
accelerator: { macos: "Cmd+Option+Down" },
184+
},
185+
],
186+
},
187+
{
188+
id: "window",
189+
label: "Window",
190+
role: "windowMenu",
191+
items: [
192+
{ type: "item", label: "Minimize", action: "window.minimize" },
193+
{ type: "item", label: "Maximize", action: "window.toggleMaximize" },
194+
{ type: "separator" },
195+
{ type: "item", label: "Close Window", action: "window.close" },
196+
],
197+
},
198+
{
199+
id: "help",
200+
label: "Help",
201+
items: [
202+
{ type: "item", label: "OpenCode Documentation", href: "https://opencode.ai/docs" },
203+
{ type: "item", label: "Support Forum", href: "https://discord.com/invite/opencode" },
204+
{ type: "separator" },
205+
{ type: "item", label: "Share Feedback", href: "https://github.com/anomalyco/opencode/issues/new?template=feature_request.yml" },
206+
{ type: "item", label: "Report a Bug", href: "https://github.com/anomalyco/opencode/issues/new?template=bug_report.yml" },
207+
],
208+
},
209+
]
210+
211+
export function desktopMenuVisible(item: { platforms?: DesktopMenuPlatform[] }, platform: DesktopMenuPlatform) {
212+
return !item.platforms || item.platforms.includes(platform)
213+
}

0 commit comments

Comments
 (0)