-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
epi_backend.rs
141 lines (116 loc) · 5.1 KB
/
epi_backend.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use glium::glutin;
use crate::*;
struct RequestRepaintEvent;
struct GliumRepaintSignal(
std::sync::Mutex<glutin::event_loop::EventLoopProxy<RequestRepaintEvent>>,
);
impl epi::backend::RepaintSignal for GliumRepaintSignal {
fn request_repaint(&self) {
self.0.lock().unwrap().send_event(RequestRepaintEvent).ok();
}
}
fn create_display(
window_builder: glutin::window::WindowBuilder,
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
) -> glium::Display {
let context_builder = glutin::ContextBuilder::new()
.with_depth_buffer(0)
.with_srgb(true)
.with_stencil_buffer(0)
.with_vsync(true);
glium::Display::new(window_builder, context_builder, event_loop).unwrap()
}
// ----------------------------------------------------------------------------
pub use epi::NativeOptions;
/// Run an egui app
pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
let persistence = egui_winit::epi::Persistence::from_app_name(app.name());
let window_settings = persistence.load_window_settings();
let window_builder =
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app.name());
let event_loop = glutin::event_loop::EventLoop::with_user_event();
let display = create_display(window_builder, &event_loop);
let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(std::sync::Mutex::new(
event_loop.create_proxy(),
)));
let mut painter = crate::Painter::new(&display);
let mut integration = egui_winit::epi::EpiIntegration::new(
"egui_glium",
painter.max_texture_side(),
display.gl_window().window(),
repaint_signal,
persistence,
app,
);
let mut is_focused = true;
event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
if !is_focused {
// On Mac, a minimized Window uses up all CPU: https://github.com/emilk/egui/issues/325
// We can't know if we are minimized: https://github.com/rust-windowing/winit/issues/208
// But we know if we are focused (in foreground). When minimized, we are not focused.
// However, a user may want an egui with an animation in the background,
// so we still need to repaint quite fast.
std::thread::sleep(std::time::Duration::from_millis(10));
}
let egui::FullOutput {
platform_output,
needs_repaint,
textures_delta,
shapes,
} = integration.update(display.gl_window().window());
integration.handle_platform_output(display.gl_window().window(), platform_output);
let clipped_primitives = integration.egui_ctx.tessellate(shapes);
// paint:
{
use glium::Surface as _;
let mut target = display.draw();
let color = integration.app.clear_color();
target.clear_color(color[0], color[1], color[2], color[3]);
painter.paint_and_update_textures(
&display,
&mut target,
integration.egui_ctx.pixels_per_point(),
clipped_primitives,
&textures_delta,
);
target.finish().unwrap();
}
{
*control_flow = if integration.should_quit() {
glutin::event_loop::ControlFlow::Exit
} else if needs_repaint {
display.gl_window().window().request_redraw();
glutin::event_loop::ControlFlow::Poll
} else {
glutin::event_loop::ControlFlow::Wait
};
}
integration.maybe_autosave(display.gl_window().window());
};
match event {
// Platform-dependent event handlers to workaround a winit bug
// See: https://github.com/rust-windowing/winit/issues/987
// See: https://github.com/rust-windowing/winit/issues/1619
glutin::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
glutin::event::Event::WindowEvent { event, .. } => {
if let glutin::event::WindowEvent::Focused(new_focused) = event {
is_focused = new_focused;
}
integration.on_event(&event);
if integration.should_quit() {
*control_flow = glium::glutin::event_loop::ControlFlow::Exit;
}
display.gl_window().window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
}
glutin::event::Event::LoopDestroyed => {
integration.on_exit(display.gl_window().window());
}
glutin::event::Event::UserEvent(RequestRepaintEvent) => {
display.gl_window().window().request_redraw();
}
_ => (),
}
});
}