Files
Reims-Go/frontend/src/components/Sidebar.tsx
T
Dnyo666 371eb11629 fix(ui): prevent titlebar menu switch flicker and overlay focus-tooltip races
- Suppress deferred Radix `onCloseAutoFocus` focus restoration in `TitleMenus` when switching between sibling menus (or opening a dialog) so the closing menu's `setTimeout(0)` refocus no longer triggers `onFocusOutside` on the newly opened menu
- Guard `TitleMenus` `onOpenChange` by active menu ownership, block sibling-trigger `onPointerDownOutside` races, add 300ms hover-click guard (primary button without Ctrl), RTL-aware `ArrowLeft`/`ArrowRight` navigation, `--wails-draggable: no-drag` on `.titlebar-menus`, and instant lateral menu-switch transitions
- Keep `Tip` permanently controlled in `controls.tsx` to eliminate uncontrolled/controlled `useControllableState` warnings and stuck ghost tooltips when toggled via `disabled`
- Guard both VMPage `⋯` menus (toolbar + SSH) and Sidebar `ContextMenu` against pointer-close tooltip pops and dialog focus fighting, and reset menu/dialog state on VM switch
- Improve `Modal` `onOpenAutoFocus` to skip disabled/hidden/unchecked-radio inputs with `preventScroll`, fall back to the dialog container instead of the `✕` close tooltip, and exclude `.dialog`/`.menu` containers from `:focus-visible` ring overrides in `app.css`
2026-11-30 17:14:23 +08:00

254 lines
11 KiB
TypeScript

import * as Ctx from '@radix-ui/react-context-menu'
import { AppWindow, Camera, CopyPlus, Cpu, Download, FolderOpen, Info, Play, Plus, Power, Puzzle, RotateCw, Settings, Square, Trash } from 'lucide-react'
import React, { useRef } from 'react'
import { I18nKey, t } from '../i18n'
import { Locale, VMListItem } from '../types'
import { IconButton, StatusShape, Tip, statusKind } from '../ui/controls'
import { VersionArt } from './VersionArt'
import { VMAction } from './VMPage'
export const HOST_ID = '__host__'
export const INSTALLERS_ID = '__installers__'
export const PLUGINS_ID = '__plugins__'
export const SETTINGS_ID = '__settings__'
export const ABOUT_ID = '__about__'
/** Pages that are not VMs; everything else selectable in the sidebar is a VM name. */
export const PAGE_IDS: readonly string[] = [HOST_ID, INSTALLERS_ID, PLUGINS_ID, SETTINGS_ID, ABOUT_ID]
interface Props {
locale: Locale
vms: VMListItem[]
transitions: Record<string, string>
selected: string
hostReady: boolean
onSelect: (id: string) => void
/** VM name → display name (its note); empty or missing shows the name. */
displayNames?: Record<string, string>
/** VM name → macOS major version for its icon; null shows the name's initial. */
macosMajors?: Record<string, number | null>
/** Double-click on a VM: its main action (start, or show its window). */
onActivate?: (name: string) => void
onNew: () => void
onVMContextAction?: (name: string, action: VMAction | 'settings') => void
/** An installer download is running (busy dot on the Installers entry). */
downloading?: boolean
/** Safe mode is active (third-party plugins skipped). */
safeMode?: boolean
/** Update capsule card shown under the brand (null when nothing to show). */
updateCard?: React.ReactNode
/** A newer release exists: dot on the About entry. */
updateDot?: boolean
}
export const Sidebar: React.FC<Props> = ({
locale, vms, transitions, selected, hostReady, onSelect, displayNames = {}, macosMajors = {}, onActivate, onNew, onVMContextAction, downloading, safeMode, updateCard, updateDot,
}) => {
const tr = (k: I18nKey) => t(locale, k)
const listRef = useRef<HTMLDivElement>(null)
const ctxOpenedByPointerRef = useRef(false)
const ids = [...vms.map((v) => v.name), INSTALLERS_ID, PLUGINS_ID, HOST_ID, SETTINGS_ID, ABOUT_ID]
// Listbox keyboard model: ↑/↓/Home/End move selection and focus together.
const onKeyDown = (e: React.KeyboardEvent) => {
const idx = ids.indexOf(selected)
let next = -1
if (e.key === 'ArrowDown') next = Math.min(ids.length - 1, idx + 1)
else if (e.key === 'ArrowUp') next = Math.max(0, idx - 1)
else if (e.key === 'Home') next = 0
else if (e.key === 'End') next = ids.length - 1
if (next < 0) return
e.preventDefault()
onSelect(ids[next])
listRef.current?.querySelector<HTMLElement>(`[data-id="${CSS.escape(ids[next])}"]`)?.focus()
}
const hasSelectedInList = ids.includes(selected)
const navBtn = (
id: string,
label: string,
icon: React.ReactNode,
badgeClass: string,
stateLabel: string,
kind: ReturnType<typeof statusKind> | null,
onDoubleClick?: () => void,
dot?: boolean,
) => (
<button
key={id}
type="button"
role="option"
data-id={id}
className="nav-item"
aria-selected={selected === id}
aria-label={stateLabel ? t(locale, 'sidebarItemAria', { label, state: stateLabel }) : label}
tabIndex={selected === id || (!hasSelectedInList && id === ids[0]) ? 0 : -1}
onClick={() => onSelect(id)}
onDoubleClick={onDoubleClick}
>
<span className={`nav-icon-badge ${badgeClass}`} aria-hidden="true">{icon}</span>
<span className="nav-label">{label}</span>
{dot && <span className="nav-dot" aria-hidden="true" />}
{kind && (
<Tip label={stateLabel} side="right">
<span className="hstack"><StatusShape kind={kind} /></span>
</Tip>
)}
</button>
)
return (
<div className="sidebar-wrap">
<aside className="sidebar rg-p-sidebar">
<div className="sidebar-brand">
<img className="sidebar-brand-mark" src="/logo.svg" alt="" aria-hidden="true" />
<span className="sidebar-brand-name">Reims-Go</span>
</div>
{updateCard}
<div className="sidebar-head rg-p-sidebar-head">
<span>{tr('sidebarVMs')}</span>
<IconButton small label={tr('newVM')} shortcut="Ctrl+N" icon={<Plus size={15} />} onClick={onNew} tipSide="right" />
</div>
<div className="nav rg-p-nav" role="listbox" aria-label={tr('sidebarVMs')} ref={listRef} onKeyDown={onKeyDown}>
<div className="nav-group nav-vms" role="group" aria-label={tr('sidebarVMs')}>
{vms.length === 0 && <div className="nav-empty rg-p-empty" data-rg-slot="empty.state">{tr('emptyVMs')}</div>}
{vms.map((vm) => {
const st = transitions[vm.name] ?? vm.state
const running = st === 'running'
const stopped = st === 'stopped'
const transitioning = st === 'starting' || st === 'stopping'
const label = displayNames[vm.name] || vm.name
const trigger = navBtn(
vm.name,
label,
<VersionArt shape="tile" major={macosMajors[vm.name] ?? null} name={label} />,
'nav-icon-art',
t(locale, `state_${st}` as I18nKey),
statusKind(st),
onActivate ? () => onActivate(vm.name) : undefined,
)
if (!onVMContextAction) return trigger
return (
<Ctx.Root key={vm.name}>
<Ctx.Trigger
asChild
onContextMenu={(e) => {
ctxOpenedByPointerRef.current = e.button === 2 && (e.clientX !== 0 || e.clientY !== 0)
}}
>
{trigger}
</Ctx.Trigger>
<Ctx.Portal>
<Ctx.Content
className="menu"
onCloseAutoFocus={(e) => {
if (
ctxOpenedByPointerRef.current
|| document.querySelector('[role="dialog"], [role="alertdialog"]')
) {
e.preventDefault()
}
}}
>
{stopped && (
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'start')}>
<Play size={14} /> {tr('start')}
</Ctx.Item>
)}
{running && (
<>
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'focus')}>
<AppWindow size={14} /> {tr('showWindow')}
</Ctx.Item>
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'screenshot')}>
<Camera size={14} /> {tr('copyScreenshot')}
</Ctx.Item>
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'stop')}>
<Square size={14} /> {tr('stop')}
</Ctx.Item>
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'restart')}>
<RotateCw size={14} /> {tr('restart')}
</Ctx.Item>
</>
)}
{(running || transitioning) && (
<Ctx.Item className="menu-item danger" onSelect={() => onVMContextAction(vm.name, 'forceStop')}>
<Power size={14} /> {tr('forceStop')}
</Ctx.Item>
)}
<Ctx.Separator className="menu-sep" />
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'settings')}>
<Settings size={14} /> {tr('settingsEllipsis')}
</Ctx.Item>
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'openSharedFolder')}>
<FolderOpen size={14} /> {tr('openSharedFolder')}
</Ctx.Item>
{stopped && (
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'cloneVM')}>
<CopyPlus size={14} /> {tr('cloneVM')}
</Ctx.Item>
)}
<Ctx.Item className="menu-item" onSelect={() => onVMContextAction(vm.name, 'openFolder')}>
<FolderOpen size={14} /> {tr('openFolder')}
</Ctx.Item>
<Ctx.Separator className="menu-sep" />
<Ctx.Item className="menu-item danger" disabled={running || transitioning} onSelect={() => onVMContextAction(vm.name, 'delete')}>
<Trash size={14} /> {tr('delete')}
</Ctx.Item>
</Ctx.Content>
</Ctx.Portal>
</Ctx.Root>
)
})}
</div>
<div className="nav-group nav-system" role="group" aria-label={tr('sidebarSystem')}>
{navBtn(
INSTALLERS_ID,
tr('sidebarInstallers'),
<Download size={13} />,
'nav-icon-installers',
downloading ? tr('installersDownloading') : '',
downloading ? 'busy' : null,
)}
{navBtn(
PLUGINS_ID,
tr('sidebarPlugins'),
<Puzzle size={13} />,
'nav-icon-plugins',
safeMode ? tr('pluginSafeModeShort') : '',
safeMode ? 'warn' : null,
)}
{navBtn(
HOST_ID,
tr('sidebarHost'),
<Cpu size={13} />,
'nav-icon-host',
tr(hostReady ? 'hostReady' : 'hostNeedsAction'),
hostReady ? 'running' : 'warn',
)}
{navBtn(
SETTINGS_ID,
tr('sidebarSettings'),
<Settings size={13} />,
'nav-icon-settings',
'',
null,
)}
{navBtn(
ABOUT_ID,
tr('settingsAbout'),
<Info size={13} />,
'nav-icon-about',
'',
null,
undefined,
updateDot,
)}
</div>
</div>
<div className="rg-slot-anchor" data-rg-slot="sidebar.footer" />
</aside>
</div>
)
}