2021-02-13 12:57:06 +11:00
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
2021-08-08 15:31:48 +10:00
|
|
|
use cacao::appkit::App;
|
2022-07-16 00:14:02 +10:00
|
|
|
use cacao::lazy_static::lazy_static;
|
2021-02-13 12:57:06 +11:00
|
|
|
|
|
|
|
use crate::CalculatorApp;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Msg {
|
|
|
|
Push(i32),
|
|
|
|
Add,
|
|
|
|
Subtract,
|
|
|
|
Multiply,
|
|
|
|
Divide,
|
|
|
|
Decimal,
|
|
|
|
Clear,
|
|
|
|
Mod,
|
|
|
|
Invert,
|
2022-08-26 12:13:36 +10:00
|
|
|
Equals,
|
2021-02-13 12:57:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Asynchronously calls back through to the top of the application
|
|
|
|
/// on the main thread.
|
|
|
|
pub fn dispatch(msg: Msg) {
|
|
|
|
println!("Dispatching UI message: {:?}", msg);
|
|
|
|
CALCULATOR.run(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref CALCULATOR: Calculator = Calculator::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Calculator(Arc<RwLock<Vec<String>>>);
|
|
|
|
|
|
|
|
impl Calculator {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Calculator(Arc::new(RwLock::new(Vec::new())))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&self, message: Msg) {
|
|
|
|
let mut expression = self.0.write().unwrap();
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
match message {
|
|
|
|
Msg::Push(i) => {
|
|
|
|
// Realistically you might want to check decimal length here or something.
|
|
|
|
// We're not bothering for this example.
|
|
|
|
(*expression).push(i.to_string());
|
|
|
|
let display = (*expression).join("").split(" ").last().unwrap_or("0").to_string();
|
|
|
|
App::<CalculatorApp, String>::dispatch_main(display);
|
|
|
|
},
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
Msg::Decimal => {
|
|
|
|
let display = (*expression).join("").split(" ").last().unwrap_or("0").to_string();
|
|
|
|
if !display.contains(".") {
|
|
|
|
(*expression).push(".".to_string());
|
|
|
|
App::<CalculatorApp, String>::dispatch_main(display + ".");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Msg::Add => {
|
|
|
|
if let Some(last_entry) = (*expression).last() {
|
|
|
|
if !last_entry.ends_with(" ") {
|
|
|
|
(*expression).push(" + ".to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
Msg::Subtract => {
|
|
|
|
(*expression).push(" - ".to_string());
|
|
|
|
},
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
Msg::Multiply => {
|
|
|
|
(*expression).push(" * ".to_string());
|
|
|
|
},
|
|
|
|
|
|
|
|
Msg::Divide => {
|
|
|
|
(*expression).push(" / ".to_string());
|
|
|
|
},
|
|
|
|
|
|
|
|
Msg::Clear => {
|
|
|
|
(*expression) = Vec::new();
|
|
|
|
App::<CalculatorApp, String>::dispatch_main("0".to_string())
|
|
|
|
},
|
|
|
|
|
|
|
|
Msg::Equals => {
|
|
|
|
let mut expr = (*expression).join("");
|
|
|
|
if expr.ends_with(" ") {
|
|
|
|
expr.truncate(expr.len() - 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Expr: {}", expr);
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
match eval::eval(&expr) {
|
2022-07-16 00:14:02 +10:00
|
|
|
Ok(val) => {
|
|
|
|
App::<CalculatorApp, String>::dispatch_main(val.to_string());
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Error parsing expression: {:?}", e);
|
2022-08-26 12:13:36 +10:00
|
|
|
},
|
2021-02-13 12:57:06 +11:00
|
|
|
}
|
2022-07-16 00:14:02 +10:00
|
|
|
},
|
2021-02-13 12:57:06 +11:00
|
|
|
|
2022-08-26 12:13:36 +10:00
|
|
|
_ => {},
|
2022-07-11 01:15:29 +10:00
|
|
|
}
|
2021-02-13 12:57:06 +11:00
|
|
|
}
|
|
|
|
}
|