Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eframe: read native window position and size #1617

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
TicClick marked this conversation as resolved.
Show resolved Hide resolved
/// of a user's first monitor.
pub position: egui::Vec2,
TicClick marked this conversation as resolved.
Show resolved Hide resolved

/// Window dimensions, in pixels.
TicClick marked this conversation as resolved.
Show resolved Hide resolved
pub size: egui::Vec2,
}

/// Information about the URL.
///
/// Everything has been percent decoded (`%20` -> ` ` etc).
Expand Down Expand Up @@ -411,6 +422,9 @@ pub struct IntegrationInfo {

/// The OS native pixels-per-point
pub native_pixels_per_point: Option<f32>,

/// Window-specific geometry information, if provided by the platform.
pub window_info: Option<WindowInfo>,
}

// ----------------------------------------------------------------------------
Expand Down
23 changes: 22 additions & 1 deletion eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
@@ -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<f64> {
Expand All @@ -8,6 +8,25 @@ pub fn points_to_size(points: egui::Vec2) -> winit::dpi::LogicalSize<f64> {
}
}

pub fn read_window_info(window: &winit::window::Window) -> Option<WindowInfo> {
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,
TicClick marked this conversation as resolved.
Show resolved Hide resolved
},
})
}
Err(_) => None,
}
}

pub fn window_builder(
native_options: &epi::NativeOptions,
window_settings: &Option<WindowSettings>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions eframe/src/web/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down