Skip to content

Commit

Permalink
Fix persistence of window position (emilk#1745)
Browse files Browse the repository at this point in the history
* Fix window position

* Better comment

* Add doc link
  • Loading branch information
Barugon authored and Titaniumtown committed Jun 20, 2022
1 parent 4b57c61 commit 3455e89
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions egui-winit/src/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct WindowSettings {
/// Inner position of window in physical pixels
inner_pos: Option<egui::Pos2>,
/// Position of window in physical pixels. This is either
/// the inner or outer position depending on the platform.
/// See [`winit::window::WindowAttributes`] for details.
position: Option<egui::Pos2>,
/// Inner size of window in logical pixels
inner_size_points: Option<egui::Vec2>,
}

impl WindowSettings {
pub fn from_display(window: &winit::window::Window) -> Self {
let inner_size_points = window.inner_size().to_logical::<f32>(window.scale_factor());

Self {
inner_pos: window
let position = if cfg!(macos) {
// MacOS uses inner position when positioning windows.
window
.inner_position()
.ok()
.map(|p| egui::pos2(p.x as f32, p.y as f32)),
.map(|p| egui::pos2(p.x as f32, p.y as f32))
} else {
// Other platforms use the outer position.
window
.outer_position()
.ok()
.map(|p| egui::pos2(p.x as f32, p.y as f32))
};

Self {
position,
inner_size_points: Some(egui::vec2(
inner_size_points.width as f32,
inner_size_points.height as f32,
Expand All @@ -35,7 +46,7 @@ impl WindowSettings {
// If this happens on Mac, the window is clamped into valid area.
// If this happens on Windows, the window is hidden and very difficult to find.
// So we don't restore window positions on Windows.
if let Some(pos) = self.inner_pos {
if let Some(pos) = self.position {
window = window.with_position(winit::dpi::PhysicalPosition {
x: pos.x as f64,
y: pos.y as f64,
Expand Down

0 comments on commit 3455e89

Please sign in to comment.