events.rs 12 KB

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