irox_egui_extras/
styles.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors

//!
//! This module has extras around the [`egui::style`] module
//!

use std::sync::Arc;

use eframe::{CreationContext, Frame, Storage};
use egui::{Context, Style};

///
/// Implementation of `eframe::App` that automatically saves the state of the
/// `egui::Style` when it changes in the background.
pub struct StylePersistingApp {
    style: Arc<Style>,
}

impl StylePersistingApp {
    #[must_use]
    pub fn new(cc: &CreationContext) -> StylePersistingApp {
        let mut style = cc.egui_ctx.style().clone();

        if let Some(storage) = cc.storage {
            if let Some(style_str) = storage.get_string("style") {
                if let Ok(parsed) = ron::from_str::<Style>(style_str.as_str()) {
                    cc.egui_ctx.set_style(parsed);
                    style = cc.egui_ctx.style();
                }
            }
        }
        StylePersistingApp { style }
    }
}

impl eframe::App for StylePersistingApp {
    fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
        self.style = ctx.style();
    }

    fn save(&mut self, storage: &mut dyn Storage) {
        if let Ok(enc) = ron::to_string(&self.style) {
            storage.set_string("style", enc);
        }
    }
}