Cleanup input
This commit is contained in:
parent
9a12104044
commit
033d2d973a
@ -1,4 +1,4 @@
|
||||
use egui::{Button, Color32, FontId, Grid, Key, RichText, Rounding, Stroke, Vec2};
|
||||
use egui::{Button, Color32, FontId, Frame, Grid, Key, Label, RichText, Rounding, Stroke, Vec2};
|
||||
use rpn_rs::calc::{errors::CalculatorError, Calculator};
|
||||
use tracing::{error, info};
|
||||
|
||||
@ -19,6 +19,9 @@ pub struct TemplateApp {
|
||||
|
||||
#[serde(skip)]
|
||||
latest_error: Option<CalculatorError>,
|
||||
|
||||
#[serde(skip)]
|
||||
error_state: ErrorState,
|
||||
}
|
||||
|
||||
impl Default for TemplateApp {
|
||||
@ -28,6 +31,7 @@ impl Default for TemplateApp {
|
||||
// Example stuff:
|
||||
label: "Hello World!".to_owned(),
|
||||
latest_error: None,
|
||||
error_state: ErrorState::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -47,54 +51,15 @@ impl TemplateApp {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn calculator_input(&mut self, c: char, error_state: &mut ErrorState) {
|
||||
fn calculator_input(&mut self, c: char) {
|
||||
if let Err(e) = self.calculator.take_input(c) {
|
||||
error_state.errored(e);
|
||||
self.error_state.errored(e);
|
||||
} else {
|
||||
error_state.success();
|
||||
}
|
||||
self.error_state.success();
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for TemplateApp {
|
||||
/// Called by the frame work to save state before shutdown.
|
||||
fn save(&mut self, storage: &mut dyn eframe::Storage) {
|
||||
eframe::set_value(storage, eframe::APP_KEY, self);
|
||||
}
|
||||
|
||||
/// Called each time the UI needs repainting, which may be many times per second.
|
||||
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
let Self { label, .. } = self;
|
||||
|
||||
// Examples of how to create different panels and windows.
|
||||
// Pick whichever suits you.
|
||||
// Tip: a good default choice is to just keep the `CentralPanel`.
|
||||
// For inspiration and more examples, go to https://emilk.github.io/egui
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))] // no File->Quit on web pages!
|
||||
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||
// The top panel is often a good place for a menu bar:
|
||||
egui::menu::bar(ui, |ui| {
|
||||
ui.menu_button("File", |ui| {
|
||||
if ui.button("Quit").clicked() {
|
||||
_frame.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
let mut error_state = ErrorState::NoModify;
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.style_mut().spacing.button_padding = BUTTON_PADDING;
|
||||
|
||||
// The central panel the region left after adding TopPanel's and SidePanel's
|
||||
|
||||
ui.heading("rpn_rs_gui");
|
||||
ui.hyperlink("https://gitea.austen-wares.com/stonewareslord/rpn_rs");
|
||||
|
||||
ui.input(|i| {
|
||||
fn handle_input(&mut self, i: &egui::InputState) {
|
||||
if i.events.is_empty() {
|
||||
return;
|
||||
}
|
||||
@ -102,28 +67,28 @@ impl eframe::App for TemplateApp {
|
||||
for e in i.events.iter() {
|
||||
match e {
|
||||
egui::Event::Text(t) => {
|
||||
self.calculator_input(t.chars().next().unwrap(), &mut error_state);
|
||||
self.calculator_input(t.chars().next().unwrap());
|
||||
}
|
||||
egui::Event::Key {
|
||||
key: Key::ArrowLeft,
|
||||
pressed: true,
|
||||
..
|
||||
} => {
|
||||
self.calculator_input('<', &mut error_state);
|
||||
self.calculator_input('<');
|
||||
}
|
||||
egui::Event::Key {
|
||||
key: Key::ArrowRight,
|
||||
pressed: true,
|
||||
..
|
||||
} => {
|
||||
self.calculator_input('>', &mut error_state);
|
||||
self.calculator_input('>');
|
||||
}
|
||||
egui::Event::Key {
|
||||
key: Key::Enter,
|
||||
pressed: true,
|
||||
..
|
||||
} => {
|
||||
self.calculator_input(' ', &mut error_state);
|
||||
self.calculator_input(' ');
|
||||
}
|
||||
|
||||
egui::Event::Copy => continue,
|
||||
@ -158,22 +123,70 @@ impl eframe::App for TemplateApp {
|
||||
egui::Event::AccessKitActionRequest(_) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for TemplateApp {
|
||||
/// Called by the frame work to save state before shutdown.
|
||||
fn save(&mut self, storage: &mut dyn eframe::Storage) {
|
||||
eframe::set_value(storage, eframe::APP_KEY, self);
|
||||
}
|
||||
|
||||
/// Called each time the UI needs repainting, which may be many times per second.
|
||||
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
let Self { label, .. } = self;
|
||||
|
||||
// Examples of how to create different panels and windows.
|
||||
// Pick whichever suits you.
|
||||
// Tip: a good default choice is to just keep the `CentralPanel`.
|
||||
// For inspiration and more examples, go to https://emilk.github.io/egui
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))] // no File->Quit on web pages!
|
||||
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||
// The top panel is often a good place for a menu bar:
|
||||
egui::menu::bar(ui, |ui| {
|
||||
ui.menu_button("File", |ui| {
|
||||
if ui.button("Quit").clicked() {
|
||||
_frame.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
self.error_state = ErrorState::default();
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.style_mut().spacing.button_padding = BUTTON_PADDING;
|
||||
|
||||
// The central panel the region left after adding TopPanel's and SidePanel's
|
||||
|
||||
ui.input(|i: &egui::InputState| self.handle_input(i));
|
||||
|
||||
Frame::none().fill(Color32::GREEN).show(ui, |ui| {
|
||||
ui.vertical(|ui| {
|
||||
ui.heading("rpn_rs_gui");
|
||||
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);
|
||||
let label = RichText::new(button.value)
|
||||
.font(DEFAULT_FONT)
|
||||
.color(Color32::WHITE);
|
||||
if ui
|
||||
.add(
|
||||
Button::new(label)
|
||||
// .frame(false)
|
||||
.stroke(Stroke::NONE)
|
||||
.rounding(Rounding::none()),
|
||||
.rounding(Rounding::none())
|
||||
.fill(Color32::from_gray(0x12)),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
info!("Clicked button {}", button.value);
|
||||
self.calculator_input(button.value, &mut error_state);
|
||||
self.calculator_input(button.value);
|
||||
// if let Err(e) = self.calculator.take_input(button.value) {
|
||||
// error!("Calculator input error: {e:?}");
|
||||
// self.latest_error = Some(e);
|
||||
@ -187,7 +200,9 @@ impl eframe::App for TemplateApp {
|
||||
}
|
||||
});
|
||||
|
||||
ui.label(
|
||||
// Stack
|
||||
// ui.add_sized( Vec2::new(ui.available_width(), ui.available_height()),
|
||||
ui.add(Label::new(
|
||||
RichText::new(
|
||||
self.calculator
|
||||
.stack
|
||||
@ -201,7 +216,7 @@ impl eframe::App for TemplateApp {
|
||||
.font(DEFAULT_FONT)
|
||||
.size(STACK_FONT_SIZE)
|
||||
.color(Color32::WHITE),
|
||||
);
|
||||
));
|
||||
|
||||
egui::Frame::none()
|
||||
.fill(egui::Color32::RED)
|
||||
@ -215,7 +230,8 @@ impl eframe::App for TemplateApp {
|
||||
);
|
||||
});
|
||||
|
||||
match error_state {
|
||||
// Reset the error state and update `self.latest_error` if required
|
||||
match std::mem::take(&mut self.error_state) {
|
||||
ErrorState::NoModify => {}
|
||||
ErrorState::Errored(e) => self.latest_error = Some(e),
|
||||
ErrorState::Clear => self.latest_error = None,
|
||||
@ -232,6 +248,8 @@ impl eframe::App for TemplateApp {
|
||||
|
||||
egui::warn_if_debug_build(ui);
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
// if false {
|
||||
// egui::Window::new("Window").show(ctx, |ui| {
|
||||
@ -257,49 +275,49 @@ impl CalculatorButton {
|
||||
|
||||
const BUTTON_LAYOUT: &[&[CalculatorButton]] = &[
|
||||
&[
|
||||
CalculatorButton::new('s', "Sin"),
|
||||
// CalculatorButton::new('s', "Sin"),
|
||||
CalculatorButton::new('u', "Undo"),
|
||||
CalculatorButton::new('U', "Redo"),
|
||||
CalculatorButton::new('>', "Swap"),
|
||||
// CalculatorButton::new('\\', "Drop"),
|
||||
CalculatorButton::new('@', "Drop"),
|
||||
],
|
||||
&[
|
||||
CalculatorButton::new('|', "AbsoluteValue"),
|
||||
// CalculatorButton::new('|', "AbsoluteValue"),
|
||||
CalculatorButton::new('v', "Sqrt"),
|
||||
CalculatorButton::new('^', "Pow"),
|
||||
CalculatorButton::new('l', "Log"),
|
||||
CalculatorButton::new('L', "Ln"),
|
||||
],
|
||||
&[
|
||||
CalculatorButton::new('c', "Cos"),
|
||||
// CalculatorButton::new('c', "Cos"),
|
||||
CalculatorButton::new('n', "Negate"),
|
||||
CalculatorButton::new('%', "Modulo"),
|
||||
CalculatorButton::new('i', "Inverse"),
|
||||
CalculatorButton::new('/', "Divide"),
|
||||
],
|
||||
&[
|
||||
CalculatorButton::new('t', "Tan"),
|
||||
// CalculatorButton::new('t', "Tan"),
|
||||
CalculatorButton::new('7', "7"),
|
||||
CalculatorButton::new('8', "8"),
|
||||
CalculatorButton::new('9', "9"),
|
||||
CalculatorButton::new('*', "Multiply"),
|
||||
],
|
||||
&[
|
||||
CalculatorButton::new('S', "ASin"),
|
||||
// CalculatorButton::new('S', "ASin"),
|
||||
CalculatorButton::new('4', "4"),
|
||||
CalculatorButton::new('5', "5"),
|
||||
CalculatorButton::new('6', "6"),
|
||||
CalculatorButton::new('-', "Subtract"),
|
||||
],
|
||||
&[
|
||||
CalculatorButton::new('C', "ACos"),
|
||||
// CalculatorButton::new('C', "ACos"),
|
||||
CalculatorButton::new('1', "1"),
|
||||
CalculatorButton::new('2', "2"),
|
||||
CalculatorButton::new('3', "3"),
|
||||
CalculatorButton::new('+', "Add"),
|
||||
],
|
||||
&[
|
||||
CalculatorButton::new('T', "ATan"),
|
||||
// CalculatorButton::new('T', "ATan"),
|
||||
CalculatorButton::new('0', "0"),
|
||||
CalculatorButton::new('\\', "Drop"),
|
||||
CalculatorButton::new('.', "Decimal"),
|
||||
@ -347,3 +365,8 @@ impl ErrorState {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Default for ErrorState {
|
||||
fn default() -> ErrorState {
|
||||
ErrorState::NoModify
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user