events.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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(false)),
  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 recieved_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 **recieved_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. recieved_events.modify(|x| *x + 1)
  223. },
  224. }
  225. div {
  226. id: "mouse_move_div",
  227. onmousemove: move |event| {
  228. println!("{:?}", event.data);
  229. assert!(event.data.modifiers().is_empty());
  230. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
  231. recieved_events.modify(|x| *x + 1)
  232. },
  233. }
  234. div {
  235. id: "mouse_click_div",
  236. onclick: move |event| {
  237. println!("{:?}", event.data);
  238. assert!(event.data.modifiers().is_empty());
  239. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
  240. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
  241. recieved_events.modify(|x| *x + 1)
  242. },
  243. }
  244. div{
  245. id: "mouse_dblclick_div",
  246. ondblclick: move |event| {
  247. println!("{:?}", event.data);
  248. assert!(event.data.modifiers().is_empty());
  249. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Primary));
  250. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
  251. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
  252. recieved_events.modify(|x| *x + 1)
  253. }
  254. }
  255. div{
  256. id: "mouse_down_div",
  257. onmousedown: move |event| {
  258. println!("{:?}", event.data);
  259. assert!(event.data.modifiers().is_empty());
  260. assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
  261. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
  262. recieved_events.modify(|x| *x + 1)
  263. }
  264. }
  265. div{
  266. id: "mouse_up_div",
  267. onmouseup: move |event| {
  268. println!("{:?}", event.data);
  269. assert!(event.data.modifiers().is_empty());
  270. assert!(event.data.held_buttons().is_empty());
  271. assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Primary));
  272. recieved_events.modify(|x| *x + 1)
  273. }
  274. }
  275. div{
  276. id: "wheel_div",
  277. width: "100px",
  278. height: "100px",
  279. background_color: "red",
  280. onwheel: move |event| {
  281. println!("{:?}", event.data);
  282. let dioxus_html::geometry::WheelDelta::Pixels(delta)= event.data.delta()else{
  283. panic!("Expected delta to be in pixels")
  284. };
  285. assert_eq!(delta, Vector3D::new(1.0, 2.0, 3.0));
  286. recieved_events.modify(|x| *x + 1)
  287. }
  288. }
  289. input{
  290. id: "key_down_div",
  291. onkeydown: move |event| {
  292. println!("{:?}", event.data);
  293. assert!(event.data.modifiers().is_empty());
  294. assert_eq!(event.data.key().to_string(), "a");
  295. assert_eq!(event.data.code().to_string(), "KeyA");
  296. assert_eq!(event.data.location, 0);
  297. assert!(event.data.is_auto_repeating());
  298. recieved_events.modify(|x| *x + 1)
  299. }
  300. }
  301. input{
  302. id: "key_up_div",
  303. onkeyup: move |event| {
  304. println!("{:?}", event.data);
  305. assert!(event.data.modifiers().is_empty());
  306. assert_eq!(event.data.key().to_string(), "a");
  307. assert_eq!(event.data.code().to_string(), "KeyA");
  308. assert_eq!(event.data.location, 0);
  309. assert!(!event.data.is_auto_repeating());
  310. recieved_events.modify(|x| *x + 1)
  311. }
  312. }
  313. input{
  314. id: "key_press_div",
  315. onkeypress: move |event| {
  316. println!("{:?}", event.data);
  317. assert!(event.data.modifiers().is_empty());
  318. assert_eq!(event.data.key().to_string(), "a");
  319. assert_eq!(event.data.code().to_string(), "KeyA");
  320. assert_eq!(event.data.location, 0);
  321. assert!(!event.data.is_auto_repeating());
  322. recieved_events.modify(|x| *x + 1)
  323. }
  324. }
  325. input{
  326. id: "focus_in_div",
  327. onfocusin: move |event| {
  328. println!("{:?}", event.data);
  329. recieved_events.modify(|x| *x + 1)
  330. }
  331. }
  332. input{
  333. id: "focus_out_div",
  334. onfocusout: move |event| {
  335. println!("{:?}", event.data);
  336. recieved_events.modify(|x| *x + 1)
  337. }
  338. }
  339. }
  340. })
  341. }