[web] implement reactive repainting

meaning: only repaint on new input
This commit is contained in:
Emil Ernerfeldt 2020-07-18 19:40:24 +02:00
parent dfa4d52a94
commit 3ecd6c0297
12 changed files with 230 additions and 134 deletions

View file

@ -207,28 +207,28 @@ function makeMutClosure(arg0, arg1, dtor, f) {
real.original = state; real.original = state;
return real; return real;
} }
function __wbg_adapter_26(arg0, arg1) { function __wbg_adapter_26(arg0, arg1, arg2) {
wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h5402719cc6dde927(arg0, arg1); wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h22fd33d9f501a695(arg0, arg1, addHeapObject(arg2));
} }
function __wbg_adapter_29(arg0, arg1, arg2) { function __wbg_adapter_29(arg0, arg1, arg2) {
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h22fd33d9f501a695(arg0, arg1, addHeapObject(arg2)); wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h22fd33d9f501a695(arg0, arg1, addHeapObject(arg2));
} }
function __wbg_adapter_32(arg0, arg1, arg2) { function __wbg_adapter_32(arg0, arg1) {
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h22fd33d9f501a695(arg0, arg1, addHeapObject(arg2)); wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h5402719cc6dde927(arg0, arg1);
} }
function __wbg_adapter_35(arg0, arg1, arg2) { function __wbg_adapter_35(arg0, arg1, arg2) {
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h22fd33d9f501a695(arg0, arg1, addHeapObject(arg2)); wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h22fd33d9f501a695(arg0, arg1, addHeapObject(arg2));
} }
function __wbg_adapter_38(arg0, arg1) { function __wbg_adapter_38(arg0, arg1, arg2) {
wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h84da5f062b972f09(arg0, arg1); wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h22fd33d9f501a695(arg0, arg1, addHeapObject(arg2));
} }
function __wbg_adapter_41(arg0, arg1, arg2) { function __wbg_adapter_41(arg0, arg1) {
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h22fd33d9f501a695(arg0, arg1, addHeapObject(arg2)); wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h84da5f062b972f09(arg0, arg1);
} }
/** /**
@ -725,28 +725,28 @@ async function init(input) {
var ret = wasm.memory; var ret = wasm.memory;
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper373 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper380 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_35);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper375 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_29);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper371 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_38); var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_38);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper365 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper377 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_26); var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_35);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper369 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper384 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_29);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper376 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_32);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper382 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_41); var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_41);
return addHeapObject(ret); return addHeapObject(ret);
}; };
imports.wbg.__wbindgen_closure_wrapper366 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper386 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_32); var ret = makeMutClosure(arg0, arg1, 71, __wbg_adapter_26);
return addHeapObject(ret); return addHeapObject(ret);
}; };

Binary file not shown.

View file

@ -195,6 +195,7 @@ impl Prepared {
} else { } else {
state.vel -= friction * state.vel.normalized(); state.vel -= friction * state.vel.normalized();
state.pos += state.vel * input.dt; state.pos += state.vel * input.dt;
ctx.request_repaint();
} }
} }

View file

@ -57,6 +57,9 @@ impl State {
let animation_time = ui.style().animation_time; let animation_time = ui.style().animation_time;
let time_since_toggle = (ui.input().time - self.toggle_time) as f32; let time_since_toggle = (ui.input().time - self.toggle_time) as f32;
let time_since_toggle = time_since_toggle + ui.input().dt; // Instant feedback 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 { if self.open {
remap_clamp(time_since_toggle, 0.0..=animation_time, 0.0..=1.0) remap_clamp(time_since_toggle, 0.0..=animation_time, 0.0..=1.0)
} else { } else {

View file

@ -92,6 +92,9 @@ impl ScrollArea {
} else { } else {
let time_since_toggle = (ui.input().time - state.toggle_time) as f32; let time_since_toggle = (ui.input().time - state.toggle_time) as f32;
let animation_time = ui.style().animation_time; let animation_time = ui.style().animation_time;
if time_since_toggle <= animation_time {
ui.ctx().request_repaint();
}
if state.show_scroll { if state.show_scroll {
remap_clamp( remap_clamp(
time_since_toggle, time_since_toggle,
@ -194,6 +197,7 @@ impl Prepared {
0.0..=ui.style().animation_time, 0.0..=ui.style().animation_time,
0.0..=max_scroll_bar_width, 0.0..=max_scroll_bar_width,
); );
ui.ctx().request_repaint();
} }
if current_scroll_bar_width > 0.0 { if current_scroll_bar_width > 0.0 {

View file

@ -75,6 +75,13 @@ impl Context {
self.output.try_lock().expect("output already locked") 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 { pub fn input(&self) -> &InputState {
&self.input &self.input
} }
@ -150,7 +157,6 @@ impl Context {
self.used_ids.lock().clear(); self.used_ids.lock().clear();
self.input = std::mem::take(&mut self.input).begin_frame(new_raw_input); self.input = std::mem::take(&mut self.input).begin_frame(new_raw_input);
let mut font_definitions = self.font_definitions.lock(); let mut font_definitions = self.font_definitions.lock();
font_definitions.pixels_per_point = self.input.pixels_per_point; font_definitions.pixels_per_point = self.input.pixels_per_point;
if self.fonts.is_none() || *self.fonts.as_ref().unwrap().definitions() != *font_definitions 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. /// Returns what has happened this frame (`Output`) as well as what you need to paint.
#[must_use] #[must_use]
pub fn end_frame(&self) -> (Output, PaintBatches) { pub fn end_frame(&self) -> (Output, PaintBatches) {
if self.input.wants_repaint() {
self.request_repaint();
}
self.memory().end_frame(); self.memory().end_frame();
let output: Output = std::mem::take(&mut self.output()); let output: Output = std::mem::take(&mut self.output());
let paint_batches = self.paint(); let paint_batches = self.paint();

View file

@ -47,6 +47,7 @@ impl FractalClock {
.input() .input()
.seconds_since_midnight .seconds_since_midnight
.unwrap_or_else(|| ui.input().time); .unwrap_or_else(|| ui.input().time);
ui.ctx().request_repaint();
} }
self.fractal_ui(ui, ui.available_finite()); self.fractal_ui(ui, ui.available_finite());

View file

@ -190,6 +190,14 @@ impl InputState {
raw: new, 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 { impl MouseInput {

View file

@ -14,6 +14,11 @@ pub struct Output {
/// Response to Event::Copy or Event::Cut. Ignore if empty. /// Response to Event::Copy or Event::Cut. Ignore if empty.
pub copied_text: String, 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)] #[derive(Clone, Copy)]

View file

@ -547,16 +547,29 @@ impl Ui {
self.add(Button::new(text)) 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 { pub fn checkbox(&mut self, text: impl Into<String>, checked: &mut bool) -> GuiResponse {
self.add(Checkbox::new(checked, text)) 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 { pub fn radio(&mut self, text: impl Into<String>, checked: bool) -> GuiResponse {
self.add(RadioButton::new(checked, text)) 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 { pub fn separator(&mut self) -> GuiResponse {
self.add(Separator::new()) self.add(Separator::new())
} }

View file

@ -9,19 +9,24 @@ use wasm_bindgen::prelude::*;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
pub struct BackendInfo { pub struct WebInfo {
pub painter_debug_info: String,
/// excludes call to paint backend
pub cpu_time: f32,
pub fps: f32,
/// e.g. "#fragment" part of "www.example.com/index.html#fragment" /// e.g. "#fragment" part of "www.example.com/index.html#fragment"
pub web_location_hash: String, pub web_location_hash: String,
} }
/// Implement this and use `egui_web::AppRunner` to run your app. /// Implement this and use `egui_web::AppRunner` to run your app.
pub trait App { pub trait App {
fn ui(&mut self, ui: &mut egui::Ui, info: &BackendInfo); fn ui(&mut self, ui: &mut egui::Ui, backend: &mut Backend, info: &WebInfo);
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RunMode {
/// Uses `request_animation_frame` to repaint the UI on each display Hz.
/// This is good for games and stuff where you want to run logic at e.g. 60 FPS.
Continuous,
/// Only repaint when there is new input (mouse movement, keyboard input etc).
Reactive,
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -31,10 +36,13 @@ pub struct Backend {
painter: webgl::Painter, painter: webgl::Painter,
frame_times: egui::MovementTracker<f32>, frame_times: egui::MovementTracker<f32>,
frame_start: Option<f64>, frame_start: Option<f64>,
/// If true, paint at full framerate always.
/// If false, only paint on input.
run_mode: RunMode,
} }
impl Backend { impl Backend {
pub fn new(canvas_id: &str) -> Result<Backend, JsValue> { pub fn new(canvas_id: &str, run_mode: RunMode) -> Result<Backend, JsValue> {
let ctx = egui::Context::new(); let ctx = egui::Context::new();
load_memory(&ctx); load_memory(&ctx);
Ok(Backend { Ok(Backend {
@ -42,9 +50,18 @@ impl Backend {
painter: webgl::Painter::new(canvas_id)?, painter: webgl::Painter::new(canvas_id)?,
frame_times: egui::MovementTracker::new(1000, 1.0), frame_times: egui::MovementTracker::new(1000, 1.0),
frame_start: None, frame_start: None,
run_mode,
}) })
} }
pub fn run_mode(&self) -> RunMode {
self.run_mode
}
pub fn set_run_mode(&mut self, run_mode: RunMode) {
self.run_mode = run_mode;
}
/// id of the canvas html element containing the rendering /// id of the canvas html element containing the rendering
pub fn canvas_id(&self) -> &str { pub fn canvas_id(&self) -> &str {
self.painter.canvas_id() self.painter.canvas_id()
@ -127,14 +144,17 @@ pub struct AppRunner {
pub backend: Backend, pub backend: Backend,
pub web_input: WebInput, pub web_input: WebInput,
pub app: Box<dyn App>, pub app: Box<dyn App>,
/// Used to prevent calling paint() twice in one frame
pub has_requested_animaiton_frame: bool,
} }
impl AppRunner { impl AppRunner {
pub fn new(canvas_id: &str, app: Box<dyn App>) -> Result<Self, JsValue> { pub fn new(backend: Backend, app: Box<dyn App>) -> Result<Self, JsValue> {
Ok(Self { Ok(Self {
backend: Backend::new(canvas_id)?, backend,
web_input: Default::default(), web_input: Default::default(),
app, app,
has_requested_animaiton_frame: false,
}) })
} }
@ -142,36 +162,33 @@ impl AppRunner {
self.backend.canvas_id() self.backend.canvas_id()
} }
pub fn paint(&mut self) -> Result<(), JsValue> { pub fn paint(&mut self) -> Result<egui::Output, JsValue> {
resize_to_screen_size(self.backend.canvas_id()); resize_to_screen_size(self.backend.canvas_id());
let raw_input = self.web_input.new_frame(); let raw_input = self.web_input.new_frame();
let info = BackendInfo { let info = WebInfo {
painter_debug_info: self.backend.painter_debug_info(),
cpu_time: self.backend.cpu_time(),
fps: self.backend.fps(),
web_location_hash: location_hash().unwrap_or_default(), web_location_hash: location_hash().unwrap_or_default(),
}; };
let mut ui = self.backend.begin_frame(raw_input); let mut ui = self.backend.begin_frame(raw_input);
self.app.ui(&mut ui, &info); self.app.ui(&mut ui, &mut self.backend, &info);
let output = self.backend.end_frame()?; let output = self.backend.end_frame()?;
handle_output(&output); handle_output(&output);
Ok(()) Ok(output)
} }
} }
/// Install event listeners to register different input events /// Install event listeners to register different input events
/// and starts running the given `AppRunner`. /// and starts running the given `AppRunner`.
pub fn run(runner: AppRunner) -> Result<AppRunnerRef, JsValue> { pub fn run(app_runner: AppRunner) -> Result<AppRunnerRef, JsValue> {
let runner = AppRunnerRef(Arc::new(Mutex::new(runner))); let runner_ref = AppRunnerRef(Arc::new(Mutex::new(app_runner)));
install_canvas_events(&runner)?; install_canvas_events(&runner_ref)?;
install_document_events(&runner)?; install_document_events(&runner_ref)?;
paint_and_schedule(runner.clone())?; paint_and_schedule(runner_ref.clone())?;
Ok(runner) Ok(runner_ref)
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -377,55 +394,57 @@ pub fn translate_key(key: &str) -> Option<egui::Key> {
#[derive(Clone)] #[derive(Clone)]
pub struct AppRunnerRef(Arc<Mutex<AppRunner>>); pub struct AppRunnerRef(Arc<Mutex<AppRunner>>);
/// If true, paint at full framerate always. fn paint_and_schedule(runner_ref: AppRunnerRef) -> Result<(), JsValue> {
/// If false, only paint on input. let needs_repaint = {
/// TODO: if this is turned off we must turn off animations too (which hasn't been implemented yet). let mut runner_lock = runner_ref.0.lock();
const ANIMATION_FRAME: bool = true; runner_lock.has_requested_animaiton_frame = false;
let output = runner_lock.paint()?;
fn paint_and_schedule(runner: AppRunnerRef) -> Result<(), JsValue> { let run_mode = runner_lock.backend.run_mode();
runner.0.lock().paint()?; run_mode == RunMode::Continuous || output.needs_repaint
if ANIMATION_FRAME { };
request_animation_frame(runner)?; if needs_repaint {
request_animation_frame(runner_ref)?;
} }
Ok(()) Ok(())
} }
fn request_animation_frame(runner: AppRunnerRef) -> Result<(), JsValue> { fn request_animation_frame(runner_ref: AppRunnerRef) -> Result<(), JsValue> {
use wasm_bindgen::JsCast; if !{ runner_ref.0.lock().has_requested_animaiton_frame } {
let window = web_sys::window().unwrap(); runner_ref.0.lock().has_requested_animaiton_frame = true;
let closure = Closure::once(move || paint_and_schedule(runner));
window.request_animation_frame(closure.as_ref().unchecked_ref())?; use wasm_bindgen::JsCast;
closure.forget(); // We must forget it, or else the callback is canceled on drop let window = web_sys::window().unwrap();
let closure = Closure::once(move || paint_and_schedule(runner_ref));
window.request_animation_frame(closure.as_ref().unchecked_ref())?;
closure.forget(); // We must forget it, or else the callback is canceled on drop
}
Ok(()) Ok(())
} }
fn invalidate(runner: &mut AppRunner) -> Result<(), JsValue> { fn invalidate(runner_ref: AppRunnerRef) -> Result<(), JsValue> {
if ANIMATION_FRAME { request_animation_frame(runner_ref)
Ok(()) // No need to invalidate - we repaint all the time
} else {
runner.paint() // TODO: schedule repaint instead?
}
} }
fn install_document_events(runner: &AppRunnerRef) -> Result<(), JsValue> { fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> {
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
let document = web_sys::window().unwrap().document().unwrap(); let document = web_sys::window().unwrap().document().unwrap();
{ {
// keydown // keydown
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
let key = event.key(); let key = event.key();
if let Some(key) = translate_key(&key) { if let Some(key) = translate_key(&key) {
runner runner_lock
.web_input .web_input
.events .events
.push(egui::Event::Key { key, pressed: true }); .push(egui::Event::Key { key, pressed: true });
} else { } else {
runner.web_input.events.push(egui::Event::Text(key)); runner_lock.web_input.events.push(egui::Event::Text(key));
} }
invalidate(&mut runner).unwrap(); drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
}) as Box<dyn FnMut(_)>); }) as Box<dyn FnMut(_)>);
document.add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref())?; document.add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref())?;
closure.forget(); closure.forget();
@ -433,16 +452,17 @@ fn install_document_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
{ {
// keyup // keyup
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
let key = event.key(); let key = event.key();
if let Some(key) = translate_key(&key) { if let Some(key) = translate_key(&key) {
runner.web_input.events.push(egui::Event::Key { runner_lock.web_input.events.push(egui::Event::Key {
key, key,
pressed: false, pressed: false,
}); });
invalidate(&mut runner).unwrap(); drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
} }
}) as Box<dyn FnMut(_)>); }) as Box<dyn FnMut(_)>);
document.add_event_listener_with_callback("keyup", closure.as_ref().unchecked_ref())?; document.add_event_listener_with_callback("keyup", closure.as_ref().unchecked_ref())?;
@ -450,9 +470,9 @@ fn install_document_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
} }
for event_name in &["load", "pagehide", "pageshow", "resize"] { for event_name in &["load", "pagehide", "pageshow", "resize"] {
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move || { let closure = Closure::wrap(Box::new(move || {
invalidate(&mut runner.0.lock()).unwrap(); invalidate(runner_ref.clone()).unwrap();
}) as Box<dyn FnMut()>); }) as Box<dyn FnMut()>);
document.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?; document.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
closure.forget(); closure.forget();
@ -461,19 +481,21 @@ fn install_document_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
Ok(()) Ok(())
} }
fn install_canvas_events(runner: &AppRunnerRef) -> Result<(), JsValue> { fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> {
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
let canvas = canvas_element(runner.0.lock().canvas_id()).unwrap(); let canvas = canvas_element(runner_ref.0.lock().canvas_id()).unwrap();
{ {
let event_name = "mousedown"; let event_name = "mousedown";
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
if !runner.web_input.is_touch { if !runner_lock.web_input.is_touch {
runner.web_input.mouse_pos = Some(pos_from_mouse_event(runner.canvas_id(), &event)); runner_lock.web_input.mouse_pos =
runner.web_input.mouse_down = true; Some(pos_from_mouse_event(runner_lock.canvas_id(), &event));
invalidate(&mut runner).unwrap(); runner_lock.web_input.mouse_down = true;
drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();
} }
@ -484,12 +506,14 @@ fn install_canvas_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
{ {
let event_name = "mousemove"; let event_name = "mousemove";
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
if !runner.web_input.is_touch { if !runner_lock.web_input.is_touch {
runner.web_input.mouse_pos = Some(pos_from_mouse_event(runner.canvas_id(), &event)); runner_lock.web_input.mouse_pos =
invalidate(&mut runner).unwrap(); Some(pos_from_mouse_event(runner_lock.canvas_id(), &event));
drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();
} }
@ -500,13 +524,15 @@ fn install_canvas_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
{ {
let event_name = "mouseup"; let event_name = "mouseup";
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
if !runner.web_input.is_touch { if !runner_lock.web_input.is_touch {
runner.web_input.mouse_pos = Some(pos_from_mouse_event(runner.canvas_id(), &event)); runner_lock.web_input.mouse_pos =
runner.web_input.mouse_down = false; Some(pos_from_mouse_event(runner_lock.canvas_id(), &event));
invalidate(&mut runner).unwrap(); runner_lock.web_input.mouse_down = false;
drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();
} }
@ -517,12 +543,13 @@ fn install_canvas_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
{ {
let event_name = "mouseleave"; let event_name = "mouseleave";
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
if !runner.web_input.is_touch { if !runner_lock.web_input.is_touch {
runner.web_input.mouse_pos = None; runner_lock.web_input.mouse_pos = None;
invalidate(&mut runner).unwrap(); drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();
} }
@ -533,13 +560,14 @@ fn install_canvas_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
{ {
let event_name = "touchstart"; let event_name = "touchstart";
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
runner.web_input.is_touch = true; runner_lock.web_input.is_touch = true;
runner.web_input.mouse_pos = Some(pos_from_touch_event(&event)); runner_lock.web_input.mouse_pos = Some(pos_from_touch_event(&event));
runner.web_input.mouse_down = true; runner_lock.web_input.mouse_down = true;
invalidate(&mut runner).unwrap(); drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();
}) as Box<dyn FnMut(_)>); }) as Box<dyn FnMut(_)>);
@ -549,12 +577,13 @@ fn install_canvas_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
{ {
let event_name = "touchmove"; let event_name = "touchmove";
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
runner.web_input.is_touch = true; runner_lock.web_input.is_touch = true;
runner.web_input.mouse_pos = Some(pos_from_touch_event(&event)); runner_lock.web_input.mouse_pos = Some(pos_from_touch_event(&event));
invalidate(&mut runner).unwrap(); drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();
}) as Box<dyn FnMut(_)>); }) as Box<dyn FnMut(_)>);
@ -564,14 +593,16 @@ fn install_canvas_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
{ {
let event_name = "touchend"; let event_name = "touchend";
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
let mut runner = runner.0.lock(); // TODO: this paints twice in one frame, which is not great
runner.web_input.is_touch = true; let mut runner_lock = runner_ref.0.lock();
runner.web_input.mouse_down = false; // First release mouse to click... runner_lock.web_input.is_touch = true;
runner.paint().unwrap(); // ...do the clicking... runner_lock.web_input.mouse_down = false; // First release mouse to click...
runner.web_input.mouse_pos = None; // ...remove hover effect runner_lock.paint().unwrap(); // ...do the clicking...
invalidate(&mut runner).unwrap(); runner_lock.web_input.mouse_pos = None; // ...remove hover effect
drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();
}) as Box<dyn FnMut(_)>); }) as Box<dyn FnMut(_)>);
@ -581,12 +612,13 @@ fn install_canvas_events(runner: &AppRunnerRef) -> Result<(), JsValue> {
{ {
let event_name = "wheel"; let event_name = "wheel";
let runner = runner.clone(); let runner_ref = runner_ref.clone();
let closure = Closure::wrap(Box::new(move |event: web_sys::WheelEvent| { let closure = Closure::wrap(Box::new(move |event: web_sys::WheelEvent| {
let mut runner = runner.0.lock(); let mut runner_lock = runner_ref.0.lock();
runner.web_input.scroll_delta.x -= event.delta_x() as f32; runner_lock.web_input.scroll_delta.x -= event.delta_x() as f32;
runner.web_input.scroll_delta.y -= event.delta_y() as f32; runner_lock.web_input.scroll_delta.y -= event.delta_y() as f32;
invalidate(&mut runner).unwrap(); drop(runner_lock);
invalidate(runner_ref.clone()).unwrap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();
}) as Box<dyn FnMut(_)>); }) as Box<dyn FnMut(_)>);

View file

@ -10,8 +10,9 @@ use wasm_bindgen::prelude::*;
/// This is the entry-point for all the web-assembly. /// This is the entry-point for all the web-assembly.
#[wasm_bindgen] #[wasm_bindgen]
pub fn start(canvas_id: &str) -> Result<(), JsValue> { pub fn start(canvas_id: &str) -> Result<(), JsValue> {
let backend = egui_web::Backend::new(canvas_id, egui_web::RunMode::Continuous)?;
let app = Box::new(MyApp::default()); let app = Box::new(MyApp::default());
let runner = egui_web::AppRunner::new(canvas_id, app)?; let runner = egui_web::AppRunner::new(backend, app)?;
egui_web::run(runner)?; egui_web::run(runner)?;
Ok(()) Ok(())
} }
@ -24,7 +25,7 @@ pub struct MyApp {
} }
impl egui_web::App for MyApp { impl egui_web::App for MyApp {
fn ui(&mut self, ui: &mut egui::Ui, info: &egui_web::BackendInfo) { fn ui(&mut self, ui: &mut egui::Ui, backend: &mut egui_web::Backend, info: &egui_web::WebInfo) {
self.example_app.ui(ui, &info.web_location_hash); self.example_app.ui(ui, &info.web_location_hash);
let mut ui = ui.centered_column(ui.available().width().min(480.0)); let mut ui = ui.centered_column(ui.available().width().min(480.0));
@ -44,16 +45,34 @@ impl egui_web::App for MyApp {
ui.label("WebGl painter info:"); ui.label("WebGl painter info:");
ui.indent("webgl region id", |ui| { ui.indent("webgl region id", |ui| {
ui.label(&info.painter_debug_info); ui.label(&backend.painter_debug_info());
}); });
ui.add( ui.add(
label!( label!(
"CPU usage: {:.2} ms (excludes painting)", "CPU usage: {:.2} ms / frame (excludes painting)",
1e3 * info.cpu_time 1e3 * backend.cpu_time()
) )
.text_style(TextStyle::Monospace), .text_style(TextStyle::Monospace),
); );
ui.add(label!("FPS: {:.1}", info.fps).text_style(TextStyle::Monospace));
ui.horizontal(|ui| {
let mut run_mode = backend.run_mode();
ui.label("Run mode:");
ui.radio_value("Continuous", &mut run_mode, egui_web::RunMode::Continuous)
.tooltip_text("Repaint everything each frame");
ui.radio_value("Reactive", &mut run_mode, egui_web::RunMode::Reactive)
.tooltip_text("Repaint when there is new input (e.g. mouse movement)");
backend.set_run_mode(run_mode);
});
if backend.run_mode() == egui_web::RunMode::Continuous {
ui.add(
label!("Repainting the UI each frame. FPS: {:.1}", backend.fps())
.text_style(TextStyle::Monospace),
);
} else {
ui.label("Only running UI code when there is new input");
}
} }
} }