From 335079ac1459bb03b8ed491f1f8573a12222a1c1 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Mon, 18 Sep 2023 22:21:50 -0400 Subject: [PATCH] Add enabled field for buttons --- rpn_rs_gui/src/app.rs | 101 +++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/rpn_rs_gui/src/app.rs b/rpn_rs_gui/src/app.rs index 90fbdee..1eb2644 100644 --- a/rpn_rs_gui/src/app.rs +++ b/rpn_rs_gui/src/app.rs @@ -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 tracing::{error, info}; @@ -132,8 +134,8 @@ impl eframe::App for TemplateApp { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { let Self { .. } = self; - let mut style: Style = (*ctx.style()).clone(); - style.spacing.button_padding = BUTTON_PADDING; + let mut style: Style = (*ctx.style()).clone(); + style.spacing.button_padding = BUTTON_PADDING; ctx.set_style(style); // #[cfg(not(target_arch = "wasm32"))] @@ -166,13 +168,15 @@ impl eframe::App for TemplateApp { .font(DEFAULT_FONT) .color(Color32::WHITE); if ui - .add( + .add_enabled( + button.enabled, Button::new(label) // .frame(false) .stroke(Stroke::NONE) .rounding(Rounding::none()) .fill(Color32::from_gray(0x12)), ) + .on_hover_text(button.help) .clicked() { info!("Clicked button {}", button.value); @@ -248,69 +252,74 @@ impl eframe::App for TemplateApp { struct CalculatorButton { value: char, help: &'static str, + enabled: bool, } impl CalculatorButton { - const fn new(value: char, help: &'static str) -> Self { - Self { value, help } + const fn new(value: char, help: &'static str, enabled: bool) -> Self { + Self { + value, + help, + enabled, + } } } const BUTTON_LAYOUT: &[&[CalculatorButton]] = &[ &[ - // CalculatorButton::new('s', "Sin"), - CalculatorButton::new('u', "Undo"), - CalculatorButton::new('U', "Redo"), - CalculatorButton::new('>', "Swap"), - CalculatorButton::new('@', "Drop"), + // CalculatorButton::new('s', "Sin", true), + CalculatorButton::new('u', "Undo", true), + CalculatorButton::new('U', "Redo", true), + CalculatorButton::new('>', "Swap", true), + CalculatorButton::new('@', "Drop", true), ], &[ - // CalculatorButton::new('|', "AbsoluteValue"), - CalculatorButton::new('v', "Sqrt"), - CalculatorButton::new('^', "Pow"), - CalculatorButton::new('l', "Log"), - CalculatorButton::new('L', "Ln"), + // CalculatorButton::new('|', "AbsoluteValue", true), + CalculatorButton::new('v', "Sqrt", true), + CalculatorButton::new('^', "Pow", true), + CalculatorButton::new('l', "Log", true), + CalculatorButton::new('L', "Ln", true), ], &[ - // CalculatorButton::new('c', "Cos"), - CalculatorButton::new('n', "Negate"), - CalculatorButton::new('%', "Modulo"), - CalculatorButton::new('i', "Inverse"), - CalculatorButton::new('/', "Divide"), + // CalculatorButton::new('c', "Cos", true), + CalculatorButton::new('n', "Negate", true), + CalculatorButton::new('%', "Modulo", true), + CalculatorButton::new('i', "Inverse", true), + CalculatorButton::new('/', "Divide", true), ], &[ - // CalculatorButton::new('t', "Tan"), - CalculatorButton::new('7', "7"), - CalculatorButton::new('8', "8"), - CalculatorButton::new('9', "9"), - CalculatorButton::new('*', "Multiply"), + // CalculatorButton::new('t', "Tan", true), + CalculatorButton::new('7', "7", true), + CalculatorButton::new('8', "8", true), + CalculatorButton::new('9', "9", true), + CalculatorButton::new('*', "Multiply", true), ], &[ - // CalculatorButton::new('S', "ASin"), - CalculatorButton::new('4', "4"), - CalculatorButton::new('5', "5"), - CalculatorButton::new('6', "6"), - CalculatorButton::new('-', "Subtract"), + // CalculatorButton::new('S', "ASin", true), + CalculatorButton::new('4', "4", true), + CalculatorButton::new('5', "5", true), + CalculatorButton::new('6', "6", true), + CalculatorButton::new('-', "Subtract", true), ], &[ - // CalculatorButton::new('C', "ACos"), - CalculatorButton::new('1', "1"), - CalculatorButton::new('2', "2"), - CalculatorButton::new('3', "3"), - CalculatorButton::new('+', "Add"), + // CalculatorButton::new('C', "ACos", true), + CalculatorButton::new('1', "1", true), + CalculatorButton::new('2', "2", true), + CalculatorButton::new('3', "3", true), + CalculatorButton::new('+', "Add", true), ], &[ - // CalculatorButton::new('T', "ATan"), - CalculatorButton::new('0', "0"), - CalculatorButton::new('\\', "Drop"), - CalculatorButton::new('.', "Decimal"), - CalculatorButton::new(' ', "Return"), + // CalculatorButton::new('T', "ATan", true), + CalculatorButton::new('0', "0", true), + CalculatorButton::new('\\', "Drop", true), + CalculatorButton::new('.', "Decimal", true), + CalculatorButton::new(' ', "Return", true), ], - // CalculatorButton::new ( '?', "IntegerDivide"), - // CalculatorButton::new ( 'V', "BuildVector"), - // CalculatorButton::new ( 'M', "BuildMatrix"), - // CalculatorButton::new ( '_', "Deconstruct"), - // CalculatorButton::new ( ')', "Transpose"), + // CalculatorButton::new ( '?', "IntegerDivide", true), + // CalculatorButton::new ( 'V', "BuildVector", true), + // CalculatorButton::new ( 'M', "BuildMatrix", true), + // CalculatorButton::new ( '_', "Deconstruct", true), + // CalculatorButton::new ( ', true)', "Transpose"), ]; enum ErrorState {