Skip to content

Commit b891eeb

Browse files
committed
simplify
1 parent 6c63dab commit b891eeb

File tree

4 files changed

+101
-140
lines changed

4 files changed

+101
-140
lines changed

app/src/gui/epi.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,4 @@ pub trait App {
1010
///
1111
/// To force a repaint, call [`egui::Context::request_repaint`] at any time (e.g. from another thread).
1212
fn update(&mut self, ctx: &egui::Context);
13-
14-
/// Background color values for the app, e.g. what is sent to `gl.clearColor`.
15-
///
16-
/// This is the background of your windows if you don't set a central panel.
17-
///
18-
/// ATTENTION:
19-
/// Since these float values go to the render as-is, any color space conversion as done
20-
/// e.g. by converting from [`egui::Color32`] to [`egui::Rgba`] may cause incorrect results.
21-
/// egui recommends that rendering backends use a normal "gamma-space" (non-sRGB-aware) blending,
22-
/// which means the values you return here should also be in `sRGB` gamma-space in the 0-1 range.
23-
/// You can use [`egui::Color32::to_normalized_gamma_f32`] for this.
24-
fn clear_color(&self, _visuals: &egui::Visuals) -> [f32; 4] {
25-
// NOTE: a bright gray makes the shadows of the windows look weird.
26-
// We use a bit of transparency so that if the user switches on the
27-
// `transparent()` option they get immediate results.
28-
egui::Color32::from_rgba_unmultiplied(12, 12, 12, 180).to_normalized_gamma_f32()
29-
30-
// _visuals.window_fill() would also be a natural choice
31-
}
3213
}

app/src/gui/wgpu_app.rs

Lines changed: 50 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
//! Note that this file contains two similar paths - one for [`glow`], one for [`wgpu`].
22
//! When making changes to one you often also want to apply it to the other.
33
4-
use std::time::Instant;
5-
6-
use egui_winit::winit;
7-
use std::sync::Arc;
8-
use winit::event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget};
9-
104
use super::epi;
115
use crate::common::APP_NAME;
12-
use crate::run;
6+
use crate::UserEvent;
137
use egui_winit::egui;
148
use egui_winit::egui::Visuals;
15-
use egui_winit::winit::event::Event;
16-
17-
#[derive(Debug)]
18-
pub enum UserEvent {
19-
OpenWindow,
20-
Exit,
21-
RequestRepaint {
22-
when: Instant,
23-
/// What the frame number was when the repaint was _requested_.
24-
frame_nr: u64,
25-
},
26-
}
9+
use egui_winit::winit;
10+
use egui_winit::winit::event::{Event, WindowEvent};
11+
use std::sync::Arc;
12+
use std::time::Instant;
13+
use winit::event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget};
2714

2815
#[derive(Debug)]
29-
pub enum EventResult {
16+
pub enum NextPaint {
17+
/// Wait for an event
3018
Wait,
3119

3220
/// Causes a synchronous repaint inside the event handler. This should only
@@ -42,8 +30,10 @@ pub enum EventResult {
4230
/// cause any delay like `RepaintNow`.
4331
RepaintNext,
4432

33+
/// Repaint at a particular time
4534
RepaintAt(Instant),
4635

36+
/// Exit the event loop
4737
Exit,
4838
}
4939

@@ -57,6 +47,12 @@ struct WgpuWinitRunning {
5747
egui_winit: egui_winit::State,
5848
}
5949

50+
impl Drop for WgpuWinitRunning {
51+
fn drop(&mut self) {
52+
self.painter.destroy();
53+
}
54+
}
55+
6056
pub type AppCreator = Box<dyn Fn() -> Box<dyn epi::App>>;
6157

6258
pub struct WgpuWinitApp {
@@ -74,10 +70,10 @@ impl WgpuWinitApp {
7470
}
7571
}
7672

77-
pub fn init_run_state(
73+
pub fn launch_window(
7874
&mut self,
7975
event_loop: &EventLoopWindowTarget<UserEvent>,
80-
) -> anyhow::Result<()> {
76+
) -> anyhow::Result<NextPaint> {
8177
let window = winit::window::WindowBuilder::new()
8278
.with_decorations(true)
8379
.with_resizable(true)
@@ -114,7 +110,6 @@ impl WgpuWinitApp {
114110

115111
let event_loop_proxy = self.repaint_proxy.clone();
116112
egui_ctx.set_request_repaint_callback(move |info| {
117-
log::trace!("request_repaint_callback: {info:?}");
118113
let when = Instant::now() + info.after;
119114
let frame_nr = info.current_frame_nr;
120115
event_loop_proxy
@@ -134,7 +129,7 @@ impl WgpuWinitApp {
134129
egui_winit,
135130
});
136131

137-
Ok(())
132+
Ok(NextPaint::RepaintNow)
138133
}
139134

140135
pub fn frame_nr(&self) -> u64 {
@@ -145,13 +140,7 @@ impl WgpuWinitApp {
145140
self.running.as_ref().map(|r| &r.window)
146141
}
147142

148-
pub fn destroy(&mut self) {
149-
if let Some(mut running) = self.running.take() {
150-
running.painter.destroy();
151-
}
152-
}
153-
154-
pub fn run_ui_and_paint(&mut self) -> EventResult {
143+
pub fn paint(&mut self) -> NextPaint {
155144
if let Some(running) = &mut self.running {
156145
let raw_input = running.egui_winit.take_egui_input(&running.window);
157146

@@ -178,57 +167,45 @@ impl WgpuWinitApp {
178167

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

170+
let clear_color =
171+
egui::Color32::from_rgba_unmultiplied(12, 12, 12, 180).to_normalized_gamma_f32();
172+
181173
running.painter.paint_and_update_textures(
182174
running.egui_ctx.pixels_per_point(),
183-
running.app.clear_color(&running.egui_ctx.style().visuals),
175+
clear_color,
184176
&clipped_primitives,
185177
&textures_delta,
186178
false,
187179
);
188180

189-
let control_flow = if repaint_after.is_zero() {
190-
EventResult::RepaintNext
191-
} else if let Some(repaint_after_instant) =
192-
Instant::now().checked_add(repaint_after)
193-
{
194-
// if repaint_after is something huge and can't be added to Instant,
195-
// we will use `ControlFlow::Wait` instead.
196-
// technically, this might lead to some weird corner cases where the user *WANTS*
197-
// winit to use `WaitUntil(MAX_INSTANT)` explicitly. they can roll their own
198-
// egui backend impl i guess.
199-
EventResult::RepaintAt(repaint_after_instant)
181+
if repaint_after.is_zero() {
182+
NextPaint::RepaintNext
183+
} else if let Some(repaint_after_instant) = Instant::now().checked_add(repaint_after) {
184+
NextPaint::RepaintAt(repaint_after_instant)
200185
} else {
201-
EventResult::Wait
202-
};
203-
204-
if running.window.is_minimized() == Some(true) {
205-
// On Mac, a minimized Window uses up all CPU:
206-
// https://github.com/emilk/egui/issues/325
207-
// crate::profile_scope!("bg_sleep");
208-
std::thread::sleep(std::time::Duration::from_millis(10));
186+
NextPaint::Wait
209187
}
210-
211-
control_flow
212188
} else {
213-
EventResult::Wait
189+
NextPaint::Wait
214190
}
215191
}
216192

217193
pub fn on_event(
218194
&mut self,
219195
event_loop: &EventLoopWindowTarget<UserEvent>,
220196
event: &Event<'_, UserEvent>,
221-
) -> anyhow::Result<EventResult> {
197+
) -> anyhow::Result<NextPaint> {
222198
Ok(match event {
223-
e if matches!(e, Event::Resumed)
224-
|| matches!(e, Event::UserEvent(UserEvent::OpenWindow)) =>
225-
{
226-
if self.running.is_none() {
227-
self.init_run_state(event_loop)?;
228-
}
229-
EventResult::RepaintNow
199+
// When the window is closed, we destroy the Window
200+
Event::WindowEvent {
201+
event: WindowEvent::CloseRequested,
202+
..
203+
} => {
204+
self.running = None;
205+
NextPaint::Wait
230206
}
231-
Event::Suspended => EventResult::Wait,
207+
208+
Event::Resumed if self.running.is_none() => self.launch_window(event_loop)?,
232209

233210
Event::WindowEvent { event, .. } => {
234211
if let Some(running) = &mut self.running {
@@ -248,7 +225,7 @@ impl WgpuWinitApp {
248225
let mut repaint_asap = false;
249226

250227
match &event {
251-
winit::event::WindowEvent::Resized(physical_size) => {
228+
WindowEvent::Resized(physical_size) => {
252229
repaint_asap = true;
253230

254231
// Resize with 0 width and height is used by winit to signal a minimize event on Windows.
@@ -260,21 +237,17 @@ impl WgpuWinitApp {
260237
.on_window_resized(physical_size.width, physical_size.height);
261238
}
262239
}
263-
winit::event::WindowEvent::ScaleFactorChanged {
264-
new_inner_size, ..
265-
} => {
240+
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
266241
repaint_asap = true;
267242
running
268243
.painter
269244
.on_window_resized(new_inner_size.width, new_inner_size.height);
270245
}
271-
winit::event::WindowEvent::CloseRequested => {
246+
WindowEvent::CloseRequested => {
272247
log::debug!("Received WindowEvent::CloseRequested");
273-
return Ok(EventResult::Exit);
248+
return Ok(NextPaint::Exit);
274249
}
275-
winit::event::WindowEvent::ThemeChanged(winit_theme)
276-
if running.follow_system_theme =>
277-
{
250+
WindowEvent::ThemeChanged(winit_theme) if running.follow_system_theme => {
278251
let visuals = visuals_from_winit_theme(*winit_theme);
279252
running.egui_ctx.set_visuals(visuals);
280253
}
@@ -285,19 +258,19 @@ impl WgpuWinitApp {
285258

286259
if event_response.repaint {
287260
if repaint_asap {
288-
EventResult::RepaintNow
261+
NextPaint::RepaintNow
289262
} else {
290-
EventResult::RepaintNext
263+
NextPaint::RepaintNext
291264
}
292265
} else {
293-
EventResult::Wait
266+
NextPaint::Wait
294267
}
295268
} else {
296-
EventResult::Wait
269+
NextPaint::Wait
297270
}
298271
}
299272

300-
_ => EventResult::Wait,
273+
_ => NextPaint::Wait,
301274
})
302275
}
303276
}

0 commit comments

Comments
 (0)