Add enabled field for buttons

This commit is contained in:
Austen Adler 2023-09-18 22:21:50 -04:00
parent c713e0dbd1
commit 335079ac14

View File

@ -1,4 +1,6 @@
use egui::{Button, Color32, FontId, Frame, Grid, Key, Label, RichText, Rounding, Stroke, Vec2, Style}; use egui::{
Button, Color32, FontId, Frame, Grid, Key, Label, RichText, Rounding, Stroke, Style, Vec2,
};
use rpn_rs::calc::{errors::CalculatorError, Calculator}; use rpn_rs::calc::{errors::CalculatorError, Calculator};
use tracing::{error, info}; use tracing::{error, info};
@ -132,8 +134,8 @@ impl eframe::App for TemplateApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let Self { .. } = self; let Self { .. } = self;
let mut style: Style = (*ctx.style()).clone(); let mut style: Style = (*ctx.style()).clone();
style.spacing.button_padding = BUTTON_PADDING; style.spacing.button_padding = BUTTON_PADDING;
ctx.set_style(style); ctx.set_style(style);
// #[cfg(not(target_arch = "wasm32"))] // #[cfg(not(target_arch = "wasm32"))]
@ -166,13 +168,15 @@ impl eframe::App for TemplateApp {
.font(DEFAULT_FONT) .font(DEFAULT_FONT)
.color(Color32::WHITE); .color(Color32::WHITE);
if ui if ui
.add( .add_enabled(
button.enabled,
Button::new(label) Button::new(label)
// .frame(false) // .frame(false)
.stroke(Stroke::NONE) .stroke(Stroke::NONE)
.rounding(Rounding::none()) .rounding(Rounding::none())
.fill(Color32::from_gray(0x12)), .fill(Color32::from_gray(0x12)),
) )
.on_hover_text(button.help)
.clicked() .clicked()
{ {
info!("Clicked button {}", button.value); info!("Clicked button {}", button.value);
@ -248,69 +252,74 @@ impl eframe::App for TemplateApp {
struct CalculatorButton { struct CalculatorButton {
value: char, value: char,
help: &'static str, help: &'static str,
enabled: bool,
} }
impl CalculatorButton { impl CalculatorButton {
const fn new(value: char, help: &'static str) -> Self { const fn new(value: char, help: &'static str, enabled: bool) -> Self {
Self { value, help } Self {
value,
help,
enabled,
}
} }
} }
const BUTTON_LAYOUT: &[&[CalculatorButton]] = &[ const BUTTON_LAYOUT: &[&[CalculatorButton]] = &[
&[ &[
// CalculatorButton::new('s', "Sin"), // CalculatorButton::new('s', "Sin", true),
CalculatorButton::new('u', "Undo"), CalculatorButton::new('u', "Undo", true),
CalculatorButton::new('U', "Redo"), CalculatorButton::new('U', "Redo", true),
CalculatorButton::new('>', "Swap"), CalculatorButton::new('>', "Swap", true),
CalculatorButton::new('@', "Drop"), CalculatorButton::new('@', "Drop", true),
], ],
&[ &[
// CalculatorButton::new('|', "AbsoluteValue"), // CalculatorButton::new('|', "AbsoluteValue", true),
CalculatorButton::new('v', "Sqrt"), CalculatorButton::new('v', "Sqrt", true),
CalculatorButton::new('^', "Pow"), CalculatorButton::new('^', "Pow", true),
CalculatorButton::new('l', "Log"), CalculatorButton::new('l', "Log", true),
CalculatorButton::new('L', "Ln"), CalculatorButton::new('L', "Ln", true),
], ],
&[ &[
// CalculatorButton::new('c', "Cos"), // CalculatorButton::new('c', "Cos", true),
CalculatorButton::new('n', "Negate"), CalculatorButton::new('n', "Negate", true),
CalculatorButton::new('%', "Modulo"), CalculatorButton::new('%', "Modulo", true),
CalculatorButton::new('i', "Inverse"), CalculatorButton::new('i', "Inverse", true),
CalculatorButton::new('/', "Divide"), CalculatorButton::new('/', "Divide", true),
], ],
&[ &[
// CalculatorButton::new('t', "Tan"), // CalculatorButton::new('t', "Tan", true),
CalculatorButton::new('7', "7"), CalculatorButton::new('7', "7", true),
CalculatorButton::new('8', "8"), CalculatorButton::new('8', "8", true),
CalculatorButton::new('9', "9"), CalculatorButton::new('9', "9", true),
CalculatorButton::new('*', "Multiply"), CalculatorButton::new('*', "Multiply", true),
], ],
&[ &[
// CalculatorButton::new('S', "ASin"), // CalculatorButton::new('S', "ASin", true),
CalculatorButton::new('4', "4"), CalculatorButton::new('4', "4", true),
CalculatorButton::new('5', "5"), CalculatorButton::new('5', "5", true),
CalculatorButton::new('6', "6"), CalculatorButton::new('6', "6", true),
CalculatorButton::new('-', "Subtract"), CalculatorButton::new('-', "Subtract", true),
], ],
&[ &[
// CalculatorButton::new('C', "ACos"), // CalculatorButton::new('C', "ACos", true),
CalculatorButton::new('1', "1"), CalculatorButton::new('1', "1", true),
CalculatorButton::new('2', "2"), CalculatorButton::new('2', "2", true),
CalculatorButton::new('3', "3"), CalculatorButton::new('3', "3", true),
CalculatorButton::new('+', "Add"), CalculatorButton::new('+', "Add", true),
], ],
&[ &[
// CalculatorButton::new('T', "ATan"), // CalculatorButton::new('T', "ATan", true),
CalculatorButton::new('0', "0"), CalculatorButton::new('0', "0", true),
CalculatorButton::new('\\', "Drop"), CalculatorButton::new('\\', "Drop", true),
CalculatorButton::new('.', "Decimal"), CalculatorButton::new('.', "Decimal", true),
CalculatorButton::new(' ', "Return"), CalculatorButton::new(' ', "Return", true),
], ],
// CalculatorButton::new ( '?', "IntegerDivide"), // CalculatorButton::new ( '?', "IntegerDivide", true),
// CalculatorButton::new ( 'V', "BuildVector"), // CalculatorButton::new ( 'V', "BuildVector", true),
// CalculatorButton::new ( 'M', "BuildMatrix"), // CalculatorButton::new ( 'M', "BuildMatrix", true),
// CalculatorButton::new ( '_', "Deconstruct"), // CalculatorButton::new ( '_', "Deconstruct", true),
// CalculatorButton::new ( ')', "Transpose"), // CalculatorButton::new ( ', true)', "Transpose"),
]; ];
enum ErrorState { enum ErrorState {