calculator.rs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. use dioxus::desktop::tao::dpi::LogicalSize;
  9. dioxus::desktop::launch_cfg(app, |cfg| {
  10. cfg.with_window(|w| {
  11. w.with_title("Calculator Demo")
  12. .with_resizable(false)
  13. .with_inner_size(LogicalSize::new(320.0, 530.0))
  14. })
  15. });
  16. }
  17. fn app(cx: Scope) -> Element {
  18. let (display_value, set_display_value) = use_state(&cx, || String::from("0"));
  19. let input_digit = move |num: u8| {
  20. if display_value == "0" {
  21. set_display_value(String::new());
  22. }
  23. set_display_value
  24. .make_mut()
  25. .push_str(num.to_string().as_str());
  26. };
  27. let input_operator = move |key: &str| {
  28. set_display_value.make_mut().push_str(key);
  29. };
  30. cx.render(rsx!(
  31. style { [include_str!("./assets/calculator.css")] }
  32. div { id: "wrapper",
  33. div { class: "app",
  34. div { class: "calculator",
  35. onkeydown: move |evt| match evt.key_code {
  36. KeyCode::Add => input_operator("+"),
  37. KeyCode::Subtract => input_operator("-"),
  38. KeyCode::Divide => input_operator("/"),
  39. KeyCode::Multiply => input_operator("*"),
  40. KeyCode::Num0 => input_digit(0),
  41. KeyCode::Num1 => input_digit(1),
  42. KeyCode::Num2 => input_digit(2),
  43. KeyCode::Num3 => input_digit(3),
  44. KeyCode::Num4 => input_digit(4),
  45. KeyCode::Num5 => input_digit(5),
  46. KeyCode::Num6 => input_digit(6),
  47. KeyCode::Num7 => input_digit(7),
  48. KeyCode::Num8 => input_digit(8),
  49. KeyCode::Num9 => input_digit(9),
  50. KeyCode::Backspace => {
  51. if !display_value.len() != 0 {
  52. set_display_value.make_mut().pop();
  53. }
  54. }
  55. _ => {}
  56. },
  57. div { class: "calculator-display", [display_value.to_string()] }
  58. div { class: "calculator-keypad",
  59. div { class: "input-keys",
  60. div { class: "function-keys",
  61. button {
  62. class: "calculator-key key-clear",
  63. onclick: move |_| {
  64. set_display_value(String::new());
  65. if !display_value.is_empty(){
  66. set_display_value("0".into());
  67. }
  68. },
  69. [if display_value.is_empty() { "C" } else { "AC" }]
  70. }
  71. button {
  72. class: "calculator-key key-sign",
  73. onclick: move |_| {
  74. let temp = calc_val(display_value.clone());
  75. if temp > 0.0 {
  76. set_display_value(format!("-{}", temp));
  77. } else {
  78. set_display_value(format!("{}", temp.abs()));
  79. }
  80. },
  81. "±"
  82. }
  83. button {
  84. class: "calculator-key key-percent",
  85. onclick: move |_| {
  86. set_display_value(
  87. format!("{}", calc_val(display_value.clone()) / 100.0)
  88. );
  89. },
  90. "%"
  91. }
  92. }
  93. div { class: "digit-keys",
  94. button { class: "calculator-key key-0", onclick: move |_| input_digit(0),
  95. "0"
  96. }
  97. button { class: "calculator-key key-dot", onclick: move |_| set_display_value.make_mut().push('.'),
  98. "●"
  99. }
  100. (1..10).map(|k| rsx!{
  101. button {
  102. class: "calculator-key {k}",
  103. name: "key-{k}",
  104. onclick: move |_| input_digit(k),
  105. "{k}"
  106. }
  107. }),
  108. }
  109. }
  110. div { class: "operator-keys",
  111. button { class: "calculator-key key-divide",
  112. onclick: move |_| input_operator("/"),
  113. "÷"
  114. }
  115. button { class: "calculator-key key-multiply",
  116. onclick: move |_| input_operator("*"),
  117. "×"
  118. }
  119. button { class: "calculator-key key-subtract",
  120. onclick: move |_| input_operator("-"),
  121. "−"
  122. }
  123. button { class: "calculator-key key-add",
  124. onclick: move |_| input_operator("+"),
  125. "+"
  126. }
  127. button { class: "calculator-key key-equals",
  128. onclick: move |_| {
  129. set_display_value(format!("{}", calc_val(display_value.clone())));
  130. },
  131. "="
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. ))
  139. }
  140. fn calc_val(val: String) -> f64 {
  141. let mut result;
  142. let mut temp = String::new();
  143. let mut operation = "+".to_string();
  144. let mut start_index = 0;
  145. let mut temp_value;
  146. let mut fin_index = 0;
  147. if &val[0..1] == "-" {
  148. temp_value = String::from("-");
  149. fin_index = 1;
  150. start_index += 1;
  151. } else {
  152. temp_value = String::from("");
  153. }
  154. for c in val[fin_index..].chars() {
  155. if c == '+' || c == '-' || c == '*' || c == '/' {
  156. break;
  157. }
  158. temp_value.push(c);
  159. start_index += 1;
  160. }
  161. result = temp_value.parse::<f64>().unwrap();
  162. if start_index + 1 >= val.len() {
  163. return result;
  164. }
  165. for c in val[start_index..].chars() {
  166. if c == '+' || c == '-' || c == '*' || c == '/' {
  167. if !temp.is_empty() {
  168. match &operation as &str {
  169. "+" => result += temp.parse::<f64>().unwrap(),
  170. "-" => result -= temp.parse::<f64>().unwrap(),
  171. "*" => result *= temp.parse::<f64>().unwrap(),
  172. "/" => result /= temp.parse::<f64>().unwrap(),
  173. _ => unreachable!(),
  174. };
  175. }
  176. operation = c.to_string();
  177. temp = String::new();
  178. } else {
  179. temp.push(c);
  180. }
  181. }
  182. if !temp.is_empty() {
  183. match &operation as &str {
  184. "+" => result += temp.parse::<f64>().unwrap(),
  185. "-" => result -= temp.parse::<f64>().unwrap(),
  186. "*" => result *= temp.parse::<f64>().unwrap(),
  187. "/" => result /= temp.parse::<f64>().unwrap(),
  188. _ => unreachable!(),
  189. };
  190. }
  191. result
  192. }