1
0

events.rs 12 KB

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