2021-10-20 11:54:08 +00:00
|
|
|
/// Can be used to store native window settings (position and size).
|
2021-09-28 15:33:28 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2021-09-30 17:18:51 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
2021-01-04 00:44:02 +00:00
|
|
|
pub struct WindowSettings {
|
2022-06-19 14:13:10 +00:00
|
|
|
/// Position of window in physical pixels. This is either
|
|
|
|
/// the inner or outer position depending on the platform.
|
2022-08-14 14:23:46 +00:00
|
|
|
/// See [`winit::window::WindowBuilder::with_position`] for details.
|
2022-06-19 14:13:10 +00:00
|
|
|
position: Option<egui::Pos2>,
|
2022-07-29 12:21:23 +00:00
|
|
|
|
|
|
|
fullscreen: bool,
|
|
|
|
|
2021-01-04 00:44:02 +00:00
|
|
|
/// Inner size of window in logical pixels
|
|
|
|
inner_size_points: Option<egui::Vec2>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowSettings {
|
2021-09-30 17:18:51 +00:00
|
|
|
pub fn from_display(window: &winit::window::Window) -> Self {
|
|
|
|
let inner_size_points = window.inner_size().to_logical::<f32>(window.scale_factor());
|
2022-06-19 14:13:10 +00:00
|
|
|
let position = if cfg!(macos) {
|
|
|
|
// MacOS uses inner position when positioning windows.
|
|
|
|
window
|
2021-10-20 11:54:08 +00:00
|
|
|
.inner_position()
|
2021-01-04 00:44:02 +00:00
|
|
|
.ok()
|
2022-06-19 14:13:10 +00:00
|
|
|
.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))
|
|
|
|
};
|
2021-01-04 00:44:02 +00:00
|
|
|
|
2022-06-19 14:13:10 +00:00
|
|
|
Self {
|
|
|
|
position,
|
2022-07-29 12:21:23 +00:00
|
|
|
|
|
|
|
fullscreen: window.fullscreen().is_some(),
|
|
|
|
|
2021-01-04 00:44:02 +00:00
|
|
|
inner_size_points: Some(egui::vec2(
|
2022-07-11 21:08:48 +00:00
|
|
|
inner_size_points.width,
|
|
|
|
inner_size_points.height,
|
2021-01-04 00:44:02 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 15:33:28 +00:00
|
|
|
pub fn initialize_window(
|
2021-01-04 00:44:02 +00:00
|
|
|
&self,
|
2021-09-28 15:33:28 +00:00
|
|
|
mut window: winit::window::WindowBuilder,
|
|
|
|
) -> winit::window::WindowBuilder {
|
|
|
|
if !cfg!(target_os = "windows") {
|
|
|
|
// 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.
|
2021-10-20 11:54:08 +00:00
|
|
|
// If this happens on Windows, the window is hidden and very difficult to find.
|
2021-09-28 15:33:28 +00:00
|
|
|
// So we don't restore window positions on Windows.
|
2022-06-19 14:13:10 +00:00
|
|
|
if let Some(pos) = self.position {
|
2021-09-28 15:33:28 +00:00
|
|
|
window = window.with_position(winit::dpi::PhysicalPosition {
|
|
|
|
x: pos.x as f64,
|
|
|
|
y: pos.y as f64,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:44:02 +00:00
|
|
|
if let Some(inner_size_points) = self.inner_size_points {
|
2022-07-29 12:21:23 +00:00
|
|
|
window
|
|
|
|
.with_inner_size(winit::dpi::LogicalSize {
|
|
|
|
width: inner_size_points.x as f64,
|
|
|
|
height: inner_size_points.y as f64,
|
|
|
|
})
|
|
|
|
.with_fullscreen(
|
|
|
|
self.fullscreen
|
|
|
|
.then(|| winit::window::Fullscreen::Borderless(None)),
|
|
|
|
)
|
2021-01-04 00:44:02 +00:00
|
|
|
} else {
|
|
|
|
window
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|