Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-pro committed Dec 8, 2023
1 parent 467bb7c commit d7fd90d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 172 deletions.
133 changes: 1 addition & 132 deletions app/src/gui/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand All @@ -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<egui::Pos2>,

/// 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<egui::Vec2>,
}


/// 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<egui::Vec2>,

/// 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<String>,

/// Set to some bool to change window decorations.
#[cfg(not(target_arch = "wasm32"))]
pub decorated: Option<bool>,

/// Set to some bool to change window fullscreen.
#[cfg(not(target_arch = "wasm32"))] // TODO: implement fullscreen on web
pub fullscreen: Option<bool>,

/// 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<egui::Pos2>,

/// Set to some bool to change window visibility.
#[cfg(not(target_arch = "wasm32"))]
pub visible: Option<bool>,

/// Set to some bool to tell the window always on top.
#[cfg(not(target_arch = "wasm32"))]
pub always_on_top: Option<bool>,

/// Set to some bool to minimize or unminimize window.
#[cfg(not(target_arch = "wasm32"))]
pub minimized: Option<bool>,

/// Set to some bool to maximize or unmaximize window.
#[cfg(not(target_arch = "wasm32"))]
pub maximized: Option<bool>,

/// Set to some bool to focus window.
#[cfg(not(target_arch = "wasm32"))]
pub focus: Option<bool>,

/// Set to request a user's attention to the native window.
#[cfg(not(target_arch = "wasm32"))]
pub attention: Option<egui::UserAttentionType>,

#[cfg(not(target_arch = "wasm32"))]
pub screenshot_requested: bool,
}
37 changes: 2 additions & 35 deletions app/src/gui/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions app/src/gui/wgpu_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d7fd90d

Please sign in to comment.