mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
Move WindowSettings from egui_glium to egui-winit
This commit is contained in:
@@ -26,6 +26,7 @@ epi = { version = "0.14.0", path = "../epi" }
|
||||
winit = "0.25"
|
||||
|
||||
copypasta = { version = "0.7", optional = true }
|
||||
serde = { version = "1.0", optional = true, features = ["derive"] }
|
||||
webbrowser = { version = "0.5", optional = true }
|
||||
|
||||
# feature screen_reader
|
||||
@@ -43,3 +44,5 @@ links = ["webbrowser"]
|
||||
|
||||
# experimental support for a screen reader
|
||||
screen_reader = ["tts"]
|
||||
|
||||
serialize = ["egui/serialize", "serde"]
|
||||
|
||||
@@ -76,6 +76,9 @@ pub use winit;
|
||||
|
||||
pub mod clipboard;
|
||||
pub mod screen_reader;
|
||||
mod window_settings;
|
||||
|
||||
pub use window_settings::WindowSettings;
|
||||
|
||||
pub fn native_pixels_per_point(window: &winit::window::Window) -> f32 {
|
||||
window.scale_factor() as f32
|
||||
|
||||
55
egui-winit/src/window_settings.rs
Normal file
55
egui-winit/src/window_settings.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
/// Can be used to store 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 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 {
|
||||
pos: window
|
||||
.outer_position()
|
||||
.ok()
|
||||
.map(|p| egui::pos2(p.x as f32, p.y as f32)),
|
||||
|
||||
inner_size_points: Some(egui::vec2(
|
||||
inner_size_points.width as f32,
|
||||
inner_size_points.height as f32,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn initialize_window(
|
||||
&self,
|
||||
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.
|
||||
// If this happens on Windows, the window is hidden and impossible to bring to get at.
|
||||
// So we don't restore window positions on Windows.
|
||||
if let Some(pos) = self.pos {
|
||||
window = window.with_position(winit::dpi::PhysicalPosition {
|
||||
x: pos.x as f64,
|
||||
y: pos.y as f64,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(inner_size_points) = self.inner_size_points {
|
||||
window.with_inner_size(winit::dpi::LogicalSize {
|
||||
width: inner_size_points.x as f64,
|
||||
height: inner_size_points.y as f64,
|
||||
})
|
||||
} else {
|
||||
window
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user