Skip to main content

ferrix_app/
settings.rs

1/* settings.rs
2 *
3 * Copyright 2025 Michail Krasnov <mskrasnov07@ya.ru>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21use anyhow::Result;
22use iced::{Theme, color};
23use serde::{Deserialize, Serialize};
24use std::{fmt::Display, fs, path::Path};
25
26use crate::fl;
27
28#[derive(Debug, Clone, Deserialize, Serialize)]
29pub struct FXSettings {
30    pub update_period: u8,
31    pub style: Style,
32}
33
34impl FXSettings {
35    pub fn read<P: AsRef<Path>>(pth: P) -> Result<Self> {
36        let contents = fs::read_to_string(pth)?;
37        let data = toml::from_str(&contents)?;
38        Ok(data)
39    }
40
41    pub fn write<'a, P: AsRef<Path>>(&'a self, pth: P) -> Result<()> {
42        let contents = toml::to_string(&self)?;
43        fs::write(pth, contents)?;
44        Ok(())
45    }
46}
47
48impl Default for FXSettings {
49    fn default() -> Self {
50        Self {
51            update_period: 1,
52            style: Style::default(),
53        }
54    }
55}
56
57#[derive(Debug, Clone, Copy, Deserialize, Serialize, Default, PartialEq)]
58pub enum Style {
59    Light,
60    #[default]
61    Dark,
62}
63
64impl Style {
65    pub const ALL: &[Self] = &[Self::Light, Self::Dark];
66
67    pub fn to_theme(&self) -> Theme {
68        match self {
69            Self::Light => {
70                let mut palette = Theme::GruvboxLight.palette();
71                palette.success = color!(0x98971a);
72                palette.danger = color!(0xaf3a03);
73                palette.warning = color!(0xb57614);
74                palette.primary = color!(0xd79921);
75                palette.background = color!(0xebdbb2);
76
77                Theme::custom("Ferrix Light Theme", palette)
78            }
79            Self::Dark => {
80                let mut palette = Theme::GruvboxDark.palette();
81                palette.success = color!(0x98971a);
82                palette.danger = color!(0xfb4934);
83                palette.warning = color!(0xfabd2f);
84                palette.primary = color!(0xfabd2f);
85
86                Theme::custom("Ferrix Dark Theme", palette)
87            }
88        }
89    }
90}
91
92impl Display for Style {
93    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94        write!(
95            f,
96            "{}",
97            match self {
98                Self::Light => fl!("style-light"),
99                Self::Dark => fl!("style-dark"),
100            }
101        )
102    }
103}