|
| 1 | +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release |
| 2 | +#![expect(rustdoc::missing_crate_level_docs)] // it's an example |
| 3 | + |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +use eframe::{ |
| 7 | + Storage, StorageProviderBuild, |
| 8 | + egui::{self, ahash::HashMap}, |
| 9 | +}; |
| 10 | + |
| 11 | +fn main() -> eframe::Result { |
| 12 | + env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). |
| 13 | + let options = eframe::NativeOptions { |
| 14 | + viewport: egui::ViewportBuilder::default().with_inner_size([350.0, 590.0]), |
| 15 | + storage_build: StorageProviderBuild::Custom(custom_storage), |
| 16 | + ..Default::default() |
| 17 | + }; |
| 18 | + eframe::run_native( |
| 19 | + "egui example: custom style", |
| 20 | + options, |
| 21 | + Box::new(|cc| Ok(Box::new(MyApp::new(cc)))), |
| 22 | + ) |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Clone, serde::Serialize, serde::Deserialize)] |
| 26 | +struct MyApp { |
| 27 | + pub custom_data: String, |
| 28 | + pub custom_data2: String, |
| 29 | +} |
| 30 | + |
| 31 | +impl Default for MyApp { |
| 32 | + fn default() -> Self { |
| 33 | + Self { |
| 34 | + custom_data: "Hello".to_string(), |
| 35 | + custom_data2: "World".to_string(), |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl MyApp { |
| 41 | + fn new(cc: &eframe::CreationContext<'_>) -> Self { |
| 42 | + egui_extras::install_image_loaders(&cc.egui_ctx); // Needed for the "Widget Gallery" demo |
| 43 | + cc.storage |
| 44 | + .and_then(|storage| eframe::get_value(storage, "app")) |
| 45 | + .unwrap_or_default() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl eframe::App for MyApp { |
| 50 | + fn save(&mut self, storage: &mut dyn Storage) { |
| 51 | + eframe::set_value(storage, "app", &self); |
| 52 | + } |
| 53 | + |
| 54 | + fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { |
| 55 | + egui::CentralPanel::default().show_inside(ui, |ui| { |
| 56 | + ui.heading("egui using a custom storage for app"); |
| 57 | + ui.label("Change data and restart the app to see it."); |
| 58 | + ui.separator(); |
| 59 | + ui.text_edit_singleline(&mut self.custom_data); |
| 60 | + ui.text_edit_singleline(&mut self.custom_data2); |
| 61 | + }); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn custom_storage(_app_name: &str) -> Option<Box<dyn Storage>> { |
| 66 | + CustomStorageData::new( |
| 67 | + std::env::current_dir() |
| 68 | + .unwrap_or_default() |
| 69 | + .join("custom_storage.json"), |
| 70 | + ) |
| 71 | + .map(|data| Box::new(data) as Box<dyn Storage>) |
| 72 | +} |
| 73 | + |
| 74 | +#[derive(Default, Clone, serde::Serialize, serde::Deserialize)] |
| 75 | +pub struct CustomStorageData { |
| 76 | + hashmap: HashMap<String, String>, |
| 77 | + path: PathBuf, |
| 78 | +} |
| 79 | + |
| 80 | +impl CustomStorageData { |
| 81 | + pub fn new(path: PathBuf) -> Option<Self> { |
| 82 | + let hashmap: HashMap<String, String> = std::fs::read(&path) |
| 83 | + .ok() |
| 84 | + .and_then(|contents| serde_json::from_slice(contents.as_slice()).ok()) |
| 85 | + .unwrap_or_default(); |
| 86 | + |
| 87 | + Some(Self { hashmap, path }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl Storage for CustomStorageData { |
| 92 | + fn get_string(&self, key: &str) -> Option<String> { |
| 93 | + self.hashmap.get(key).cloned() |
| 94 | + } |
| 95 | + |
| 96 | + fn set_string(&mut self, key: &str, value: String) { |
| 97 | + self.hashmap.insert(key.to_string(), value); |
| 98 | + } |
| 99 | + |
| 100 | + fn flush(&mut self) { |
| 101 | + let Ok(content) = serde_json::to_string_pretty(&self.hashmap) else { |
| 102 | + return; |
| 103 | + }; |
| 104 | + _ = std::fs::write(&self.path, content); |
| 105 | + } |
| 106 | +} |
0 commit comments