Documentation and examples
Also derive useful traits
This commit is contained in:
1 parent
0dbd3e370e
commit
3c447d7266
2 files changed
+36
-2
No files matched your search
@@ -10,6 +10,8 @@ fn app(cx: Scope) -> Element {
|
|||||||
let page_coordinates = use_state(&cx, || "".to_string());
|
let page_coordinates = use_state(&cx, || "".to_string());
|
||||||
let screen_coordinates = use_state(&cx, || "".to_string());
|
let screen_coordinates = use_state(&cx, || "".to_string());
|
||||||
let element_coordinates = use_state(&cx, || "".to_string());
|
let element_coordinates = use_state(&cx, || "".to_string());
|
||||||
|
let buttons = use_state(&cx, || "".to_string());
|
||||||
|
let modifiers = use_state(&cx, || "".to_string());
|
||||||
|
|
||||||
let container_style = r#"
|
let container_style = r#"
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -30,6 +32,9 @@ fn app(cx: Scope) -> Element {
|
|||||||
element_coordinates.set(format!("{:?}", mouse_data.element_coordinates()));
|
element_coordinates.set(format!("{:?}", mouse_data.element_coordinates()));
|
||||||
|
|
||||||
// Note: client coordinates are also available, but they would be the same as the page coordinates in this example, because there is no scrolling.
|
// Note: client coordinates are also available, but they would be the same as the page coordinates in this example, because there is no scrolling.
|
||||||
|
|
||||||
|
buttons.set(format!("{:?}", mouse_data.held_buttons()));
|
||||||
|
modifiers.set(format!("{:?}", mouse_data.modifiers()));
|
||||||
};
|
};
|
||||||
|
|
||||||
cx.render(rsx! (
|
cx.render(rsx! (
|
||||||
@@ -39,10 +44,13 @@ fn app(cx: Scope) -> Element {
|
|||||||
div {
|
div {
|
||||||
style: "{rect_style}",
|
style: "{rect_style}",
|
||||||
onmousemove: update_mouse_position,
|
onmousemove: update_mouse_position,
|
||||||
|
prevent_default: "mousedown",
|
||||||
}
|
}
|
||||||
div {"Page coordinates: {page_coordinates}"},
|
div {"Page coordinates: {page_coordinates}"},
|
||||||
div {"Screen coordinates: {screen_coordinates}"},
|
div {"Screen coordinates: {screen_coordinates}"},
|
||||||
div {"Element coordinates: {element_coordinates}"},
|
div {"Element coordinates: {element_coordinates}"},
|
||||||
|
div {"{buttons}"},
|
||||||
|
div {"{modifiers}"},
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@@ -561,8 +561,8 @@ pub mod on {
|
|||||||
|
|
||||||
impl MouseData {
|
impl MouseData {
|
||||||
/// The event's coordinates relative to the application's viewport (as opposed to the coordinate within the page).
|
/// The event's coordinates relative to the application's viewport (as opposed to the coordinate within the page).
|
||||||
//
|
///
|
||||||
// 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.
|
/// 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.
|
||||||
pub fn client_coordinates(&self) -> ClientPoint {
|
pub fn client_coordinates(&self) -> ClientPoint {
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
ClientPoint::new(self.client_x.into(), self.client_y.into())
|
ClientPoint::new(self.client_x.into(), self.client_y.into())
|
||||||
@@ -634,6 +634,9 @@ pub mod on {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A set of modifier keys
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct ModifierSet {
|
pub struct ModifierSet {
|
||||||
has_alt: bool,
|
has_alt: bool,
|
||||||
has_ctrl: bool,
|
has_ctrl: bool,
|
||||||
@@ -642,24 +645,33 @@ pub mod on {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ModifierSet {
|
impl ModifierSet {
|
||||||
|
/// Whether the set includes the alt key
|
||||||
pub fn has_alt(&self) -> bool {
|
pub fn has_alt(&self) -> bool {
|
||||||
self.has_alt
|
self.has_alt
|
||||||
}
|
}
|
||||||
|
/// Whether the set includes the control key
|
||||||
pub fn has_ctrl(&self) -> bool {
|
pub fn has_ctrl(&self) -> bool {
|
||||||
self.has_ctrl
|
self.has_ctrl
|
||||||
}
|
}
|
||||||
|
/// Whether the set includes the meta (windows/command) key
|
||||||
|
|
||||||
pub fn has_meta(&self) -> bool {
|
pub fn has_meta(&self) -> bool {
|
||||||
self.has_meta
|
self.has_meta
|
||||||
}
|
}
|
||||||
|
/// Whether the set includes the shift key
|
||||||
pub fn has_shift(&self) -> bool {
|
pub fn has_shift(&self) -> bool {
|
||||||
self.has_shift
|
self.has_shift
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// True if the set contains no keys
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
!(self.has_alt || self.has_ctrl || self.has_meta || self.has_shift)
|
!(self.has_alt || self.has_ctrl || self.has_meta || self.has_shift)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A set of mouse buttons
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct MouseButtonSet {
|
pub struct MouseButtonSet {
|
||||||
has_primary: bool,
|
has_primary: bool,
|
||||||
has_secondary: bool,
|
has_secondary: bool,
|
||||||
@@ -669,29 +681,43 @@ pub mod on {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MouseButtonSet {
|
impl MouseButtonSet {
|
||||||
|
/// Whether the set includes the primary button (typically the left button)
|
||||||
pub fn has_primary(&self) -> bool {
|
pub fn has_primary(&self) -> bool {
|
||||||
self.has_primary
|
self.has_primary
|
||||||
}
|
}
|
||||||
|
/// Whether the set includes the secondary button (typically the right button)
|
||||||
pub fn has_secondary(&self) -> bool {
|
pub fn has_secondary(&self) -> bool {
|
||||||
self.has_secondary
|
self.has_secondary
|
||||||
}
|
}
|
||||||
|
/// Whether the set includes the auxiliary button (typically the middle button)
|
||||||
pub fn has_auxiliary(&self) -> bool {
|
pub fn has_auxiliary(&self) -> bool {
|
||||||
self.has_auxiliary
|
self.has_auxiliary
|
||||||
}
|
}
|
||||||
|
/// Whether the set includes the fourth button (typically the "Browser Back" button)
|
||||||
pub fn has_fourth(&self) -> bool {
|
pub fn has_fourth(&self) -> bool {
|
||||||
self.has_fourth
|
self.has_fourth
|
||||||
}
|
}
|
||||||
|
/// Whether the set includes the fifth button (typically the "Browser Forward" button)
|
||||||
pub fn has_fifth(&self) -> bool {
|
pub fn has_fifth(&self) -> bool {
|
||||||
self.has_fifth
|
self.has_fifth
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A mouse button type (such as Primary/Secondary)
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub enum MouseButton {
|
pub enum MouseButton {
|
||||||
|
/// Primary button (typically the left button)
|
||||||
Primary,
|
Primary,
|
||||||
|
/// Secondary button (typically the right button)
|
||||||
Secondary,
|
Secondary,
|
||||||
|
/// Auxiliary button (typically the middle button)
|
||||||
Auxiliary,
|
Auxiliary,
|
||||||
|
/// Fourth button (typically the "Browser Back" button)
|
||||||
Fourth,
|
Fourth,
|
||||||
|
/// Fifth button (typically the "Browser Forward" button)
|
||||||
Fifth,
|
Fifth,
|
||||||
|
/// A button with an unknown code
|
||||||
Other(i16),
|
Other(i16),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user