egui/eframe/examples/confirm_exit.rs
Emil Ernerfeldt b7ebe16cfb
Storage and frame refactor (#1418)
The purpose of this is to expose `frame.storage()` and `frame.storage_mut()` so users can save/load app state from the `App::update` function, without having to add another parameter to that function.

Changes:
* Added `Frame::storage()` and `Frame::storage_mut()`
* `App::update` now takes a `&mut Frame` rather than just `&Frame`
* `Frame` is no longer `Clone` or `Sync` (doesn't have to be since https://github.com/emilk/egui/pull/1366)
2022-03-25 21:19:31 +01:00

49 lines
1.3 KiB
Rust

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use eframe::egui;
fn main() {
let options = eframe::NativeOptions::default();
eframe::run_native(
"Confirm exit",
options,
Box::new(|_cc| Box::new(MyApp::default())),
);
}
#[derive(Default)]
struct MyApp {
can_exit: bool,
is_exiting: bool,
}
impl eframe::App for MyApp {
fn on_exit_event(&mut self) -> bool {
self.is_exiting = true;
self.can_exit
}
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Try to close the window");
});
if self.is_exiting {
egui::Window::new("Do you want to quit?")
.collapsible(false)
.resizable(false)
.show(ctx, |ui| {
ui.horizontal(|ui| {
if ui.button("Yes!").clicked() {
self.can_exit = true;
frame.quit();
}
if ui.button("Not yet").clicked() {
self.is_exiting = false;
}
});
});
}
}
}