From 4eebba0cf93e0a988d44db2e5990f04fa280760d Mon Sep 17 00:00:00 2001 From: wj-xiao Date: Sat, 31 Jan 2026 15:59:18 +0800 Subject: [PATCH] fix: menu bar position calculation incorrect --- browser/src/components/menu/index.tsx | 56 +++++----------- .../renderer/src/components/menu/index.tsx | 66 ++++++------------- 2 files changed, 36 insertions(+), 86 deletions(-) diff --git a/browser/src/components/menu/index.tsx b/browser/src/components/menu/index.tsx index cd170b7..6582ce7 100644 --- a/browser/src/components/menu/index.tsx +++ b/browser/src/components/menu/index.tsx @@ -21,65 +21,37 @@ export const Menu = () => { const [isMenuOpen, setIsMenuOpen] = useState(true); const [menuBounds, setMenuBounds] = useState({ left: 0, right: 0, top: 0, bottom: 0 }); - const [position, setPosition] = useState({ x: 0, y: 0 }); - const [initialized, setInitialized] = useState(false); const nodeRef = useRef(null); const handleResize = useCallback(() => { if (!nodeRef.current) return; - const { innerWidth, innerHeight } = window; - const { offsetWidth, offsetHeight } = nodeRef.current; + const elementRect = nodeRef.current.getBoundingClientRect(); + const width = (window.innerWidth - elementRect.width) / 2; setMenuBounds({ - left: 0, - top: 0, - right: innerWidth - offsetWidth, - bottom: innerHeight - offsetHeight + left: -width, + top: -10, + right: width, + bottom: window.innerHeight - elementRect.height - 10 }); }, []); - const initializePosition = useCallback(() => { - if (!nodeRef.current || initialized) return; - - const { innerWidth } = window; - - requestAnimationFrame(() => { - if (!nodeRef.current) return; - const width = nodeRef.current.offsetWidth; - setPosition({ - x: (innerWidth - width) / 2, - y: 10 - }); - setInitialized(true); - }); - }, [initialized]); - useEffect(() => { const isOpen = storage.getIsMenuOpen(); setIsMenuOpen(isOpen); window.addEventListener('resize', handleResize); - const observer = new ResizeObserver(() => { - handleResize(); - }); - - if (nodeRef.current) { - observer.observe(nodeRef.current); - } - return () => { window.removeEventListener('resize', handleResize); - observer.disconnect(); }; }, [handleResize]); useEffect(() => { handleResize(); - initializePosition(); - }, [isMenuOpen, handleResize, initializePosition]); + }, [isMenuOpen, serialState, handleResize]); function toggleMenu() { const isOpen = !isMenuOpen; @@ -93,10 +65,12 @@ export const Menu = () => { nodeRef={nodeRef} bounds={menuBounds} handle="strong" - position={position} - onDrag={(_, data) => setPosition({ x: data.x, y: data.y })} + positionOffset={{ x: '-50%', y: '0%' }} > -
+
{/* Menubar */}
{ )} > -
+
@@ -143,13 +117,13 @@ export const Menu = () => { {!isMenuOpen && (
-
+
diff --git a/desktop/src/renderer/src/components/menu/index.tsx b/desktop/src/renderer/src/components/menu/index.tsx index 7923635..98f6747 100644 --- a/desktop/src/renderer/src/components/menu/index.tsx +++ b/desktop/src/renderer/src/components/menu/index.tsx @@ -1,4 +1,4 @@ -import { ReactElement, useCallback, useEffect, useRef, useState } from 'react' +import { ReactElement, useEffect, useRef, useState } from 'react' import { Divider } from 'antd' import clsx from 'clsx' import { ChevronRightIcon, GripVerticalIcon, XIcon } from 'lucide-react' @@ -16,65 +16,39 @@ import { Video } from './video' export const Menu = (): ReactElement => { const [isMenuOpen, setIsMenuOpen] = useState(true) const [menuBounds, setMenuBounds] = useState({ left: 0, right: 0, top: 0, bottom: 0 }) - const [position, setPosition] = useState({ x: 0, y: 0 }) - const [initialized, setInitialized] = useState(false) const nodeRef = useRef(null) - const handleResize = useCallback(() => { + const handleResize = (): void => { if (!nodeRef.current) return - const { innerWidth, innerHeight } = window - const { offsetWidth, offsetHeight } = nodeRef.current + const elementRect = nodeRef.current.getBoundingClientRect() + const width = (window.innerWidth - elementRect.width) / 2 setMenuBounds({ - left: 0, - top: 0, - right: innerWidth - offsetWidth, - bottom: innerHeight - offsetHeight + left: -width, + top: -10, + right: width, + bottom: window.innerHeight - elementRect.height - 10 }) - }, []) - - const initializePosition = useCallback(() => { - if (!nodeRef.current || initialized) return - - const { innerWidth } = window - - requestAnimationFrame(() => { - if (!nodeRef.current) return - const width = nodeRef.current.offsetWidth - setPosition({ - x: (innerWidth - width) / 2, - y: 10 - }) - setInitialized(true) - }) - }, [initialized]) + } useEffect(() => { const isOpen = storage.getIsMenuOpen() setIsMenuOpen(isOpen) + handleResize() + window.addEventListener('resize', handleResize) - const observer = new ResizeObserver(() => { - handleResize() - }) - - if (nodeRef.current) { - observer.observe(nodeRef.current) - } - return () => { window.removeEventListener('resize', handleResize) - observer.disconnect() } - }, [handleResize]) + }, []) useEffect(() => { handleResize() - initializePosition() - }, [isMenuOpen, handleResize, initializePosition]) + }, [isMenuOpen]) function toggleMenu(): void { setIsMenuOpen(!isMenuOpen) @@ -85,10 +59,12 @@ export const Menu = (): ReactElement => { nodeRef={nodeRef} bounds={menuBounds} handle="strong" - position={position} - onDrag={(_, data) => setPosition({ x: data.x, y: data.y })} + positionOffset={{ x: '-50%', y: '0%' }} > -
+
{/* Menubar */}
{ )} > -
+
@@ -127,13 +103,13 @@ export const Menu = (): ReactElement => { {!isMenuOpen && (
-
+