Nicer handling of fragment links to #clock demo

This commit is contained in:
Emil Ernerfeldt 2020-05-12 17:09:54 +02:00
parent d508372334
commit 6f7bc3cfac
2 changed files with 36 additions and 24 deletions

View file

@ -26,8 +26,8 @@ pub struct Context {
memory: Arc<Mutex<Memory>>, memory: Arc<Mutex<Memory>>,
// Input releated stuff: // Input releated stuff:
/// Raw input from last frame. Use `input()` instead. raw_input: RawInput,
last_raw_input: RawInput, previus_input: GuiInput,
input: GuiInput, input: GuiInput,
mouse_tracker: MovementTracker<Pos2>, mouse_tracker: MovementTracker<Pos2>,
@ -49,7 +49,8 @@ impl Clone for Context {
fonts: self.fonts.clone(), fonts: self.fonts.clone(),
new_fonts: Mutex::new(self.new_fonts.lock().clone()), new_fonts: Mutex::new(self.new_fonts.lock().clone()),
memory: self.memory.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(), input: self.input.clone(),
mouse_tracker: self.mouse_tracker.clone(), mouse_tracker: self.mouse_tracker.clone(),
graphics: Mutex::new(self.graphics.lock().clone()), graphics: Mutex::new(self.graphics.lock().clone()),
@ -69,7 +70,8 @@ impl Context {
new_fonts: Default::default(), new_fonts: Default::default(),
memory: Default::default(), memory: Default::default(),
last_raw_input: Default::default(), raw_input: Default::default(),
previus_input: Default::default(),
input: Default::default(), input: Default::default(),
mouse_tracker: MovementTracker::new(1000, 0.1), mouse_tracker: MovementTracker::new(1000, 0.1),
@ -96,6 +98,11 @@ impl Context {
self.output.lock() 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 { pub fn input(&self) -> &GuiInput {
&self.input &self.input
} }
@ -105,11 +112,6 @@ impl Context {
self.mouse_tracker.velocity().unwrap_or_default() 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 { pub fn fonts(&self) -> &Fonts {
&*self.fonts &*self.fonts
} }
@ -157,8 +159,8 @@ impl Context {
*self = Arc::new(self_); *self = Arc::new(self_);
} }
fn begin_frame_mut(&mut self, new_input: RawInput) { fn begin_frame_mut(&mut self, new_raw_input: RawInput) {
if !self.last_raw_input.mouse_down || self.last_raw_input.mouse_pos.is_none() { if !self.raw_input.mouse_down || self.raw_input.mouse_pos.is_none() {
self.memory().active_id = None; self.memory().active_id = None;
} }
@ -168,14 +170,15 @@ impl Context {
self.fonts = new_fonts; self.fonts = new_fonts;
} }
if let Some(mouse_pos) = new_input.mouse_pos { if let Some(mouse_pos) = new_raw_input.mouse_pos {
self.mouse_tracker.add(new_input.time, mouse_pos); self.mouse_tracker.add(new_raw_input.time, mouse_pos);
} else { } else {
self.mouse_tracker.clear(); 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.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) { pub fn end_frame(&self) -> (Output, PaintBatches) {
@ -480,9 +483,7 @@ impl Context {
use crate::containers::*; use crate::containers::*;
ui.collapsing("Input", |ui| { ui.collapsing("Input", |ui| {
CollapsingHeader::new("Raw Input") CollapsingHeader::new("Raw Input").show(ui, |ui| ui.ctx().raw_input.clone().ui(ui));
.default_open()
.show(ui, |ui| ui.ctx().last_raw_input().clone().ui(ui));
CollapsingHeader::new("Input") CollapsingHeader::new("Input")
.default_open() .default_open()
.show(ui, |ui| ui.input().clone().ui(ui)); .show(ui, |ui| ui.input().clone().ui(ui));

View file

@ -8,8 +8,8 @@ use crate::{color::*, containers::*, examples::FractalClock, widgets::*, *};
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[derive(Default, Deserialize, Serialize)] #[derive(Default, Deserialize, Serialize)]
#[serde(default)]
pub struct ExampleApp { pub struct ExampleApp {
has_initialized: bool,
example_window: ExampleWindow, example_window: ExampleWindow,
open_windows: OpenWindows, open_windows: OpenWindows,
fractal_clock: FractalClock, fractal_clock: FractalClock,
@ -28,23 +28,25 @@ impl ExampleApp {
// TODO: window manager for automatic positioning? // TODO: window manager for automatic positioning?
let ExampleApp { let ExampleApp {
has_initialized,
example_window, example_window,
open_windows, open_windows,
fractal_clock, fractal_clock,
} = self; } = self;
if !*has_initialized { if ctx.previus_input().web != ctx.input().web {
// #fragment end of URL:
let location_hash = ctx let location_hash = ctx
.input() .input()
.web .web
.as_ref() .as_ref()
.map(|web| web.location_hash.as_str()); .map(|web| web.location_hash.as_str());
// #fragment end of URL:
if location_hash == Some("#clock") { if location_hash == Some("#clock") {
open_windows.fractal_clock = true; *open_windows = OpenWindows {
fractal_clock: true,
..OpenWindows::none()
};
} }
*has_initialized = true;
} }
Window::new("Examples") Window::new("Examples")
@ -96,6 +98,15 @@ impl Default for OpenWindows {
fn default() -> Self { fn default() -> Self {
Self { Self {
examples: true, examples: true,
..OpenWindows::none()
}
}
}
impl OpenWindows {
fn none() -> Self {
Self {
examples: false,
settings: false, settings: false,
inspection: false, inspection: false,
memory: false, memory: false,