mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 05:40: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:
@@ -1,5 +1,4 @@
|
||||
use egui::TexturesDelta;
|
||||
use wasm_bindgen::JsValue;
|
||||
|
||||
use crate::{epi, App};
|
||||
|
||||
@@ -17,7 +16,10 @@ pub struct AppRunner {
|
||||
screen_reader: super::screen_reader::ScreenReader,
|
||||
pub(crate) text_cursor_pos: Option<egui::Pos2>,
|
||||
pub(crate) mutable_text_under_cursor: bool,
|
||||
|
||||
// Output for the last run:
|
||||
textures_delta: TexturesDelta,
|
||||
clipped_primitives: Option<Vec<egui::ClippedPrimitive>>,
|
||||
}
|
||||
|
||||
impl Drop for AppRunner {
|
||||
@@ -115,6 +117,7 @@ impl AppRunner {
|
||||
text_cursor_pos: None,
|
||||
mutable_text_under_cursor: false,
|
||||
textures_delta: Default::default(),
|
||||
clipped_primitives: None,
|
||||
};
|
||||
|
||||
runner.input.raw.max_texture_side = Some(runner.painter.max_texture_side());
|
||||
@@ -170,8 +173,26 @@ impl AppRunner {
|
||||
self.painter.destroy();
|
||||
}
|
||||
|
||||
/// Call [`Self::paint`] later to paint
|
||||
pub fn logic(&mut self) -> Vec<egui::ClippedPrimitive> {
|
||||
/// Runs the user code and paints the UI.
|
||||
///
|
||||
/// If there is already an outstanding frame of output,
|
||||
/// that is painted instead.
|
||||
pub fn run_and_paint(&mut self) {
|
||||
if self.clipped_primitives.is_none() {
|
||||
// Run user code, and paint the results:
|
||||
self.logic();
|
||||
self.paint();
|
||||
} else {
|
||||
// We have already run the logic, e.g. in an on-click event,
|
||||
// so let's only present the results:
|
||||
self.paint();
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs the logic, but doesn't paint the result.
|
||||
///
|
||||
/// The result can be painted later with a call to [`Self::run_and_paint`] or [`Self::paint`].
|
||||
pub fn logic(&mut self) {
|
||||
let frame_start = now_sec();
|
||||
|
||||
super::resize_canvas_to_screen_size(self.canvas_id(), self.web_options.max_size_points);
|
||||
@@ -203,25 +224,26 @@ impl AppRunner {
|
||||
|
||||
self.handle_platform_output(platform_output);
|
||||
self.textures_delta.append(textures_delta);
|
||||
let clipped_primitives = self.egui_ctx.tessellate(shapes, pixels_per_point);
|
||||
self.clipped_primitives = Some(self.egui_ctx.tessellate(shapes, pixels_per_point));
|
||||
|
||||
self.frame.info.cpu_usage = Some((now_sec() - frame_start) as f32);
|
||||
|
||||
clipped_primitives
|
||||
}
|
||||
|
||||
/// Paint the results of the last call to [`Self::logic`].
|
||||
pub fn paint(&mut self, clipped_primitives: &[egui::ClippedPrimitive]) -> Result<(), JsValue> {
|
||||
pub fn paint(&mut self) {
|
||||
let textures_delta = std::mem::take(&mut self.textures_delta);
|
||||
let clipped_primitives = std::mem::take(&mut self.clipped_primitives);
|
||||
|
||||
self.painter.paint_and_update_textures(
|
||||
self.app.clear_color(&self.egui_ctx.style().visuals),
|
||||
clipped_primitives,
|
||||
self.egui_ctx.pixels_per_point(),
|
||||
&textures_delta,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
if let Some(clipped_primitives) = clipped_primitives {
|
||||
if let Err(err) = self.painter.paint_and_update_textures(
|
||||
self.app.clear_color(&self.egui_ctx.style().visuals),
|
||||
&clipped_primitives,
|
||||
self.egui_ctx.pixels_per_point(),
|
||||
&textures_delta,
|
||||
) {
|
||||
log::error!("Failed to paint: {}", super::string_from_js_value(&err));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_platform_output(&mut self, platform_output: egui::PlatformOutput) {
|
||||
|
||||
Reference in New Issue
Block a user