fixed settings panel showing default settings on load

This commit is contained in:
Vomitblood 2024-08-12 20:55:26 +08:00
parent 166b90444d
commit 063b27a7eb
6 changed files with 34 additions and 16 deletions

View file

@ -1,11 +1,15 @@
import { Box, Stack } from "@mui/material"; import { Box, Stack, useTheme } from "@mui/material";
import { useSettings } from "../../contexts/SettingsContext"; import { useSettings } from "../../contexts/SettingsContext";
import { Settings } from "../HeaderBar/Settings/Settings"; import { Settings } from "../HeaderBar/Settings/Settings";
import { WindowButtons } from "../HeaderBar/WindowButtons"; import { WindowButtons } from "../HeaderBar/WindowButtons";
import { hexToRgba } from "../../lib/utils/color";
export const FooterBar = () => { export const FooterBar = () => {
// contexts // contexts
const { settings } = useSettings(); const { settings } = useSettings();
const theme = useTheme();
const { r, g, b, a } = hexToRgba(theme.palette.background.paper);
return ( return (
<Box <Box
@ -14,7 +18,7 @@ export const FooterBar = () => {
display: "flex", display: "flex",
flexDirection: "row", flexDirection: "row",
height: "66px", height: "66px",
zIndex: 1000000, // zIndex: 1000000,
}} }}
> >
<Box <Box
@ -22,7 +26,7 @@ export const FooterBar = () => {
data-tauri-drag-region="true" data-tauri-drag-region="true"
sx={{ sx={{
alignItems: "center", alignItems: "center",
backdropFilter: "blur(10px)", backdropFilter: `blur(${settings.style.blur_radius}px)`,
backgroundColor: "rgba(0, 0, 0, 0.5)", backgroundColor: "rgba(0, 0, 0, 0.5)",
borderRadius: settings.style.radius + "px", borderRadius: settings.style.radius + "px",
display: "flex", display: "flex",

View file

@ -24,7 +24,6 @@ export const Layout = () => {
return ( return (
<Box <Box
sx={{ sx={{
// Use the URL function for background images
backgroundColor: theme.palette.background.default, backgroundColor: theme.palette.background.default,
backgroundImage: settings.background.background_image_path ? `url(${imageUrl})` : "", backgroundImage: settings.background.background_image_path ? `url(${imageUrl})` : "",
backgroundSize: "cover", backgroundSize: "cover",
@ -44,13 +43,13 @@ export const Layout = () => {
}} }}
> >
<Box> <Box>
<Button <button
onClick={() => { onClick={() => {
console.log(imageUrl); console.log(settings);
}} }}
> >
set background testing
</Button> </button>
</Box> </Box>
</Box> </Box>
<FooterBar /> <FooterBar />

View file

@ -8,21 +8,19 @@ import {
import { TabContext, TabList, TabPanel } from "@mui/lab"; import { TabContext, TabList, TabPanel } from "@mui/lab";
import { Box, Button, IconButton, Tab, Tooltip, useTheme } from "@mui/material"; import { Box, Button, IconButton, Tab, Tooltip, useTheme } from "@mui/material";
import { useAtom } from "jotai"; import { useAtom } from "jotai";
import { useRouter } from "next/router";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useSettings } from "../../../contexts/SettingsContext"; import { useSettings } from "../../../contexts/SettingsContext";
import { stagedSettingsAtom } from "../../../lib/store/jotai/settings"; import { stagedSettingsAtom } from "../../../lib/store/jotai/settings";
import { FloatingDialog } from "../../Generic/FloatingDialog"; import { FloatingDialog } from "../../Generic/FloatingDialog";
import { Background } from "./SettingsTabs/Background"; import { Background } from "./SettingsTabs/Background";
import { Debug } from "./SettingsTabs/Debug";
import { Style } from "./SettingsTabs/Style"; import { Style } from "./SettingsTabs/Style";
import { Window } from "./SettingsTabs/Window"; import { Window } from "./SettingsTabs/Window";
import { Debug } from "./SettingsTabs/Debug";
export const Settings = () => { export const Settings = () => {
// contexts // contexts
const theme = useTheme(); const theme = useTheme();
const { settings, updateSettings } = useSettings(); const { fetchSettings, settings, updateSettings } = useSettings();
const router = useRouter();
// atoms // atoms
const [stagedSettings, setStagedSettings] = useAtom(stagedSettingsAtom); const [stagedSettings, setStagedSettings] = useAtom(stagedSettingsAtom);
@ -63,6 +61,7 @@ export const Settings = () => {
// set staged settings back to current settings on close // set staged settings back to current settings on close
useEffect(() => { useEffect(() => {
if (settingsOpenState) fetchSettings();
if (!settingsOpenState) setStagedSettings(settings); if (!settingsOpenState) setStagedSettings(settings);
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [settingsOpenState]); }, [settingsOpenState]);

View file

@ -27,7 +27,6 @@ export const Background: FC<BackgroundProps> = ({ sx }) => {
const [oldWallpaperPath, setOldWallpaperPath] = useState<string | null>(null); const [oldWallpaperPath, setOldWallpaperPath] = useState<string | null>(null);
const [targetWallpaperPath, setTargetWallpaperPath] = useState<string | null>(null); const [targetWallpaperPath, setTargetWallpaperPath] = useState<string | null>(null);
const [imageBlob, setImageBlob] = useState<string | null>(null); const [imageBlob, setImageBlob] = useState<string | null>(null);
const [noImageSelectedIcon, setNoImageSelectedIcon] = useState<string | null>(null);
const handleSettingsBackgroundValueChange = ( const handleSettingsBackgroundValueChange = (
settingKey: string, settingKey: string,

View file

@ -2,6 +2,9 @@ import { Box, Typography } from "@mui/material";
import { FC } from "react"; import { FC } from "react";
import { useSettings } from "../../../../contexts/SettingsContext"; import { useSettings } from "../../../../contexts/SettingsContext";
import { CategoryTitle } from "../CategoryTitle"; import { CategoryTitle } from "../CategoryTitle";
import { stagedSettingsAtom } from "../../../../lib/store/jotai/settings";
import { useAtom } from "jotai";
import { defaultSettings } from "../../../../lib/settings";
interface DebugProps { interface DebugProps {
sx?: any; sx?: any;
@ -11,6 +14,9 @@ export const Debug: FC<DebugProps> = ({ sx }) => {
// contexts // contexts
const { settings, resetSettings } = useSettings(); const { settings, resetSettings } = useSettings();
// atoms
const [stagedSettings, setStagedSettings] = useAtom(stagedSettingsAtom);
return ( return (
<Box sx={{ sx }}> <Box sx={{ sx }}>
<CategoryTitle title="Debug Panel" /> <CategoryTitle title="Debug Panel" />
@ -22,7 +28,7 @@ export const Debug: FC<DebugProps> = ({ sx }) => {
</Typography> </Typography>
<button <button
onClick={() => { onClick={() => {
resetSettings(); setStagedSettings(defaultSettings);
}} }}
> >
reset settings reset settings

View file

@ -1,13 +1,16 @@
import { createContext, FC, ReactNode, useContext, useEffect, useState } from "react"; import { createContext, FC, ReactNode, useContext, useEffect, useState } from "react";
import { logcat } from "../lib/logcatService"; import { logcat } from "../lib/logcatService";
import { defaultSettings, readConfigFile, SettingsType, writeConfigFile } from "../lib/settings"; import { defaultSettings, readConfigFile, SettingsType, writeConfigFile } from "../lib/settings";
import { useAtom } from "jotai";
import { stagedSettingsAtom } from "../lib/store/jotai/settings";
// settings context // settings context
type SettingsContextProps = { type SettingsContextProps = {
fetchSettings: () => void;
resetSettings: () => void;
settings: SettingsType; settings: SettingsType;
settingsLoading: boolean; settingsLoading: boolean;
updateSettings: (updates: SettingsType) => void; updateSettings: (updates: SettingsType) => void;
resetSettings: () => void;
}; };
const SettingsContext = createContext<SettingsContextProps | undefined>(undefined); const SettingsContext = createContext<SettingsContextProps | undefined>(undefined);
@ -15,6 +18,9 @@ const SettingsContext = createContext<SettingsContextProps | undefined>(undefine
export const SettingsProvider: FC<{ children: ReactNode }> = ({ children }) => { export const SettingsProvider: FC<{ children: ReactNode }> = ({ children }) => {
logcat.log("Initializing settings...", "INFO"); logcat.log("Initializing settings...", "INFO");
// atoms
const [stagedSettings, setStagedSettings] = useAtom(stagedSettingsAtom);
// states // states
const [settings, setSettings] = useState<SettingsType>(defaultSettings); const [settings, setSettings] = useState<SettingsType>(defaultSettings);
const [settingsLoading, setSettingsLoading] = useState<boolean>(true); const [settingsLoading, setSettingsLoading] = useState<boolean>(true);
@ -22,7 +28,11 @@ export const SettingsProvider: FC<{ children: ReactNode }> = ({ children }) => {
const fetchSettings = async () => { const fetchSettings = async () => {
try { try {
const existingSettings = await readConfigFile(); const existingSettings = await readConfigFile();
// set settings state to existing settings
setSettings(existingSettings); setSettings(existingSettings);
// also update the settings atom
setStagedSettings(existingSettings);
logcat.log("Settings loaded successfully", "INFO");
} catch (error) { } catch (error) {
logcat.log(`Failed to load settings: ${error}`, "ERROR"); logcat.log(`Failed to load settings: ${error}`, "ERROR");
} finally { } finally {
@ -60,10 +70,11 @@ export const SettingsProvider: FC<{ children: ReactNode }> = ({ children }) => {
return ( return (
<SettingsContext.Provider <SettingsContext.Provider
value={{ value={{
fetchSettings,
resetSettings,
settings, settings,
settingsLoading, settingsLoading,
updateSettings, updateSettings,
resetSettings,
}} }}
> >
{children} {children}