use egui::{Color32, Context, RichText, Theme, Ui, Visuals}; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] #[serde(default)] pub struct Preferences { /// Enable animations pub animations: bool, /// Enable high-contrast theme pub high_contrast: bool, /// Hide the cursor when handwriting pub hide_handwriting_cursor: bool, #[serde(skip)] has_applied_theme: bool, } impl Default for Preferences { fn default() -> Self { Self { animations: true, high_contrast: false, has_applied_theme: false, hide_handwriting_cursor: false, } } } impl Preferences { /// Apply preferences, if they haven't already been applied. pub fn apply(&mut self, ctx: &Context) { if !self.has_applied_theme { self.has_applied_theme = true; if self.high_contrast { // widgets.active: color of headers in textedit // widgets.inactive: color of button labels // widgets.hovered: color of hovered button labels // widgets.noninteractive: color of labels and normal textedit text let mut dark_visuals = Visuals::dark(); let mut light_visuals = Visuals::light(); dark_visuals.widgets.noninteractive.fg_stroke.color = Color32::WHITE; dark_visuals.widgets.inactive.fg_stroke.color = Color32::WHITE; dark_visuals.widgets.hovered.fg_stroke.color = Color32::WHITE; light_visuals.widgets.noninteractive.fg_stroke.color = Color32::BLACK; light_visuals.widgets.inactive.fg_stroke.color = Color32::BLACK; light_visuals.widgets.hovered.fg_stroke.color = Color32::BLACK; ctx.set_visuals_of(Theme::Dark, dark_visuals); ctx.set_visuals_of(Theme::Light, light_visuals); } else { ctx.set_visuals_of(Theme::Dark, Visuals::dark()); ctx.set_visuals_of(Theme::Light, Visuals::light()); } } } /// Show preference switches pub fn show(&mut self, ui: &mut Ui) { ui.label(RichText::new("Prefs").weak()); ui.toggle_value(&mut self.animations, "Animations"); let high_contrast_toggle = ui.toggle_value(&mut self.high_contrast, "High Contrast"); if high_contrast_toggle.clicked() { self.has_applied_theme = false; self.apply(ui.ctx()); } ui.toggle_value(&mut self.hide_handwriting_cursor, "Hide Handwriting Cursor"); egui::widgets::global_theme_preference_buttons(ui); } }