math_app: extract toggle_button helper to reduce UI code duplication

This commit is contained in:
Simon Gardling 2025-12-05 20:13:57 -05:00
parent dab002bd15
commit 2d7c987f11
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 41 additions and 33 deletions

View File

@ -3,6 +3,7 @@ use crate::{
function_entry::Riemann, function_entry::Riemann,
function_manager::FunctionManager, function_manager::FunctionManager,
misc::option_vec_printer, misc::option_vec_printer,
widgets::toggle_button,
}; };
use eframe::App; use eframe::App;
use egui::{ use egui::{
@ -14,7 +15,7 @@ use egui_plot::Plot;
use emath::{Align, Align2}; use emath::{Align, Align2};
use epaint::Margin; use epaint::Margin;
use itertools::Itertools; use itertools::Itertools;
use std::{io::Read, ops::BitXorAssign}; use std::io::Read;
/// Stores current settings/state of [`MathApp`] /// Stores current settings/state of [`MathApp`]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -319,22 +320,20 @@ impl MathApp {
}); });
ui.horizontal(|ui| { ui.horizontal(|ui| {
self.settings.do_extrema.bitxor_assign( toggle_button(
ui.add(Button::new("Extrema")) ui,
.on_hover_text(match self.settings.do_extrema { &mut self.settings.do_extrema,
true => "Disable Displaying Extrema", "Extrema",
false => "Display Extrema", "Disable Displaying Extrema",
}) "Display Extrema",
.clicked(),
); );
self.settings.do_roots.bitxor_assign( toggle_button(
ui.add(Button::new("Roots")) ui,
.on_hover_text(match self.settings.do_roots { &mut self.settings.do_roots,
true => "Disable Displaying Roots", "Roots",
false => "Display Roots", "Disable Displaying Roots",
}) "Display Roots",
.clicked(),
); );
}); });
@ -376,22 +375,21 @@ impl App for MathApp {
// If keyboard input isn't being grabbed, check for key combos // If keyboard input isn't being grabbed, check for key combos
if !ctx.wants_keyboard_input() { if !ctx.wants_keyboard_input() {
// If `H` key is pressed, toggle Side Panel // If `H` key is pressed, toggle Side Panel
self.opened if ctx.input_mut(|x| x.consume_key(egui::Modifiers::NONE, Key::H)) {
.side_panel self.opened.side_panel = !self.opened.side_panel;
.bitxor_assign(ctx.input_mut(|x| x.consume_key(egui::Modifiers::NONE, Key::H))); }
} }
// Creates Top bar that contains some general options // Creates Top bar that contains some general options
TopBottomPanel::top("top_bar").show(ctx, |ui| { TopBottomPanel::top("top_bar").show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
// Button in top bar to toggle showing the side panel // Button in top bar to toggle showing the side panel
self.opened.side_panel.bitxor_assign( toggle_button(
ui.add(Button::new("Panel")) ui,
.on_hover_text(match self.opened.side_panel { &mut self.opened.side_panel,
true => "Hide Side Panel", "Panel",
false => "Show Side Panel", "Hide Side Panel",
}) "Show Side Panel",
.clicked(),
); );
// Button to add a new function // Button to add a new function
@ -407,13 +405,12 @@ impl App for MathApp {
} }
// Toggles opening the Help window // Toggles opening the Help window
self.opened.help.bitxor_assign( toggle_button(
ui.add(Button::new("Help")) ui,
.on_hover_text(match self.opened.help { &mut self.opened.help,
true => "Close Help Window", "Help",
false => "Open Help Window", "Close Help Window",
}) "Open Help Window",
.clicked(),
); );
// Display Area and time of last frame // Display Area and time of last frame

View File

@ -1,5 +1,6 @@
use crate::misc::Offset; use crate::misc::Offset;
use egui::{Id, InnerResponse}; use egui::{Button, Id, InnerResponse, Ui};
use std::ops::BitXorAssign;
/// Creates an area ontop of a widget with an y offset /// Creates an area ontop of a widget with an y offset
pub fn widgets_ontop<R>( pub fn widgets_ontop<R>(
@ -15,3 +16,13 @@ pub fn widgets_ontop<R>(
area.show(ui.ctx(), |ui| add_contents(ui)) area.show(ui.ctx(), |ui| add_contents(ui))
} }
/// A toggle button that XORs its state when clicked.
/// Shows different hover text based on current state.
pub fn toggle_button(ui: &mut Ui, state: &mut bool, label: &str, enabled_tip: &str, disabled_tip: &str) {
state.bitxor_assign(
ui.add(Button::new(label))
.on_hover_text(if *state { enabled_tip } else { disabled_tip })
.clicked(),
);
}