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 d7fd90d commit f2ac9ee
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 126 deletions.
95 changes: 0 additions & 95 deletions app/src/gui/epi_integration.rs

This file was deleted.

1 change: 0 additions & 1 deletion app/src/gui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod wgpu_app;
mod epi_integration;
pub mod epi;
94 changes: 64 additions & 30 deletions app/src/gui/wgpu_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use egui_winit::winit;
use std::sync::Arc;

use egui_winit::egui;
use egui_winit::egui::Visuals;
use egui_winit::winit::event::Event;
use crate::common::APP_NAME;
use crate::run;
use super::epi;
use super::epi_integration::{self, EpiIntegration};

#[derive(Debug)]
pub enum UserEvent {
Expand Down Expand Up @@ -51,9 +52,13 @@ pub enum EventResult {
/// initialized once the application has an associated `SurfaceView`.
struct WgpuWinitRunning {
painter: egui_wgpu::winit::Painter,
integration: epi_integration::EpiIntegration,
app: Box<dyn epi::App>,
window: winit::window::Window,
follow_system_theme: bool,

egui_ctx: egui::Context,
pending_full_output: egui::FullOutput,
egui_winit: egui_winit::State,
}

pub type AppCreator = Box<dyn Fn() -> Box<dyn epi::App>>;
Expand Down Expand Up @@ -104,18 +109,27 @@ impl WgpuWinitApp {
);
pollster::block_on(painter.set_window(Some(&window)))?;

let integration = EpiIntegration::new(
event_loop,
painter.max_texture_side().unwrap_or(2048),
&window,
);
let max_texture_side = painter.max_texture_side().unwrap_or(2048);

let egui_ctx = egui::Context::default();

let memory = Default::default();
egui_ctx.memory_mut(|mem| *mem = memory);

let native_pixels_per_point = window.scale_factor() as f32;

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);

let system_theme = window.theme();
egui_ctx.set_visuals(system_theme.map(visuals_from_winit_theme).unwrap_or(Visuals::light()));

window.set_ime_allowed(true);

{
let event_loop_proxy = self.repaint_proxy.clone();
integration
.egui_ctx
egui_ctx
.set_request_repaint_callback(move |info| {
log::trace!("request_repaint_callback: {info:?}");
let when = Instant::now() + info.after;
Expand All @@ -132,9 +146,12 @@ impl WgpuWinitApp {

self.running = Some(WgpuWinitRunning {
painter,
integration,
app,
window
window,
follow_system_theme: system_theme.is_some(),
egui_ctx,
pending_full_output: Default::default(),
egui_winit
});

Ok(())
Expand All @@ -143,7 +160,7 @@ impl WgpuWinitApp {
pub fn frame_nr(&self) -> u64 {
self.running
.as_ref()
.map_or(0, |r| r.integration.egui_ctx.frame_nr())
.map_or(0, |r| r.egui_ctx.frame_nr())
}

pub fn window(&self) -> Option<&winit::window::Window> {
Expand All @@ -158,29 +175,35 @@ impl WgpuWinitApp {

pub fn run_ui_and_paint(&mut self) -> EventResult {
if let Some(running) = &mut self.running {
let WgpuWinitRunning {
app,
integration,
painter,
window
} = running;

let raw_input = running.egui_winit.take_egui_input(&running.window);

// Run user code:
let full_output = running.egui_ctx.run(raw_input, |egui_ctx| {
running.app.update(egui_ctx);
});

running.pending_full_output.append(full_output);
let full_output = std::mem::take(&mut running.pending_full_output);

let egui::FullOutput {
platform_output,
repaint_after,
textures_delta,
shapes,
} = integration.update(app.as_mut(), window);
} = full_output;

running.egui_winit
.handle_platform_output(&running.window, &running.egui_ctx, platform_output);

integration.handle_platform_output(window, platform_output);

let clipped_primitives = {
integration.egui_ctx.tessellate(shapes)
running.egui_ctx.tessellate(shapes)
};

painter.paint_and_update_textures(
integration.egui_ctx.pixels_per_point(),
app.clear_color(&integration.egui_ctx.style().visuals),
running.painter.paint_and_update_textures(
running.egui_ctx.pixels_per_point(),
running.app.clear_color(&running.egui_ctx.style().visuals),
&clipped_primitives,
&textures_delta,
false,
Expand All @@ -201,7 +224,7 @@ impl WgpuWinitApp {
EventResult::Wait
};

if window.is_minimized() == Some(true) {
if running.window.is_minimized() == Some(true) {
// On Mac, a minimized Window uses up all CPU:
// https://github.com/emilk/egui/issues/325
// crate::profile_scope!("bg_sleep");
Expand All @@ -217,7 +240,7 @@ impl WgpuWinitApp {
pub fn on_event(
&mut self,
event_loop: &EventLoopWindowTarget<UserEvent>,
event: &winit::event::Event<'_, UserEvent>,
event: &Event<'_, UserEvent>,
) -> anyhow::Result<EventResult> {
Ok(match event {
e if matches!(e, Event::Resumed) || matches!(e, Event::UserEvent(UserEvent::OpenWindow)) => {
Expand All @@ -227,11 +250,11 @@ impl WgpuWinitApp {
}
EventResult::RepaintNow
}
winit::event::Event::Suspended => {
Event::Suspended => {
EventResult::Wait
}

winit::event::Event::WindowEvent { event, .. } => {
Event::WindowEvent { event, .. } => {
if let Some(running) = &mut self.running {
// On Windows, if a window is resized by the user, it should repaint synchronously, inside the
// event handler.
Expand Down Expand Up @@ -276,11 +299,14 @@ impl WgpuWinitApp {
log::debug!("Received WindowEvent::CloseRequested");
return Ok(EventResult::Exit);
}
winit::event::WindowEvent::ThemeChanged(winit_theme) if running.follow_system_theme => {
let visuals = visuals_from_winit_theme(*winit_theme);
running.egui_ctx.set_visuals(visuals);
}
_ => {}
};

let event_response =
running.integration.on_event(event);
let event_response = running.egui_winit.on_event(&running.egui_ctx, event);


if event_response.repaint {
Expand All @@ -301,4 +327,12 @@ impl WgpuWinitApp {
_ => EventResult::Wait,
})
}
}


fn visuals_from_winit_theme(theme: winit::window::Theme) -> Visuals {
match theme {
winit::window::Theme::Dark => Visuals::dark(),
winit::window::Theme::Light => Visuals::light(),
}
}

0 comments on commit f2ac9ee

Please sign in to comment.