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