From 8859f39820c5a6ff5cc676c6d5566cca3c7a1cbd Mon Sep 17 00:00:00 2001 From: Qian Date: Thu, 9 Oct 2025 17:04:21 +0800 Subject: [PATCH 1/2] fix: font size setting input --- gui/src/context/LocalStorage.tsx | 53 +++++++++++++++++-- .../pages/config/components/UserSetting.tsx | 35 +++++++++++- gui/src/util/localStorage.ts | 7 +++ 3 files changed, 89 insertions(+), 6 deletions(-) diff --git a/gui/src/context/LocalStorage.tsx b/gui/src/context/LocalStorage.tsx index 4e5df8c1b..d5864f895 100644 --- a/gui/src/context/LocalStorage.tsx +++ b/gui/src/context/LocalStorage.tsx @@ -18,13 +18,56 @@ export const LocalStorageProvider: React.FC<{ children: React.ReactNode }> = ({ }) => { const [values, setValues] = useState(DEFAULT_LOCAL_STORAGE); - // TODO setvalue - useEffect(() => { + // Helper function to sync state with localStorage + const syncWithLocalStorage = () => { const isJetbrains = getLocalStorage("ide") === "jetbrains"; - let fontSize = getLocalStorage("fontSize") ?? (isJetbrains ? 15 : 14); - setValues({ + const fontSize = getLocalStorage("fontSize") ?? (isJetbrains ? 15 : 14); + + setValues((prev) => ({ + ...prev, fontSize, - }); + })); + }; + + // Initialize with values from localStorage + useEffect(() => { + syncWithLocalStorage(); + }, []); + + // Listen for localStorage changes from other tabs + useEffect(() => { + const handleStorageChange = (event: StorageEvent) => { + if (event.key === "fontSize") { + syncWithLocalStorage(); + } + }; + + window.addEventListener("storage", handleStorageChange); + + return () => { + window.removeEventListener("storage", handleStorageChange); + }; + }, []); + + // Listen for current tab changes using CustomEvent + useEffect(() => { + const handleLocalStorageChange = (event: CustomEvent) => { + if (event.detail?.key === "fontSize") { + syncWithLocalStorage(); + } + }; + + window.addEventListener( + "localStorageChange", + handleLocalStorageChange as EventListener, + ); + + return () => { + window.removeEventListener( + "localStorageChange", + handleLocalStorageChange as EventListener, + ); + }; }, []); return ( diff --git a/gui/src/pages/config/components/UserSetting.tsx b/gui/src/pages/config/components/UserSetting.tsx index 522a55c58..8f49657da 100644 --- a/gui/src/pages/config/components/UserSetting.tsx +++ b/gui/src/pages/config/components/UserSetting.tsx @@ -79,7 +79,40 @@ export function UserSetting(props: UserSettingProps) { props.onChange(Number(e.target.value))} + onChange={(e) => { + const value = Number(e.target.value); + // Allow temporary invalid values during input + props.onChange(value); + }} + onBlur={(e) => { + // Apply min/max constraints when input loses focus + const value = Number(e.target.value); + const min = props.min ?? 0; + const max = props.max ?? 100; + + if (value < min) { + props.onChange(min); + } else if (value > max) { + props.onChange(max); + } + }} + onKeyDown={(e) => { + // Apply constraints when user presses Enter + if (e.key === "Enter") { + const value = Number(e.currentTarget.value); + const min = props.min ?? 0; + const max = props.max ?? 100; + + if (value < min) { + props.onChange(min); + } else if (value > max) { + props.onChange(max); + } + + // Blur the input to complete the editing + e.currentTarget.blur(); + } + }} min={props.min ?? 0} max={props.max ?? 100} disabled={disabled} diff --git a/gui/src/util/localStorage.ts b/gui/src/util/localStorage.ts index 182cf486d..cab68d7b6 100644 --- a/gui/src/util/localStorage.ts +++ b/gui/src/util/localStorage.ts @@ -49,4 +49,11 @@ export function setLocalStorage( value: LocalStorageTypes[T], ): void { localStorage.setItem(key, JSON.stringify(value)); + + // Dispatch custom event to notify current tab listeners + window.dispatchEvent( + new CustomEvent("localStorageChange", { + detail: { key, value }, + }), + ); } From d9f285c072482bf3f2551cb915e2c7d1fce7b4ee Mon Sep 17 00:00:00 2001 From: Qian Date: Fri, 10 Oct 2025 10:16:36 +0800 Subject: [PATCH 2/2] Remove cross-tab localStorage synchronization --- gui/src/context/LocalStorage.tsx | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/gui/src/context/LocalStorage.tsx b/gui/src/context/LocalStorage.tsx index d5864f895..bcda2698e 100644 --- a/gui/src/context/LocalStorage.tsx +++ b/gui/src/context/LocalStorage.tsx @@ -34,21 +34,6 @@ export const LocalStorageProvider: React.FC<{ children: React.ReactNode }> = ({ syncWithLocalStorage(); }, []); - // Listen for localStorage changes from other tabs - useEffect(() => { - const handleStorageChange = (event: StorageEvent) => { - if (event.key === "fontSize") { - syncWithLocalStorage(); - } - }; - - window.addEventListener("storage", handleStorageChange); - - return () => { - window.removeEventListener("storage", handleStorageChange); - }; - }, []); - // Listen for current tab changes using CustomEvent useEffect(() => { const handleLocalStorageChange = (event: CustomEvent) => {