Skip to content

Commit 8859f39

Browse files
committed
fix: font size setting input
1 parent 579bcbf commit 8859f39

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

gui/src/context/LocalStorage.tsx

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,56 @@ export const LocalStorageProvider: React.FC<{ children: React.ReactNode }> = ({
1818
}) => {
1919
const [values, setValues] = useState<LocalStorageType>(DEFAULT_LOCAL_STORAGE);
2020

21-
// TODO setvalue
22-
useEffect(() => {
21+
// Helper function to sync state with localStorage
22+
const syncWithLocalStorage = () => {
2323
const isJetbrains = getLocalStorage("ide") === "jetbrains";
24-
let fontSize = getLocalStorage("fontSize") ?? (isJetbrains ? 15 : 14);
25-
setValues({
24+
const fontSize = getLocalStorage("fontSize") ?? (isJetbrains ? 15 : 14);
25+
26+
setValues((prev) => ({
27+
...prev,
2628
fontSize,
27-
});
29+
}));
30+
};
31+
32+
// Initialize with values from localStorage
33+
useEffect(() => {
34+
syncWithLocalStorage();
35+
}, []);
36+
37+
// Listen for localStorage changes from other tabs
38+
useEffect(() => {
39+
const handleStorageChange = (event: StorageEvent) => {
40+
if (event.key === "fontSize") {
41+
syncWithLocalStorage();
42+
}
43+
};
44+
45+
window.addEventListener("storage", handleStorageChange);
46+
47+
return () => {
48+
window.removeEventListener("storage", handleStorageChange);
49+
};
50+
}, []);
51+
52+
// Listen for current tab changes using CustomEvent
53+
useEffect(() => {
54+
const handleLocalStorageChange = (event: CustomEvent) => {
55+
if (event.detail?.key === "fontSize") {
56+
syncWithLocalStorage();
57+
}
58+
};
59+
60+
window.addEventListener(
61+
"localStorageChange",
62+
handleLocalStorageChange as EventListener,
63+
);
64+
65+
return () => {
66+
window.removeEventListener(
67+
"localStorageChange",
68+
handleLocalStorageChange as EventListener,
69+
);
70+
};
2871
}, []);
2972

3073
return (

gui/src/pages/config/components/UserSetting.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,40 @@ export function UserSetting(props: UserSettingProps) {
7979
<input
8080
type="number"
8181
value={props.value}
82-
onChange={(e) => props.onChange(Number(e.target.value))}
82+
onChange={(e) => {
83+
const value = Number(e.target.value);
84+
// Allow temporary invalid values during input
85+
props.onChange(value);
86+
}}
87+
onBlur={(e) => {
88+
// Apply min/max constraints when input loses focus
89+
const value = Number(e.target.value);
90+
const min = props.min ?? 0;
91+
const max = props.max ?? 100;
92+
93+
if (value < min) {
94+
props.onChange(min);
95+
} else if (value > max) {
96+
props.onChange(max);
97+
}
98+
}}
99+
onKeyDown={(e) => {
100+
// Apply constraints when user presses Enter
101+
if (e.key === "Enter") {
102+
const value = Number(e.currentTarget.value);
103+
const min = props.min ?? 0;
104+
const max = props.max ?? 100;
105+
106+
if (value < min) {
107+
props.onChange(min);
108+
} else if (value > max) {
109+
props.onChange(max);
110+
}
111+
112+
// Blur the input to complete the editing
113+
e.currentTarget.blur();
114+
}
115+
}}
83116
min={props.min ?? 0}
84117
max={props.max ?? 100}
85118
disabled={disabled}

gui/src/util/localStorage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@ export function setLocalStorage<T extends keyof LocalStorageTypes>(
4949
value: LocalStorageTypes[T],
5050
): void {
5151
localStorage.setItem(key, JSON.stringify(value));
52+
53+
// Dispatch custom event to notify current tab listeners
54+
window.dispatchEvent(
55+
new CustomEvent("localStorageChange", {
56+
detail: { key, value },
57+
}),
58+
);
5259
}

0 commit comments

Comments
 (0)