From d7d8b67cdc017d592c8e65743c5687db250d9a6a Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 27 Jan 2025 03:33:03 +0900 Subject: [PATCH 1/2] Add `window::monitor_size` task --- runtime/src/window.rs | 10 ++++++++++ winit/src/lib.rs | 14 +++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index a2d8b339..bcebc631 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -175,6 +175,9 @@ pub enum Action { /// Set the window size increment. SetResizeIncrements(Id, Option), + /// Get the size of the monitor on which the window currently resides in logical dimensions. + GetMonitorSize(Id, oneshot::Sender>), + /// Redraws all the windows. RedrawAll, @@ -491,3 +494,10 @@ pub fn enable_mouse_passthrough(id: Id) -> Task { pub fn disable_mouse_passthrough(id: Id) -> Task { task::effect(crate::Action::Window(Action::DisableMousePassthrough(id))) } + +/// Get the size of the monitor on which the window currently resides in logical dimensions. +pub fn monitor_size(id: Id) -> Task> { + task::oneshot(move |channel| { + crate::Action::Window(Action::GetMonitorSize(id, channel)) + }) +} diff --git a/winit/src/lib.rs b/winit/src/lib.rs index d06fa6e2..1e6394e4 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -1631,7 +1631,7 @@ fn run_action<'a, P, C>( let _ = channel.send(core::window::Screenshot::new( bytes, window.state.physical_size(), - window.state.viewport().scale_factor(), + window.state.scale_factor(), )); } } @@ -1645,6 +1645,18 @@ fn run_action<'a, P, C>( let _ = window.raw.set_cursor_hittest(true); } } + window::Action::GetMonitorSize(id, channel) => { + if let Some(window) = window_manager.get(id) { + let size = window.raw.current_monitor().map(|monitor| { + let scale = window.state.scale_factor(); + let size = monitor.size().to_logical(f64::from(scale)); + + Size::new(size.width, size.height) + }); + + let _ = channel.send(size); + } + } window::Action::RedrawAll => { for (_id, window) in window_manager.iter_mut() { window.raw.request_redraw(); From 7052bf3781019f2023d636bd1171786976dcbd38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 20 Nov 2025 01:50:17 +0100 Subject: [PATCH 2/2] Improve consistency of `runtime::window` documentation --- runtime/src/window.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index bcebc631..2a05a96d 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -16,7 +16,7 @@ use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; /// An operation to be performed on some window. pub enum Action { - /// Opens a new window with some [`Settings`]. + /// Open a new window with some [`Settings`]. Open(Id, Settings, oneshot::Sender), /// Close the window and exits the application. @@ -151,7 +151,7 @@ pub enum Action { /// Screenshot the viewport of the window. Screenshot(Id, oneshot::Sender), - /// Enables mouse passthrough for the given window. + /// Enable mouse passthrough for the given window. /// /// This disables mouse events for the window and passes mouse events /// through to whatever window is underneath. @@ -175,13 +175,13 @@ pub enum Action { /// Set the window size increment. SetResizeIncrements(Id, Option), - /// Get the size of the monitor on which the window currently resides in logical dimensions. + /// Get the logical dimensions of the monitor containing the window with the given [`Id`]. GetMonitorSize(Id, oneshot::Sender>), - /// Redraws all the windows. + /// Redraw all the windows. RedrawAll, - /// Recomputes the layouts of all the windows. + /// Recompute the layouts of all the windows. RelayoutAll, } @@ -495,7 +495,7 @@ pub fn disable_mouse_passthrough(id: Id) -> Task { task::effect(crate::Action::Window(Action::DisableMousePassthrough(id))) } -/// Get the size of the monitor on which the window currently resides in logical dimensions. +/// Get the logical dimensions of the monitor containing the window with the given [`Id`]. pub fn monitor_size(id: Id) -> Task> { task::oneshot(move |channel| { crate::Action::Window(Action::GetMonitorSize(id, channel))