calculator.rs 4.9 KB

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