events.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. use dioxus::html::geometry::euclid::Vector3D;
  2. use dioxus::prelude::*;
  3. use dioxus_desktop::DesktopContext;
  4. pub(crate) fn check_app_exits(app: Component) {
  5. use dioxus_desktop::tao::window::WindowBuilder;
  6. use dioxus_desktop::Config;
  7. // This is a deadman's switch to ensure that the app exits
  8. let should_panic = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
  9. let should_panic_clone = should_panic.clone();
  10. std::thread::spawn(move || {
  11. std::thread::sleep(std::time::Duration::from_secs(100));
  12. if should_panic_clone.load(std::sync::atomic::Ordering::SeqCst) {
  13. std::process::exit(exitcode::SOFTWARE);
  14. }
  15. });
  16. dioxus_desktop::launch_cfg(
  17. app,
  18. Config::new().with_window(WindowBuilder::new().with_visible(true)),
  19. );
  20. // Stop deadman's switch
  21. should_panic.store(false, std::sync::atomic::Ordering::SeqCst);
  22. }
  23. pub fn main() {
  24. check_app_exits(app);
  25. }
  26. fn mock_event(cx: &ScopeState, id: &'static str, value: &'static str) {
  27. let eval_provider = use_eval(cx).clone();
  28. use_effect(cx, (), move |_| async move {
  29. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  30. let js = format!(
  31. r#"
  32. //console.log("ran");
  33. // Dispatch a synthetic event
  34. let event = {};
  35. let element = document.getElementById('{}');
  36. console.log(element, event);
  37. element.dispatchEvent(event);
  38. "#,
  39. value, id
  40. );
  41. eval_provider(&js).unwrap();
  42. })
  43. }
  44. #[allow(deprecated)]
  45. fn app(cx: Scope) -> Element {
  46. let desktop_context: DesktopContext = cx.consume_context().unwrap();
  47. let received_events = use_state(cx, || 0);
  48. // button
  49. mock_event(
  50. cx,
  51. "button",
  52. r#"new MouseEvent("click", {
  53. view: window,
  54. bubbles: true,
  55. cancelable: true,
  56. button: 0,
  57. })"#,
  58. );
  59. // mouse_move_div
  60. mock_event(
  61. cx,
  62. "mouse_move_div",
  63. r#"new MouseEvent("mousemove", {
  64. view: window,
  65. bubbles: true,
  66. cancelable: true,
  67. buttons: 2,
  68. })"#,
  69. );
  70. // mouse_click_div
  71. mock_event(
  72. cx,
  73. "mouse_click_div",
  74. r#"new MouseEvent("click", {
  75. view: window,
  76. bubbles: true,
  77. cancelable: true,
  78. buttons: 2,
  79. button: 2,
  80. })"#,
  81. );
  82. // mouse_dblclick_div
  83. mock_event(
  84. cx,
  85. "mouse_dblclick_div",
  86. r#"new MouseEvent("dblclick", {
  87. view: window,
  88. bubbles: true,
  89. cancelable: true,
  90. buttons: 1|2,
  91. button: 2,
  92. })"#,
  93. );
  94. // mouse_down_div
  95. mock_event(
  96. cx,
  97. "mouse_down_div",
  98. r#"new MouseEvent("mousedown", {
  99. view: window,
  100. bubbles: true,
  101. cancelable: true,
  102. buttons: 2,
  103. button: 2,
  104. })"#,
  105. );
  106. // mouse_up_div
  107. mock_event(
  108. cx,
  109. "mouse_up_div",
  110. r#"new MouseEvent("mouseup", {
  111. view: window,
  112. bubbles: true,
  113. cancelable: true,
  114. buttons: 0,
  115. button: 0,
  116. })"#,
  117. );
  118. // wheel_div
  119. mock_event(
  120. cx,
  121. "wheel_div",
  122. r#"new WheelEvent("wheel", {
  123. view: window,
  124. deltaX: 1.0,
  125. deltaY: 2.0,
  126. deltaZ: 3.0,
  127. deltaMode: 0x00,
  128. bubbles: true,
  129. })"#,
  130. );
  131. // key_down_div
  132. mock_event(
  133. cx,
  134. "key_down_div",
  135. r#"new KeyboardEvent("keydown", {
  136. key: "a",
  137. code: "KeyA",
  138. location: 0,
  139. repeat: true,
  140. keyCode: 65,
  141. charCode: 97,
  142. char: "a",
  143. charCode: 0,
  144. altKey: false,
  145. ctrlKey: false,
  146. metaKey: false,
  147. shiftKey: false,
  148. isComposing: false,
  149. which: 65,
  150. bubbles: true,
  151. })"#,
  152. );
  153. // key_up_div
  154. mock_event(
  155. cx,
  156. "key_up_div",
  157. r#"new KeyboardEvent("keyup", {
  158. key: "a",
  159. code: "KeyA",
  160. location: 0,
  161. repeat: false,
  162. keyCode: 65,
  163. charCode: 97,
  164. char: "a",
  165. charCode: 0,
  166. altKey: false,
  167. ctrlKey: false,
  168. metaKey: false,
  169. shiftKey: false,
  170. isComposing: false,
  171. which: 65,
  172. bubbles: true,
  173. })"#,
  174. );
  175. // key_press_div
  176. mock_event(
  177. cx,
  178. "key_press_div",
  179. r#"new KeyboardEvent("keypress", {
  180. key: "a",
  181. code: "KeyA",
  182. location: 0,
  183. repeat: false,
  184. keyCode: 65,
  185. charCode: 97,
  186. char: "a",
  187. charCode: 0,
  188. altKey: false,
  189. ctrlKey: false,
  190. metaKey: false,
  191. shiftKey: false,
  192. isComposing: false,
  193. which: 65,
  194. bubbles: true,
  195. })"#,
  196. );
  197. // focus_in_div
  198. mock_event(
  199. cx,
  200. "focus_in_div",
  201. r#"new FocusEvent("focusin", {bubbles: true})"#,
  202. );
  203. // focus_out_div
  204. mock_event(
  205. cx,
  206. "focus_out_div",
  207. r#"new FocusEvent("focusout",{bubbles: true})"#,
  208. );
  209. if **received_events == 12 {
  210. println!("all events recieved");
  211. desktop_context.close();
  212. }
  213. cx.render(rsx! {
  214. div {
  215. button {
  216. id: "button",
  217. onclick: move |event| {
  218. println!("{:?}", event.data);
  219. assert!(event.data.modifiers().is_empty());
  220. assert!(event.data.held_buttons().is_empty());
  221. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Primary));
  222. received_events.modify(|x| *x + 1)
  223. },
  224. assert_eq!(
  225. event.data.trigger_button(),
  226. Some(dioxus_html::input_data::MouseButton::Primary),
  227. );
  228. recieved_events.modify(|x| *x + 1)
  229. }
  230. }
  231. div {
  232. id: "mouse_move_div",
  233. onmousemove: move |event| {
  234. println!("{:?}", event.data);
  235. assert!(event.data.modifiers().is_empty());
  236. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
  237. received_events.modify(|x| *x + 1)
  238. }
  239. }
  240. div {
  241. id: "mouse_click_div",
  242. onclick: move |event| {
  243. println!("{:?}", event.data);
  244. assert!(event.data.modifiers().is_empty());
  245. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
  246. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
  247. received_events.modify(|x| *x + 1)
  248. }
  249. }
  250. div {
  251. id: "mouse_dblclick_div",
  252. ondoubleclick: move |event| {
  253. println!("{:?}", event.data);
  254. assert!(event.data.modifiers().is_empty());
  255. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Primary));
  256. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
  257. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
  258. received_events.modify(|x| *x + 1)
  259. }
  260. }
  261. div {
  262. id: "mouse_down_div",
  263. onmousedown: move |event| {
  264. println!("{:?}", event.data);
  265. assert!(event.data.modifiers().is_empty());
  266. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
  267. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
  268. received_events.modify(|x| *x + 1)
  269. }
  270. }
  271. div {
  272. id: "mouse_up_div",
  273. onmouseup: move |event| {
  274. println!("{:?}", event.data);
  275. assert!(event.data.modifiers().is_empty());
  276. assert!(event.data.held_buttons().is_empty());
  277. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Primary));
  278. received_events.modify(|x| *x + 1)
  279. }
  280. }
  281. div {
  282. id: "wheel_div",
  283. width: "100px",
  284. height: "100px",
  285. background_color: "red",
  286. onwheel: move |event| {
  287. println!("{:?}", event.data);
  288. let dioxus_html::geometry::WheelDelta::Pixels(delta) = event.data.delta() else {
  289. panic!("Expected delta to be in pixels") };
  290. assert_eq!(delta, Vector3D::new(1.0, 2.0, 3.0));
  291. received_events.modify(|x| *x + 1)
  292. }
  293. }
  294. input {
  295. id: "key_down_div",
  296. onkeydown: move |event| {
  297. println!("{:?}", event.data);
  298. assert!(event.data.modifiers().is_empty());
  299. assert_eq!(event.data.key().to_string(), "a");
  300. assert_eq!(event.data.code().to_string(), "KeyA");
  301. assert_eq!(event.data.location, 0);
  302. assert!(event.data.is_auto_repeating());
  303. received_events.modify(|x| *x + 1)
  304. }
  305. }
  306. input {
  307. id: "key_up_div",
  308. onkeyup: move |event| {
  309. println!("{:?}", event.data);
  310. assert!(event.data.modifiers().is_empty());
  311. assert_eq!(event.data.key().to_string(), "a");
  312. assert_eq!(event.data.code().to_string(), "KeyA");
  313. assert_eq!(event.data.location, 0);
  314. assert!(!event.data.is_auto_repeating());
  315. received_events.modify(|x| *x + 1)
  316. }
  317. }
  318. input {
  319. id: "key_press_div",
  320. onkeypress: move |event| {
  321. println!("{:?}", event.data);
  322. assert!(event.data.modifiers().is_empty());
  323. assert_eq!(event.data.key().to_string(), "a");
  324. assert_eq!(event.data.code().to_string(), "KeyA");
  325. assert_eq!(event.data.location, 0);
  326. assert!(!event.data.is_auto_repeating());
  327. received_events.modify(|x| *x + 1)
  328. }
  329. }
  330. input {
  331. id: "focus_in_div",
  332. onfocusin: move |event| {
  333. println!("{:?}", event.data);
  334. received_events.modify(|x| *x + 1)
  335. }
  336. }
  337. input {
  338. id: "focus_out_div",
  339. onfocusout: move |event| {
  340. println!("{:?}", event.data);
  341. received_events.modify(|x| *x + 1)
  342. }
  343. }
  344. }
  345. })
  346. }