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

View File

@ -1,5 +1,6 @@
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
pub fn widgets_ontop<R>(
@ -15,3 +16,13 @@ pub fn widgets_ontop<R>(
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(),
);
}