1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

eframe web: Don't throw away frames on click/copy/cut (#3623)

* Follow-up to https://github.com/emilk/egui/pull/3621 and
https://github.com/emilk/egui/pull/3513

To work around a Safari limitation, we run the app logic in the event
handler of copy, cut, and mouse up and down.

Previously the output of that frame was discarded, but in this PR it is
now saved to be used in the next requestAnimationFrame.

The result is noticeable more distinct clicks on buttons (one more frame
of highlight)

Bonus: also fix auto-save of a sleeping web app
This commit is contained in:
Emil Ernerfeldt
2023-11-24 10:08:43 +01:00
committed by GitHub
parent 0d24a3a73b
commit 23732be0e5
5 changed files with 73 additions and 29 deletions

View File

@@ -8,22 +8,19 @@ use super::*;
fn paint_and_schedule(runner_ref: &WebRunner) -> Result<(), JsValue> {
// Only paint and schedule if there has been no panic
if let Some(mut runner_lock) = runner_ref.try_lock() {
paint_if_needed(&mut runner_lock)?;
paint_if_needed(&mut runner_lock);
drop(runner_lock);
request_animation_frame(runner_ref.clone())?;
}
Ok(())
}
fn paint_if_needed(runner: &mut AppRunner) -> Result<(), JsValue> {
if runner.needs_repaint.when_to_repaint() <= now_sec() {
fn paint_if_needed(runner: &mut AppRunner) {
if runner.needs_repaint.needs_repaint() {
runner.needs_repaint.clear();
let clipped_primitives = runner.logic();
runner.paint(&clipped_primitives)?;
runner.auto_save_if_needed();
runner.run_and_paint();
}
Ok(())
runner.auto_save_if_needed();
}
pub(crate) fn request_animation_frame(runner_ref: WebRunner) -> Result<(), JsValue> {
@@ -177,10 +174,14 @@ pub(crate) fn install_document_events(runner_ref: &WebRunner) -> Result<(), JsVa
"cut",
|event: web_sys::ClipboardEvent, runner| {
runner.input.raw.events.push(egui::Event::Cut);
// In Safari we are only allowed to write to the clipboard during the
// event callback, which is why we run the app logic here and now:
runner.logic(); // we ignore the returned triangles, but schedule a repaint right after
runner.logic();
// Make sure we paint the output of the above logic call asap:
runner.needs_repaint.repaint_asap();
event.stop_propagation();
event.prevent_default();
},
@@ -192,10 +193,14 @@ pub(crate) fn install_document_events(runner_ref: &WebRunner) -> Result<(), JsVa
"copy",
|event: web_sys::ClipboardEvent, runner| {
runner.input.raw.events.push(egui::Event::Copy);
// In Safari we are only allowed to write to the clipboard during the
// event callback, which is why we run the app logic here and now:
runner.logic(); // we ignore the returned triangles, but schedule a repaint right after
runner.logic();
// Make sure we paint the output of the above logic call asap:
runner.needs_repaint.repaint_asap();
event.stop_propagation();
event.prevent_default();
},
@@ -281,9 +286,12 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
pressed: true,
modifiers,
});
// In Safari we are only allowed to write to the clipboard during the
// event callback, which is why we run the app logic here and now:
runner.logic(); // we ignore the returned triangles, but schedule a repaint right after
runner.logic();
// Make sure we paint the output of the above logic call asap:
runner.needs_repaint.repaint_asap();
}
event.stop_propagation();
@@ -313,9 +321,12 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
pressed: false,
modifiers,
});
// In Safari we are only allowed to write to the clipboard during the
// event callback, which is why we run the app logic here and now:
runner.logic(); // we ignore the returned triangles, but schedule a repaint right after
runner.logic();
// Make sure we paint the output of the above logic call asap:
runner.needs_repaint.repaint_asap();
text_agent::update_text_agent(runner);