1
0

calculator.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. use separator::Separatable;
  8. fn main() {
  9. use dioxus::desktop::tao::dpi::LogicalSize;
  10. dioxus::desktop::launch_cfg(app, |cfg| {
  11. cfg.with_window(|w| {
  12. w.with_title("Calculator Demo")
  13. .with_resizable(false)
  14. .with_inner_size(LogicalSize::new(320.0, 530.0))
  15. })
  16. });
  17. }
  18. fn app(cx: Scope) -> Element {
  19. let cur_val = use_state(&cx, || 0.0_f64);
  20. let operator = use_state(&cx, Option::default);
  21. let display_value = use_state(&cx, String::new);
  22. let input_digit = move |num: u8| display_value.modify().push_str(num.to_string().as_str());
  23. cx.render(rsx!(
  24. style { [include_str!("./assets/calculator.css")] }
  25. div { id: "wrapper",
  26. div { class: "app",
  27. div { class: "calculator",
  28. onkeydown: move |evt| match evt.key_code {
  29. KeyCode::Add => operator.set(Some("+")),
  30. KeyCode::Subtract => operator.set(Some("-")),
  31. KeyCode::Divide => operator.set(Some("/")),
  32. KeyCode::Multiply => operator.set(Some("*")),
  33. KeyCode::Num0 => input_digit(0),
  34. KeyCode::Num1 => input_digit(1),
  35. KeyCode::Num2 => input_digit(2),
  36. KeyCode::Num3 => input_digit(3),
  37. KeyCode::Num4 => input_digit(4),
  38. KeyCode::Num5 => input_digit(5),
  39. KeyCode::Num6 => input_digit(6),
  40. KeyCode::Num7 => input_digit(7),
  41. KeyCode::Num8 => input_digit(8),
  42. KeyCode::Num9 => input_digit(9),
  43. KeyCode::Backspace => {
  44. if !display_value.as_str().eq("0") {
  45. display_value.modify().pop();
  46. }
  47. }
  48. _ => {}
  49. },
  50. div { class: "calculator-display", [cur_val.separated_string()] }
  51. div { class: "calculator-keypad",
  52. div { class: "input-keys",
  53. div { class: "function-keys",
  54. button {
  55. class: "calculator-key key-clear",
  56. onclick: move |_| {
  57. display_value.set("0".to_string());
  58. if display_value != "0" {
  59. operator.set(None);
  60. cur_val.set(0.0);
  61. }
  62. },
  63. [if display_value == "0" { "C" } else { "AC" }]
  64. }
  65. button {
  66. class: "calculator-key key-sign",
  67. onclick: move |_| {
  68. if display_value.starts_with("-") {
  69. display_value.set(display_value.trim_start_matches("-").to_string())
  70. } else {
  71. display_value.set(format!("-{}", *display_value))
  72. }
  73. },
  74. "±"
  75. }
  76. button {
  77. class: "calculator-key key-percent",
  78. onclick: move |_| {
  79. let pct = (display_value.parse::<f64>().unwrap() / 100.0).to_string();
  80. display_value.set(pct);
  81. },
  82. "%"
  83. }
  84. }
  85. div { class: "digit-keys",
  86. button { class: "calculator-key key-0", onclick: move |_| input_digit(0),
  87. "0"
  88. }
  89. button { class: "calculator-key key-dot", onclick: move |_| display_value.modify().push_str("."),
  90. "●"
  91. }
  92. (1..10).map(|k| rsx!{
  93. button {
  94. class: "calculator-key {k}",
  95. name: "key-{k}",
  96. onclick: move |_| input_digit(k),
  97. "{k}"
  98. }
  99. }),
  100. }
  101. }
  102. div { class: "operator-keys",
  103. button { class: "calculator-key key-divide",
  104. onclick: move |_| operator.set(Some("/")),
  105. "÷"
  106. }
  107. button { class: "calculator-key key-multiply",
  108. onclick: move |_| operator.set(Some("*")),
  109. "×"
  110. }
  111. button { class: "calculator-key key-subtract",
  112. onclick: move |_| operator.set(Some("-")),
  113. "−"
  114. }
  115. button { class: "calculator-key key-add",
  116. onclick: move |_| operator.set(Some("+")),
  117. "+"
  118. }
  119. button { class: "calculator-key key-equals",
  120. onclick: move |_| {
  121. if let Some(op) = operator.as_ref() {
  122. let rhs = display_value.parse::<f64>().unwrap();
  123. let new_val = match *op {
  124. "+" => *cur_val + rhs,
  125. "-" => *cur_val - rhs,
  126. "*" => *cur_val * rhs,
  127. "/" => *cur_val / rhs,
  128. _ => unreachable!(),
  129. };
  130. cur_val.set(new_val);
  131. display_value.set(new_val.to_string());
  132. operator.set(None);
  133. }
  134. },
  135. "="
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. ))
  143. }