calculator.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 calc_val(val: String) -> f64 {
  19. let mut result = 0.0_f64;
  20. let mut temp = String::new();
  21. let mut operation = "+".to_string();
  22. for c in val.chars() {
  23. println!("{:?}: {:?} - {:?}", result, c, val);
  24. if c == '+' || c == '-' || c == '*' || c == '/' {
  25. match &operation as &str {
  26. "+" => {result += temp.parse::<f64>().unwrap();},
  27. "-" => {result -= temp.parse::<f64>().unwrap();},
  28. "*" => {result *= temp.parse::<f64>().unwrap();},
  29. "/" => {result /= temp.parse::<f64>().unwrap();},
  30. _ => unreachable!(),
  31. };
  32. operation = c.to_string();
  33. temp = String::new();
  34. } else {
  35. temp.push(c);
  36. }
  37. }
  38. if temp != "" {
  39. match &operation as &str {
  40. "+" => {result += temp.parse::<f64>().unwrap();},
  41. "-" => {result -= temp.parse::<f64>().unwrap();},
  42. "*" => {result *= temp.parse::<f64>().unwrap();},
  43. "/" => {result /= temp.parse::<f64>().unwrap();},
  44. _ => unreachable!(),
  45. };
  46. }
  47. result
  48. }
  49. fn app(cx: Scope) -> Element {
  50. let cur_val = use_state(&cx, || 0.0_f64);
  51. let display_value: UseState<String> = use_state(&cx,String::new);
  52. let input_digit = move |num: u8| {
  53. display_value.modify().push_str(num.to_string().as_str());
  54. };
  55. let input_operator = move |key: &str| {
  56. display_value.modify().push_str(key);
  57. };
  58. cx.render(rsx!(
  59. style { [include_str!("./assets/calculator.css")] }
  60. div { id: "wrapper",
  61. div { class: "app",
  62. div { class: "calculator",
  63. onkeydown: move |evt| match evt.key_code {
  64. KeyCode::Add => input_operator("+"),
  65. KeyCode::Subtract => input_operator("-"),
  66. KeyCode::Divide => input_operator("/"),
  67. KeyCode::Multiply => input_operator("*"),
  68. KeyCode::Num0 => input_digit(0),
  69. KeyCode::Num1 => input_digit(1),
  70. KeyCode::Num2 => input_digit(2),
  71. KeyCode::Num3 => input_digit(3),
  72. KeyCode::Num4 => input_digit(4),
  73. KeyCode::Num5 => input_digit(5),
  74. KeyCode::Num6 => input_digit(6),
  75. KeyCode::Num7 => input_digit(7),
  76. KeyCode::Num8 => input_digit(8),
  77. KeyCode::Num9 => input_digit(9),
  78. KeyCode::Backspace => {
  79. if !display_value.len() != 0 {
  80. display_value.modify().pop();
  81. }
  82. }
  83. _ => {}
  84. },
  85. div { class: "calculator-display", [cur_val.separated_string()] }
  86. div { class: "calculator-keypad",
  87. div { class: "input-keys",
  88. div { class: "function-keys",
  89. button {
  90. class: "calculator-key key-clear",
  91. onclick: move |_| {
  92. display_value.set(String::new());
  93. if display_value != "" {
  94. cur_val.set(0.0);
  95. }
  96. },
  97. [if display_value == "" { "C" } else { "AC" }]
  98. }
  99. button {
  100. class: "calculator-key key-sign",
  101. onclick: move |_| {
  102. // if display_value.starts_with("-") {
  103. // display_value.set(display_value.trim_start_matches("-").to_string())
  104. // } else {
  105. // display_value.set(format!("-{}", *display_value))
  106. // }
  107. },
  108. "±"
  109. }
  110. button {
  111. class: "calculator-key key-percent",
  112. onclick: move |_| {
  113. cur_val.set(calc_val(display_value.get().clone()) / 100.0);
  114. },
  115. "%"
  116. }
  117. }
  118. div { class: "digit-keys",
  119. button { class: "calculator-key key-0", onclick: move |_| input_digit(0),
  120. "0"
  121. }
  122. button { class: "calculator-key key-dot", onclick: move |_| display_value.modify().push_str("."),
  123. "●"
  124. }
  125. (1..10).map(|k| rsx!{
  126. button {
  127. class: "calculator-key {k}",
  128. name: "key-{k}",
  129. onclick: move |_| input_digit(k),
  130. "{k}"
  131. }
  132. }),
  133. }
  134. }
  135. div { class: "operator-keys",
  136. button { class: "calculator-key key-divide",
  137. onclick: move |_| input_operator("/"),
  138. "÷"
  139. }
  140. button { class: "calculator-key key-multiply",
  141. onclick: move |_| input_operator("*"),
  142. "×"
  143. }
  144. button { class: "calculator-key key-subtract",
  145. onclick: move |_| input_operator("-"),
  146. "−"
  147. }
  148. button { class: "calculator-key key-add",
  149. onclick: move |_| input_operator("+"),
  150. "+"
  151. }
  152. button { class: "calculator-key key-equals",
  153. onclick: move |_| {
  154. cur_val.set(calc_val(display_value.get().clone()));
  155. },
  156. "="
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. ))
  164. }