events.rs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. use bumpalo::boxed::Box as BumpBox;
  2. use dioxus_core::exports::bumpalo;
  3. use dioxus_core::*;
  4. pub mod on {
  5. //! Input events and associated data
  6. use crate::geometry::{
  7. ClientPoint, Coordinates, ElementPoint, LinesVector, PagePoint, PagesVector, PixelsVector,
  8. ScreenPoint, WheelDelta,
  9. };
  10. use crate::input_data::{
  11. decode_key_location, decode_mouse_button_set, encode_key_location, encode_mouse_button_set,
  12. MouseButton, MouseButtonSet,
  13. };
  14. use euclid::UnknownUnit;
  15. use keyboard_types::{Code, Key, Location, Modifiers};
  16. use std::collections::HashMap;
  17. use std::convert::TryInto;
  18. use std::fmt::{Debug, Formatter};
  19. use std::str::FromStr;
  20. use super::*;
  21. macro_rules! event_directory {
  22. ( $(
  23. $( #[$attr:meta] )*
  24. $wrapper:ident($data:ident): [
  25. $(
  26. $( #[$method_attr:meta] )*
  27. $name:ident
  28. )*
  29. ];
  30. )* ) => {
  31. $(
  32. $(
  33. $(#[$method_attr])*
  34. pub fn $name<'a>(
  35. factory: NodeFactory<'a>,
  36. mut callback: impl FnMut($wrapper) + 'a,
  37. // mut callback: impl FnMut(UiEvent<$data>) + 'a,
  38. ) -> Listener<'a>
  39. {
  40. let bump = &factory.bump();
  41. use dioxus_core::{AnyEvent};
  42. // we can't allocate unsized in bumpalo's box, so we need to craft the box manually
  43. // safety: this is essentially the same as calling Box::new() but manually
  44. // The box is attached to the lifetime of the bumpalo allocator
  45. let cb: &mut dyn FnMut(AnyEvent) = bump.alloc(move |evt: AnyEvent| {
  46. let event = evt.downcast::<$data>().unwrap();
  47. callback(event)
  48. });
  49. let callback: BumpBox<dyn FnMut(AnyEvent) + 'a> = unsafe { BumpBox::from_raw(cb) };
  50. // ie oncopy
  51. let event_name = stringify!($name);
  52. // ie copy
  53. let shortname: &'static str = &event_name[2..];
  54. let handler = bump.alloc(std::cell::RefCell::new(Some(callback)));
  55. factory.listener(shortname, handler)
  56. }
  57. )*
  58. )*
  59. };
  60. }
  61. // The Dioxus Synthetic event system
  62. // todo: move these into the html event system. dioxus accepts *any* event, so having these here doesn't make sense.
  63. event_directory! {
  64. ClipboardEvent(ClipboardData): [
  65. /// Called when "copy"
  66. oncopy
  67. /// oncut
  68. oncut
  69. /// onpaste
  70. onpaste
  71. ];
  72. CompositionEvent(CompositionData): [
  73. /// oncompositionend
  74. oncompositionend
  75. /// oncompositionstart
  76. oncompositionstart
  77. /// oncompositionupdate
  78. oncompositionupdate
  79. ];
  80. KeyboardEvent(KeyboardData): [
  81. /// onkeydown
  82. onkeydown
  83. /// onkeypress
  84. onkeypress
  85. /// onkeyup
  86. onkeyup
  87. ];
  88. FocusEvent(FocusData): [
  89. /// onfocus
  90. onfocus
  91. // onfocusout
  92. onfocusout
  93. // onfocusin
  94. onfocusin
  95. /// onblur
  96. onblur
  97. ];
  98. FormEvent(FormData): [
  99. /// onchange
  100. onchange
  101. /// oninput handler
  102. oninput
  103. /// oninvalid
  104. oninvalid
  105. /// onreset
  106. onreset
  107. /// onsubmit
  108. onsubmit
  109. ];
  110. /// A synthetic event that wraps a web-style [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent)
  111. ///
  112. ///
  113. /// The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse).
  114. ///
  115. /// ## Trait implementation:
  116. /// ```rust, ignore
  117. /// fn alt_key(&self) -> bool;
  118. /// fn button(&self) -> i16;
  119. /// fn buttons(&self) -> u16;
  120. /// fn client_x(&self) -> i32;
  121. /// fn client_y(&self) -> i32;
  122. /// fn ctrl_key(&self) -> bool;
  123. /// fn meta_key(&self) -> bool;
  124. /// fn page_x(&self) -> i32;
  125. /// fn page_y(&self) -> i32;
  126. /// fn screen_x(&self) -> i32;
  127. /// fn screen_y(&self) -> i32;
  128. /// fn shift_key(&self) -> bool;
  129. /// fn get_modifier_state(&self, key_code: &str) -> bool;
  130. /// ```
  131. ///
  132. /// ## Event Handlers
  133. /// - [`onclick`]
  134. /// - [`oncontextmenu`]
  135. /// - [`ondoubleclick`]
  136. /// - [`ondrag`]
  137. /// - [`ondragend`]
  138. /// - [`ondragenter`]
  139. /// - [`ondragexit`]
  140. /// - [`ondragleave`]
  141. /// - [`ondragover`]
  142. /// - [`ondragstart`]
  143. /// - [`ondrop`]
  144. /// - [`onmousedown`]
  145. /// - [`onmouseenter`]
  146. /// - [`onmouseleave`]
  147. /// - [`onmousemove`]
  148. /// - [`onmouseout`]
  149. /// - [`onmouseover`]
  150. /// - [`onmouseup`]
  151. MouseEvent(MouseData): [
  152. /// Execute a callback when a button is clicked.
  153. ///
  154. /// ## Description
  155. ///
  156. /// An element receives a click event when a pointing device button (such as a mouse's primary mouse button)
  157. /// is both pressed and released while the pointer is located inside the element.
  158. ///
  159. /// - Bubbles: Yes
  160. /// - Cancelable: Yes
  161. /// - Interface(InteData): [`MouseEvent`]
  162. ///
  163. /// If the button is pressed on one element and the pointer is moved outside the element before the button
  164. /// is released, the event is fired on the most specific ancestor element that contained both elements.
  165. /// `click` fires after both the `mousedown` and `mouseup` events have fired, in that order.
  166. ///
  167. /// ## Example
  168. /// ```
  169. /// rsx!( button { "click me", onclick: move |_| log::info!("Clicked!`") } )
  170. /// ```
  171. ///
  172. /// ## Reference
  173. /// - <https://www.w3schools.com/tags/ev_onclick.asp>
  174. /// - <https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event>
  175. onclick
  176. /// oncontextmenu
  177. oncontextmenu
  178. /// ondoubleclick
  179. ondoubleclick
  180. /// ondoubleclick
  181. ondblclick
  182. /// ondrag
  183. ondrag
  184. /// ondragend
  185. ondragend
  186. /// ondragenter
  187. ondragenter
  188. /// ondragexit
  189. ondragexit
  190. /// ondragleave
  191. ondragleave
  192. /// ondragover
  193. ondragover
  194. /// ondragstart
  195. ondragstart
  196. /// ondrop
  197. ondrop
  198. /// onmousedown
  199. onmousedown
  200. /// onmouseenter
  201. onmouseenter
  202. /// onmouseleave
  203. onmouseleave
  204. /// onmousemove
  205. onmousemove
  206. /// onmouseout
  207. onmouseout
  208. ///
  209. onscroll
  210. /// onmouseover
  211. ///
  212. /// Triggered when the users's mouse hovers over an element.
  213. onmouseover
  214. /// onmouseup
  215. onmouseup
  216. ];
  217. PointerEvent(PointerData): [
  218. /// pointerdown
  219. onpointerdown
  220. /// pointermove
  221. onpointermove
  222. /// pointerup
  223. onpointerup
  224. /// pointercancel
  225. onpointercancel
  226. /// gotpointercapture
  227. ongotpointercapture
  228. /// lostpointercapture
  229. onlostpointercapture
  230. /// pointerenter
  231. onpointerenter
  232. /// pointerleave
  233. onpointerleave
  234. /// pointerover
  235. onpointerover
  236. /// pointerout
  237. onpointerout
  238. ];
  239. SelectionEvent(SelectionData): [
  240. /// onselect
  241. onselect
  242. ];
  243. TouchEvent(TouchData): [
  244. /// ontouchcancel
  245. ontouchcancel
  246. /// ontouchend
  247. ontouchend
  248. /// ontouchmove
  249. ontouchmove
  250. /// ontouchstart
  251. ontouchstart
  252. ];
  253. WheelEvent(WheelData): [
  254. ///
  255. onwheel
  256. ];
  257. MediaEvent(MediaData): [
  258. ///abort
  259. onabort
  260. ///canplay
  261. oncanplay
  262. ///canplaythrough
  263. oncanplaythrough
  264. ///durationchange
  265. ondurationchange
  266. ///emptied
  267. onemptied
  268. ///encrypted
  269. onencrypted
  270. ///ended
  271. onended
  272. ///error
  273. onerror
  274. ///loadeddata
  275. onloadeddata
  276. ///loadedmetadata
  277. onloadedmetadata
  278. ///loadstart
  279. onloadstart
  280. ///pause
  281. onpause
  282. ///play
  283. onplay
  284. ///playing
  285. onplaying
  286. ///progress
  287. onprogress
  288. ///ratechange
  289. onratechange
  290. ///seeked
  291. onseeked
  292. ///seeking
  293. onseeking
  294. ///stalled
  295. onstalled
  296. ///suspend
  297. onsuspend
  298. ///timeupdate
  299. ontimeupdate
  300. ///volumechange
  301. onvolumechange
  302. ///waiting
  303. onwaiting
  304. ];
  305. AnimationEvent(AnimationData): [
  306. /// onanimationstart
  307. onanimationstart
  308. /// onanimationend
  309. onanimationend
  310. /// onanimationiteration
  311. onanimationiteration
  312. ];
  313. TransitionEvent(TransitionData): [
  314. ///
  315. ontransitionend
  316. ];
  317. ToggleEvent(ToggleData): [
  318. ///
  319. ontoggle
  320. ];
  321. }
  322. pub type ClipboardEvent = UiEvent<ClipboardData>;
  323. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  324. #[derive(Debug, Clone)]
  325. pub struct ClipboardData {
  326. // DOMDataTransfer clipboardData
  327. }
  328. pub type CompositionEvent = UiEvent<CompositionData>;
  329. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  330. #[derive(Debug, Clone)]
  331. pub struct CompositionData {
  332. pub data: String,
  333. }
  334. pub type KeyboardEvent = UiEvent<KeyboardData>;
  335. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  336. #[derive(Debug, Clone)]
  337. pub struct KeyboardData {
  338. #[deprecated(
  339. since = "0.3.0",
  340. note = "This may not work in all environments. Use key() instead."
  341. )]
  342. pub char_code: u32,
  343. /// Identify which "key" was entered.
  344. #[deprecated(since = "0.3.0", note = "use key() instead")]
  345. pub key: String,
  346. /// Get the key code as an enum Variant.
  347. #[deprecated(
  348. since = "0.3.0",
  349. note = "This may not work in all environments. Use code() instead."
  350. )]
  351. pub key_code: KeyCode,
  352. /// the physical key on the keyboard
  353. code: Code,
  354. /// Indicate if the `alt` modifier key was pressed during this keyboard event
  355. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  356. pub alt_key: bool,
  357. /// Indicate if the `ctrl` modifier key was pressed during this keyboard event
  358. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  359. pub ctrl_key: bool,
  360. /// Indicate if the `meta` modifier key was pressed during this keyboard event
  361. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  362. pub meta_key: bool,
  363. /// Indicate if the `shift` modifier key was pressed during this keyboard event
  364. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  365. pub shift_key: bool,
  366. #[deprecated(since = "0.3.0", note = "use location() instead")]
  367. pub location: usize,
  368. #[deprecated(since = "0.3.0", note = "use is_auto_repeating() instead")]
  369. pub repeat: bool,
  370. #[deprecated(since = "0.3.0", note = "use code() or key() instead")]
  371. pub which: usize,
  372. }
  373. impl KeyboardData {
  374. pub fn new(
  375. key: Key,
  376. code: Code,
  377. location: Location,
  378. is_auto_repeating: bool,
  379. modifiers: Modifiers,
  380. ) -> Self {
  381. #[allow(deprecated)]
  382. KeyboardData {
  383. char_code: key.legacy_charcode(),
  384. key: key.to_string(),
  385. key_code: KeyCode::from_raw_code(
  386. key.legacy_keycode()
  387. .try_into()
  388. .expect("could not convert keycode to u8"),
  389. ),
  390. code,
  391. alt_key: modifiers.contains(Modifiers::ALT),
  392. ctrl_key: modifiers.contains(Modifiers::CONTROL),
  393. meta_key: modifiers.contains(Modifiers::META),
  394. shift_key: modifiers.contains(Modifiers::SHIFT),
  395. location: encode_key_location(location),
  396. repeat: is_auto_repeating,
  397. which: key
  398. .legacy_charcode()
  399. .try_into()
  400. .expect("could not convert charcode to usize"),
  401. }
  402. }
  403. /// The value of the key pressed by the user, taking into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout.
  404. pub fn key(&self) -> Key {
  405. #[allow(deprecated)]
  406. FromStr::from_str(&self.key).expect("could not parse")
  407. }
  408. /// A physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value that isn't altered by keyboard layout or the state of the modifier keys.
  409. pub fn code(&self) -> Code {
  410. self.code
  411. }
  412. /// The set of modifier keys which were pressed when the event occurred
  413. pub fn modifiers(&self) -> Modifiers {
  414. let mut modifiers = Modifiers::empty();
  415. #[allow(deprecated)]
  416. {
  417. if self.alt_key {
  418. modifiers.insert(Modifiers::ALT);
  419. }
  420. if self.ctrl_key {
  421. modifiers.insert(Modifiers::CONTROL);
  422. }
  423. if self.meta_key {
  424. modifiers.insert(Modifiers::META);
  425. }
  426. if self.shift_key {
  427. modifiers.insert(Modifiers::SHIFT);
  428. }
  429. }
  430. modifiers
  431. }
  432. /// The location of the key on the keyboard or other input device.
  433. pub fn location(&self) -> Location {
  434. #[allow(deprecated)]
  435. decode_key_location(self.location)
  436. }
  437. /// `true` iff the key is being held down such that it is automatically repeating.
  438. pub fn is_auto_repeating(&self) -> bool {
  439. #[allow(deprecated)]
  440. self.repeat
  441. }
  442. }
  443. pub type FocusEvent = UiEvent<FocusData>;
  444. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  445. #[derive(Debug, Clone)]
  446. pub struct FocusData {/* DOMEventInner: Send + SyncTarget relatedTarget */}
  447. pub type FormEvent = UiEvent<FormData>;
  448. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  449. #[derive(Debug, Clone)]
  450. pub struct FormData {
  451. pub value: String,
  452. pub values: HashMap<String, String>,
  453. /* DOMEvent: Send + SyncTarget relatedTarget */
  454. }
  455. pub type MouseEvent = UiEvent<MouseData>;
  456. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  457. #[derive(Clone)]
  458. /// Data associated with a mouse event
  459. ///
  460. /// Do not use the deprecated fields; they may change or become private in the future.
  461. pub struct MouseData {
  462. /// True if the alt key was down when the mouse event was fired.
  463. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  464. pub alt_key: bool,
  465. /// The button number that was pressed (if applicable) when the mouse event was fired.
  466. #[deprecated(since = "0.3.0", note = "use trigger_button() instead")]
  467. pub button: i16,
  468. /// Indicates which buttons are pressed on the mouse (or other input device) when a mouse event is triggered.
  469. ///
  470. /// Each button that can be pressed is represented by a given number (see below). If more than one button is pressed, the button values are added together to produce a new number. For example, if the secondary (2) and auxiliary (4) buttons are pressed simultaneously, the value is 6 (i.e., 2 + 4).
  471. ///
  472. /// - 1: Primary button (usually the left button)
  473. /// - 2: Secondary button (usually the right button)
  474. /// - 4: Auxiliary button (usually the mouse wheel button or middle button)
  475. /// - 8: 4th button (typically the "Browser Back" button)
  476. /// - 16 : 5th button (typically the "Browser Forward" button)
  477. #[deprecated(since = "0.3.0", note = "use held_buttons() instead")]
  478. pub buttons: u16,
  479. /// The horizontal coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page).
  480. ///
  481. /// For example, clicking on the left edge of the viewport will always result in a mouse event with a clientX value of 0, regardless of whether the page is scrolled horizontally.
  482. #[deprecated(since = "0.3.0", note = "use client_coordinates() instead")]
  483. pub client_x: i32,
  484. /// The vertical coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page).
  485. ///
  486. /// For example, clicking on the top edge of the viewport will always result in a mouse event with a clientY value of 0, regardless of whether the page is scrolled vertically.
  487. #[deprecated(since = "0.3.0", note = "use client_coordinates() instead")]
  488. pub client_y: i32,
  489. /// True if the control key was down when the mouse event was fired.
  490. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  491. pub ctrl_key: bool,
  492. /// True if the meta key was down when the mouse event was fired.
  493. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  494. pub meta_key: bool,
  495. /// The offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node.
  496. #[deprecated(since = "0.3.0", note = "use element_coordinates() instead")]
  497. pub offset_x: i32,
  498. /// The offset in the Y coordinate of the mouse pointer between that event and the padding edge of the target node.
  499. #[deprecated(since = "0.3.0", note = "use element_coordinates() instead")]
  500. pub offset_y: i32,
  501. /// The X (horizontal) coordinate (in pixels) of the mouse, relative to the left edge of the entire document. This includes any portion of the document not currently visible.
  502. ///
  503. /// Being based on the edge of the document as it is, this property takes into account any horizontal scrolling of the page. For example, if the page is scrolled such that 200 pixels of the left side of the document are scrolled out of view, and the mouse is clicked 100 pixels inward from the left edge of the view, the value returned by pageX will be 300.
  504. #[deprecated(since = "0.3.0", note = "use page_coordinates() instead")]
  505. pub page_x: i32,
  506. /// The Y (vertical) coordinate in pixels of the event relative to the whole document.
  507. ///
  508. /// See `page_x`.
  509. #[deprecated(since = "0.3.0", note = "use page_coordinates() instead")]
  510. pub page_y: i32,
  511. /// The X coordinate of the mouse pointer in global (screen) coordinates.
  512. #[deprecated(since = "0.3.0", note = "use screen_coordinates() instead")]
  513. pub screen_x: i32,
  514. /// The Y coordinate of the mouse pointer in global (screen) coordinates.
  515. #[deprecated(since = "0.3.0", note = "use screen_coordinates() instead")]
  516. pub screen_y: i32,
  517. /// True if the shift key was down when the mouse event was fired.
  518. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  519. pub shift_key: bool,
  520. // fn get_modifier_state(&self, key_code: &str) -> bool;
  521. }
  522. impl MouseData {
  523. /// Construct MouseData with the specified properties
  524. ///
  525. /// Note: the current implementation truncates coordinates. In the future, when we change the internal representation, it may also support a fractional part.
  526. pub fn new(
  527. coordinates: Coordinates,
  528. trigger_button: Option<MouseButton>,
  529. held_buttons: MouseButtonSet,
  530. modifiers: Modifiers,
  531. ) -> Self {
  532. let alt_key = modifiers.contains(Modifiers::ALT);
  533. let ctrl_key = modifiers.contains(Modifiers::CONTROL);
  534. let meta_key = modifiers.contains(Modifiers::META);
  535. let shift_key = modifiers.contains(Modifiers::SHIFT);
  536. let [client_x, client_y]: [i32; 2] = coordinates.client().cast().into();
  537. let [offset_x, offset_y]: [i32; 2] = coordinates.element().cast().into();
  538. let [page_x, page_y]: [i32; 2] = coordinates.page().cast().into();
  539. let [screen_x, screen_y]: [i32; 2] = coordinates.screen().cast().into();
  540. #[allow(deprecated)]
  541. Self {
  542. alt_key,
  543. ctrl_key,
  544. meta_key,
  545. shift_key,
  546. button: trigger_button.map_or(0, |b| b.into_web_code()),
  547. buttons: encode_mouse_button_set(held_buttons),
  548. client_x,
  549. client_y,
  550. offset_x,
  551. offset_y,
  552. page_x,
  553. page_y,
  554. screen_x,
  555. screen_y,
  556. }
  557. }
  558. /// The event's coordinates relative to the application's viewport (as opposed to the coordinate within the page).
  559. ///
  560. /// For example, clicking in the top left corner of the viewport will always result in a mouse event with client coordinates (0., 0.), regardless of whether the page is scrolled horizontally.
  561. pub fn client_coordinates(&self) -> ClientPoint {
  562. #[allow(deprecated)]
  563. ClientPoint::new(self.client_x.into(), self.client_y.into())
  564. }
  565. /// The event's coordinates relative to the padding edge of the target element
  566. ///
  567. /// For example, clicking in the top left corner of an element will result in element coordinates (0., 0.)
  568. pub fn element_coordinates(&self) -> ElementPoint {
  569. #[allow(deprecated)]
  570. ElementPoint::new(self.offset_x.into(), self.offset_y.into())
  571. }
  572. /// The event's coordinates relative to the entire document. This includes any portion of the document not currently visible.
  573. ///
  574. /// For example, if the page is scrolled 200 pixels to the right and 300 pixels down, clicking in the top left corner of the viewport would result in page coordinates (200., 300.)
  575. pub fn page_coordinates(&self) -> PagePoint {
  576. #[allow(deprecated)]
  577. PagePoint::new(self.page_x.into(), self.page_y.into())
  578. }
  579. /// The event's coordinates relative to the entire screen. This takes into account the window's offset.
  580. pub fn screen_coordinates(&self) -> ScreenPoint {
  581. #[allow(deprecated)]
  582. ScreenPoint::new(self.screen_x.into(), self.screen_y.into())
  583. }
  584. pub fn coordinates(&self) -> Coordinates {
  585. Coordinates::new(
  586. self.screen_coordinates(),
  587. self.client_coordinates(),
  588. self.element_coordinates(),
  589. self.page_coordinates(),
  590. )
  591. }
  592. /// The set of modifier keys which were pressed when the event occurred
  593. pub fn modifiers(&self) -> Modifiers {
  594. let mut modifiers = Modifiers::empty();
  595. #[allow(deprecated)]
  596. {
  597. if self.alt_key {
  598. modifiers.insert(Modifiers::ALT);
  599. }
  600. if self.ctrl_key {
  601. modifiers.insert(Modifiers::CONTROL);
  602. }
  603. if self.meta_key {
  604. modifiers.insert(Modifiers::META);
  605. }
  606. if self.shift_key {
  607. modifiers.insert(Modifiers::SHIFT);
  608. }
  609. }
  610. modifiers
  611. }
  612. /// The set of mouse buttons which were held when the event occurred.
  613. pub fn held_buttons(&self) -> MouseButtonSet {
  614. #[allow(deprecated)]
  615. decode_mouse_button_set(self.buttons)
  616. }
  617. /// The mouse button that triggered the event
  618. ///
  619. // todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here
  620. /// This is only guaranteed to indicate which button was pressed during events caused by pressing or releasing a button. As such, it is not reliable for events such as mouseenter, mouseleave, mouseover, mouseout, or mousemove. For example, a value of MouseButton::Primary may also indicate that no button was pressed.
  621. pub fn trigger_button(&self) -> Option<MouseButton> {
  622. #[allow(deprecated)]
  623. Some(MouseButton::from_web_code(self.button))
  624. }
  625. }
  626. impl Debug for MouseData {
  627. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  628. f.debug_struct("MouseData")
  629. .field("coordinates", &self.coordinates())
  630. .field("modifiers", &self.modifiers())
  631. .field("held_buttons", &self.held_buttons())
  632. .field("trigger_button", &self.trigger_button())
  633. .finish()
  634. }
  635. }
  636. pub type PointerEvent = UiEvent<PointerData>;
  637. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  638. #[derive(Debug, Clone)]
  639. pub struct PointerData {
  640. // Mouse only
  641. pub alt_key: bool,
  642. pub button: i16,
  643. pub buttons: u16,
  644. pub client_x: i32,
  645. pub client_y: i32,
  646. pub ctrl_key: bool,
  647. pub meta_key: bool,
  648. pub page_x: i32,
  649. pub page_y: i32,
  650. pub screen_x: i32,
  651. pub screen_y: i32,
  652. pub shift_key: bool,
  653. pub pointer_id: i32,
  654. pub width: i32,
  655. pub height: i32,
  656. pub pressure: f32,
  657. pub tangential_pressure: f32,
  658. pub tilt_x: i32,
  659. pub tilt_y: i32,
  660. pub twist: i32,
  661. pub pointer_type: String,
  662. pub is_primary: bool,
  663. // pub get_modifier_state: bool,
  664. }
  665. pub type SelectionEvent = UiEvent<SelectionData>;
  666. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  667. #[derive(Debug, Clone)]
  668. pub struct SelectionData {}
  669. pub type TouchEvent = UiEvent<TouchData>;
  670. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  671. #[derive(Debug, Clone)]
  672. pub struct TouchData {
  673. pub alt_key: bool,
  674. pub ctrl_key: bool,
  675. pub meta_key: bool,
  676. pub shift_key: bool,
  677. // get_modifier_state: bool,
  678. // changedTouches: DOMTouchList,
  679. // targetTouches: DOMTouchList,
  680. // touches: DOMTouchList,
  681. }
  682. pub type WheelEvent = UiEvent<WheelData>;
  683. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  684. #[derive(Debug, Clone)]
  685. pub struct WheelData {
  686. #[deprecated(since = "0.3.0", note = "use delta() instead")]
  687. pub delta_mode: u32,
  688. #[deprecated(since = "0.3.0", note = "use delta() instead")]
  689. pub delta_x: f64,
  690. #[deprecated(since = "0.3.0", note = "use delta() instead")]
  691. pub delta_y: f64,
  692. #[deprecated(since = "0.3.0", note = "use delta() instead")]
  693. pub delta_z: f64,
  694. }
  695. impl WheelData {
  696. /// Construct a new WheelData with the specified wheel movement delta
  697. pub fn new(delta: WheelDelta) -> Self {
  698. let (delta_mode, vector) = match delta {
  699. WheelDelta::Pixels(v) => (0, v.cast_unit::<UnknownUnit>()),
  700. WheelDelta::Lines(v) => (1, v.cast_unit::<UnknownUnit>()),
  701. WheelDelta::Pages(v) => (2, v.cast_unit::<UnknownUnit>()),
  702. };
  703. #[allow(deprecated)]
  704. WheelData {
  705. delta_mode,
  706. delta_x: vector.x,
  707. delta_y: vector.y,
  708. delta_z: vector.z,
  709. }
  710. }
  711. /// Construct from the attributes of the web wheel event
  712. pub fn from_web_attributes(
  713. delta_mode: u32,
  714. delta_x: f64,
  715. delta_y: f64,
  716. delta_z: f64,
  717. ) -> Self {
  718. #[allow(deprecated)]
  719. Self {
  720. delta_mode,
  721. delta_x,
  722. delta_y,
  723. delta_z,
  724. }
  725. }
  726. /// The amount of wheel movement
  727. #[allow(deprecated)]
  728. pub fn delta(&self) -> WheelDelta {
  729. let x = self.delta_x;
  730. let y = self.delta_y;
  731. let z = self.delta_z;
  732. match self.delta_mode {
  733. 0 => WheelDelta::Pixels(PixelsVector::new(x, y, z)),
  734. 1 => WheelDelta::Lines(LinesVector::new(x, y, z)),
  735. 2 => WheelDelta::Pages(PagesVector::new(x, y, z)),
  736. _ => panic!("Invalid delta mode, {:?}", self.delta_mode),
  737. }
  738. }
  739. }
  740. pub type MediaEvent = UiEvent<MediaData>;
  741. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  742. #[derive(Debug, Clone)]
  743. pub struct MediaData {}
  744. pub type ImageEvent = UiEvent<ImageData>;
  745. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  746. #[derive(Debug, Clone)]
  747. pub struct ImageData {
  748. pub load_error: bool,
  749. }
  750. pub type AnimationEvent = UiEvent<AnimationData>;
  751. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  752. #[derive(Debug, Clone)]
  753. pub struct AnimationData {
  754. pub animation_name: String,
  755. pub pseudo_element: String,
  756. pub elapsed_time: f32,
  757. }
  758. pub type TransitionEvent = UiEvent<TransitionData>;
  759. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  760. #[derive(Debug, Clone)]
  761. pub struct TransitionData {
  762. pub property_name: String,
  763. pub pseudo_element: String,
  764. pub elapsed_time: f32,
  765. }
  766. pub type ToggleEvent = UiEvent<ToggleData>;
  767. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  768. #[derive(Debug, Clone)]
  769. pub struct ToggleData {}
  770. }
  771. #[cfg_attr(
  772. feature = "serialize",
  773. derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr)
  774. )]
  775. #[derive(Clone, Copy, Debug, PartialEq)]
  776. #[repr(u8)]
  777. pub enum KeyCode {
  778. // That key has no keycode, = 0
  779. // break, = 3
  780. // backspace / delete, = 8
  781. // tab, = 9
  782. // clear, = 12
  783. // enter, = 13
  784. // shift, = 16
  785. // ctrl, = 17
  786. // alt, = 18
  787. // pause/break, = 19
  788. // caps lock, = 20
  789. // hangul, = 21
  790. // hanja, = 25
  791. // escape, = 27
  792. // conversion, = 28
  793. // non-conversion, = 29
  794. // spacebar, = 32
  795. // page up, = 33
  796. // page down, = 34
  797. // end, = 35
  798. // home, = 36
  799. // left arrow, = 37
  800. // up arrow, = 38
  801. // right arrow, = 39
  802. // down arrow, = 40
  803. // select, = 41
  804. // print, = 42
  805. // execute, = 43
  806. // Print Screen, = 44
  807. // insert, = 45
  808. // delete, = 46
  809. // help, = 47
  810. // 0, = 48
  811. // 1, = 49
  812. // 2, = 50
  813. // 3, = 51
  814. // 4, = 52
  815. // 5, = 53
  816. // 6, = 54
  817. // 7, = 55
  818. // 8, = 56
  819. // 9, = 57
  820. // :, = 58
  821. // semicolon (firefox), equals, = 59
  822. // <, = 60
  823. // equals (firefox), = 61
  824. // ß, = 63
  825. // @ (firefox), = 64
  826. // a, = 65
  827. // b, = 66
  828. // c, = 67
  829. // d, = 68
  830. // e, = 69
  831. // f, = 70
  832. // g, = 71
  833. // h, = 72
  834. // i, = 73
  835. // j, = 74
  836. // k, = 75
  837. // l, = 76
  838. // m, = 77
  839. // n, = 78
  840. // o, = 79
  841. // p, = 80
  842. // q, = 81
  843. // r, = 82
  844. // s, = 83
  845. // t, = 84
  846. // u, = 85
  847. // v, = 86
  848. // w, = 87
  849. // x, = 88
  850. // y, = 89
  851. // z, = 90
  852. // Windows Key / Left ⌘ / Chromebook Search key, = 91
  853. // right window key, = 92
  854. // Windows Menu / Right ⌘, = 93
  855. // sleep, = 95
  856. // numpad 0, = 96
  857. // numpad 1, = 97
  858. // numpad 2, = 98
  859. // numpad 3, = 99
  860. // numpad 4, = 100
  861. // numpad 5, = 101
  862. // numpad 6, = 102
  863. // numpad 7, = 103
  864. // numpad 8, = 104
  865. // numpad 9, = 105
  866. // multiply, = 106
  867. // add, = 107
  868. // numpad period (firefox), = 108
  869. // subtract, = 109
  870. // decimal point, = 110
  871. // divide, = 111
  872. // f1, = 112
  873. // f2, = 113
  874. // f3, = 114
  875. // f4, = 115
  876. // f5, = 116
  877. // f6, = 117
  878. // f7, = 118
  879. // f8, = 119
  880. // f9, = 120
  881. // f10, = 121
  882. // f11, = 122
  883. // f12, = 123
  884. // f13, = 124
  885. // f14, = 125
  886. // f15, = 126
  887. // f16, = 127
  888. // f17, = 128
  889. // f18, = 129
  890. // f19, = 130
  891. // f20, = 131
  892. // f21, = 132
  893. // f22, = 133
  894. // f23, = 134
  895. // f24, = 135
  896. // f25, = 136
  897. // f26, = 137
  898. // f27, = 138
  899. // f28, = 139
  900. // f29, = 140
  901. // f30, = 141
  902. // f31, = 142
  903. // f32, = 143
  904. // num lock, = 144
  905. // scroll lock, = 145
  906. // airplane mode, = 151
  907. // ^, = 160
  908. // !, = 161
  909. // ؛ (arabic semicolon), = 162
  910. // #, = 163
  911. // $, = 164
  912. // ù, = 165
  913. // page backward, = 166
  914. // page forward, = 167
  915. // refresh, = 168
  916. // closing paren (AZERTY), = 169
  917. // *, = 170
  918. // ~ + * key, = 171
  919. // home key, = 172
  920. // minus (firefox), mute/unmute, = 173
  921. // decrease volume level, = 174
  922. // increase volume level, = 175
  923. // next, = 176
  924. // previous, = 177
  925. // stop, = 178
  926. // play/pause, = 179
  927. // e-mail, = 180
  928. // mute/unmute (firefox), = 181
  929. // decrease volume level (firefox), = 182
  930. // increase volume level (firefox), = 183
  931. // semi-colon / ñ, = 186
  932. // equal sign, = 187
  933. // comma, = 188
  934. // dash, = 189
  935. // period, = 190
  936. // forward slash / ç, = 191
  937. // grave accent / ñ / æ / ö, = 192
  938. // ?, / or °, = 193
  939. // numpad period (chrome), = 194
  940. // open bracket, = 219
  941. // back slash, = 220
  942. // close bracket / å, = 221
  943. // single quote / ø / ä, = 222
  944. // `, = 223
  945. // left or right ⌘ key (firefox), = 224
  946. // altgr, = 225
  947. // < /git >, left back slash, = 226
  948. // GNOME Compose Key, = 230
  949. // ç, = 231
  950. // XF86Forward, = 233
  951. // XF86Back, = 234
  952. // non-conversion, = 235
  953. // alphanumeric, = 240
  954. // hiragana/katakana, = 242
  955. // half-width/full-width, = 243
  956. // kanji, = 244
  957. // unlock trackpad (Chrome/Edge), = 251
  958. // toggle touchpad, = 255
  959. NA = 0,
  960. Break = 3,
  961. Backspace = 8,
  962. Tab = 9,
  963. Clear = 12,
  964. Enter = 13,
  965. Shift = 16,
  966. Ctrl = 17,
  967. Alt = 18,
  968. Pause = 19,
  969. CapsLock = 20,
  970. // hangul, = 21
  971. // hanja, = 25
  972. Escape = 27,
  973. // conversion, = 28
  974. // non-conversion, = 29
  975. Space = 32,
  976. PageUp = 33,
  977. PageDown = 34,
  978. End = 35,
  979. Home = 36,
  980. LeftArrow = 37,
  981. UpArrow = 38,
  982. RightArrow = 39,
  983. DownArrow = 40,
  984. // select, = 41
  985. // print, = 42
  986. // execute, = 43
  987. // Print Screen, = 44
  988. Insert = 45,
  989. Delete = 46,
  990. // help, = 47
  991. Num0 = 48,
  992. Num1 = 49,
  993. Num2 = 50,
  994. Num3 = 51,
  995. Num4 = 52,
  996. Num5 = 53,
  997. Num6 = 54,
  998. Num7 = 55,
  999. Num8 = 56,
  1000. Num9 = 57,
  1001. // :, = 58
  1002. // semicolon (firefox), equals, = 59
  1003. // <, = 60
  1004. // equals (firefox), = 61
  1005. // ß, = 63
  1006. // @ (firefox), = 64
  1007. A = 65,
  1008. B = 66,
  1009. C = 67,
  1010. D = 68,
  1011. E = 69,
  1012. F = 70,
  1013. G = 71,
  1014. H = 72,
  1015. I = 73,
  1016. J = 74,
  1017. K = 75,
  1018. L = 76,
  1019. M = 77,
  1020. N = 78,
  1021. O = 79,
  1022. P = 80,
  1023. Q = 81,
  1024. R = 82,
  1025. S = 83,
  1026. T = 84,
  1027. U = 85,
  1028. V = 86,
  1029. W = 87,
  1030. X = 88,
  1031. Y = 89,
  1032. Z = 90,
  1033. LeftWindow = 91,
  1034. RightWindow = 92,
  1035. SelectKey = 93,
  1036. Numpad0 = 96,
  1037. Numpad1 = 97,
  1038. Numpad2 = 98,
  1039. Numpad3 = 99,
  1040. Numpad4 = 100,
  1041. Numpad5 = 101,
  1042. Numpad6 = 102,
  1043. Numpad7 = 103,
  1044. Numpad8 = 104,
  1045. Numpad9 = 105,
  1046. Multiply = 106,
  1047. Add = 107,
  1048. Subtract = 109,
  1049. DecimalPoint = 110,
  1050. Divide = 111,
  1051. F1 = 112,
  1052. F2 = 113,
  1053. F3 = 114,
  1054. F4 = 115,
  1055. F5 = 116,
  1056. F6 = 117,
  1057. F7 = 118,
  1058. F8 = 119,
  1059. F9 = 120,
  1060. F10 = 121,
  1061. F11 = 122,
  1062. F12 = 123,
  1063. // f13, = 124
  1064. // f14, = 125
  1065. // f15, = 126
  1066. // f16, = 127
  1067. // f17, = 128
  1068. // f18, = 129
  1069. // f19, = 130
  1070. // f20, = 131
  1071. // f21, = 132
  1072. // f22, = 133
  1073. // f23, = 134
  1074. // f24, = 135
  1075. // f25, = 136
  1076. // f26, = 137
  1077. // f27, = 138
  1078. // f28, = 139
  1079. // f29, = 140
  1080. // f30, = 141
  1081. // f31, = 142
  1082. // f32, = 143
  1083. NumLock = 144,
  1084. ScrollLock = 145,
  1085. // airplane mode, = 151
  1086. // ^, = 160
  1087. // !, = 161
  1088. // ؛ (arabic semicolon), = 162
  1089. // #, = 163
  1090. // $, = 164
  1091. // ù, = 165
  1092. // page backward, = 166
  1093. // page forward, = 167
  1094. // refresh, = 168
  1095. // closing paren (AZERTY), = 169
  1096. // *, = 170
  1097. // ~ + * key, = 171
  1098. // home key, = 172
  1099. // minus (firefox), mute/unmute, = 173
  1100. // decrease volume level, = 174
  1101. // increase volume level, = 175
  1102. // next, = 176
  1103. // previous, = 177
  1104. // stop, = 178
  1105. // play/pause, = 179
  1106. // e-mail, = 180
  1107. // mute/unmute (firefox), = 181
  1108. // decrease volume level (firefox), = 182
  1109. // increase volume level (firefox), = 183
  1110. Semicolon = 186,
  1111. EqualSign = 187,
  1112. Comma = 188,
  1113. Dash = 189,
  1114. Period = 190,
  1115. ForwardSlash = 191,
  1116. GraveAccent = 192,
  1117. // ?, / or °, = 193
  1118. // numpad period (chrome), = 194
  1119. OpenBracket = 219,
  1120. BackSlash = 220,
  1121. CloseBraket = 221,
  1122. SingleQuote = 222,
  1123. // `, = 223
  1124. // left or right ⌘ key (firefox), = 224
  1125. // altgr, = 225
  1126. // < /git >, left back slash, = 226
  1127. // GNOME Compose Key, = 230
  1128. // ç, = 231
  1129. // XF86Forward, = 233
  1130. // XF86Back, = 234
  1131. // non-conversion, = 235
  1132. // alphanumeric, = 240
  1133. // hiragana/katakana, = 242
  1134. // half-width/full-width, = 243
  1135. // kanji, = 244
  1136. // unlock trackpad (Chrome/Edge), = 251
  1137. // toggle touchpad, = 255
  1138. #[cfg_attr(feature = "serialize", serde(other))]
  1139. Unknown,
  1140. }
  1141. impl KeyCode {
  1142. pub fn from_raw_code(i: u8) -> Self {
  1143. use KeyCode::*;
  1144. match i {
  1145. 8 => Backspace,
  1146. 9 => Tab,
  1147. 13 => Enter,
  1148. 16 => Shift,
  1149. 17 => Ctrl,
  1150. 18 => Alt,
  1151. 19 => Pause,
  1152. 20 => CapsLock,
  1153. 27 => Escape,
  1154. 33 => PageUp,
  1155. 34 => PageDown,
  1156. 35 => End,
  1157. 36 => Home,
  1158. 37 => LeftArrow,
  1159. 38 => UpArrow,
  1160. 39 => RightArrow,
  1161. 40 => DownArrow,
  1162. 45 => Insert,
  1163. 46 => Delete,
  1164. 48 => Num0,
  1165. 49 => Num1,
  1166. 50 => Num2,
  1167. 51 => Num3,
  1168. 52 => Num4,
  1169. 53 => Num5,
  1170. 54 => Num6,
  1171. 55 => Num7,
  1172. 56 => Num8,
  1173. 57 => Num9,
  1174. 65 => A,
  1175. 66 => B,
  1176. 67 => C,
  1177. 68 => D,
  1178. 69 => E,
  1179. 70 => F,
  1180. 71 => G,
  1181. 72 => H,
  1182. 73 => I,
  1183. 74 => J,
  1184. 75 => K,
  1185. 76 => L,
  1186. 77 => M,
  1187. 78 => N,
  1188. 79 => O,
  1189. 80 => P,
  1190. 81 => Q,
  1191. 82 => R,
  1192. 83 => S,
  1193. 84 => T,
  1194. 85 => U,
  1195. 86 => V,
  1196. 87 => W,
  1197. 88 => X,
  1198. 89 => Y,
  1199. 90 => Z,
  1200. 91 => LeftWindow,
  1201. 92 => RightWindow,
  1202. 93 => SelectKey,
  1203. 96 => Numpad0,
  1204. 97 => Numpad1,
  1205. 98 => Numpad2,
  1206. 99 => Numpad3,
  1207. 100 => Numpad4,
  1208. 101 => Numpad5,
  1209. 102 => Numpad6,
  1210. 103 => Numpad7,
  1211. 104 => Numpad8,
  1212. 105 => Numpad9,
  1213. 106 => Multiply,
  1214. 107 => Add,
  1215. 109 => Subtract,
  1216. 110 => DecimalPoint,
  1217. 111 => Divide,
  1218. 112 => F1,
  1219. 113 => F2,
  1220. 114 => F3,
  1221. 115 => F4,
  1222. 116 => F5,
  1223. 117 => F6,
  1224. 118 => F7,
  1225. 119 => F8,
  1226. 120 => F9,
  1227. 121 => F10,
  1228. 122 => F11,
  1229. 123 => F12,
  1230. 144 => NumLock,
  1231. 145 => ScrollLock,
  1232. 186 => Semicolon,
  1233. 187 => EqualSign,
  1234. 188 => Comma,
  1235. 189 => Dash,
  1236. 190 => Period,
  1237. 191 => ForwardSlash,
  1238. 192 => GraveAccent,
  1239. 219 => OpenBracket,
  1240. 220 => BackSlash,
  1241. 221 => CloseBraket,
  1242. 222 => SingleQuote,
  1243. _ => Unknown,
  1244. }
  1245. }
  1246. // get the raw code
  1247. pub fn raw_code(&self) -> u32 {
  1248. *self as u32
  1249. }
  1250. }
  1251. pub(crate) fn _event_meta(event: &UserEvent) -> (bool, EventPriority) {
  1252. use EventPriority::*;
  1253. match event.name {
  1254. // clipboard
  1255. "copy" | "cut" | "paste" => (true, Medium),
  1256. // Composition
  1257. "compositionend" | "compositionstart" | "compositionupdate" => (true, Low),
  1258. // Keyboard
  1259. "keydown" | "keypress" | "keyup" => (true, High),
  1260. // Focus
  1261. "focus" | "blur" | "focusout" | "focusin" => (true, Low),
  1262. // Form
  1263. "change" | "input" | "invalid" | "reset" | "submit" => (true, Medium),
  1264. // Mouse
  1265. "click" | "contextmenu" | "doubleclick" | "drag" | "dragend" | "dragenter" | "dragexit"
  1266. | "dragleave" | "dragover" | "dragstart" | "drop" | "mousedown" | "mouseenter"
  1267. | "mouseleave" | "mouseout" | "mouseover" | "mouseup" => (true, High),
  1268. "mousemove" => (false, Medium),
  1269. // Pointer
  1270. "pointerdown" | "pointermove" | "pointerup" | "pointercancel" | "gotpointercapture"
  1271. | "lostpointercapture" | "pointerenter" | "pointerleave" | "pointerover" | "pointerout" => {
  1272. (true, Medium)
  1273. }
  1274. // Selection
  1275. "select" | "touchcancel" | "touchend" => (true, Medium),
  1276. // Touch
  1277. "touchmove" | "touchstart" => (true, Medium),
  1278. // Wheel
  1279. "scroll" | "wheel" => (false, Medium),
  1280. // Media
  1281. "abort" | "canplay" | "canplaythrough" | "durationchange" | "emptied" | "encrypted"
  1282. | "ended" | "error" | "loadeddata" | "loadedmetadata" | "loadstart" | "pause" | "play"
  1283. | "playing" | "progress" | "ratechange" | "seeked" | "seeking" | "stalled" | "suspend"
  1284. | "timeupdate" | "volumechange" | "waiting" => (true, Medium),
  1285. // Animation
  1286. "animationstart" | "animationend" | "animationiteration" => (true, Medium),
  1287. // Transition
  1288. "transitionend" => (true, Medium),
  1289. // Toggle
  1290. "toggle" => (true, Medium),
  1291. _ => (true, Low),
  1292. }
  1293. }
  1294. pub fn event_bubbles(evt: &str) -> bool {
  1295. match evt {
  1296. "copy" => true,
  1297. "cut" => true,
  1298. "paste" => true,
  1299. "compositionend" => true,
  1300. "compositionstart" => true,
  1301. "compositionupdate" => true,
  1302. "keydown" => true,
  1303. "keypress" => true,
  1304. "keyup" => true,
  1305. "focus" => false,
  1306. "focusout" => true,
  1307. "focusin" => true,
  1308. "blur" => false,
  1309. "change" => true,
  1310. "input" => true,
  1311. "invalid" => true,
  1312. "reset" => true,
  1313. "submit" => true,
  1314. "click" => true,
  1315. "contextmenu" => true,
  1316. "doubleclick" => true,
  1317. "dblclick" => true,
  1318. "drag" => true,
  1319. "dragend" => true,
  1320. "dragenter" => false,
  1321. "dragexit" => false,
  1322. "dragleave" => true,
  1323. "dragover" => true,
  1324. "dragstart" => true,
  1325. "drop" => true,
  1326. "mousedown" => true,
  1327. "mouseenter" => false,
  1328. "mouseleave" => false,
  1329. "mousemove" => true,
  1330. "mouseout" => true,
  1331. "scroll" => false,
  1332. "mouseover" => true,
  1333. "mouseup" => true,
  1334. "pointerdown" => true,
  1335. "pointermove" => true,
  1336. "pointerup" => true,
  1337. "pointercancel" => true,
  1338. "gotpointercapture" => true,
  1339. "lostpointercapture" => true,
  1340. "pointerenter" => false,
  1341. "pointerleave" => false,
  1342. "pointerover" => true,
  1343. "pointerout" => true,
  1344. "select" => true,
  1345. "touchcancel" => true,
  1346. "touchend" => true,
  1347. "touchmove" => true,
  1348. "touchstart" => true,
  1349. "wheel" => true,
  1350. "abort" => false,
  1351. "canplay" => true,
  1352. "canplaythrough" => true,
  1353. "durationchange" => true,
  1354. "emptied" => true,
  1355. "encrypted" => true,
  1356. "ended" => true,
  1357. "error" => false,
  1358. "loadeddata" => true,
  1359. "loadedmetadata" => true,
  1360. "loadstart" => false,
  1361. "pause" => true,
  1362. "play" => true,
  1363. "playing" => true,
  1364. "progress" => false,
  1365. "ratechange" => true,
  1366. "seeked" => true,
  1367. "seeking" => true,
  1368. "stalled" => true,
  1369. "suspend" => true,
  1370. "timeupdate" => true,
  1371. "volumechange" => true,
  1372. "waiting" => true,
  1373. "animationstart" => true,
  1374. "animationend" => true,
  1375. "animationiteration" => true,
  1376. "transitionend" => true,
  1377. "toggle" => true,
  1378. _ => panic!("unsupported event type {:?}", evt),
  1379. }
  1380. }