This commit is contained in:
nora 2023-04-18 15:38:14 +02:00
parent 12163d1338
commit 550b1644cb
363 changed files with 84081 additions and 16 deletions

View file

@ -0,0 +1,14 @@
[package]
name = "confirm_exit"
version = "0.1.0"
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.65"
publish = false
[dependencies]
eframe = { path = "../../crates/eframe", features = [
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO
] }

View file

@ -0,0 +1,7 @@
Example how to show a confirm dialog before exiting an application.
```sh
cargo run -p confirm_exit
```
![](screenshot.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 3 KiB

View file

@ -0,0 +1,53 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use eframe::egui;
fn main() -> Result<(), eframe::Error> {
let options = eframe::NativeOptions {
initial_window_size: Some(egui::vec2(320.0, 240.0)),
..Default::default()
};
eframe::run_native(
"Confirm exit",
options,
Box::new(|_cc| Box::new(MyApp::default())),
)
}
#[derive(Default)]
struct MyApp {
allowed_to_close: bool,
show_confirmation_dialog: bool,
}
impl eframe::App for MyApp {
fn on_close_event(&mut self) -> bool {
self.show_confirmation_dialog = true;
self.allowed_to_close
}
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.show_confirmation_dialog {
// Show confirmation dialog:
egui::Window::new("Do you want to quit?")
.collapsible(false)
.resizable(false)
.show(ctx, |ui| {
ui.horizontal(|ui| {
if ui.button("Cancel").clicked() {
self.show_confirmation_dialog = false;
}
if ui.button("Yes!").clicked() {
self.allowed_to_close = true;
frame.close();
}
});
});
}
}
}