Skip to content

Commit

Permalink
revert unrelated changes + fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
jprochazk committed May 24, 2024
1 parent a14bc86 commit 28c970d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 70 deletions.
2 changes: 1 addition & 1 deletion crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
15 changes: 14 additions & 1 deletion crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 0 additions & 51 deletions crates/eframe/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
20 changes: 3 additions & 17 deletions crates/eframe/src/web/web_runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
cell::{Cell, RefCell},
rc::Rc,
};
use std::{cell::RefCell, rc::Rc};

use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())?;
Expand Down

0 comments on commit 28c970d

Please sign in to comment.