zng_wgt_input/
touch.rs

1//! Touch events, [`on_touch_move`](fn@on_touch_move), [`on_touch_tap`](fn@on_touch_tap),
2//! [`on_touch_start`](fn@on_touch_start) and more.
3//!
4//! There events are low level and directly tied to touch inputs.
5//! Before using them review the [`gesture`](super::gesture) events, in particular the
6//! [`on_click`](fn@super::gesture::on_click) event.
7
8use zng_ext_input::touch::{
9    TOUCH_INPUT_EVENT, TOUCH_LONG_PRESS_EVENT, TOUCH_MOVE_EVENT, TOUCH_TAP_EVENT, TOUCH_TRANSFORM_EVENT, TOUCHED_EVENT, TouchInputArgs,
10    TouchLongPressArgs, TouchMoveArgs, TouchTapArgs, TouchTransformArgs, TouchedArgs,
11};
12use zng_wgt::prelude::*;
13
14event_property! {
15    /// Touch contact moved over the widget and cursor capture allows it.
16    #[property(EVENT)]
17    pub fn on_touch_move<on_pre_touch_move>(child: impl IntoUiNode, handler: Handler<TouchMoveArgs>) -> UiNode {
18        const PRE: bool;
19        EventNodeBuilder::new(TOUCH_MOVE_EVENT)
20            .filter(|| {
21                let wgt = (WINDOW.id(), WIDGET.id());
22                move |args| args.capture_allows(wgt)
23            })
24            .build::<PRE>(child, handler)
25    }
26
27    /// Touch contact started or ended over the widget, it is enabled and cursor capture allows it.
28    #[property(EVENT)]
29    pub fn on_touch_input<on_pre_touch_input>(child: impl IntoUiNode, handler: Handler<TouchInputArgs>) -> UiNode {
30        const PRE: bool;
31        EventNodeBuilder::new(TOUCH_INPUT_EVENT)
32            .filter(|| {
33                let wgt = (WINDOW.id(), WIDGET.id());
34                move |args| args.target.contains_enabled(wgt.1) && args.capture_allows(wgt)
35            })
36            .build::<PRE>(child, handler)
37    }
38
39    /// Touch contact started or ended over the widget, it is disabled and cursor capture allows it.
40    #[property(EVENT)]
41    pub fn on_disabled_touch_input<on_pre_disabled_touch_input>(child: impl IntoUiNode, handler: Handler<TouchInputArgs>) -> UiNode {
42        const PRE: bool;
43        EventNodeBuilder::new(TOUCH_INPUT_EVENT)
44            .filter(|| {
45                let wgt = (WINDOW.id(), WIDGET.id());
46                move |args| args.target.contains_disabled(wgt.1) && args.capture_allows(wgt)
47            })
48            .build::<PRE>(child, handler)
49    }
50
51    /// Touch contact started over the widget, it is enabled and cursor capture allows it.
52    #[property(EVENT)]
53    pub fn on_touch_start<on_pre_touch_start>(child: impl IntoUiNode, handler: Handler<TouchInputArgs>) -> UiNode {
54        const PRE: bool;
55        EventNodeBuilder::new(TOUCH_INPUT_EVENT)
56            .filter(|| {
57                let wgt = (WINDOW.id(), WIDGET.id());
58                move |args| args.is_touch_start() && args.target.contains_enabled(wgt.1) && args.capture_allows(wgt)
59            })
60            .build::<PRE>(child, handler)
61    }
62
63    /// Touch contact ended over the widget, it is enabled and cursor capture allows it.
64    #[property(EVENT)]
65    pub fn on_touch_end<on_pre_touch_end>(child: impl IntoUiNode, handler: Handler<TouchInputArgs>) -> UiNode {
66        const PRE: bool;
67        EventNodeBuilder::new(TOUCH_INPUT_EVENT)
68            .filter(|| {
69                let wgt = (WINDOW.id(), WIDGET.id());
70                move |args| args.is_touch_end() && args.target.contains_enabled(wgt.1) && args.capture_allows(wgt)
71            })
72            .build::<PRE>(child, handler)
73    }
74
75    /// Touch contact canceled over the widget, it is enabled and cursor capture allows it.
76    #[property(EVENT)]
77    pub fn on_touch_cancel<on_pre_touch_cancel>(child: impl IntoUiNode, handler: Handler<TouchInputArgs>) -> UiNode {
78        const PRE: bool;
79        EventNodeBuilder::new(TOUCH_INPUT_EVENT)
80            .filter(|| {
81                let wgt = (WINDOW.id(), WIDGET.id());
82                move |args| args.is_touch_cancel() && args.target.contains_enabled(wgt.1) && args.capture_allows(wgt)
83            })
84            .build::<PRE>(child, handler)
85    }
86
87    /// Touch tap on the widget and it is enabled.
88    #[property(EVENT)]
89    pub fn on_touch_tap<on_pre_touch_tap>(child: impl IntoUiNode, handler: Handler<TouchTapArgs>) -> UiNode {
90        const PRE: bool;
91        EventNodeBuilder::new(TOUCH_TAP_EVENT)
92            .filter(|| {
93                let id = WIDGET.id();
94                move |args| args.target.contains_enabled(id)
95            })
96            .build::<PRE>(child, handler)
97    }
98
99    /// Touch tap on the widget and it is disabled.
100    #[property(EVENT)]
101    pub fn on_disabled_touch_tap<on_pre_disabled_touch_tap>(child: impl IntoUiNode, handler: Handler<TouchTapArgs>) -> UiNode {
102        const PRE: bool;
103        EventNodeBuilder::new(TOUCH_TAP_EVENT)
104            .filter(|| {
105                let id = WIDGET.id();
106                move |args| args.target.contains_disabled(id)
107            })
108            .build::<PRE>(child, handler)
109    }
110
111    /// Touch contact is now over the widget or a descendant and it is enabled.
112    #[property(EVENT)]
113    pub fn on_touch_enter<on_pre_touch_enter>(child: impl IntoUiNode, handler: Handler<TouchedArgs>) -> UiNode {
114        const PRE: bool;
115        EventNodeBuilder::new(TOUCHED_EVENT)
116            .filter(|| {
117                let wgt = (WINDOW.id(), WIDGET.id());
118                move |args| args.is_touch_enter_enabled(wgt)
119            })
120            .build::<PRE>(child, handler)
121    }
122
123    /// Touch contact is no longer over the widget or any descendant and it is enabled.
124    #[property(EVENT)]
125    pub fn on_touch_leave<on_pre_touch_leave>(child: impl IntoUiNode, handler: Handler<TouchedArgs>) -> UiNode {
126        const PRE: bool;
127        EventNodeBuilder::new(TOUCHED_EVENT)
128            .filter(|| {
129                let wgt = (WINDOW.id(), WIDGET.id());
130                move |args| args.is_touch_leave_enabled(wgt)
131            })
132            .build::<PRE>(child, handler)
133    }
134
135    /// Touch contact entered or left the widget and descendants area and it is enabled.
136    ///
137    /// You can use the [`is_touch_enter`] and [`is_touch_leave`] methods to determinate the state change.
138    ///
139    /// [`is_touch_enter`]: TouchedArgs::is_touch_enter
140    /// [`is_touch_leave`]: TouchedArgs::is_touch_leave
141    #[property(EVENT)]
142    pub fn on_touched<on_pre_touched>(child: impl IntoUiNode, handler: Handler<TouchedArgs>) -> UiNode {
143        const PRE: bool;
144        EventNodeBuilder::new(TOUCHED_EVENT)
145            .filter(|| {
146                let id = WIDGET.id();
147                move |args| args.is_enabled(id)
148            })
149            .build::<PRE>(child, handler)
150    }
151
152    /// Touch gesture to translate, scale or rotate happened over this widget.
153    #[property(EVENT)]
154    pub fn on_touch_transform<on_pre_touch_transform>(child: impl IntoUiNode, handler: Handler<TouchTransformArgs>) -> UiNode {
155        const PRE: bool;
156        EventNodeBuilder::new(TOUCH_TRANSFORM_EVENT)
157            .filter(|| {
158                let id = WIDGET.id();
159                move |args| args.target.contains_enabled(id)
160            })
161            .build::<PRE>(child, handler)
162    }
163
164    /// Single touch contact was made and held in place for a duration of time (default 500ms) on
165    /// the widget and the widget is enabled.
166    #[property(EVENT)]
167    pub fn on_touch_long_press<on_pre_touch_long_press>(child: impl IntoUiNode, handler: Handler<TouchLongPressArgs>) -> UiNode {
168        const PRE: bool;
169        EventNodeBuilder::new(TOUCH_LONG_PRESS_EVENT)
170            .filter(|| {
171                let id = WIDGET.id();
172                move |args| args.target.contains_enabled(id)
173            })
174            .build::<PRE>(child, handler)
175    }
176
177    /// Single touch contact was made and held in place for a duration of time (default 500ms) on
178    /// the widget and the widget is disabled.
179    #[property(EVENT)]
180    pub fn on_disabled_touch_long_press<on_pre_disabled_touch_long_press>(
181        child: impl IntoUiNode,
182        handler: Handler<TouchLongPressArgs>,
183    ) -> UiNode {
184        const PRE: bool;
185        EventNodeBuilder::new(TOUCH_LONG_PRESS_EVENT)
186            .filter(|| {
187                let id = WIDGET.id();
188                move |args| args.target.contains_disabled(id)
189            })
190            .build::<PRE>(child, handler)
191    }
192}