math_app: extract toggle_button helper to reduce UI code duplication

This commit is contained in:
2025-12-05 20:13:57 -05:00
parent dab002bd15
commit 2d7c987f11
2 changed files with 41 additions and 33 deletions

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(),
);
}