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);
  7. }
  8. use dioxus::events::on::*;
  9. use dioxus::prelude::*;
  10. enum Operator {
  11. Add,
  12. Sub,
  13. Mul,
  14. Div,
  15. }
  16. static 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 =
  23. move |num: u8| display_value.modify(move |f| f.push_str(num.to_string().as_str()));
  24. let input_dot = move || display_value.modify(move |f| f.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 => display_value.modify(|f| {
  56. if !f.as_str().eq("0") {
  57. f.pop();
  58. }
  59. }),
  60. KeyCode::_0 => input_digit(0),
  61. KeyCode::_1 => input_digit(1),
  62. KeyCode::_2 => input_digit(2),
  63. KeyCode::_3 => input_digit(3),
  64. KeyCode::_4 => input_digit(4),
  65. KeyCode::_5 => input_digit(5),
  66. KeyCode::_6 => input_digit(6),
  67. KeyCode::_7 => input_digit(7),
  68. KeyCode::_8 => input_digit(8),
  69. KeyCode::_9 => 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, 'r>(cx: Context<'a, CalculatorKeyProps<'r>>) -> VNode<'a> {
  110. cx.render(rsx! {
  111. button {
  112. class: "calculator-key {cx.name}"
  113. onclick: {cx.onclick}
  114. {cx.children()}
  115. }
  116. })
  117. }
  118. #[derive(Props, PartialEq)]
  119. struct CalculatorDisplayProps {
  120. val: f64,
  121. }
  122. fn CalculatorDisplay(cx: Context<CalculatorDisplayProps>) -> VNode {
  123. use separator::Separatable;
  124. // Todo, add float support to the num-format crate
  125. let formatted = cx.val.separated_string();
  126. // TODO: make it autoscaling with css
  127. cx.render(rsx! {
  128. div { class: "calculator-display"
  129. "{formatted}"
  130. }
  131. })
  132. }