[web] implement reactive repainting
meaning: only repaint on new input
This commit is contained in:
@@ -195,6 +195,7 @@ impl Prepared {
|
||||
} else {
|
||||
state.vel -= friction * state.vel.normalized();
|
||||
state.pos += state.vel * input.dt;
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,9 @@ impl State {
|
||||
let animation_time = ui.style().animation_time;
|
||||
let time_since_toggle = (ui.input().time - self.toggle_time) as f32;
|
||||
let time_since_toggle = time_since_toggle + ui.input().dt; // Instant feedback
|
||||
if time_since_toggle <= animation_time {
|
||||
ui.ctx().request_repaint();
|
||||
}
|
||||
if self.open {
|
||||
remap_clamp(time_since_toggle, 0.0..=animation_time, 0.0..=1.0)
|
||||
} else {
|
||||
|
||||
@@ -92,6 +92,9 @@ impl ScrollArea {
|
||||
} else {
|
||||
let time_since_toggle = (ui.input().time - state.toggle_time) as f32;
|
||||
let animation_time = ui.style().animation_time;
|
||||
if time_since_toggle <= animation_time {
|
||||
ui.ctx().request_repaint();
|
||||
}
|
||||
if state.show_scroll {
|
||||
remap_clamp(
|
||||
time_since_toggle,
|
||||
@@ -194,6 +197,7 @@ impl Prepared {
|
||||
0.0..=ui.style().animation_time,
|
||||
0.0..=max_scroll_bar_width,
|
||||
);
|
||||
ui.ctx().request_repaint();
|
||||
}
|
||||
|
||||
if current_scroll_bar_width > 0.0 {
|
||||
|
||||
+11
-1
@@ -75,6 +75,13 @@ impl Context {
|
||||
self.output.try_lock().expect("output already locked")
|
||||
}
|
||||
|
||||
/// Call this if there is need to repaint the UI, i.e. if you are showing an animation.
|
||||
/// If this is called at least once in a frame, then there will be another frame right after this.
|
||||
/// Call as many times as you wish, only one repaint will be issued.
|
||||
pub fn request_repaint(&self) {
|
||||
self.output().needs_repaint = true;
|
||||
}
|
||||
|
||||
pub fn input(&self) -> &InputState {
|
||||
&self.input
|
||||
}
|
||||
@@ -150,7 +157,6 @@ impl Context {
|
||||
self.used_ids.lock().clear();
|
||||
|
||||
self.input = std::mem::take(&mut self.input).begin_frame(new_raw_input);
|
||||
|
||||
let mut font_definitions = self.font_definitions.lock();
|
||||
font_definitions.pixels_per_point = self.input.pixels_per_point;
|
||||
if self.fonts.is_none() || *self.fonts.as_ref().unwrap().definitions() != *font_definitions
|
||||
@@ -163,6 +169,10 @@ impl Context {
|
||||
/// Returns what has happened this frame (`Output`) as well as what you need to paint.
|
||||
#[must_use]
|
||||
pub fn end_frame(&self) -> (Output, PaintBatches) {
|
||||
if self.input.wants_repaint() {
|
||||
self.request_repaint();
|
||||
}
|
||||
|
||||
self.memory().end_frame();
|
||||
let output: Output = std::mem::take(&mut self.output());
|
||||
let paint_batches = self.paint();
|
||||
|
||||
@@ -47,6 +47,7 @@ impl FractalClock {
|
||||
.input()
|
||||
.seconds_since_midnight
|
||||
.unwrap_or_else(|| ui.input().time);
|
||||
ui.ctx().request_repaint();
|
||||
}
|
||||
|
||||
self.fractal_ui(ui, ui.available_finite());
|
||||
|
||||
@@ -190,6 +190,14 @@ impl InputState {
|
||||
raw: new,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wants_repaint(&self) -> bool {
|
||||
self.mouse.pressed
|
||||
|| self.mouse.released
|
||||
|| self.mouse.delta != Vec2::zero()
|
||||
|| self.scroll_delta != Vec2::zero()
|
||||
|| !self.events.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl MouseInput {
|
||||
|
||||
@@ -14,6 +14,11 @@ pub struct Output {
|
||||
|
||||
/// Response to Event::Copy or Event::Cut. Ignore if empty.
|
||||
pub copied_text: String,
|
||||
|
||||
/// Set to `true` to request another repaint right after this one.
|
||||
/// This is only used in reactive backends (i.e. backends where we repaint on new input).
|
||||
/// For instance, you may want to set this to `true` while there is an animation.
|
||||
pub needs_repaint: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
||||
+15
-2
@@ -547,16 +547,29 @@ impl Ui {
|
||||
self.add(Button::new(text))
|
||||
}
|
||||
|
||||
// TODO: argument order?
|
||||
// Argument order matching that of Dear ImGui
|
||||
pub fn checkbox(&mut self, text: impl Into<String>, checked: &mut bool) -> GuiResponse {
|
||||
self.add(Checkbox::new(checked, text))
|
||||
}
|
||||
|
||||
// TODO: argument order?
|
||||
// Argument order matching that of Dear ImGui
|
||||
pub fn radio(&mut self, text: impl Into<String>, checked: bool) -> GuiResponse {
|
||||
self.add(RadioButton::new(checked, text))
|
||||
}
|
||||
|
||||
pub fn radio_value<Value: PartialEq>(
|
||||
&mut self,
|
||||
text: impl Into<String>,
|
||||
curr_value: &mut Value,
|
||||
radio_value: Value,
|
||||
) -> GuiResponse {
|
||||
let response = self.radio(text, *curr_value == radio_value);
|
||||
if response.clicked {
|
||||
*curr_value = radio_value;
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
pub fn separator(&mut self) -> GuiResponse {
|
||||
self.add(Separator::new())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user