persist viewMode across restarts

This commit is contained in:
Eduard Gert
2026-05-27 15:52:15 +02:00
parent a8ad73d2d9
commit ec5da43d73
4 changed files with 34 additions and 4 deletions
+24 -1
View File
@@ -1,5 +1,7 @@
import { createContext, useCallback, useContext, useState, type ReactNode } from "react";
import { createContext, useCallback, useContext, useEffect, useState, type ReactNode } from "react";
import { Window } from "@wailsio/runtime";
import { Preferences } from "@bindings/services";
import { ViewMode as ViewModePref } from "@bindings/preferences/models.js";
export type ViewMode = "default" | "advanced";
@@ -20,12 +22,33 @@ const ViewModeContext = createContext<ViewModeContextValue | null>(null);
export const ViewModeProvider = ({ children }: { children: ReactNode }) => {
const [viewMode, setMode] = useState<ViewMode>("default");
// Hydrate from the persisted preference. The Go side has already sized
// the main window to match (see main.go), so this only catches the
// React state and dropdown checkmark up — no resize is triggered here.
useEffect(() => {
let cancelled = false;
void Preferences.Get()
.then((prefs) => {
if (cancelled) return;
const saved = prefs?.viewMode as ViewMode | undefined;
if (saved === "default" || saved === "advanced") {
setMode(saved);
}
})
.catch(() => {});
return () => {
cancelled = true;
};
}, []);
const setViewMode = useCallback(
(mode: ViewMode) => {
setMode((prev) => {
if (prev === mode) return prev;
const { width, height } = VIEW_SIZE[mode];
void Window.SetSize(width, height).catch(() => {});
void Preferences.SetViewMode(mode as unknown as ViewModePref).catch(() => {});
return mode;
});
},