From 3aa8851d33794dc23853926d6ba6734c52db65dd Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 27 Jan 2025 03:33:03 +0900 Subject: [PATCH] Add `iced::window::get_monitor_size` --- runtime/src/window.rs | 10 ++++++++++ winit/src/program.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 183fab971c..66a148e016 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>), } /// Subscribes to the frames of the window of the running application. @@ -479,3 +482,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 get_monitor_size(id: Id) -> Task> { + task::oneshot(move |channel| { + crate::Action::Window(Action::GetMonitorSize(id, channel)) + }) +} diff --git a/winit/src/program.rs b/winit/src/program.rs index 5387e5e531..cdcd47c911 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1491,6 +1491,16 @@ fn run_action( 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 factor = monitor.scale_factor(); + let size = monitor.size().to_logical(factor); + Size::new(size.width, size.height) + }); + let _ = channel.send(size); + } + } }, Action::System(action) => match action { system::Action::QueryInformation(_channel) => {