calculator.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //! Example: Calculator
  2. //! -------------------------
  3. // use dioxus::events::on::*;
  4. // use dioxus::prelude::*;
  5. fn main() {
  6. env_logger::init();
  7. dioxus::desktop::launch(App, |cfg| cfg);
  8. }
  9. use dioxus::events::on::*;
  10. use dioxus::prelude::*;
  11. enum Operator {
  12. Add,
  13. Sub,
  14. Mul,
  15. Div,
  16. }
  17. const App: FC<()> = |cx, props| {
  18. let cur_val = use_state(cx, || 0.0_f64);
  19. let operator = use_state(cx, || None as Option<Operator>);
  20. let display_value = use_state(cx, || "".to_string());
  21. let clear_display = display_value == "0";
  22. let clear_text = if clear_display { "C" } else { "AC" };
  23. let input_digit = move |num: u8| display_value.modify().push_str(num.to_string().as_str());
  24. let input_dot = move || display_value.modify().push_str(".");
  25. let perform_operation = move || {
  26. if let Some(op) = operator.as_ref() {
  27. let rhs = display_value.parse::<f64>().unwrap();
  28. let new_val = match op {
  29. Operator::Add => *cur_val + rhs,
  30. Operator::Sub => *cur_val - rhs,
  31. Operator::Mul => *cur_val * rhs,
  32. Operator::Div => *cur_val / rhs,
  33. };
  34. cur_val.set(new_val);
  35. display_value.set(new_val.to_string());
  36. operator.set(None);
  37. }
  38. };
  39. let toggle_sign = move |_| {
  40. if display_value.starts_with("-") {
  41. display_value.set(display_value.trim_start_matches("-").to_string())
  42. } else {
  43. display_value.set(format!("-{}", *display_value))
  44. }
  45. };
  46. let toggle_percent = move |_| todo!();
  47. let clear_key = move |_| {
  48. display_value.set("0".to_string());
  49. if !clear_display {
  50. operator.set(None);
  51. cur_val.set(0.0);
  52. }
  53. };
  54. let keydownhandler = move |evt: KeyboardEvent| match evt.key_code() {
  55. KeyCode::Backspace => {
  56. if !display_value.as_str().eq("0") {
  57. display_value.modify().pop();
  58. }
  59. }
  60. KeyCode::Num0 => input_digit(0),
  61. KeyCode::Num1 => input_digit(1),
  62. KeyCode::Num2 => input_digit(2),
  63. KeyCode::Num3 => input_digit(3),
  64. KeyCode::Num4 => input_digit(4),
  65. KeyCode::Num5 => input_digit(5),
  66. KeyCode::Num6 => input_digit(6),
  67. KeyCode::Num7 => input_digit(7),
  68. KeyCode::Num8 => input_digit(8),
  69. KeyCode::Num9 => input_digit(9),
  70. KeyCode::Add => operator.set(Some(Operator::Add)),
  71. KeyCode::Subtract => operator.set(Some(Operator::Sub)),
  72. KeyCode::Divide => operator.set(Some(Operator::Div)),
  73. KeyCode::Multiply => operator.set(Some(Operator::Mul)),
  74. _ => {}
  75. };
  76. cx.render(rsx! {
  77. div { class: "calculator", onkeydown: {keydownhandler}
  78. div { class: "input-keys"
  79. div { class: "function-keys"
  80. CalculatorKey { name: "key-clear", onclick: {clear_key} "{clear_text}" }
  81. CalculatorKey { name: "key-sign", onclick: {toggle_sign}, "±"}
  82. CalculatorKey { name: "key-percent", onclick: {toggle_percent} "%"}
  83. }
  84. div { class: "digit-keys"
  85. CalculatorKey { name: "key-0", onclick: move |_| input_digit(0), "0" }
  86. CalculatorKey { name: "key-dot", onclick: move |_| input_dot(), "●" }
  87. {(1..9).map(move |k| rsx!{
  88. CalculatorKey { key: "{k}", name: "key-{k}", onclick: move |_| input_digit(k), "{k}" }
  89. })}
  90. }
  91. div { class: "operator-keys"
  92. CalculatorKey { name: "key-divide", onclick: move |_| operator.set(Some(Operator::Div)) "÷" }
  93. CalculatorKey { name: "key-multiply", onclick: move |_| operator.set(Some(Operator::Mul)) "×" }
  94. CalculatorKey { name: "key-subtract", onclick: move |_| operator.set(Some(Operator::Sub)) "−" }
  95. CalculatorKey { name: "key-add", onclick: move |_| operator.set(Some(Operator::Add)) "+" }
  96. CalculatorKey { name: "key-equals", onclick: move |_| perform_operation() "=" }
  97. }
  98. }
  99. }
  100. })
  101. };
  102. #[derive(Props)]
  103. struct CalculatorKeyProps<'a> {
  104. /// Name!
  105. name: &'static str,
  106. /// Click!
  107. onclick: &'a dyn Fn(MouseEvent),
  108. }
  109. fn CalculatorKey<'a>(cx: Context<'a>, props: &'a CalculatorKeyProps) -> DomTree<'a> {
  110. cx.render(rsx! {
  111. button {
  112. class: "calculator-key {props.name}"
  113. onclick: {props.onclick}
  114. {cx.children()}
  115. }
  116. })
  117. }
  118. #[derive(Props, PartialEq)]
  119. struct CalculatorDisplayProps {
  120. val: f64,
  121. }
  122. fn CalculatorDisplay<'a>(cx: Context<'a>, props: &CalculatorDisplayProps) -> DomTree<'a> {
  123. use separator::Separatable;
  124. // Todo, add float support to the num-format crate
  125. let formatted = props.val.separated_string();
  126. // TODO: make it autoscaling with css
  127. cx.render(rsx! {
  128. div { class: "calculator-display"
  129. "{formatted}"
  130. }
  131. })
  132. }