gupaxx/src/app/quit.rs
Louis-Marie Baer 045dd7ab03 feat: separate logic into smaller modules
The original sources, while well documented, had files with over 2.5K LoC.
Separating the logic in different modules add clarity making the code easier to work with.
2024-03-03 08:37:35 +01:00

48 lines
1.6 KiB
Rust

use log::info;
use crate::errors::ErrorButtons;
use crate::errors::ErrorFerris;
use super::App;
impl App {
pub(super) fn quit(&mut self, ctx: &egui::Context) {
// If closing.
// Used to be `eframe::App::on_close_event(&mut self) -> bool`.
let close_signal = ctx.input(|input| {
use egui::viewport::ViewportCommand;
if !input.viewport().close_requested() {
return None;
}
info!("quit");
if self.state.gupax.ask_before_quit {
// If we're already on the [ask_before_quit] screen and
// the user tried to exit again, exit.
if self.error_state.quit_twice {
if self.state.gupax.save_before_quit {
self.save_before_quit();
}
return Some(ViewportCommand::Close);
}
// Else, set the error
self.error_state
.set("", ErrorFerris::Oops, ErrorButtons::StayQuit);
self.error_state.quit_twice = true;
Some(ViewportCommand::CancelClose)
// Else, just quit.
} else {
if self.state.gupax.save_before_quit {
self.save_before_quit();
}
Some(ViewportCommand::Close)
}
});
// This will either:
// 1. Cancel a close signal
// 2. Close the program
if let Some(cmd) = close_signal {
ctx.send_viewport_cmd(cmd);
}
}
}