events.rs 12 KB

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