1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00
Files
egui/examples/confirm_exit/src/main.rs
Emil Ernerfeldt 8d98763fe1 Replace #[allow attributes with expect (#7796)
We do have `clippy::allow_attributes` turned on, but it doesn't seem to
work properly
2025-12-19 20:55:50 +01:00

61 lines
2.0 KiB
Rust

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![expect(rustdoc::missing_crate_level_docs)] // it's an example
use eframe::egui;
fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
..Default::default()
};
eframe::run_native(
"Confirm exit",
options,
Box::new(|_cc| Ok(Box::<MyApp>::default())),
)
}
#[derive(Default)]
struct MyApp {
show_confirmation_dialog: bool,
allowed_to_close: bool,
}
impl eframe::App for MyApp {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show_inside(ui, |ui| {
ui.heading("Try to close the window");
});
if ui.input(|i| i.viewport().close_requested()) {
if self.allowed_to_close {
// do nothing - we will close
} else {
ui.send_viewport_cmd(egui::ViewportCommand::CancelClose);
self.show_confirmation_dialog = true;
}
}
if self.show_confirmation_dialog {
egui::Window::new("Do you want to quit?")
.collapsible(false)
.resizable(false)
.show(ui.ctx(), |ui| {
ui.horizontal(|ui| {
if ui.button("No").clicked() {
self.show_confirmation_dialog = false;
self.allowed_to_close = false;
}
if ui.button("Yes").clicked() {
self.show_confirmation_dialog = false;
self.allowed_to_close = true;
ui.send_viewport_cmd(egui::ViewportCommand::Close);
}
});
});
}
}
}