events.rs 11 KB

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