calculator.rs 7.6 KB

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