import { t } from '../i18n' import type { Locale, VMAction } from '../types' import { vmFeatures, type VMFeatures } from './endpoints' import type { ThemeMode } from './useAppearance' export const REPO_URL = 'https://github.com/Nwflower/Reims-Go' export function siteURL(locale: Locale, path = ''): string { const base = locale.toLowerCase().startsWith('zh') ? 'https://reims.dev' : 'https://reims.dev/en' return path ? `${base}${path}` : base } export const MENU_IDS = ['vm', 'view', 'help'] as const export type MenuId = (typeof MENU_IDS)[number] export type MenuCommand = 'new' | 'vmSettings' | 'sidebar' | 'logs' | 'installers' | 'plugins' | 'host' | 'settings' | 'website' | 'docs' | 'project' | 'report' | 'update' | 'about' | `vm:${VMAction}` | `theme:${ThemeMode}` export type MenuRow = | { kind: 'separator' } | { kind: 'label'; label: string } | { kind: 'action' | 'check' | 'radio'; command: MenuCommand; label: string; disabled?: boolean; checked?: boolean; danger?: boolean; shortcut?: string; macKey?: string } export interface TitleMenuState { locale: Locale sidebarOpen: boolean logsOpen: boolean themeMode: ThemeMode canLight: boolean canDark: boolean vm: { name: string; driver_id?: string; state: string; busy: boolean; graphics?: string; canSuspend: boolean; features: VMFeatures } | null fullscreenHotkey?: string appVersion?: string } export interface TitleMenuProps extends TitleMenuState { onToggleSidebar: () => void onToggleLogs: () => void onThemeMode: (mode: ThemeMode) => void onNewVM: () => void onVMAction: (action: VMAction) => void onVMSettings: () => void onVMDelete: () => void onNavigate: (page: 'installers' | 'plugins' | 'host' | 'settings' | 'about') => void onOpenURL: (url: string) => void } export function buildTitleMenus(state: TitleMenuState, fullscreen: boolean | undefined = undefined) { const { vm, locale, appVersion, fullscreenHotkey, sidebarOpen, logsOpen, canLight, canDark, themeMode } = state const tr = (key: Parameters[1]) => t(locale, key) const running = vm?.state === 'running' const stopped = vm?.state === 'stopped' const suspended = vm?.state === 'suspended' const transitioning = vm?.state === 'starting' || vm?.state === 'stopping' || vm?.state === 'suspending' || vm?.state === 'resuming' const busy = !vm || vm.busy const features = vm?.features ?? vmFeatures(null) const canFullscreen = running && features.fullscreen && vm?.graphics !== 'vmware-svga' const fullscreenLabel = !canFullscreen || fullscreen === undefined ? tr('toggleFullscreen') : fullscreen ? tr('exitFullscreen') : tr('enterFullscreen') const sep = { kind: 'separator' } as const const vmRows: MenuRow[] = [{ kind: 'action', command: 'new', label: tr('newVMEllipsis'), shortcut: 'Ctrl+N', macKey: 'n' }, sep] if (running) { if (features.window) vmRows.push({ kind: 'action', command: 'vm:focus', label: tr('showWindow'), shortcut: 'Ctrl+Enter', macKey: '\r', disabled: busy }) } else if (suspended) { vmRows.push({ kind: 'action', command: 'vm:resume', label: tr('resume'), shortcut: vm.canSuspend ? 'Ctrl+Enter' : undefined, macKey: vm.canSuspend ? '\r' : undefined, disabled: busy || !vm.canSuspend }) if (!vm.canSuspend) vmRows.push({ kind: 'label', label: tr('resumeUnavailable') }) } else { vmRows.push({ kind: 'action', command: 'vm:start', label: tr('start'), shortcut: 'Ctrl+Enter', macKey: '\r', disabled: busy || !stopped }) } if (features.fullscreen) vmRows.push({ kind: 'action', command: 'vm:fullscreen', label: fullscreenLabel, shortcut: fullscreenHotkey, disabled: busy || !canFullscreen }) if (features.suspend) vmRows.push({ kind: 'action', command: 'vm:suspend', label: tr('suspend'), disabled: busy || !running || !vm?.canSuspend }) vmRows.push( { kind: 'action', command: 'vm:stop', label: tr('stop'), disabled: busy || !running }, { kind: 'action', command: 'vm:restart', label: tr('restart'), disabled: busy || !running }, ) if (features.screenshot) vmRows.push({ kind: 'action', command: 'vm:screenshot', label: tr('copyScreenshot'), disabled: busy || !running }) if (features.macos) vmRows.push(sep, { kind: 'action', command: 'vmSettings', label: tr('settingsEllipsis'), shortcut: 'Ctrl+,', disabled: !vm || suspended || transitioning }, { kind: 'action', command: 'vm:openFolder', label: tr('openFolder'), disabled: !vm }, ) vmRows.push(sep) if (suspended) vmRows.push({ kind: 'action', command: 'vm:discardSuspend', label: tr('discardSuspend'), danger: true, disabled: busy }) vmRows.push( { kind: 'action', command: 'vm:forceStop', label: tr('forceStop'), danger: true, disabled: !vm || !(running || transitioning) }, { kind: 'action', command: 'vm:delete', label: tr('deleteEllipsis'), danger: true, disabled: busy || running || suspended || transitioning }, ) const viewRows: MenuRow[] = [ { kind: 'check', command: 'sidebar', label: tr('sidebarLabel'), checked: sidebarOpen, shortcut: 'Ctrl+B', macKey: 'b' }, { kind: 'check', command: 'logs', label: tr('logButton'), checked: logsOpen, shortcut: 'Ctrl+L', macKey: 'l' }, sep, { kind: 'label', label: tr('appearance') }, ...(['light', 'dark', 'system'] as const).map((mode): MenuRow => ({ kind: 'radio', command: `theme:${mode}`, checked: themeMode === mode, label: tr(mode === 'light' ? 'themeLight' : mode === 'dark' ? 'themeDark' : 'themeSystem'), disabled: mode === 'light' ? !canLight : mode === 'dark' ? !canDark : !canLight || !canDark, })), sep, { kind: 'action', command: 'installers', label: tr('sidebarInstallers') }, { kind: 'action', command: 'plugins', label: tr('sidebarPlugins') }, { kind: 'action', command: 'host', label: tr('sidebarHost') }, { kind: 'action', command: 'settings', label: tr('sidebarSettings'), macKey: ',' }, ] const helpRows: MenuRow[] = [ { kind: 'action', command: 'website', label: tr('helpWebsite') }, { kind: 'action', command: 'docs', label: tr('helpDocs') }, { kind: 'action', command: 'project', label: tr('helpProjectPage') }, { kind: 'action', command: 'report', label: tr('helpReportIssue') }, { kind: 'action', command: 'update', label: tr('helpCheckUpdate') }, { kind: 'action', command: 'about', label: tr('helpAbout') }, ] if (appVersion) helpRows.push(sep, { kind: 'label', label: `Reims-Go ${appVersion}` }) return { vm: { label: tr('menuVM'), rows: vmRows }, view: { label: tr('menuView'), rows: viewRows }, help: { label: tr('menuHelp'), rows: helpRows }, } } export function runMenuCommand(props: TitleMenuProps, command: MenuCommand): void { switch (command) { case 'new': return props.onNewVM() case 'vmSettings': return props.onVMSettings() case 'vm:delete': return props.onVMDelete() case 'sidebar': return props.onToggleSidebar() case 'logs': return props.onToggleLogs() case 'installers': case 'plugins': case 'host': case 'settings': case 'about': return props.onNavigate(command) case 'update': return props.onNavigate('about') case 'website': return props.onOpenURL(siteURL(props.locale)) case 'docs': return props.onOpenURL(siteURL(props.locale, '/docs/quick-start')) case 'project': return props.onOpenURL(REPO_URL) case 'report': return props.onOpenURL(`${REPO_URL}/issues`) } if (command.startsWith('vm:')) props.onVMAction(command.slice(3) as VMAction) else if (command.startsWith('theme:')) props.onThemeMode(command.slice(6) as ThemeMode) }