1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Web: Fix incorrect scale when moving to screen with new DPI (#5631)

* Closes https://github.com/emilk/egui/issues/5246

Tested on
* [x] Chromium
* [x] Firefox
* [x] Safari

On Chromium and Firefox we get one annoying frame with the wrong size,
which can mess up the layout of egui apps, but this PR is still a huge
improvement, and I don't want to spend more time on this right now.
This commit is contained in:
Emil Ernerfeldt
2025-01-23 12:11:29 +01:00
committed by GitHub
parent 304c6518e3
commit 6680e9c079
5 changed files with 212 additions and 89 deletions

View File

@@ -51,6 +51,9 @@ use input::{
// ----------------------------------------------------------------------------
/// Debug browser resizing?
const DEBUG_RESIZE: bool = false;
pub(crate) fn string_from_js_value(value: &JsValue) -> String {
value.as_string().unwrap_or_else(|| format!("{value:#?}"))
}
@@ -152,7 +155,10 @@ fn canvas_content_rect(canvas: &web_sys::HtmlCanvasElement) -> egui::Rect {
}
fn canvas_size_in_points(canvas: &web_sys::HtmlCanvasElement, ctx: &egui::Context) -> egui::Vec2 {
let pixels_per_point = ctx.pixels_per_point();
// ctx.pixels_per_point can be outdated
let pixels_per_point = ctx.zoom_factor() * native_pixels_per_point();
egui::vec2(
canvas.width() as f32 / pixels_per_point,
canvas.height() as f32 / pixels_per_point,
@@ -352,3 +358,8 @@ pub fn percent_decode(s: &str) -> String {
.decode_utf8_lossy()
.to_string()
}
/// Are we running inside the Safari browser?
pub fn is_safari_browser() -> bool {
web_sys::window().is_some_and(|window| window.has_own_property(&JsValue::from("safari")))
}