mirror of
https://github.com/emilk/egui.git
synced 2026-06-27 07:03:14 -04:00
* Part of https://github.com/emilk/egui/issues/3556 This PR replaces a bunch of options in `eframe::NativeOptions` with `egui::ViewportBuilder`. For instance: ``` diff let options = eframe::NativeOptions { - initial_window_size: Some(egui::vec2(320.0, 240.0)), - drag_and_drop_support: true, + viewport: egui::ViewportBuilder::default() + .with_inner_size([320.0, 240.0]) + .with_drag_and_drop(true), centered: true, ..Default::default() }; ```
55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
|
|
|
use eframe::egui;
|
|
|
|
fn main() -> Result<(), eframe::Error> {
|
|
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| Box::<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;
|
|
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|