From fd0a365bf3be31caab96e95c48cfd0fe5ededbe0 Mon Sep 17 00:00:00 2001 From: TicClick Date: Tue, 10 May 2022 22:29:35 +0200 Subject: [PATCH 1/3] Add ability to read the main window's geometry on native platforms Position and dimensions are available via `eframe::Frame::info().window_info` --- eframe/src/epi.rs | 14 ++++++++++++++ eframe/src/native/epi_integration.rs | 23 ++++++++++++++++++++++- eframe/src/web/backend.rs | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/eframe/src/epi.rs b/eframe/src/epi.rs index a7c3ece4b72..19bc7334039 100644 --- a/eframe/src/epi.rs +++ b/eframe/src/epi.rs @@ -341,6 +341,17 @@ pub struct WebInfo { pub location: Location, } +/// Information about the application's main window, if available. +#[derive(Clone, Debug)] +pub struct WindowInfo { + /// Coordinates of the window's top left corner, relative to the top left corner + /// of a user's first monitor. + pub position: egui::Vec2, + + /// Window dimensions, in pixels. + pub size: egui::Vec2, +} + /// Information about the URL. /// /// Everything has been percent decoded (`%20` -> ` ` etc). @@ -411,6 +422,9 @@ pub struct IntegrationInfo { /// The OS native pixels-per-point pub native_pixels_per_point: Option, + + /// Window-specific geometry information, if provided by the platform. + pub window_info: Option, } // ---------------------------------------------------------------------------- diff --git a/eframe/src/native/epi_integration.rs b/eframe/src/native/epi_integration.rs index 3f9cd45481f..897eabe79c4 100644 --- a/eframe/src/native/epi_integration.rs +++ b/eframe/src/native/epi_integration.rs @@ -1,4 +1,4 @@ -use crate::epi; +use crate::{epi, WindowInfo}; use egui_winit::{native_pixels_per_point, WindowSettings}; pub fn points_to_size(points: egui::Vec2) -> winit::dpi::LogicalSize { @@ -8,6 +8,25 @@ pub fn points_to_size(points: egui::Vec2) -> winit::dpi::LogicalSize { } } +pub fn read_window_info(window: &winit::window::Window) -> Option { + match window.outer_position() { + Ok(pos) => { + let size = window.inner_size(); + Some(WindowInfo { + position: egui::Vec2 { + x: pos.x as f32, + y: pos.y as f32, + }, + size: egui::Vec2 { + x: size.width as f32, + y: size.height as f32, + }, + }) + } + Err(_) => None, + } +} + pub fn window_builder( native_options: &epi::NativeOptions, window_settings: &Option, @@ -176,6 +195,7 @@ impl EpiIntegration { prefer_dark_mode, cpu_usage: None, native_pixels_per_point: Some(native_pixels_per_point(window)), + window_info: read_window_info(window), }, output: Default::default(), storage, @@ -238,6 +258,7 @@ impl EpiIntegration { ) -> egui::FullOutput { let frame_start = std::time::Instant::now(); + self.frame.info.window_info = read_window_info(window); let raw_input = self.egui_winit.take_egui_input(window); let full_output = self.egui_ctx.run(raw_input, |egui_ctx| { crate::profile_scope!("App::update"); diff --git a/eframe/src/web/backend.rs b/eframe/src/web/backend.rs index 41608e8a721..c871cd893c4 100644 --- a/eframe/src/web/backend.rs +++ b/eframe/src/web/backend.rs @@ -152,6 +152,7 @@ impl AppRunner { prefer_dark_mode, cpu_usage: None, native_pixels_per_point: Some(native_pixels_per_point()), + window_info: None, }; let storage = LocalStorage::default(); From 5e9495c6d5791382c15758d7ee297985d3febbac Mon Sep 17 00:00:00 2001 From: TicClick Date: Wed, 11 May 2022 13:44:10 +0200 Subject: [PATCH 2/3] measure the window in egui logical pixels --- eframe/src/epi.rs | 8 ++++---- eframe/src/native/epi_integration.rs | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/eframe/src/epi.rs b/eframe/src/epi.rs index 19bc7334039..9f5c10f1bad 100644 --- a/eframe/src/epi.rs +++ b/eframe/src/epi.rs @@ -344,11 +344,11 @@ pub struct WebInfo { /// Information about the application's main window, if available. #[derive(Clone, Debug)] pub struct WindowInfo { - /// Coordinates of the window's top left corner, relative to the top left corner - /// of a user's first monitor. - pub position: egui::Vec2, + /// Coordinates of the window's outer top left corner, relative to the top left corner. + /// Unit: egui points (logical pixels). + pub position: egui::Pos2, - /// Window dimensions, in pixels. + /// Window inner size in egui points (logical pixels). pub size: egui::Vec2, } diff --git a/eframe/src/native/epi_integration.rs b/eframe/src/native/epi_integration.rs index 897eabe79c4..3d082fbdedb 100644 --- a/eframe/src/native/epi_integration.rs +++ b/eframe/src/native/epi_integration.rs @@ -8,18 +8,21 @@ pub fn points_to_size(points: egui::Vec2) -> winit::dpi::LogicalSize { } } -pub fn read_window_info(window: &winit::window::Window) -> Option { +pub fn read_window_info( + window: &winit::window::Window, + pixels_per_point: f32, +) -> Option { match window.outer_position() { Ok(pos) => { - let size = window.inner_size(); + let pos = pos.to_logical::(pixels_per_point.into()); + let size = window + .inner_size() + .to_logical::(pixels_per_point.into()); Some(WindowInfo { - position: egui::Vec2 { - x: pos.x as f32, - y: pos.y as f32, - }, + position: egui::Pos2 { x: pos.x, y: pos.y }, size: egui::Vec2 { - x: size.width as f32, - y: size.height as f32, + x: size.width, + y: size.height, }, }) } @@ -195,7 +198,7 @@ impl EpiIntegration { prefer_dark_mode, cpu_usage: None, native_pixels_per_point: Some(native_pixels_per_point(window)), - window_info: read_window_info(window), + window_info: read_window_info(window, egui_ctx.pixels_per_point()), }, output: Default::default(), storage, @@ -258,7 +261,7 @@ impl EpiIntegration { ) -> egui::FullOutput { let frame_start = std::time::Instant::now(); - self.frame.info.window_info = read_window_info(window); + self.frame.info.window_info = read_window_info(window, self.egui_ctx.pixels_per_point()); let raw_input = self.egui_winit.take_egui_input(window); let full_output = self.egui_ctx.run(raw_input, |egui_ctx| { crate::profile_scope!("App::update"); From e9554297c9f68c5f3f6efcfbb509ed0790bec999 Mon Sep 17 00:00:00 2001 From: TicClick Date: Wed, 11 May 2022 13:55:27 +0200 Subject: [PATCH 3/3] complete the docstring --- eframe/src/epi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eframe/src/epi.rs b/eframe/src/epi.rs index 9f5c10f1bad..5e8c1d11c8e 100644 --- a/eframe/src/epi.rs +++ b/eframe/src/epi.rs @@ -344,7 +344,7 @@ pub struct WebInfo { /// Information about the application's main window, if available. #[derive(Clone, Debug)] pub struct WindowInfo { - /// Coordinates of the window's outer top left corner, relative to the top left corner. + /// Coordinates of the window's outer top left corner, relative to the top left corner of the first display. /// Unit: egui points (logical pixels). pub position: egui::Pos2,