/ eframe / examples / confirm_exit.rs
confirm_exit.rs
 1  #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
 2  
 3  use eframe::egui;
 4  
 5  fn main() {
 6      let options = eframe::NativeOptions::default();
 7      eframe::run_native(
 8          "Confirm exit",
 9          options,
10          Box::new(|_cc| Box::new(MyApp::default())),
11      );
12  }
13  
14  #[derive(Default)]
15  struct MyApp {
16      can_exit: bool,
17      is_exiting: bool,
18  }
19  
20  impl eframe::App for MyApp {
21      fn on_exit_event(&mut self) -> bool {
22          self.is_exiting = true;
23          self.can_exit
24      }
25  
26      fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
27          egui::CentralPanel::default().show(ctx, |ui| {
28              ui.heading("Try to close the window");
29          });
30  
31          if self.is_exiting {
32              egui::Window::new("Do you want to quit?")
33                  .collapsible(false)
34                  .resizable(false)
35                  .show(ctx, |ui| {
36                      ui.horizontal(|ui| {
37                          if ui.button("Yes!").clicked() {
38                              self.can_exit = true;
39                              frame.quit();
40                          }
41  
42                          if ui.button("Not yet").clicked() {
43                              self.is_exiting = false;
44                          }
45                      });
46                  });
47          }
48      }
49  }