From d7fd90de08737bcc04801775d8e5c4977738cf4a Mon Sep 17 00:00:00 2001 From: Jacob Halsey Date: Fri, 8 Dec 2023 23:36:47 +0000 Subject: [PATCH] simplify --- app/src/gui/epi.rs | 133 +-------------------------------- app/src/gui/epi_integration.rs | 37 +-------- app/src/gui/wgpu_app.rs | 7 +- app/src/main.rs | 2 +- 4 files changed, 7 insertions(+), 172 deletions(-) diff --git a/app/src/gui/epi.rs b/app/src/gui/epi.rs index 40b825c..ce2a415 100644 --- a/app/src/gui/epi.rs +++ b/app/src/gui/epi.rs @@ -9,22 +9,8 @@ pub trait App { /// The [`egui::Context`] can be cloned and saved if you like. /// /// To force a repaint, call [`egui::Context::request_repaint`] at any time (e.g. from another thread). - fn update(&mut self, ctx: &egui::Context, frame: &mut Frame); + fn update(&mut self, ctx: &egui::Context); - // --------- - // Settings: - - /// Time between automatic calls to [`Self::save`] - fn auto_save_interval(&self) -> std::time::Duration { - std::time::Duration::from_secs(30) - } - - /// The size limit of the web app canvas. - /// - /// By default the max size is [`egui::Vec2::INFINITY`], i.e. unlimited. - fn max_size_points(&self) -> egui::Vec2 { - egui::Vec2::INFINITY - } /// Background color values for the app, e.g. what is sent to `gl.clearColor`. /// @@ -44,121 +30,4 @@ pub trait App { // _visuals.window_fill() would also be a natural choice } - - /// Called each time after the rendering the UI. - /// - /// Can be used to access pixel data with [`Frame::screenshot`] - fn post_rendering(&mut self, _window_size_px: [u32; 2], _frame: &Frame) {} -} - -/// Information about the application's main window, if available. -#[cfg(not(target_arch = "wasm32"))] -#[derive(Clone, Debug)] -pub struct WindowInfo { - /// Coordinates of the window's outer top left corner, relative to the top left corner of the first display. - /// - /// Unit: egui points (logical pixels). - /// - /// `None` = unknown. - pub position: Option, - - /// Are we in fullscreen mode? - pub fullscreen: bool, - - /// Are we minimized? - pub minimized: bool, - - /// Are we maximized? - pub maximized: bool, - - /// Is the window focused and able to receive input? - /// - /// This should be the same as [`egui::InputState::focused`]. - pub focused: bool, - - /// Window inner size in egui points (logical pixels). - pub size: egui::Vec2, - - /// Current monitor size in egui points (logical pixels) - pub monitor_size: Option, -} - - -/// Information about the integration passed to the use app each frame. -#[derive(Clone, Debug)] -pub struct IntegrationInfo { - /// The position and size of the native window. - #[cfg(not(target_arch = "wasm32"))] - pub window_info: WindowInfo, -} - -/// Represents the surroundings of your app. -/// -/// It provides methods to inspect the surroundings (are we on the web?), -/// allocate textures, and change settings (e.g. window size). -pub struct Frame { - - /// Where the app can issue commands back to the integration. - pub(crate) output: AppOutput, -} - - -/// Action that can be taken by the user app. -#[derive(Clone, Debug, Default)] -#[must_use] -pub struct AppOutput { - /// Set to `true` to close the native window (which often quits the app). - #[cfg(not(target_arch = "wasm32"))] - pub close: bool, - - /// Set to some size to resize the outer window (e.g. glium window) to this size. - #[cfg(not(target_arch = "wasm32"))] - pub window_size: Option, - - /// Set to some string to rename the outer window (e.g. glium window) to this title. - #[cfg(not(target_arch = "wasm32"))] - pub window_title: Option, - - /// Set to some bool to change window decorations. - #[cfg(not(target_arch = "wasm32"))] - pub decorated: Option, - - /// Set to some bool to change window fullscreen. - #[cfg(not(target_arch = "wasm32"))] // TODO: implement fullscreen on web - pub fullscreen: Option, - - /// Set to true to drag window while primary mouse button is down. - #[cfg(not(target_arch = "wasm32"))] - pub drag_window: bool, - - /// Set to some position to move the outer window (e.g. glium window) to this position - #[cfg(not(target_arch = "wasm32"))] - pub window_pos: Option, - - /// Set to some bool to change window visibility. - #[cfg(not(target_arch = "wasm32"))] - pub visible: Option, - - /// Set to some bool to tell the window always on top. - #[cfg(not(target_arch = "wasm32"))] - pub always_on_top: Option, - - /// Set to some bool to minimize or unminimize window. - #[cfg(not(target_arch = "wasm32"))] - pub minimized: Option, - - /// Set to some bool to maximize or unmaximize window. - #[cfg(not(target_arch = "wasm32"))] - pub maximized: Option, - - /// Set to some bool to focus window. - #[cfg(not(target_arch = "wasm32"))] - pub focus: Option, - - /// Set to request a user's attention to the native window. - #[cfg(not(target_arch = "wasm32"))] - pub attention: Option, - - #[cfg(not(target_arch = "wasm32"))] - pub screenshot_requested: bool, } diff --git a/app/src/gui/epi_integration.rs b/app/src/gui/epi_integration.rs index f7d083c..26cf710 100644 --- a/app/src/gui/epi_integration.rs +++ b/app/src/gui/epi_integration.rs @@ -6,15 +6,11 @@ use egui_winit::egui::Visuals; use super::epi::{self}; -// ---------------------------------------------------------------------------- - /// Everything needed to make a winit-based integration for [`epi`]. pub struct EpiIntegration { - pub frame: epi::Frame, pub egui_ctx: egui::Context, pending_full_output: egui::FullOutput, egui_winit: egui_winit::State, - can_drag_window: bool, follow_system_theme: bool, } @@ -31,13 +27,6 @@ impl EpiIntegration { let native_pixels_per_point = window.scale_factor() as f32; - let frame = epi::Frame { - output: epi::AppOutput { - visible: Some(true), - ..Default::default() - }, - }; - let mut egui_winit = egui_winit::State::new(event_loop); egui_winit.set_max_texture_side(max_texture_side); egui_winit.set_pixels_per_point(native_pixels_per_point); @@ -46,29 +35,20 @@ impl EpiIntegration { egui_ctx.set_visuals(system_theme.map(visuals_from_winit_theme).unwrap_or(Visuals::light())); Self { - frame, egui_ctx, egui_winit, pending_full_output: Default::default(), - can_drag_window: false, follow_system_theme: system_theme.is_some(), } } pub fn on_event( &mut self, - app: &mut dyn epi::App, event: &winit::event::WindowEvent<'_>, ) -> EventResponse { - use winit::event::{ElementState, MouseButton, WindowEvent}; + use winit::event::WindowEvent; match event { - WindowEvent::MouseInput { - button: MouseButton::Left, - state: ElementState::Pressed, - .. - } => self.can_drag_window = true, - WindowEvent::ThemeChanged(winit_theme) if self.follow_system_theme => { let visuals = visuals_from_winit_theme(*winit_theme); self.egui_ctx.set_visuals(visuals); @@ -88,7 +68,7 @@ impl EpiIntegration { // Run user code: let full_output = self.egui_ctx.run(raw_input, |egui_ctx| { - app.update(egui_ctx, &mut self.frame); + app.update(egui_ctx); }); self.pending_full_output.append(full_output); @@ -97,19 +77,6 @@ impl EpiIntegration { full_output } - pub fn post_rendering(&mut self, app: &mut dyn epi::App, window: &winit::window::Window) { - let inner_size = window.inner_size(); - let window_size_px = [inner_size.width, inner_size.height]; - - app.post_rendering(window_size_px, &self.frame); - } - - pub fn post_present(&mut self, window: &winit::window::Window) { - if let Some(visible) = self.frame.output.visible.take() { - window.set_visible(visible); - } - } - pub fn handle_platform_output( &mut self, window: &winit::window::Window, diff --git a/app/src/gui/wgpu_app.rs b/app/src/gui/wgpu_app.rs index b594b60..1a4f9ea 100644 --- a/app/src/gui/wgpu_app.rs +++ b/app/src/gui/wgpu_app.rs @@ -186,9 +186,6 @@ impl WgpuWinitApp { false, ); - integration.post_rendering(app.as_mut(), window); - integration.post_present(window); - let control_flow = if repaint_after.is_zero() { EventResult::RepaintNext } else if let Some(repaint_after_instant) = @@ -283,7 +280,9 @@ impl WgpuWinitApp { }; let event_response = - running.integration.on_event(running.app.as_mut(), event); + running.integration.on_event(event); + + if event_response.repaint { if repaint_asap { EventResult::RepaintNow diff --git a/app/src/main.rs b/app/src/main.rs index 9f14bb2..0290841 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -25,7 +25,7 @@ struct MyEguiApp { } impl gui::epi::App for MyEguiApp { - fn update(&mut self, ctx: &egui::Context, frame: &mut gui::epi::Frame) { + fn update(&mut self, ctx: &egui::Context) { egui::CentralPanel::default().show(ctx, |ui| { ui.heading("Hello World!"); if ui.button("Click each year").clicked() {