From 8ccc36937fdf1553548d43118b5d9c101912c3b6 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 19 Oct 2020 20:25:05 +0200 Subject: [PATCH] [egui_glium] Add option not to persist app to file --- demo_glium/src/main.rs | 12 ++++++++---- egui/src/app.rs | 4 ++++ egui_glium/src/backend.rs | 24 +++++++++++++----------- egui_glium/src/storage.rs | 14 +++++++------- example_glium/src/main.rs | 11 ++++++++--- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/demo_glium/src/main.rs b/demo_glium/src/main.rs index ad1631e0..a523bcaf 100644 --- a/demo_glium/src/main.rs +++ b/demo_glium/src/main.rs @@ -1,11 +1,15 @@ #![deny(warnings)] #![warn(clippy::all)] -use egui_glium::storage::FileStorage; - fn main() { let title = "Egui glium demo"; - let storage = FileStorage::from_path(".egui_demo_glium.json".into()); + + // Persist app state to file: + let storage = egui_glium::storage::FileStorage::from_path(".egui_demo_glium.json".into()); + + // Alternative: store nowhere + // let storage = egui::app::DummyStorage::default(); + let app: egui::DemoApp = egui::app::get_value(&storage, egui::app::APP_KEY).unwrap_or_default(); - egui_glium::run(title, storage, app); + egui_glium::run(title, Box::new(storage), app); } diff --git a/egui/src/app.rs b/egui/src/app.rs index c3a39844..536e7043 100644 --- a/egui/src/app.rs +++ b/egui/src/app.rs @@ -75,6 +75,9 @@ pub trait TextureAllocator { pub trait Storage { fn get_string(&self, key: &str) -> Option<&str>; fn set_string(&mut self, key: &str, value: String); + + /// write-to-disk or similar + fn flush(&mut self); } /// Stores nothing. @@ -86,6 +89,7 @@ impl Storage for DummyStorage { None } fn set_string(&mut self, _key: &str, _value: String) {} + fn flush(&mut self) {} } #[cfg(feature = "serde_json")] diff --git a/egui_glium/src/backend.rs b/egui_glium/src/backend.rs index c9c42a57..c5eaabb1 100644 --- a/egui_glium/src/backend.rs +++ b/egui_glium/src/backend.rs @@ -1,9 +1,6 @@ use std::time::Instant; -use crate::{ - storage::{FileStorage, WindowSettings}, - *, -}; +use crate::{storage::WindowSettings, *}; pub use egui::{ app::{self, App, Storage}, @@ -24,7 +21,11 @@ impl egui::app::TextureAllocator for Painter { } /// Run an egui app -pub fn run(title: &str, mut storage: FileStorage, mut app: impl App + 'static) -> ! { +pub fn run( + title: &str, + mut storage: Box, + mut app: impl App + 'static, +) -> ! { let event_loop = glutin::event_loop::EventLoop::new(); let mut window = glutin::window::WindowBuilder::new() .with_decorations(true) @@ -32,7 +33,8 @@ pub fn run(title: &str, mut storage: FileStorage, mut app: impl App + 'static) - .with_title(title) .with_transparent(false); - let window_settings: Option = egui::app::get_value(&storage, WINDOW_KEY); + let window_settings: Option = + egui::app::get_value(storage.as_ref(), WINDOW_KEY); if let Some(window_settings) = &window_settings { window = window_settings.initialize_size(window); } @@ -49,7 +51,7 @@ pub fn run(title: &str, mut storage: FileStorage, mut app: impl App + 'static) - } let mut ctx = egui::Context::new(); - *ctx.memory() = egui::app::get_value(&storage, EGUI_MEMORY_KEY).unwrap_or_default(); + *ctx.memory() = egui::app::get_value(storage.as_ref(), EGUI_MEMORY_KEY).unwrap_or_default(); let mut raw_input = egui::RawInput { pixels_per_point: Some(native_pixels_per_point(&display)), @@ -119,13 +121,13 @@ pub fn run(title: &str, mut storage: FileStorage, mut app: impl App + 'static) - } glutin::event::Event::LoopDestroyed => { egui::app::set_value( - &mut storage, + storage.as_mut(), WINDOW_KEY, &WindowSettings::from_display(&display), ); - egui::app::set_value(&mut storage, EGUI_MEMORY_KEY, &*ctx.memory()); - app.on_exit(&mut storage); - storage.save(); + egui::app::set_value(storage.as_mut(), EGUI_MEMORY_KEY, &*ctx.memory()); + app.on_exit(storage.as_mut()); + storage.flush(); } _ => (), } diff --git a/egui_glium/src/storage.rs b/egui_glium/src/storage.rs index e23f07a0..0a99bc8f 100644 --- a/egui_glium/src/storage.rs +++ b/egui_glium/src/storage.rs @@ -18,13 +18,6 @@ impl FileStorage { dirty: false, } } - - pub fn save(&mut self) { - if self.dirty { - serde_json::to_writer(std::fs::File::create(&self.path).unwrap(), &self.kv).unwrap(); - self.dirty = false; - } - } } impl egui::app::Storage for FileStorage { @@ -38,6 +31,13 @@ impl egui::app::Storage for FileStorage { self.dirty = true; } } + + fn flush(&mut self) { + if self.dirty { + serde_json::to_writer(std::fs::File::create(&self.path).unwrap(), &self.kv).unwrap(); + self.dirty = false; + } + } } // ---------------------------------------------------------------------------- diff --git a/example_glium/src/main.rs b/example_glium/src/main.rs index 4dabbc0a..923acd26 100644 --- a/example_glium/src/main.rs +++ b/example_glium/src/main.rs @@ -4,7 +4,6 @@ #![warn(clippy::all)] use egui::{Slider, Window}; -use egui_glium::storage::FileStorage; /// We derive Deserialize/Serialize so we can persist app state on shutdown. #[derive(Default, serde::Deserialize, serde::Serialize)] @@ -44,9 +43,15 @@ impl egui::app::App for MyApp { fn main() { let title = "My Egui Window"; - let storage = FileStorage::from_path(".egui_example_glium.json".into()); // Where to persist app state + + // Persist app state to file: + let storage = egui_glium::storage::FileStorage::from_path(".egui_example_glium.json".into()); + + // Alternative: store nowhere + // let storage = egui::app::DummyStorage::default(); + let app: MyApp = egui::app::get_value(&storage, egui::app::APP_KEY).unwrap_or_default(); // Restore `MyApp` from file, or create new `MyApp`. - egui_glium::run(title, storage, app); + egui_glium::run(title, Box::new(storage), app); } fn my_save_function() {