From 28c970de3e05461e4444b8a247908673560f5102 Mon Sep 17 00:00:00 2001 From: jprochazk <1665677+jprochazk@users.noreply.github.com> Date: Fri, 24 May 2024 20:40:47 +0200 Subject: [PATCH] revert unrelated changes + fix lints --- crates/eframe/src/web/app_runner.rs | 2 +- crates/eframe/src/web/events.rs | 15 ++++++++- crates/eframe/src/web/mod.rs | 51 ----------------------------- crates/eframe/src/web/web_runner.rs | 20 ++--------- 4 files changed, 18 insertions(+), 70 deletions(-) diff --git a/crates/eframe/src/web/app_runner.rs b/crates/eframe/src/web/app_runner.rs index ec81ff726c5..872921591ac 100644 --- a/crates/eframe/src/web/app_runner.rs +++ b/crates/eframe/src/web/app_runner.rs @@ -5,6 +5,7 @@ use crate::{epi, App}; use super::{now_sec, web_painter::WebPainter, NeedRepaint}; pub struct AppRunner { + #[allow(dead_code)] web_options: crate::WebOptions, pub(crate) frame: epi::Frame, egui_ctx: egui::Context, @@ -184,7 +185,6 @@ impl AppRunner { /// /// The result can be painted later with a call to [`Self::run_and_paint`] or [`Self::paint`]. pub fn logic(&mut self) { - // super::resize_canvas_to_screen_size(self.canvas(), self.web_options.max_size_points); let canvas_size = super::canvas_size_in_points(self.canvas(), self.egui_ctx()); let raw_input = self.input.new_frame(canvas_size); diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index 9c9c99173d7..0c3806907be 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -2,7 +2,20 @@ use super::*; // ------------------------------------------------------------------------ -pub(crate) fn paint_if_needed(runner: &mut AppRunner) { +/// Calls `request_animation_frame` to schedule repaint. +/// +/// It will only paint if needed, but will always call `request_animation_frame` immediately. +pub(crate) 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); + drop(runner_lock); + runner_ref.request_animation_frame()?; + } + Ok(()) +} + +fn paint_if_needed(runner: &mut AppRunner) { if runner.needs_repaint.needs_repaint() { if runner.has_outstanding_paint_data() { // We have already run the logic, e.g. in an on-click event, diff --git a/crates/eframe/src/web/mod.rs b/crates/eframe/src/web/mod.rs index 566c9b03c8d..f98bbc1ed01 100644 --- a/crates/eframe/src/web/mod.rs +++ b/crates/eframe/src/web/mod.rs @@ -40,7 +40,6 @@ pub(crate) type ActiveWebPainter = web_painter_wgpu::WebPainterWgpu; pub use backend::*; -use egui::Vec2; use wasm_bindgen::prelude::*; use web_sys::MediaQueryList; @@ -124,56 +123,6 @@ fn canvas_size_in_points(canvas: &web_sys::HtmlCanvasElement, ctx: &egui::Contex ) } -fn resize_canvas_to_screen_size( - canvas: &web_sys::HtmlCanvasElement, - max_size_points: egui::Vec2, -) -> Option<()> { - let parent = canvas.parent_element()?; - - // In this function we use "pixel" to mean physical pixel, - // and "point" to mean "logical CSS pixel". - let pixels_per_point = native_pixels_per_point(); - - // Prefer the client width and height so that if the parent - // element is resized that the egui canvas resizes appropriately. - let parent_size_points = Vec2 { - x: parent.client_width() as f32, - y: parent.client_height() as f32, - }; - - if parent_size_points.x <= 0.0 || parent_size_points.y <= 0.0 { - log::error!("The parent element of the egui canvas is {}x{}. Try adding `html, body {{ height: 100%; width: 100% }}` to your CSS!", parent_size_points.x, parent_size_points.y); - } - - // We take great care here to ensure the rendered canvas aligns - // perfectly to the physical pixel grid, lest we get blurry text. - // At the time of writing, we get pixel perfection on Chromium and Firefox on Mac, - // but Desktop Safari will be blurry on most zoom levels. - // See https://github.com/emilk/egui/issues/4241 for more. - - let canvas_size_pixels = pixels_per_point * parent_size_points.min(max_size_points); - - // Make sure that the size is always an even number of pixels, - // otherwise, the page renders blurry on some platforms. - // See https://github.com/emilk/egui/issues/103 - let canvas_size_pixels = (canvas_size_pixels / 2.0).round() * 2.0; - - let canvas_size_points = canvas_size_pixels / pixels_per_point; - - canvas - .style() - .set_property("width", &format!("{}px", canvas_size_points.x)) - .ok()?; - canvas - .style() - .set_property("height", &format!("{}px", canvas_size_points.y)) - .ok()?; - canvas.set_width(canvas_size_pixels.x as u32); - canvas.set_height(canvas_size_pixels.y as u32); - - Some(()) -} - // ---------------------------------------------------------------------------- /// Set the cursor icon. diff --git a/crates/eframe/src/web/web_runner.rs b/crates/eframe/src/web/web_runner.rs index 0cedaf68dea..17119fe8eb8 100644 --- a/crates/eframe/src/web/web_runner.rs +++ b/crates/eframe/src/web/web_runner.rs @@ -1,7 +1,4 @@ -use std::{ - cell::{Cell, RefCell}, - rc::Rc, -}; +use std::{cell::RefCell, rc::Rc}; use wasm_bindgen::prelude::*; @@ -128,6 +125,7 @@ impl WebRunner { if let Some(frame) = self.frame.take() { let window = web_sys::window().unwrap(); window.cancel_animation_frame(frame.id).ok(); + drop(frame.closure); } if let Some(runner) = self.runner.replace(None) { @@ -211,19 +209,7 @@ impl WebRunner { let window = web_sys::window().unwrap(); let closure = Closure::once({ let runner_ref = self.clone(); - move || { - // animation frame is running and can't be cancelled anymore - let _ = runner_ref.frame.take(); - - // if there hasn't been a panic, then paint and schedule. - if let Some(mut runner_lock) = runner_ref.try_lock() { - events::paint_if_needed(&mut runner_lock); - drop(runner_lock); - runner_ref.request_animation_frame()?; - } - - Ok(()) - } + move || events::paint_and_schedule(&runner_ref) }); let id = window.request_animation_frame(closure.as_ref().unchecked_ref())?;