diff --git a/emigui/src/context.rs b/emigui/src/context.rs index 8a693270..68723de2 100644 --- a/emigui/src/context.rs +++ b/emigui/src/context.rs @@ -26,8 +26,8 @@ pub struct Context { memory: Arc>, // Input releated stuff: - /// Raw input from last frame. Use `input()` instead. - last_raw_input: RawInput, + raw_input: RawInput, + previus_input: GuiInput, input: GuiInput, mouse_tracker: MovementTracker, @@ -49,7 +49,8 @@ impl Clone for Context { fonts: self.fonts.clone(), new_fonts: Mutex::new(self.new_fonts.lock().clone()), memory: self.memory.clone(), - last_raw_input: self.last_raw_input.clone(), + raw_input: self.raw_input.clone(), + previus_input: self.previus_input.clone(), input: self.input.clone(), mouse_tracker: self.mouse_tracker.clone(), graphics: Mutex::new(self.graphics.lock().clone()), @@ -69,7 +70,8 @@ impl Context { new_fonts: Default::default(), memory: Default::default(), - last_raw_input: Default::default(), + raw_input: Default::default(), + previus_input: Default::default(), input: Default::default(), mouse_tracker: MovementTracker::new(1000, 0.1), @@ -96,6 +98,11 @@ impl Context { self.output.lock() } + /// Input previous frame. Compare to `input()` to check for changes. + pub fn previus_input(&self) -> &GuiInput { + &self.previus_input + } + pub fn input(&self) -> &GuiInput { &self.input } @@ -105,11 +112,6 @@ impl Context { self.mouse_tracker.velocity().unwrap_or_default() } - /// Raw input from last frame. Use `input()` instead. - pub fn last_raw_input(&self) -> &RawInput { - &self.last_raw_input - } - pub fn fonts(&self) -> &Fonts { &*self.fonts } @@ -157,8 +159,8 @@ impl Context { *self = Arc::new(self_); } - fn begin_frame_mut(&mut self, new_input: RawInput) { - if !self.last_raw_input.mouse_down || self.last_raw_input.mouse_pos.is_none() { + fn begin_frame_mut(&mut self, new_raw_input: RawInput) { + if !self.raw_input.mouse_down || self.raw_input.mouse_pos.is_none() { self.memory().active_id = None; } @@ -168,14 +170,15 @@ impl Context { self.fonts = new_fonts; } - if let Some(mouse_pos) = new_input.mouse_pos { - self.mouse_tracker.add(new_input.time, mouse_pos); + if let Some(mouse_pos) = new_raw_input.mouse_pos { + self.mouse_tracker.add(new_raw_input.time, mouse_pos); } else { self.mouse_tracker.clear(); } - self.input = GuiInput::from_last_and_new(&self.last_raw_input, &new_input); + let new_input = GuiInput::from_last_and_new(&self.raw_input, &new_raw_input); + self.previus_input = std::mem::replace(&mut self.input, new_input); self.input.mouse_velocity = self.mouse_vel(); - self.last_raw_input = new_input; + self.raw_input = new_raw_input; } pub fn end_frame(&self) -> (Output, PaintBatches) { @@ -480,9 +483,7 @@ impl Context { use crate::containers::*; ui.collapsing("Input", |ui| { - CollapsingHeader::new("Raw Input") - .default_open() - .show(ui, |ui| ui.ctx().last_raw_input().clone().ui(ui)); + CollapsingHeader::new("Raw Input").show(ui, |ui| ui.ctx().raw_input.clone().ui(ui)); CollapsingHeader::new("Input") .default_open() .show(ui, |ui| ui.input().clone().ui(ui)); diff --git a/emigui/src/examples/app.rs b/emigui/src/examples/app.rs index bac95b97..b0ba5ab1 100644 --- a/emigui/src/examples/app.rs +++ b/emigui/src/examples/app.rs @@ -8,8 +8,8 @@ use crate::{color::*, containers::*, examples::FractalClock, widgets::*, *}; // ---------------------------------------------------------------------------- #[derive(Default, Deserialize, Serialize)] +#[serde(default)] pub struct ExampleApp { - has_initialized: bool, example_window: ExampleWindow, open_windows: OpenWindows, fractal_clock: FractalClock, @@ -28,23 +28,25 @@ impl ExampleApp { // TODO: window manager for automatic positioning? let ExampleApp { - has_initialized, example_window, open_windows, fractal_clock, } = self; - if !*has_initialized { - // #fragment end of URL: + if ctx.previus_input().web != ctx.input().web { let location_hash = ctx .input() .web .as_ref() .map(|web| web.location_hash.as_str()); + + // #fragment end of URL: if location_hash == Some("#clock") { - open_windows.fractal_clock = true; + *open_windows = OpenWindows { + fractal_clock: true, + ..OpenWindows::none() + }; } - *has_initialized = true; } Window::new("Examples") @@ -96,6 +98,15 @@ impl Default for OpenWindows { fn default() -> Self { Self { examples: true, + ..OpenWindows::none() + } + } +} + +impl OpenWindows { + fn none() -> Self { + Self { + examples: false, settings: false, inspection: false, memory: false,