calculator.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. This example is a simple iOS-style calculator. This particular example can run any platform - Web, Mobile, Desktop.
  3. This calculator version uses React-style state management. All state is held as individual use_states.
  4. */
  5. use dioxus::events::*;
  6. use dioxus::prelude::*;
  7. fn main() {
  8. dioxus::desktop::launch(APP, |cfg| cfg);
  9. }
  10. const APP: FC<()> = |cx, _| {
  11. let cur_val = use_state(cx, || 0.0_f64);
  12. let operator = use_state(cx, || None as Option<&'static str>);
  13. let display_value = use_state(cx, || String::from(""));
  14. let clear_display = display_value == "0";
  15. let clear_text = if clear_display { "C" } else { "AC" };
  16. let input_digit = move |num: u8| display_value.modify().push_str(num.to_string().as_str());
  17. let input_dot = move || display_value.modify().push_str(".");
  18. let perform_operation = move || {
  19. if let Some(op) = operator.as_ref() {
  20. let rhs = display_value.parse::<f64>().unwrap();
  21. let new_val = match *op {
  22. "+" => *cur_val + rhs,
  23. "-" => *cur_val - rhs,
  24. "*" => *cur_val * rhs,
  25. "/" => *cur_val / rhs,
  26. _ => unreachable!(),
  27. };
  28. cur_val.set(new_val);
  29. display_value.set(new_val.to_string());
  30. operator.set(None);
  31. }
  32. };
  33. let toggle_sign = move |_| {
  34. if display_value.starts_with("-") {
  35. display_value.set(display_value.trim_start_matches("-").to_string())
  36. } else {
  37. display_value.set(format!("-{}", *display_value))
  38. }
  39. };
  40. let toggle_percent = move |_| todo!();
  41. let clear_key = move |_| {
  42. display_value.set("0".to_string());
  43. if !clear_display {
  44. operator.set(None);
  45. cur_val.set(0.0);
  46. }
  47. };
  48. let keydownhandler = move |evt: KeyboardEvent| match evt.key_code {
  49. KeyCode::Add => operator.set(Some("+")),
  50. KeyCode::Subtract => operator.set(Some("-")),
  51. KeyCode::Divide => operator.set(Some("/")),
  52. KeyCode::Multiply => operator.set(Some("*")),
  53. KeyCode::Num0 => input_digit(0),
  54. KeyCode::Num1 => input_digit(1),
  55. KeyCode::Num2 => input_digit(2),
  56. KeyCode::Num3 => input_digit(3),
  57. KeyCode::Num4 => input_digit(4),
  58. KeyCode::Num5 => input_digit(5),
  59. KeyCode::Num6 => input_digit(6),
  60. KeyCode::Num7 => input_digit(7),
  61. KeyCode::Num8 => input_digit(8),
  62. KeyCode::Num9 => input_digit(9),
  63. KeyCode::Backspace => {
  64. if !display_value.as_str().eq("0") {
  65. display_value.modify().pop();
  66. }
  67. }
  68. _ => {}
  69. };
  70. use separator::Separatable;
  71. let formatted_display = cur_val.separated_string();
  72. rsx!(cx, div {
  73. class: "calculator", onkeydown: {keydownhandler}
  74. div { class: "calculator-display", "{formatted_display}" }
  75. div { class: "input-keys"
  76. div { class: "function-keys"
  77. CalculatorKey { name: "key-clear", onclick: {clear_key} "{clear_text}" }
  78. CalculatorKey { name: "key-sign", onclick: {toggle_sign}, "±"}
  79. CalculatorKey { name: "key-percent", onclick: {toggle_percent} "%"}
  80. }
  81. div { class: "digit-keys"
  82. CalculatorKey { name: "key-0", onclick: move |_| input_digit(0), "0" }
  83. CalculatorKey { name: "key-dot", onclick: move |_| input_dot(), "●" }
  84. {(1..9).map(|k| rsx!{
  85. CalculatorKey { key: "{k}", name: "key-{k}", onclick: move |_| input_digit(k), "{k}" }
  86. })}
  87. }
  88. div { class: "operator-keys"
  89. CalculatorKey { name: "key-divide", onclick: move |_| operator.set(Some("/")) "÷" }
  90. CalculatorKey { name: "key-multiply", onclick: move |_| operator.set(Some("*")) "×" }
  91. CalculatorKey { name: "key-subtract", onclick: move |_| operator.set(Some("-")) "−" }
  92. CalculatorKey { name: "key-add", onclick: move |_| operator.set(Some("+")) "+" }
  93. CalculatorKey { name: "key-equals", onclick: move |_| perform_operation() "=" }
  94. }
  95. }
  96. })
  97. };
  98. #[derive(Props)]
  99. struct CalculatorKeyProps<'a> {
  100. name: &'static str,
  101. onclick: &'a dyn Fn(MouseEvent),
  102. }
  103. fn CalculatorKey<'a>(cx: Context<'a>, props: &'a CalculatorKeyProps) -> DomTree<'a> {
  104. rsx!(cx, button {
  105. class: "calculator-key {props.name}"
  106. onclick: {props.onclick}
  107. {cx.children()}
  108. })
  109. }