diff --git a/gui/src/context/LocalStorage.tsx b/gui/src/context/LocalStorage.tsx index 4e5df8c1b..bcda2698e 100644 --- a/gui/src/context/LocalStorage.tsx +++ b/gui/src/context/LocalStorage.tsx @@ -18,13 +18,41 @@ 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 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 }, + }), + ); }