From 9c72cc2a983401ad96f40555751bbfa1335259b6 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 17 Aug 2020 16:48:29 -0700 Subject: [PATCH] Fix HiDPI vs. set_cursor_icon for web (#1652) PhysicalSize is recorded as canvas.size, whereas LogicalSize is stored as canvas.style.size. The previous cursor behavior on stdweb clobbered all style - thus losing the LogicalSize. --- CHANGELOG.md | 1 + src/platform_impl/web/event_loop/mod.rs | 3 +-- src/platform_impl/web/stdweb/mod.rs | 8 ++++++-- src/platform_impl/web/web_sys/mod.rs | 12 +++++++----- src/platform_impl/web/window.rs | 3 +-- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c875fe41..e27cf5ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - On Unix, X11 and Wayland are now optional features (enabled by default) - On X11, fix deadlock when calling `set_fullscreen_inner`. - On Web, prevent the webpage from scrolling when the user is focused on a winit canvas +- On Web, calling `window.set_cursor_icon` no longer breaks HiDPI scaling - On Windows, drag and drop is now optional and must be enabled with `WindowBuilderExtWindows::with_drag_and_drop(true)`. - On Wayland, fix deadlock when calling to `set_inner_size` from a callback. - On macOS, add `hide__other_applications` to `EventLoopWindowTarget` via existing `EventLoopWindowTargetExtMacOS` trait. `hide_other_applications` will hide other applications by calling `-[NSApplication hideOtherApplications: nil]`. diff --git a/src/platform_impl/web/event_loop/mod.rs b/src/platform_impl/web/event_loop/mod.rs index fa01b5df..bf9806bd 100644 --- a/src/platform_impl/web/event_loop/mod.rs +++ b/src/platform_impl/web/event_loop/mod.rs @@ -6,11 +6,10 @@ mod window_target; pub use self::proxy::Proxy; pub use self::window_target::WindowTarget; -use super::{backend, device, monitor, window}; +use super::{backend, device, window}; use crate::event::Event; use crate::event_loop as root; -use std::collections::{vec_deque::IntoIter as VecDequeIter, VecDeque}; use std::marker::PhantomData; pub struct EventLoop { diff --git a/src/platform_impl/web/stdweb/mod.rs b/src/platform_impl/web/stdweb/mod.rs index 3632307c..e3dafb1b 100644 --- a/src/platform_impl/web/stdweb/mod.rs +++ b/src/platform_impl/web/stdweb/mod.rs @@ -67,9 +67,13 @@ pub fn set_canvas_size(raw: &CanvasElement, size: Size) { raw.set_width(physical_size.width); raw.set_height(physical_size.height); + set_canvas_style_property(raw, "width", &format!("{}px", logical_size.width)); + set_canvas_style_property(raw, "height", &format!("{}px", logical_size.height)); +} + +pub fn set_canvas_style_property(raw: &CanvasElement, style_attribute: &str, value: &str) { js! { - @{raw.as_ref()}.style.width = @{logical_size.width} + "px"; - @{raw.as_ref()}.style.height = @{logical_size.height} + "px"; + @{raw.as_ref()}.style[@{style_attribute}] = @{value}; } } diff --git a/src/platform_impl/web/web_sys/mod.rs b/src/platform_impl/web/web_sys/mod.rs index 94efe1ec..a4268b52 100644 --- a/src/platform_impl/web/web_sys/mod.rs +++ b/src/platform_impl/web/web_sys/mod.rs @@ -81,13 +81,15 @@ pub fn set_canvas_size(raw: &HtmlCanvasElement, size: Size) { raw.set_width(physical_size.width); raw.set_height(physical_size.height); + set_canvas_style_property(raw, "width", &format!("{}px", logical_size.width)); + set_canvas_style_property(raw, "height", &format!("{}px", logical_size.height)); +} + +pub fn set_canvas_style_property(raw: &HtmlCanvasElement, property: &str, value: &str) { let style = raw.style(); style - .set_property("width", &format!("{}px", logical_size.width)) - .expect("Failed to set canvas width"); - style - .set_property("height", &format!("{}px", logical_size.height)) - .expect("Failed to set canvas height"); + .set_property(property, value) + .expect(&format!("Failed to set {}", property)); } pub fn is_fullscreen(canvas: &HtmlCanvasElement) -> bool { diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index 58d55135..bcf70116 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -165,8 +165,7 @@ impl Window { CursorIcon::RowResize => "row-resize", }; *self.previous_pointer.borrow_mut() = text; - self.canvas - .set_attribute("style", &format!("cursor: {}", text)); + backend::set_canvas_style_property(self.canvas.raw(), "cursor", text); } #[inline]