Files
planet/client/src/main/menu.ts
T

253 lines
8.4 KiB
TypeScript

import { app, Menu, shell, type BrowserWindow, type MenuItemConstructorOptions } from 'electron'
import type { DesktopConfigStore, GpuRenderMode, SplashTheme } from './config'
import type { GpuRuntimeSummary } from './gpu-monitor'
import type { HotUpdateVersionInfo } from './hot-updater'
import { openServerSettingsDialog, toggleWindowOrientation } from './window'
function formatGpuSummaryLabel(summary: GpuRuntimeSummary): string {
const targetLabel =
summary.activeTarget === 'low-power'
? '集成核显'
: summary.activeTarget === 'software'
? 'CPU 软渲染'
: summary.activeTarget === 'high-performance' || summary.latestVramSample
? '独立显卡'
: '硬件 GPU'
const modeTag = summary.requestedMode === 'auto' ? '自动' : '手动'
const vramTag = summary.latestVramSample
? ` · 独显空闲 ${(summary.latestVramSample.freeMiB / 1024).toFixed(1)} GB`
: ''
return `当前生效: ${targetLabel} (${modeTag}${vramTag})`
}
export function buildApplicationMenu(options: {
mainWindow: BrowserWindow
configStore: DesktopConfigStore
getVersionInfo?: () => HotUpdateVersionInfo
onCheckForUpdates?: () => void
getGpuStatus?: () => GpuRuntimeSummary
onSelectGpuMode?: (mode: GpuRenderMode) => void | Promise<void>
}): Menu {
const sendAction = (action: string) => {
if (!options.mainWindow.isDestroyed()) {
options.mainWindow.webContents.send('planet:action', action)
}
}
const openAdminPath = (routePath: string) => {
const adminBase = options.configStore.get().adminUrl.replace(/\/+$/, '')
void shell.openExternal(`${adminBase}${routePath}`)
}
const currentSplashTheme = options.configStore.get().splashTheme
const currentGpuMode = options.configStore.get().gpuMode
const gpuSummary = options.getGpuStatus?.()
const selectSplashTheme = (theme: SplashTheme) => {
options.configStore.updateSplashTheme(theme)
Menu.setApplicationMenu(buildApplicationMenu(options))
if (theme !== 'off' && !options.mainWindow.isDestroyed()) {
options.mainWindow.webContents.send('planet:replay-splash', { theme })
}
}
const replayActiveSplash = () => {
const activeTheme = options.configStore.get().splashTheme
const targetTheme: SplashTheme = activeTheme === 'off' ? 'singularity' : activeTheme
if (!options.mainWindow.isDestroyed()) {
options.mainWindow.webContents.send('planet:replay-splash', { theme: targetTheme })
}
}
const template: MenuItemConstructorOptions[] = [
...(process.platform === 'darwin'
? [
{
label: app.name,
submenu: [
{ role: 'about' as const },
{ type: 'separator' as const },
{ role: 'hide' as const },
{ role: 'hideOthers' as const },
{ role: 'unhide' as const },
{ type: 'separator' as const },
{ role: 'quit' as const },
],
},
]
: []),
{
label: 'Planet Earth',
submenu: [
{
label: '重新加载数据 / Reload Data',
accelerator: 'CmdOrCtrl+R',
click: () => sendAction('reload-data'),
},
{
label: '服务端连接设置 / Server Settings...',
click: () => {
openServerSettingsDialog(options.mainWindow, options.configStore, () => {
if (!options.mainWindow.isDestroyed()) {
options.mainWindow.reload()
}
})
},
},
{
label: '启动画面 / Splash Screen',
submenu: [
{
label: '启用启动画面 / Enable Splash Screen',
type: 'checkbox',
checked: currentSplashTheme !== 'off',
click: () => selectSplashTheme(currentSplashTheme === 'off' ? 'singularity' : 'off'),
},
{ type: 'separator' },
{
label: '立即重播启动画面 / Replay Splash Screen',
accelerator: 'CmdOrCtrl+Shift+S',
click: replayActiveSplash,
},
],
},
{
label: '渲染设备 / GPU Mode',
submenu: [
...(gpuSummary
? [
{
label: formatGpuSummaryLabel(gpuSummary),
enabled: false,
},
{ type: 'separator' as const },
]
: []),
{
label: '自动(卡顿缺显存切核显,显存空出自动回独显) / Auto',
type: 'radio',
checked: currentGpuMode === 'auto',
click: () => {
void options.onSelectGpuMode?.('auto')
},
},
{
label: '独立显卡(高性能) / Discrete GPU',
type: 'radio',
checked: currentGpuMode === 'high-performance',
click: () => {
void options.onSelectGpuMode?.('high-performance')
},
},
{
label: '集成核显(不占独显显存) / Integrated GPU',
type: 'radio',
checked: currentGpuMode === 'low-power',
click: () => {
void options.onSelectGpuMode?.('low-power')
},
},
{
label: '纯 CPU 软渲染 / Software GL',
type: 'radio',
checked: currentGpuMode === 'software',
click: () => {
void options.onSelectGpuMode?.('software')
},
},
],
},
{ type: 'separator' },
{
label: '打开运维控制台 / Open Admin Console',
click: () => openAdminPath('/admin'),
},
{
label: '打开操作文档 / Open Documentation',
click: () => openAdminPath('/docs/manual'),
},
{ type: 'separator' },
{
label: '检查更新 / Check for Updates...',
click: () => options.onCheckForUpdates?.(),
},
...(options.getVersionInfo
? [
(() => {
const info = options.getVersionInfo!()
const suffix = info.usingHotfix ? ' (热更 / Hotfix)' : ''
return {
label: `当前版本: Shell v${info.shellVersion} · Earth v${info.activeBundleVersion}${suffix}`,
enabled: false,
}
})(),
]
: []),
{ type: 'separator' },
{ role: 'quit', label: '退出 / Quit' },
],
},
{
label: '视图 / View',
submenu: [
{
label: '放大视角 / Zoom In',
accelerator: 'CmdOrCtrl+=',
click: () => sendAction('zoom-in'),
},
{
label: '缩小视角 / Zoom Out',
accelerator: 'CmdOrCtrl+-',
click: () => sendAction('zoom-out'),
},
{
label: '重置缩放 / Reset Zoom (100%)',
accelerator: 'CmdOrCtrl+0',
click: () => sendAction('zoom-reset'),
},
{
label: '重置地球视角 / Reset View',
click: () => sendAction('reset-view'),
},
{ type: 'separator' },
{
label: '切换横竖屏 / Toggle Portrait & Landscape',
accelerator: 'CmdOrCtrl+Shift+M',
click: () => {
if (!options.mainWindow.isDestroyed()) {
toggleWindowOrientation(options.mainWindow)
}
},
},
{
label: '切换全屏 / Toggle Fullscreen (F11)',
// Keep accelerator unregistered on Win/Linux so controls.js F11 shortcut never double-toggles.
...(process.platform === 'darwin' ? { accelerator: 'Ctrl+Command+F' } : {}),
click: () => {
if (!options.mainWindow.isDestroyed()) {
options.mainWindow.setFullScreen(!options.mainWindow.isFullScreen())
}
},
},
{
label: '显示/隐藏帧率 / Toggle FPS',
click: () => sendAction('toggle-fps'),
},
{ type: 'separator' },
{
label: '开发者工具 / Toggle DevTools',
accelerator: 'F12',
click: () => {
if (!options.mainWindow.isDestroyed()) {
options.mainWindow.webContents.toggleDevTools()
}
},
},
],
},
]
return Menu.buildFromTemplate(template)
}