egui/egui_glium/src/backend.rs

312 lines
11 KiB
Rust
Raw Normal View History

use crate::{window_settings::WindowSettings, *};
use egui::Color32;
use std::time::Instant;
#[cfg(feature = "persistence")]
const EGUI_MEMORY_KEY: &str = "egui";
#[cfg(feature = "persistence")]
const WINDOW_KEY: &str = "window";
#[cfg(feature = "persistence")]
fn deserialize_window_settings(storage: &Option<Box<dyn epi::Storage>>) -> Option<WindowSettings> {
epi::get_value(&**storage.as_ref()?, WINDOW_KEY)
}
#[cfg(not(feature = "persistence"))]
fn deserialize_window_settings(_: &Option<Box<dyn epi::Storage>>) -> Option<WindowSettings> {
None
}
#[cfg(feature = "persistence")]
fn deserialize_memory(storage: &Option<Box<dyn epi::Storage>>) -> Option<egui::Memory> {
epi::get_value(&**storage.as_ref()?, EGUI_MEMORY_KEY)
}
#[cfg(not(feature = "persistence"))]
fn deserialize_memory(_: &Option<Box<dyn epi::Storage>>) -> Option<egui::Memory> {
None
}
2020-12-29 13:15:46 +00:00
impl epi::TextureAllocator for Painter {
fn alloc_srgba_premultiplied(
&mut self,
size: (usize, usize),
2021-01-02 16:02:18 +00:00
srgba_pixels: &[Color32],
) -> egui::TextureId {
let id = self.alloc_user_texture();
self.set_user_texture(id, size, srgba_pixels);
id
}
fn free(&mut self, id: egui::TextureId) {
self.free_user_texture(id)
}
}
struct RequestRepaintEvent;
struct GliumRepaintSignal(
std::sync::Mutex<glutin::event_loop::EventLoopProxy<RequestRepaintEvent>>,
);
2020-12-29 13:15:46 +00:00
impl epi::RepaintSignal for GliumRepaintSignal {
fn request_repaint(&self) {
self.0.lock().unwrap().send_event(RequestRepaintEvent).ok();
}
}
2020-10-24 16:49:44 +00:00
fn create_display(
title: &str,
2020-10-24 16:49:44 +00:00
window_settings: Option<WindowSettings>,
is_resizable: bool,
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
2020-10-24 16:49:44 +00:00
) -> glium::Display {
let mut window_builder = glutin::window::WindowBuilder::new()
.with_decorations(true)
.with_resizable(is_resizable)
.with_title(title)
.with_transparent(false);
if let Some(window_settings) = &window_settings {
2020-10-24 16:49:44 +00:00
window_builder = window_settings.initialize_size(window_builder);
}
2020-10-24 16:49:44 +00:00
let context_builder = glutin::ContextBuilder::new()
.with_depth_buffer(0)
.with_srgb(true)
.with_stencil_buffer(0)
.with_vsync(true);
let display = glium::Display::new(window_builder, context_builder, &event_loop).unwrap();
if let Some(window_settings) = &window_settings {
window_settings.restore_positions(&display);
}
display
2020-10-24 16:49:44 +00:00
}
#[cfg(not(feature = "persistence"))]
fn create_storage(_app_name: &str) -> Option<Box<dyn epi::Storage>> {
None
}
#[cfg(feature = "persistence")]
2020-12-29 13:15:46 +00:00
fn create_storage(app_name: &str) -> Option<Box<dyn epi::Storage>> {
if let Some(proj_dirs) = directories_next::ProjectDirs::from("", "", app_name) {
let data_dir = proj_dirs.data_dir().to_path_buf();
if let Err(err) = std::fs::create_dir_all(&data_dir) {
eprintln!(
"Saving disabled: Failed to create app path at {:?}: {}",
data_dir, err
);
None
} else {
let mut config_dir = data_dir;
config_dir.push("app.json");
let storage = crate::persistence::FileStorage::from_path(config_dir);
Some(Box::new(storage))
}
} else {
eprintln!("Saving disabled: Failed to find path to data_dir.");
None
}
}
fn integration_info(
display: &glium::Display,
previous_frame_time: Option<f32>,
) -> epi::IntegrationInfo {
epi::IntegrationInfo {
web_info: None,
cpu_usage: previous_frame_time,
seconds_since_midnight: seconds_since_midnight(),
native_pixels_per_point: Some(native_pixels_per_point(&display)),
}
}
2020-10-24 16:49:44 +00:00
/// Run an egui app
2020-12-29 13:15:46 +00:00
pub fn run(mut app: Box<dyn epi::App>) -> ! {
let mut storage = create_storage(app.name());
if let Some(storage) = &mut storage {
app.load(storage.as_ref());
}
let window_settings = deserialize_window_settings(&storage);
let event_loop = glutin::event_loop::EventLoop::with_user_event();
let display = create_display(app.name(), window_settings, app.is_resizable(), &event_loop);
let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(std::sync::Mutex::new(
event_loop.create_proxy(),
)));
let mut ctx = egui::CtxRef::default();
*ctx.memory() = deserialize_memory(&storage).unwrap_or_default();
app.setup(&ctx);
2020-11-13 10:04:45 +00:00
let mut input_state = GliumInputState::from_pixels_per_point(native_pixels_per_point(&display));
let start_time = Instant::now();
let mut previous_frame_time = None;
let mut painter = Painter::new(&display);
let mut clipboard = init_clipboard();
#[cfg(feature = "persistence")]
let mut last_auto_save = Instant::now();
#[cfg(feature = "http")]
2020-12-31 13:31:11 +00:00
let http = std::sync::Arc::new(crate::http::GliumHttp {});
if app.warm_up_enabled() {
// let warm_up_start = Instant::now();
input_state.raw.time = Some(0.0);
input_state.raw.screen_rect = Some(Rect::from_min_size(
Default::default(),
screen_size_in_pixels(&display) / input_state.raw.pixels_per_point.unwrap(),
));
ctx.begin_frame(input_state.raw.take());
let mut app_output = epi::backend::AppOutput::default();
let mut frame = epi::backend::FrameBuilder {
info: integration_info(&display, None),
tex_allocator: Some(&mut painter),
#[cfg(feature = "http")]
http: http.clone(),
output: &mut app_output,
repaint_signal: repaint_signal.clone(),
}
.build();
let saved_memory = ctx.memory().clone();
ctx.memory().set_everything_is_visible(true);
app.update(&ctx, &mut frame);
*ctx.memory() = saved_memory; // We don't want to remember that windows were huge.
ctx.clear_animations();
2021-01-10 10:43:01 +00:00
let (egui_output, _shapes) = ctx.end_frame();
handle_output(egui_output, &display, clipboard.as_mut());
// TODO: handle app_output
// eprintln!("Warmed up in {} ms", warm_up_start.elapsed().as_millis())
}
event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
let frame_start = Instant::now();
2020-12-16 20:22:45 +00:00
input_state.raw.time = Some(start_time.elapsed().as_nanos() as f64 * 1e-9);
input_state.raw.screen_rect = Some(Rect::from_min_size(
Default::default(),
screen_size_in_pixels(&display) / input_state.raw.pixels_per_point.unwrap(),
));
2020-11-13 10:04:45 +00:00
ctx.begin_frame(input_state.raw.take());
2020-12-31 13:31:11 +00:00
let mut app_output = epi::backend::AppOutput::default();
let mut frame = epi::backend::FrameBuilder {
info: integration_info(&display, previous_frame_time),
tex_allocator: Some(&mut painter),
#[cfg(feature = "http")]
2020-12-31 13:31:11 +00:00
http: http.clone(),
output: &mut app_output,
repaint_signal: repaint_signal.clone(),
2020-12-31 13:31:11 +00:00
}
.build();
app.update(&ctx, &mut frame);
2021-01-10 10:43:01 +00:00
let (egui_output, shapes) = ctx.end_frame();
let clipped_meshes = ctx.tessellate(shapes);
let frame_time = (Instant::now() - frame_start).as_secs_f64() as f32;
previous_frame_time = Some(frame_time);
2021-01-25 20:23:24 +00:00
painter.paint_meshes(
&display,
ctx.pixels_per_point(),
app.clear_color(),
clipped_meshes,
&ctx.texture(),
);
{
2020-12-31 13:31:11 +00:00
let epi::backend::AppOutput {
quit,
window_size,
pixels_per_point,
} = app_output;
if let Some(pixels_per_point) = pixels_per_point {
// User changed GUI scale
2020-11-13 10:04:45 +00:00
input_state.raw.pixels_per_point = Some(pixels_per_point);
}
if let Some(window_size) = window_size {
display.gl_window().window().set_inner_size(
glutin::dpi::PhysicalSize {
width: (ctx.pixels_per_point() * window_size.x).round(),
height: (ctx.pixels_per_point() * window_size.y).round(),
}
.to_logical::<f32>(native_pixels_per_point(&display) as f64),
);
}
*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());
#[cfg(feature = "persistence")]
if let Some(storage) = &mut storage {
let now = Instant::now();
if now - last_auto_save > app.auto_save_interval() {
2020-12-29 13:15:46 +00:00
epi::set_value(
storage.as_mut(),
WINDOW_KEY,
&WindowSettings::from_display(&display),
);
2020-12-29 13:15:46 +00:00
epi::set_value(storage.as_mut(), EGUI_MEMORY_KEY, &*ctx.memory());
app.save(storage.as_mut());
storage.flush();
last_auto_save = now;
}
}
};
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
glutin::event::Event::WindowEvent { event, .. } => {
2020-11-13 10:04:45 +00:00
input_to_egui(event, clipboard.as_mut(), &mut input_state, control_flow);
display.gl_window().window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
}
glutin::event::Event::LoopDestroyed => {
app.on_exit();
#[cfg(feature = "persistence")]
if let Some(storage) = &mut storage {
2020-12-29 13:15:46 +00:00
epi::set_value(
storage.as_mut(),
WINDOW_KEY,
&WindowSettings::from_display(&display),
);
2020-12-29 13:15:46 +00:00
epi::set_value(storage.as_mut(), EGUI_MEMORY_KEY, &*ctx.memory());
app.save(storage.as_mut());
storage.flush();
}
}
glutin::event::Event::UserEvent(RequestRepaintEvent) => {
display.gl_window().window().request_redraw();
}
_ => (),
}
});
}