egui-winit: restore window position accurately

previous code had mixed up inner and outer coordinates
This commit is contained in:
Emil Ernerfeldt 2021-10-20 13:54:08 +02:00
parent f025513998
commit 3ba406c0fe

View file

@ -1,9 +1,9 @@
/// Can be used to store window settings (position and size).
/// Can be used to store native window settings (position and size).
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct WindowSettings {
/// outer position of window in physical pixels
pos: Option<egui::Pos2>,
/// Inner position of window in physical pixels
inner_pos: Option<egui::Pos2>,
/// Inner size of window in logical pixels
inner_size_points: Option<egui::Vec2>,
}
@ -13,8 +13,8 @@ impl WindowSettings {
let inner_size_points = window.inner_size().to_logical::<f32>(window.scale_factor());
Self {
pos: window
.outer_position()
inner_pos: window
.inner_position()
.ok()
.map(|p| egui::pos2(p.x as f32, p.y as f32)),
@ -33,9 +33,9 @@ impl WindowSettings {
// If the app last ran on two monitors and only one is now connected, then
// the given position is invalid.
// If this happens on Mac, the window is clamped into valid area.
// If this happens on Windows, the window is hidden and impossible to bring to get at.
// 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.pos {
if let Some(pos) = self.inner_pos {
window = window.with_position(winit::dpi::PhysicalPosition {
x: pos.x as f64,
y: pos.y as f64,