events.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. use std::collections::HashMap;
  2. use dioxus::html::geometry::euclid::Vector3D;
  3. use dioxus::prelude::*;
  4. use dioxus_desktop::DesktopContext;
  5. #[path = "./utils.rs"]
  6. mod utils;
  7. pub fn main() {
  8. #[cfg(not(windows))]
  9. utils::check_app_exits(app);
  10. }
  11. static RECEIVED_EVENTS: GlobalSignal<usize> = Signal::global(|| 0);
  12. fn app() -> Element {
  13. let desktop_context: DesktopContext = consume_context();
  14. let received = RECEIVED_EVENTS();
  15. let expected = utils::EXPECTED_EVENTS();
  16. use_memo(move || {
  17. println!("expecting {} events", utils::EXPECTED_EVENTS());
  18. println!("received {} events", RECEIVED_EVENTS());
  19. });
  20. if expected != 0 && received == expected {
  21. println!("all events received");
  22. desktop_context.close();
  23. }
  24. rsx! {
  25. div {
  26. test_mounted {}
  27. test_button {}
  28. test_mouse_move_div {}
  29. test_mouse_click_div {}
  30. test_mouse_dblclick_div {}
  31. test_mouse_down_div {}
  32. test_mouse_up_div {}
  33. test_mouse_scroll_div {}
  34. test_key_down_div {}
  35. test_key_up_div {}
  36. test_key_press_div {}
  37. test_focus_in_div {}
  38. test_focus_out_div {}
  39. test_form_input {}
  40. test_form_submit {}
  41. test_select_multiple_options {}
  42. test_unicode {}
  43. }
  44. }
  45. }
  46. fn test_mounted() -> Element {
  47. use_hook(|| utils::EXPECTED_EVENTS.with_mut(|x| *x += 1));
  48. let mut onmounted_triggered = use_signal(|| false);
  49. rsx! {
  50. div {
  51. width: "100px",
  52. height: "100px",
  53. onmounted: move |evt| async move {
  54. let rect = evt.get_client_rect().await.unwrap();
  55. println!("rect: {:?}", rect);
  56. assert_eq!(rect.width(), 100.0);
  57. assert_eq!(rect.height(), 100.0);
  58. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  59. // Onmounted should only be called once
  60. let mut onmounted_triggered_write = onmounted_triggered.write();
  61. assert!(!*onmounted_triggered_write);
  62. *onmounted_triggered_write = true;
  63. }
  64. }
  65. }
  66. }
  67. fn test_button() -> Element {
  68. utils::mock_event(
  69. "button",
  70. r#"new MouseEvent("click", {
  71. view: window,
  72. bubbles: true,
  73. cancelable: true,
  74. button: 0,
  75. })"#,
  76. );
  77. rsx! {
  78. button {
  79. id: "button",
  80. onclick: move |event| {
  81. println!("{:?}", event.data);
  82. assert!(event.data.modifiers().is_empty());
  83. assert!(event.data.held_buttons().is_empty());
  84. assert_eq!(
  85. event.data.trigger_button(),
  86. Some(dioxus_html::input_data::MouseButton::Primary),
  87. );
  88. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  89. }
  90. }
  91. }
  92. }
  93. fn test_mouse_move_div() -> Element {
  94. utils::mock_event(
  95. "mouse_move_div",
  96. r#"new MouseEvent("mousemove", {
  97. view: window,
  98. bubbles: true,
  99. cancelable: true,
  100. buttons: 2,
  101. })"#,
  102. );
  103. rsx! {
  104. div {
  105. id: "mouse_move_div",
  106. onmousemove: move |event| {
  107. println!("{:?}", event.data);
  108. assert!(event.data.modifiers().is_empty());
  109. assert!(
  110. event
  111. .data
  112. .held_buttons()
  113. .contains(dioxus_html::input_data::MouseButton::Secondary),
  114. );
  115. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  116. }
  117. }
  118. }
  119. }
  120. fn test_mouse_click_div() -> Element {
  121. utils::mock_event(
  122. "mouse_click_div",
  123. r#"new MouseEvent("click", {
  124. view: window,
  125. bubbles: true,
  126. cancelable: true,
  127. buttons: 2,
  128. button: 2,
  129. })"#,
  130. );
  131. rsx! {
  132. div {
  133. id: "mouse_click_div",
  134. onclick: move |event| {
  135. println!("{:?}", event.data);
  136. assert!(event.data.modifiers().is_empty());
  137. assert!(
  138. event
  139. .data
  140. .held_buttons()
  141. .contains(dioxus_html::input_data::MouseButton::Secondary),
  142. );
  143. assert_eq!(
  144. event.data.trigger_button(),
  145. Some(dioxus_html::input_data::MouseButton::Secondary),
  146. );
  147. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  148. }
  149. }
  150. }
  151. }
  152. fn test_mouse_dblclick_div() -> Element {
  153. utils::mock_event(
  154. "mouse_dblclick_div",
  155. r#"new MouseEvent("dblclick", {
  156. view: window,
  157. bubbles: true,
  158. cancelable: true,
  159. buttons: 1|2,
  160. button: 2,
  161. })"#,
  162. );
  163. rsx! {
  164. div {
  165. id: "mouse_dblclick_div",
  166. ondoubleclick: move |event| {
  167. println!("{:?}", event.data);
  168. assert!(event.data.modifiers().is_empty());
  169. assert!(
  170. event
  171. .data
  172. .held_buttons()
  173. .contains(dioxus_html::input_data::MouseButton::Primary),
  174. );
  175. assert!(
  176. event
  177. .data
  178. .held_buttons()
  179. .contains(dioxus_html::input_data::MouseButton::Secondary),
  180. );
  181. assert_eq!(
  182. event.data.trigger_button(),
  183. Some(dioxus_html::input_data::MouseButton::Secondary),
  184. );
  185. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  186. }
  187. }
  188. }
  189. }
  190. fn test_mouse_down_div() -> Element {
  191. utils::mock_event(
  192. "mouse_down_div",
  193. r#"new MouseEvent("mousedown", {
  194. view: window,
  195. bubbles: true,
  196. cancelable: true,
  197. buttons: 2,
  198. button: 2,
  199. })"#,
  200. );
  201. rsx! {
  202. div {
  203. id: "mouse_down_div",
  204. onmousedown: move |event| {
  205. println!("{:?}", event.data);
  206. assert!(event.data.modifiers().is_empty());
  207. assert!(
  208. event
  209. .data
  210. .held_buttons()
  211. .contains(dioxus_html::input_data::MouseButton::Secondary),
  212. );
  213. assert_eq!(
  214. event.data.trigger_button(),
  215. Some(dioxus_html::input_data::MouseButton::Secondary),
  216. );
  217. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  218. }
  219. }
  220. }
  221. }
  222. fn test_mouse_up_div() -> Element {
  223. utils::mock_event(
  224. "mouse_up_div",
  225. r#"new MouseEvent("mouseup", {
  226. view: window,
  227. bubbles: true,
  228. cancelable: true,
  229. buttons: 0,
  230. button: 0,
  231. })"#,
  232. );
  233. rsx! {
  234. div {
  235. id: "mouse_up_div",
  236. onmouseup: move |event| {
  237. println!("{:?}", event.data);
  238. assert!(event.data.modifiers().is_empty());
  239. assert!(event.data.held_buttons().is_empty());
  240. assert_eq!(
  241. event.data.trigger_button(),
  242. Some(dioxus_html::input_data::MouseButton::Primary),
  243. );
  244. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  245. }
  246. }
  247. }
  248. }
  249. fn test_mouse_scroll_div() -> Element {
  250. utils::mock_event(
  251. "wheel_div",
  252. r#"new WheelEvent("wheel", {
  253. view: window,
  254. deltaX: 1.0,
  255. deltaY: 2.0,
  256. deltaZ: 3.0,
  257. deltaMode: 0x00,
  258. bubbles: true,
  259. })"#,
  260. );
  261. rsx! {
  262. div {
  263. id: "wheel_div",
  264. width: "100px",
  265. height: "100px",
  266. background_color: "red",
  267. onwheel: move |event| {
  268. println!("{:?}", event.data);
  269. let dioxus_html::geometry::WheelDelta::Pixels(delta) = event.data.delta() else {
  270. panic!("Expected delta to be in pixels")
  271. };
  272. assert_eq!(delta, Vector3D::new(1.0, 2.0, 3.0));
  273. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  274. }
  275. }
  276. }
  277. }
  278. fn test_key_down_div() -> Element {
  279. utils::mock_event(
  280. "key_down_div",
  281. r#"new KeyboardEvent("keydown", {
  282. key: "a",
  283. code: "KeyA",
  284. location: 0,
  285. repeat: true,
  286. keyCode: 65,
  287. charCode: 97,
  288. char: "a",
  289. charCode: 0,
  290. altKey: false,
  291. ctrlKey: false,
  292. metaKey: false,
  293. shiftKey: false,
  294. isComposing: true,
  295. which: 65,
  296. bubbles: true,
  297. })"#,
  298. );
  299. rsx! {
  300. input {
  301. id: "key_down_div",
  302. onkeydown: 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(), Location::Standard);
  308. assert!(event.data.is_auto_repeating());
  309. assert!(event.data.is_composing());
  310. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  311. }
  312. }
  313. }
  314. }
  315. fn test_key_up_div() -> Element {
  316. utils::mock_event(
  317. "key_up_div",
  318. r#"new KeyboardEvent("keyup", {
  319. key: "a",
  320. code: "KeyA",
  321. location: 0,
  322. repeat: false,
  323. keyCode: 65,
  324. charCode: 97,
  325. char: "a",
  326. charCode: 0,
  327. altKey: false,
  328. ctrlKey: false,
  329. metaKey: false,
  330. shiftKey: false,
  331. isComposing: false,
  332. which: 65,
  333. bubbles: true,
  334. })"#,
  335. );
  336. rsx! {
  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(), Location::Standard);
  345. assert!(!event.data.is_auto_repeating());
  346. assert!(!event.data.is_composing());
  347. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  348. }
  349. }
  350. }
  351. }
  352. fn test_key_press_div() -> Element {
  353. utils::mock_event(
  354. "key_press_div",
  355. r#"new KeyboardEvent("keypress", {
  356. key: "a",
  357. code: "KeyA",
  358. location: 0,
  359. repeat: false,
  360. keyCode: 65,
  361. charCode: 97,
  362. char: "a",
  363. charCode: 0,
  364. altKey: false,
  365. ctrlKey: false,
  366. metaKey: false,
  367. shiftKey: false,
  368. isComposing: false,
  369. which: 65,
  370. bubbles: true,
  371. })"#,
  372. );
  373. rsx! {
  374. input {
  375. id: "key_press_div",
  376. onkeypress: move |event| {
  377. println!("{:?}", event.data);
  378. assert!(event.data.modifiers().is_empty());
  379. assert_eq!(event.data.key().to_string(), "a");
  380. assert_eq!(event.data.code().to_string(), "KeyA");
  381. assert_eq!(event.data.location(), Location::Standard);
  382. assert!(!event.data.is_auto_repeating());
  383. assert!(!event.data.is_composing());
  384. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  385. }
  386. }
  387. }
  388. }
  389. fn test_focus_in_div() -> Element {
  390. utils::mock_event(
  391. "focus_in_div",
  392. r#"new FocusEvent("focusin", {bubbles: true})"#,
  393. );
  394. rsx! {
  395. input {
  396. id: "focus_in_div",
  397. onfocusin: move |event| {
  398. println!("{:?}", event.data);
  399. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  400. }
  401. }
  402. }
  403. }
  404. fn test_focus_out_div() -> Element {
  405. utils::mock_event(
  406. "focus_out_div",
  407. r#"new FocusEvent("focusout",{bubbles: true})"#,
  408. );
  409. rsx! {
  410. input {
  411. id: "focus_out_div",
  412. onfocusout: move |event| {
  413. println!("{:?}", event.data);
  414. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  415. }
  416. }
  417. }
  418. }
  419. fn test_form_input() -> Element {
  420. let mut values = use_signal(HashMap::new);
  421. utils::mock_event_with_extra(
  422. "form-username",
  423. r#"new Event("input", { bubbles: true, cancelable: true, composed: true })"#,
  424. r#"element.value = "hello";"#,
  425. );
  426. let set_username = move |ev: FormEvent| {
  427. values.set(ev.values());
  428. // The value of the input should match
  429. assert_eq!(ev.value(), "hello");
  430. // And then the value the form gives us should also match
  431. values.with_mut(|x| {
  432. assert_eq!(x.get("username").unwrap(), "hello");
  433. assert_eq!(x.get("full-name").unwrap(), "lorem");
  434. assert_eq!(x.get("password").unwrap(), "ipsum");
  435. assert_eq!(x.get("color").unwrap(), "red");
  436. });
  437. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  438. };
  439. rsx! {
  440. div {
  441. h1 { "Form" }
  442. form {
  443. id: "form",
  444. oninput: move |ev| {
  445. values.set(ev.values());
  446. },
  447. onsubmit: move |ev| {
  448. println!("{:?}", ev);
  449. },
  450. input {
  451. r#type: "text",
  452. name: "username",
  453. id: "form-username",
  454. oninput: set_username
  455. }
  456. input { r#type: "text", name: "full-name", value: "lorem" }
  457. input { r#type: "password", name: "password", value: "ipsum" }
  458. input {
  459. r#type: "radio",
  460. name: "color",
  461. value: "red",
  462. checked: true
  463. }
  464. input { r#type: "radio", name: "color", value: "blue" }
  465. button { r#type: "submit", value: "Submit", "Submit the form" }
  466. }
  467. }
  468. }
  469. }
  470. fn test_form_submit() -> Element {
  471. let mut values = use_signal(HashMap::new);
  472. utils::mock_event_with_extra(
  473. "form-submitter",
  474. r#"new Event("submit", { bubbles: true, cancelable: true, composed: true })"#,
  475. r#"element.submit();"#,
  476. );
  477. let set_values = move |ev: FormEvent| {
  478. values.set(ev.values());
  479. values.with_mut(|x| {
  480. assert_eq!(x.get("username").unwrap(), "goodbye");
  481. assert_eq!(x.get("full-name").unwrap(), "lorem");
  482. assert_eq!(x.get("password").unwrap(), "ipsum");
  483. assert_eq!(x.get("color").unwrap(), "red");
  484. });
  485. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  486. };
  487. rsx! {
  488. div {
  489. h1 { "Form" }
  490. form { id: "form-submitter", onsubmit: set_values,
  491. input {
  492. r#type: "text",
  493. name: "username",
  494. id: "username",
  495. value: "goodbye"
  496. }
  497. input { r#type: "text", name: "full-name", value: "lorem" }
  498. input { r#type: "password", name: "password", value: "ipsum" }
  499. input {
  500. r#type: "radio",
  501. name: "color",
  502. value: "red",
  503. checked: true
  504. }
  505. input { r#type: "radio", name: "color", value: "blue" }
  506. button { r#type: "submit", value: "Submit", "Submit the form" }
  507. }
  508. }
  509. }
  510. }
  511. fn test_select_multiple_options() -> Element {
  512. utils::mock_event_with_extra(
  513. "select-many",
  514. r#"new Event("input", { bubbles: true, cancelable: true, composed: true })"#,
  515. r#"
  516. document.getElementById('usa').selected = true;
  517. document.getElementById('canada').selected = true;
  518. document.getElementById('mexico').selected = false;
  519. "#,
  520. );
  521. rsx! {
  522. select {
  523. id: "select-many",
  524. name: "country",
  525. multiple: true,
  526. oninput: move |ev| {
  527. let values = ev.value();
  528. let values = values.split(',').collect::<Vec<_>>();
  529. assert_eq!(values, vec!["usa", "canada"]);
  530. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  531. },
  532. option { id: "usa", value: "usa", "USA" }
  533. option { id: "canada", value: "canada", "Canada" }
  534. option { id: "mexico", value: "mexico", selected: true, "Mexico" }
  535. }
  536. }
  537. }
  538. fn test_unicode() -> Element {
  539. // emulate an oninput event with a unicode character
  540. utils::mock_event_with_extra(
  541. "unicode",
  542. r#"new InputEvent("input", {
  543. inputType: 'insertText',
  544. bubbles: true,
  545. cancelable: true,
  546. })"#,
  547. r#"
  548. element.value = "🦀";
  549. "#,
  550. );
  551. rsx! {
  552. input {
  553. id: "unicode",
  554. oninput: move |event| {
  555. println!("{:?}", event.data);
  556. assert_eq!(event.data.value(), "🦀");
  557. RECEIVED_EVENTS.with_mut(|x| *x += 1);
  558. }
  559. }
  560. }
  561. }