1use 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
76 Theme::custom("Ferrix Light Theme", palette)
77 }
78 Self::Dark => {
79 let mut palette = Theme::GruvboxDark.palette();
80 palette.success = color!(0x98971a);
81 palette.danger = color!(0xfb4934);
82 palette.warning = color!(0xfabd2f);
83 palette.primary = color!(0xfabd2f);
84
85 Theme::custom("Ferrix Dark Theme", palette)
86 }
87 }
88 }
89}
90
91impl Display for Style {
92 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93 write!(
94 f,
95 "{}",
96 match self {
97 Self::Light => fl!("style-light"),
98 Self::Dark => fl!("style-dark"),
99 }
100 )
101 }
102}