lgui_core/core/view/declarative/
events.rs1use super::*;
2
3pub trait IntoClickHandler<Arguments> {
4 fn into_click_handler(self) -> UiEventHandler;
5}
6
7pub enum NoClickArguments {}
8pub enum ClickEventArgument {}
9
10impl<F> IntoClickHandler<NoClickArguments> for F
11where
12 F: Fn() + Send + Sync + 'static,
13{
14 fn into_click_handler(self) -> UiEventHandler {
15 Arc::new(move |_| self())
16 }
17}
18
19impl<F> IntoClickHandler<ClickEventArgument> for F
20where
21 F: Fn(&mut UiEventContext) + Send + Sync + 'static,
22{
23 fn into_click_handler(self) -> UiEventHandler {
24 Arc::new(self)
25 }
26}
27
28impl Element {
29 pub fn on_click_handler(mut self, handler: UiEventHandler) -> Self {
30 self.click_handler = Some(handler);
31 self
32 }
33
34 pub fn on_click_capture_handler(mut self, handler: UiEventHandler) -> Self {
35 self.click_capture_handler = Some(handler);
36 self
37 }
38
39 pub fn event_policy(mut self, policy: EventPolicy) -> Self {
40 self.event_policy = Some(policy);
41 self
42 }
43
44 pub fn on_click<F, Arguments>(self, handler: F) -> Self
45 where
46 F: IntoClickHandler<Arguments>,
47 {
48 self.on_click_handler(handler.into_click_handler())
49 }
50
51 pub fn on_click_event<F>(self, handler: F) -> Self
52 where
53 F: Fn(&mut UiEventContext) + Send + Sync + 'static,
54 {
55 self.on_click_handler(Arc::new(handler))
56 }
57
58 pub fn on_click_async<F, Fut>(self, handler: F) -> Self
59 where
60 F: Fn(UiAsyncContext) -> Fut + Send + Sync + 'static,
61 Fut: Future<Output = ()> + Send + 'static,
62 {
63 self.on_click_handler(async_handler(handler))
64 }
65
66 pub fn on_click_capture<F>(self, handler: F) -> Self
67 where
68 F: Fn(&mut UiEventContext) + Send + Sync + 'static,
69 {
70 self.on_click_capture_handler(Arc::new(handler))
71 }
72
73 pub fn on_event_handler(
74 mut self,
75 kind: UiEventKind,
76 capture: bool,
77 handler: UiInputEventHandler,
78 ) -> Self {
79 self.input_event_handlers.push(UiInputEventBinding {
80 kind,
81 capture,
82 handler,
83 });
84 self
85 }
86
87 pub fn on_event<F>(self, kind: UiEventKind, handler: F) -> Self
88 where
89 F: Fn(&mut UiEventContext, &UiEventPayload) + Send + Sync + 'static,
90 {
91 self.on_event_handler(kind, false, Arc::new(handler))
92 }
93
94 pub fn on_event_capture<F>(self, kind: UiEventKind, handler: F) -> Self
95 where
96 F: Fn(&mut UiEventContext, &UiEventPayload) + Send + Sync + 'static,
97 {
98 self.on_event_handler(kind, true, Arc::new(handler))
99 }
100
101 pub fn on_pointer_down<F>(self, handler: F) -> Self
102 where
103 F: Fn(&mut UiEventContext, PointerData) + Send + Sync + 'static,
104 {
105 self.on_event(UiEventKind::PointerDown, move |cx, payload| {
106 if let UiEventPayload::PointerDown { pointer } = payload {
107 handler(cx, *pointer);
108 }
109 })
110 }
111
112 pub fn on_pointer_move<F>(self, handler: F) -> Self
113 where
114 F: Fn(&mut UiEventContext, PointerData) + Send + Sync + 'static,
115 {
116 self.on_event(UiEventKind::PointerMove, move |cx, payload| {
117 if let UiEventPayload::PointerMove { pointer } = payload {
118 handler(cx, *pointer);
119 }
120 })
121 }
122
123 pub fn on_pointer_up<F>(self, handler: F) -> Self
124 where
125 F: Fn(&mut UiEventContext, PointerData) + Send + Sync + 'static,
126 {
127 self.on_event(UiEventKind::PointerUp, move |cx, payload| {
128 if let UiEventPayload::PointerUp { pointer } = payload {
129 handler(cx, *pointer);
130 }
131 })
132 }
133
134 pub fn on_wheel<F>(self, handler: F) -> Self
135 where
136 F: Fn(&mut UiEventContext, WheelDelta) + Send + Sync + 'static,
137 {
138 self.on_event(UiEventKind::Wheel, move |cx, payload| {
139 if let UiEventPayload::Wheel { delta } = payload {
140 handler(cx, *delta);
141 }
142 })
143 }
144
145 pub fn on_key_down<F>(self, handler: F) -> Self
146 where
147 F: Fn(&mut UiEventContext, &KeyboardEvent) + Send + Sync + 'static,
148 {
149 self.on_event(UiEventKind::KeyDown, move |cx, payload| {
150 if let UiEventPayload::Keyboard { event } = payload {
151 handler(cx, event);
152 }
153 })
154 }
155
156 pub fn on_key_up<F>(self, handler: F) -> Self
157 where
158 F: Fn(&mut UiEventContext, &KeyboardEvent) + Send + Sync + 'static,
159 {
160 self.on_event(UiEventKind::KeyUp, move |cx, payload| {
161 if let UiEventPayload::Keyboard { event } = payload {
162 handler(cx, event);
163 }
164 })
165 }
166
167 pub fn on_input<F>(self, handler: F) -> Self
168 where
169 F: Fn(&mut UiEventContext, &str) + Send + Sync + 'static,
170 {
171 self.on_event(UiEventKind::Input, move |cx, payload| {
172 if let UiEventPayload::Input { text } = payload {
173 handler(cx, text);
174 }
175 })
176 }
177
178 pub fn on_composition_start<F>(self, handler: F) -> Self
179 where
180 F: Fn(&mut UiEventContext) + Send + Sync + 'static,
181 {
182 self.on_event(UiEventKind::CompositionStart, move |cx, _| handler(cx))
183 }
184
185 pub fn on_composition_update<F>(self, handler: F) -> Self
186 where
187 F: Fn(&mut UiEventContext, &str, Option<std::ops::Range<usize>>) + Send + Sync + 'static,
188 {
189 self.on_event(UiEventKind::CompositionUpdate, move |cx, payload| {
190 if let UiEventPayload::CompositionUpdate { text, cursor } = payload {
191 handler(cx, text, cursor.clone());
192 }
193 })
194 }
195
196 pub fn on_composition_end<F>(self, handler: F) -> Self
197 where
198 F: Fn(&mut UiEventContext) + Send + Sync + 'static,
199 {
200 self.on_event(UiEventKind::CompositionEnd, move |cx, _| handler(cx))
201 }
202
203 pub fn on_focus<F>(self, handler: F) -> Self
204 where
205 F: Fn(&mut UiEventContext) + Send + Sync + 'static,
206 {
207 self.on_event(UiEventKind::Focus, move |cx, _| handler(cx))
208 }
209
210 pub fn on_blur<F>(self, handler: F) -> Self
211 where
212 F: Fn(&mut UiEventContext) + Send + Sync + 'static,
213 {
214 self.on_event(UiEventKind::Blur, move |cx, _| handler(cx))
215 }
216
217 pub fn on_change<F>(self, handler: F) -> Self
218 where
219 F: Fn(&mut UiEventContext, Option<&str>) + Send + Sync + 'static,
220 {
221 self.on_event(UiEventKind::Change, move |cx, payload| {
222 if let UiEventPayload::Change { value } = payload {
223 handler(cx, value.as_deref());
224 }
225 })
226 }
227}