From 371eb1162997f78eb95e4d593b283f17da8a1531 Mon Sep 17 00:00:00 2001 From: Dnyo666 Date: Mon, 30 Nov 2026 17:14:14 +0800 Subject: [PATCH] fix(ui): prevent titlebar menu switch flicker and overlay focus-tooltip races MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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` --- frontend/src/components/Sidebar.tsx | 22 ++++- frontend/src/components/TitleMenus.tsx | 123 +++++++++++++++++++++++-- frontend/src/components/VMPage.tsx | 106 ++++++++++++++++++--- frontend/src/styles/app.css | 11 ++- frontend/src/ui/controls.tsx | 29 +++--- frontend/src/ui/dialog.tsx | 17 +++- 6 files changed, 266 insertions(+), 42 deletions(-) diff --git a/frontend/src/components/Sidebar.tsx b/frontend/src/components/Sidebar.tsx index dd3f404..ffda520 100644 --- a/frontend/src/components/Sidebar.tsx +++ b/frontend/src/components/Sidebar.tsx @@ -46,6 +46,7 @@ export const Sidebar: React.FC = ({ }) => { const tr = (k: I18nKey) => t(locale, k) const listRef = useRef(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. @@ -129,9 +130,26 @@ export const Sidebar: React.FC = ({ if (!onVMContextAction) return trigger return ( - {trigger} + { + ctxOpenedByPointerRef.current = e.button === 2 && (e.clientX !== 0 || e.clientY !== 0) + }} + > + {trigger} + - + { + if ( + ctxOpenedByPointerRef.current + || document.querySelector('[role="dialog"], [role="alertdialog"]') + ) { + e.preventDefault() + } + }} + > {stopped && ( onVMContextAction(vm.name, 'start')}> {tr('start')} diff --git a/frontend/src/components/TitleMenus.tsx b/frontend/src/components/TitleMenus.tsx index a0fee22..7e6d7d5 100644 --- a/frontend/src/components/TitleMenus.tsx +++ b/frontend/src/components/TitleMenus.tsx @@ -1,6 +1,6 @@ import * as Menu from '@radix-ui/react-dropdown-menu' import { Check, PanelLeft } from 'lucide-react' -import React, { useState } from 'react' +import React, { useRef, useState } from 'react' import { I18nKey, t } from '../i18n' import { Locale } from '../types' import { Tip } from '../ui/controls' @@ -10,6 +10,11 @@ import { VMAction } from './VMPage' export const REPO_URL = 'https://github.com/Nwflower/Reims-Go' +const MENU_IDS = ['vm', 'view', 'help'] as const +type MenuId = (typeof MENU_IDS)[number] + +const HOVER_CLICK_GUARD_MS = 300 + interface Props { locale: Locale sidebarOpen: boolean @@ -42,7 +47,17 @@ export const TitleMenus: React.FC = ({ vm, onNewVM, onVMAction, onVMSettings, onVMDelete, onNavigate, onOpenURL, appVersion, }) => { const tr = (k: I18nKey, v?: Record) => t(locale, k, v) - const [open, setOpen] = useState(null) + const [menuState, setMenuState] = useState<{ id: MenuId | null; switched: boolean }>({ id: null, switched: false }) + const openRef = useRef(null) + const navRef = useRef(null) + const hoverOpenedRef = useRef<{ id: MenuId; at: number } | null>(null) + const openedByPointerRef = useRef(false) + const pointerClosedRef = useRef(false) + + const setActiveMenu = (next: MenuId | null, switched: boolean) => { + openRef.current = next + setMenuState({ id: next, switched }) + } const running = vm?.state === 'running' const stopped = vm?.state === 'stopped' @@ -64,19 +79,111 @@ export const TitleMenus: React.FC = ({ ) // `indented`: the menu has check marks, so every row shares their indent. - const menu = (id: string, label: string, content: React.ReactNode, indented = false) => ( - setOpen(o ? id : null)}> + const menu = (id: MenuId, label: string, content: React.ReactNode, indented = false) => ( + { + if (o) { + hoverOpenedRef.current = null + pointerClosedRef.current = false + setActiveMenu(id, openRef.current !== null && openRef.current !== id) + } else if (openRef.current === id) { + hoverOpenedRef.current = null + setActiveMenu(null, false) + } + }} + > - + { + if (!(e.target as HTMLElement | null)?.closest('[data-disabled]')) { + pointerClosedRef.current = true + } + }} + onKeyDownCapture={() => { + pointerClosedRef.current = false + }} + onPointerDownOutside={(e) => { + const target = e.target as Node | null + if (target && navRef.current?.contains(target)) { + e.preventDefault() + return + } + pointerClosedRef.current = true + }} + onCloseAutoFocus={(e) => { + // Prevent Radix's deferred setTimeout(0) focus restoration from pulling focus + // back to the old trigger when switching to a sibling menu (which triggers + // onFocusOutside on the newly opened menu), when closed via pointer, or when + // an item opens a modal dialog. + if ( + openRef.current !== null + || openedByPointerRef.current + || pointerClosedRef.current + || document.querySelector('[role="dialog"], [role="alertdialog"]') + ) { + e.preventDefault() + } + }} + onKeyDown={(e) => { + if (e.key !== 'ArrowLeft' && e.key !== 'ArrowRight') return + e.preventDefault() + const isRTL = navRef.current ? getComputedStyle(navRef.current).direction === 'rtl' : false + const delta = (e.key === 'ArrowRight') !== isRTL ? 1 : -1 + const idx = MENU_IDS.indexOf(id) + const nextId = MENU_IDS[(idx + delta + MENU_IDS.length) % MENU_IDS.length] + hoverOpenedRef.current = null + openedByPointerRef.current = false + pointerClosedRef.current = false + setActiveMenu(nextId, true) + }} + > {content} @@ -85,7 +192,7 @@ export const TitleMenus: React.FC = ({ return (
e.stopPropagation()}> - + -