2020-07-22 16:46:12 +00:00
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
use crate::{
|
2020-07-23 16:54:16 +00:00
|
|
|
storage::{FileStorage, WindowSettings},
|
2020-07-22 16:46:12 +00:00
|
|
|
*,
|
|
|
|
};
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
pub use egui::{
|
2020-10-17 10:33:30 +00:00
|
|
|
app::{self, App, Storage},
|
2020-09-11 06:56:47 +00:00
|
|
|
Srgba,
|
|
|
|
};
|
2020-07-23 16:54:16 +00:00
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
const EGUI_MEMORY_KEY: &str = "egui";
|
|
|
|
const WINDOW_KEY: &str = "window";
|
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
impl egui::app::TextureAllocator for Painter {
|
2020-09-11 06:56:47 +00:00
|
|
|
fn new_texture_srgba_premultiplied(
|
|
|
|
&mut self,
|
|
|
|
size: (usize, usize),
|
|
|
|
pixels: &[Srgba],
|
|
|
|
) -> egui::TextureId {
|
2020-10-17 10:33:30 +00:00
|
|
|
self.new_user_texture(size, pixels)
|
2020-09-11 06:56:47 +00:00
|
|
|
}
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run an egui app
|
2020-09-16 06:03:40 +00:00
|
|
|
pub fn run(title: &str, mut storage: FileStorage, mut app: impl App + 'static) -> ! {
|
2020-07-22 16:46:12 +00:00
|
|
|
let event_loop = glutin::event_loop::EventLoop::new();
|
|
|
|
let mut window = glutin::window::WindowBuilder::new()
|
|
|
|
.with_decorations(true)
|
|
|
|
.with_resizable(true)
|
|
|
|
.with_title(title)
|
|
|
|
.with_transparent(false);
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
let window_settings: Option<WindowSettings> = egui::app::get_value(&storage, WINDOW_KEY);
|
2020-07-22 16:46:12 +00:00
|
|
|
if let Some(window_settings) = &window_settings {
|
|
|
|
window = window_settings.initialize_size(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
let context = glutin::ContextBuilder::new()
|
|
|
|
.with_depth_buffer(0)
|
|
|
|
.with_srgb(true)
|
|
|
|
.with_stencil_buffer(0)
|
|
|
|
.with_vsync(true);
|
|
|
|
let display = glium::Display::new(window, context, &event_loop).unwrap();
|
|
|
|
|
|
|
|
if let Some(window_settings) = &window_settings {
|
|
|
|
window_settings.restore_positions(&display);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut ctx = egui::Context::new();
|
2020-07-23 16:54:16 +00:00
|
|
|
*ctx.memory() = egui::app::get_value(&storage, EGUI_MEMORY_KEY).unwrap_or_default();
|
2020-07-22 16:46:12 +00:00
|
|
|
|
|
|
|
let mut raw_input = make_raw_input(&display);
|
|
|
|
|
|
|
|
let start_time = Instant::now();
|
2020-10-17 10:33:30 +00:00
|
|
|
let mut previous_frame_time = None;
|
|
|
|
let mut painter = Painter::new(&display);
|
2020-07-22 16:46:12 +00:00
|
|
|
let mut clipboard = init_clipboard();
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2020-09-14 18:55:35 +00:00
|
|
|
let mut redraw = || {
|
|
|
|
let egui_start = Instant::now();
|
|
|
|
raw_input.time = start_time.elapsed().as_nanos() as f64 * 1e-9;
|
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
let backend_info = egui::app::BackendInfo {
|
|
|
|
web_info: None,
|
|
|
|
cpu_usage: previous_frame_time,
|
|
|
|
seconds_since_midnight: Some(seconds_since_midnight()),
|
|
|
|
};
|
|
|
|
|
2020-09-14 18:55:35 +00:00
|
|
|
let mut ui = ctx.begin_frame(raw_input.take());
|
2020-10-17 10:33:30 +00:00
|
|
|
let app_output = app.ui(&mut ui, &backend_info, Some(&mut painter));
|
|
|
|
let (egui_output, paint_jobs) = ctx.end_frame();
|
2020-09-14 18:55:35 +00:00
|
|
|
|
|
|
|
let frame_time = (Instant::now() - egui_start).as_secs_f64() as f32;
|
2020-10-17 10:33:30 +00:00
|
|
|
previous_frame_time = Some(frame_time);
|
|
|
|
painter.paint_jobs(&display, paint_jobs, &ctx.texture());
|
|
|
|
|
|
|
|
{
|
|
|
|
let egui::app::AppOutput { quit } = app_output;
|
|
|
|
|
|
|
|
*control_flow = if quit {
|
|
|
|
glutin::event_loop::ControlFlow::Exit
|
|
|
|
} else if egui_output.needs_repaint {
|
|
|
|
display.gl_window().window().request_redraw();
|
|
|
|
glutin::event_loop::ControlFlow::Poll
|
|
|
|
} else {
|
|
|
|
glutin::event_loop::ControlFlow::Wait
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_output(egui_output, &display, clipboard.as_mut());
|
2020-09-14 18:55:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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(),
|
2020-07-22 20:26:49 +00:00
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
glutin::event::Event::WindowEvent { event, .. } => {
|
|
|
|
input_to_egui(event, clipboard.as_mut(), &mut raw_input, control_flow);
|
2020-10-01 20:25:44 +00:00
|
|
|
display.gl_window().window().request_redraw(); // TODO: ask Egui if the events warrants a repaint instead
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
glutin::event::Event::LoopDestroyed => {
|
2020-07-23 16:54:16 +00:00
|
|
|
egui::app::set_value(
|
|
|
|
&mut storage,
|
|
|
|
WINDOW_KEY,
|
|
|
|
&WindowSettings::from_display(&display),
|
|
|
|
);
|
|
|
|
egui::app::set_value(&mut storage, EGUI_MEMORY_KEY, &*ctx.memory());
|
|
|
|
app.on_exit(&mut storage);
|
|
|
|
storage.save();
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|