From f3cee84c51152cf5e56f8c5b32c1d3670e3bd647 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Mon, 18 Sep 2023 22:48:07 -0400 Subject: [PATCH] Split drawing into functions --- rpn_rs_gui/src/app.rs | 140 +++++++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 62 deletions(-) diff --git a/rpn_rs_gui/src/app.rs b/rpn_rs_gui/src/app.rs index 1eb2644..8e8c40e 100644 --- a/rpn_rs_gui/src/app.rs +++ b/rpn_rs_gui/src/app.rs @@ -1,5 +1,6 @@ use egui::{ - Button, Color32, FontId, Frame, Grid, Key, Label, RichText, Rounding, Stroke, Style, Vec2, + Button, Color32, FontId, Frame, Grid, Key, Label, RichText, Rounding, ScrollArea, Stroke, + Style, Vec2, }; use rpn_rs::calc::{errors::CalculatorError, Calculator}; use tracing::{error, info}; @@ -56,6 +57,76 @@ impl TemplateApp { } } + fn draw_stack(&mut self, ui: &mut egui::Ui) { + ui.add(Label::new( + RichText::new( + self.calculator + .stack + .iter() + .rev() + .map(|e| e.to_string()) + .collect::>() + .join("\n"), + ) + .background_color(egui::Color32::RED) + .font(DEFAULT_FONT) + .size(STACK_FONT_SIZE) + .color(Color32::WHITE), + )); + } + + fn draw_error(&mut self, ui: &mut egui::Ui) { + if let Some(ref e) = self.latest_error { + ui.label( + RichText::new(e.to_string()) + .font(DEFAULT_FONT) + .size(STACK_FONT_SIZE) + .color(Color32::RED), + ); + } + } + + fn draw_input(&mut self, ui: &mut egui::Ui) { + egui::Frame::none() + .fill(egui::Color32::RED) + .inner_margin(5.0) + .stroke(Stroke::new(10.0, Color32::BLUE)) + .show(ui, |ui| { + ui.label( + RichText::new(self.calculator.get_l()) + .color(Color32::WHITE) + .font(DEFAULT_FONT), + ); + }); + } + + fn draw_buttons(&mut self, ui: &mut egui::Ui) { + Grid::new("grid").spacing(BUTTON_SPACING).show(ui, |ui| { + for row in BUTTON_LAYOUT.iter() { + for button in row.iter() { + let label = RichText::new(button.value) + .font(DEFAULT_FONT) + .color(Color32::WHITE); + if ui + .add_enabled( + button.enabled, + Button::new(label) + .stroke(Stroke::NONE) + .rounding(Rounding::none()) + .fill(Color32::from_gray(0x12)), + ) + .on_hover_text(button.help) + .clicked() + { + info!("Clicked button {}", button.value); + self.calculator_input(button.value); + } + } + ui.end_row(); + } + }); + } + fn handle_input(&mut self, i: &egui::InputState) { if i.events.is_empty() { return; @@ -161,62 +232,15 @@ impl eframe::App for TemplateApp { ui.hyperlink("https://gitea.austen-wares.com/stonewareslord/rpn_rs"); // Buttons - Grid::new("grid").spacing(BUTTON_SPACING).show(ui, |ui| { - for row in BUTTON_LAYOUT.iter() { - for button in row.iter() { - let label = RichText::new(button.value) - .font(DEFAULT_FONT) - .color(Color32::WHITE); - if ui - .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); - self.calculator_input(button.value); - } - } - ui.end_row(); - } - }); + self.draw_buttons(ui); // Stack - // ui.add_sized( Vec2::new(ui.available_width(), ui.available_height()), - ui.add(Label::new( - RichText::new( - self.calculator - .stack - .iter() - .rev() - .map(|e| e.to_string()) - .collect::>() - .join("\n"), - ) - .background_color(egui::Color32::RED) - .font(DEFAULT_FONT) - .size(STACK_FONT_SIZE) - .color(Color32::WHITE), - )); - egui::Frame::none() - .fill(egui::Color32::RED) - .inner_margin(5.0) - .stroke(Stroke::new(10.0, Color32::BLUE)) - .show(ui, |ui| { - ui.label( - RichText::new(self.calculator.get_l()) - .color(Color32::WHITE) - .font(DEFAULT_FONT), - ); - }); + ScrollArea::vertical().show(ui, |ui| { + self.draw_stack(ui); + }); + self.draw_input(ui); // Reset the error state and update `self.latest_error` if required match std::mem::take(&mut self.error_state) { ErrorState::NoModify => {} @@ -224,15 +248,7 @@ impl eframe::App for TemplateApp { ErrorState::Clear => self.latest_error = None, } - if let Some(ref e) = self.latest_error { - ui.label( - RichText::new(e.to_string()) - .font(DEFAULT_FONT) - .size(STACK_FONT_SIZE) - .color(Color32::RED), - ); - } - + self.draw_error(ui); egui::warn_if_debug_build(ui); }); })