events.rs 10 KB

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