Improve "Reset everything" button in demo (#3255)

Closes https://github.com/emilk/egui/issues/3254
This commit is contained in:
Emil Ernerfeldt 2023-08-14 18:57:39 +02:00 committed by GitHub
parent 8ee506ec37
commit 481f44828c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 9 deletions

View File

@ -124,6 +124,15 @@ impl Default for Anchor {
// ----------------------------------------------------------------------------
#[derive(Clone, Copy, Debug)]
#[must_use]
enum Command {
Nothing,
ResetEverything,
}
// ----------------------------------------------------------------------------
/// The state that we persist (serialize).
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
@ -240,18 +249,19 @@ impl eframe::App for WrapApp {
frame.set_fullscreen(!frame.info().window_info.fullscreen);
}
let mut cmd = Command::Nothing;
egui::TopBottomPanel::top("wrap_app_top_bar").show(ctx, |ui| {
egui::trace!(ui);
ui.horizontal_wrapped(|ui| {
ui.visuals_mut().button_frame = false;
self.bar_contents(ui, frame);
self.bar_contents(ui, frame, &mut cmd);
});
});
self.state.backend_panel.update(ctx, frame);
if !is_mobile(ctx) {
self.backend_panel(ctx, frame);
cmd = self.backend_panel(ctx, frame);
}
self.show_selected_app(ctx, frame);
@ -264,6 +274,8 @@ impl eframe::App for WrapApp {
if !frame.is_web() {
egui::gui_zoom::zoom_with_keyboard_shortcuts(ctx, frame.info().native_pixels_per_point);
}
self.run_cmd(ctx, cmd);
}
#[cfg(feature = "glow")]
@ -280,12 +292,14 @@ impl eframe::App for WrapApp {
}
impl WrapApp {
fn backend_panel(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
fn backend_panel(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) -> Command {
// The backend-panel can be toggled on/off.
// We show a little animation when the user switches it.
let is_open =
self.state.backend_panel.open || ctx.memory(|mem| mem.everything_is_visible());
let mut cmd = Command::Nothing;
egui::SidePanel::left("backend_panel")
.resizable(false)
.show_animated(ctx, is_open, |ui| {
@ -294,11 +308,28 @@ impl WrapApp {
});
ui.separator();
self.backend_panel_contents(ui, frame);
self.backend_panel_contents(ui, frame, &mut cmd);
});
cmd
}
fn backend_panel_contents(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
fn run_cmd(&mut self, ctx: &egui::Context, cmd: Command) {
match cmd {
Command::Nothing => {}
Command::ResetEverything => {
self.state = Default::default();
ctx.memory_mut(|mem| *mem = Default::default());
}
}
}
fn backend_panel_contents(
&mut self,
ui: &mut egui::Ui,
frame: &mut eframe::Frame,
cmd: &mut Command,
) {
self.state.backend_panel.ui(ui, frame);
ui.separator();
@ -314,8 +345,7 @@ impl WrapApp {
}
if ui.button("Reset everything").clicked() {
self.state = Default::default();
ui.ctx().memory_mut(|mem| *mem = Default::default());
*cmd = Command::ResetEverything;
ui.close_menu();
}
});
@ -330,7 +360,7 @@ impl WrapApp {
}
}
fn bar_contents(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
fn bar_contents(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cmd: &mut Command) {
egui::widgets::global_dark_light_mode_switch(ui);
ui.separator();
@ -338,7 +368,7 @@ impl WrapApp {
if is_mobile(ui.ctx()) {
ui.menu_button("💻 Backend", |ui| {
ui.set_style(ui.ctx().style()); // ignore the "menu" style set by `menu_button`.
self.backend_panel_contents(ui, frame);
self.backend_panel_contents(ui, frame, cmd);
});
} else {
ui.toggle_value(&mut self.state.backend_panel.open, "💻 Backend");