[web backend] Make sure to only request animation once per frame
Earlier we call `requestAnimationFrame` every time on `invalidate_rect`
or `request_anim_frame`. This lead to even `render()` being called many
times per frame and hence poor performance.
@@ -112,6 +112,7 @@ struct WindowState {
invalid: RefCell<Region>,
click_counter: ClickCounter,
active_text_input: Cell<Option<TextFieldToken>>,
+ rendering_soon: Cell<bool>,
}
// TODO: support custom cursors
@@ -443,6 +444,7 @@ impl WindowBuilder {
invalid: RefCell::new(Region::EMPTY),
click_counter: ClickCounter::default(),
active_text_input: Cell::new(None),
+ rendering_soon: Cell::new(false),
});
setup_web_callbacks(&window);
@@ -631,12 +633,16 @@ impl WindowHandle {
fn render_soon(&self) {
if let Some(s) = self.0.upgrade() {
let state = s.clone();
+ if !state.rendering_soon.get() {
+ state.rendering_soon.set(true);
s.request_animation_frame(move || {
+ state.rendering_soon.set(false);
state.render();
})
.expect("Failed to request animation frame");
}
}
+ }
pub fn file_dialog(
&self,
