From e2bdd40985ed2ba37109357fec3217b5dd67e68a Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 30 Sep 2021 19:18:51 +0200 Subject: [PATCH] Move WindowSettings from egui_glium to egui-winit --- Cargo.lock | 1 + egui-winit/Cargo.toml | 3 +++ egui-winit/src/lib.rs | 3 +++ .../src/window_settings.rs | 23 ++++--------------- egui_glium/Cargo.toml | 1 + egui_glium/src/backend.rs | 7 +++--- egui_glium/src/lib.rs | 1 - 7 files changed, 17 insertions(+), 22 deletions(-) rename {egui_glium => egui-winit}/src/window_settings.rs (70%) diff --git a/Cargo.lock b/Cargo.lock index e39da5444..18bb9305f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -844,6 +844,7 @@ dependencies = [ "copypasta", "egui", "epi", + "serde", "tts", "webbrowser", "winit", diff --git a/egui-winit/Cargo.toml b/egui-winit/Cargo.toml index 83b1f4e26..eaf2b5823 100644 --- a/egui-winit/Cargo.toml +++ b/egui-winit/Cargo.toml @@ -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"] diff --git a/egui-winit/src/lib.rs b/egui-winit/src/lib.rs index ed8a006bd..47cea53d0 100644 --- a/egui-winit/src/lib.rs +++ b/egui-winit/src/lib.rs @@ -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 diff --git a/egui_glium/src/window_settings.rs b/egui-winit/src/window_settings.rs similarity index 70% rename from egui_glium/src/window_settings.rs rename to egui-winit/src/window_settings.rs index 8bec20791..687b4abfe 100644 --- a/egui_glium/src/window_settings.rs +++ b/egui-winit/src/window_settings.rs @@ -1,7 +1,6 @@ -use egui_winit::winit; - +/// Can be used to store window settings (position and size). #[derive(Clone, Copy, Debug)] -#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct WindowSettings { /// outer position of window in physical pixels pos: Option, @@ -10,23 +9,11 @@ pub struct WindowSettings { } impl WindowSettings { - #[cfg(feature = "persistence")] - pub fn from_ron_file(settings_ron_path: impl AsRef) -> Option { - crate::persistence::read_ron(settings_ron_path) - } - - pub fn from_display(display: &glium::Display) -> Self { - let scale_factor = display.gl_window().window().scale_factor(); - let inner_size_points = display - .gl_window() - .window() - .inner_size() - .to_logical::(scale_factor); + pub fn from_display(window: &winit::window::Window) -> Self { + let inner_size_points = window.inner_size().to_logical::(window.scale_factor()); Self { - pos: display - .gl_window() - .window() + pos: window .outer_position() .ok() .map(|p| egui::pos2(p.x as f32, p.y as f32)), diff --git a/egui_glium/Cargo.toml b/egui_glium/Cargo.toml index 575c7e973..6052ec72a 100644 --- a/egui_glium/Cargo.toml +++ b/egui_glium/Cargo.toml @@ -54,6 +54,7 @@ links = ["egui-winit/links"] persistence = [ "directories-next", + "egui-winit/serialize", "egui/persistence", "epi/persistence", "ron", diff --git a/egui_glium/src/backend.rs b/egui_glium/src/backend.rs index e32767f07..67d655f8a 100644 --- a/egui_glium/src/backend.rs +++ b/egui_glium/src/backend.rs @@ -1,5 +1,6 @@ -use crate::{window_settings::WindowSettings, *}; +use crate::*; use egui::Color32; +use egui_winit::WindowSettings; #[cfg(target_os = "windows")] use glium::glutin::platform::windows::WindowBuilderExtWindows; use std::time::Instant; @@ -309,7 +310,7 @@ pub fn run(mut app: Box, native_options: &epi::NativeOptions) -> ! epi::set_value( storage.as_mut(), WINDOW_KEY, - &WindowSettings::from_display(&display), + &WindowSettings::from_display(display.gl_window().window()), ); } if app.persist_egui_memory() { @@ -350,7 +351,7 @@ pub fn run(mut app: Box, native_options: &epi::NativeOptions) -> ! epi::set_value( storage.as_mut(), WINDOW_KEY, - &WindowSettings::from_display(&display), + &WindowSettings::from_display(display.gl_window().window()), ); } if app.persist_egui_memory() { diff --git a/egui_glium/src/lib.rs b/egui_glium/src/lib.rs index 87064b7a2..28e48a9f9 100644 --- a/egui_glium/src/lib.rs +++ b/egui_glium/src/lib.rs @@ -80,7 +80,6 @@ mod backend; mod painter; #[cfg(feature = "persistence")] pub mod persistence; -pub mod window_settings; pub use backend::*; pub use painter::Painter;