Skip to main content

graphics/
event.rs

1use crate::{Key, lvgl};
2use core::{ffi::c_void, ptr::NonNull};
3
4/// An event a window queued for one of its objects.
5///
6/// The target may have been deleted, by safe code too, between the moment the
7/// window queued the event and the moment it is read. The target is therefore
8/// private: [`Event::is_target`] compares addresses, and
9/// [`Event::find_target_in`] gives a pointer only once it has found the target
10/// in a live object tree.
11///
12/// ```compile_fail,E0616
13/// fn target(event: &graphics::Event) -> *mut graphics::lvgl::lv_obj_t {
14///     event.target
15/// }
16/// ```
17#[derive(Debug, Clone)]
18pub struct Event {
19    pub code: EventKind,
20    pub(super) target: *mut lvgl::lv_obj_t,
21    pub key: Option<Key>,
22    /// Pointer position relative to the target content, plus content width/height,
23    /// captured at event time rather than when a consumer drains the queue.
24    pub pointer: Option<[i32; 4]>,
25}
26
27impl Event {
28    /// Compare the queued address with a candidate widget. The event carries
29    /// no lifetime guarantee, and LVGL may reuse an address after deletion.
30    /// A window clears the targets of events aimed at its descendants as soon
31    /// as LVGL reports their deletion, so a queued event never matches a
32    /// widget created later at the same address; the event itself is kept,
33    /// with no target, and a cleared target matches nothing, not even a null
34    /// candidate.
35    pub fn is_target(&self, candidate: *const lvgl::lv_obj_t) -> bool {
36        !self.target.is_null() && core::ptr::eq(self.target, candidate)
37    }
38
39    /// The target, if it is `root` or one of its descendants.
40    ///
41    /// Walks `root`'s live subtree and compares addresses, so a deleted target
42    /// is never dereferenced, and the object returned is live while the
43    /// graphics lock stays held. Unlike the window's own clearing (see
44    /// [`Event::is_target`]), this holds whatever ran between the deletion
45    /// and the read.
46    ///
47    /// # Safety
48    ///
49    /// `root` must be a live LVGL object, and the caller must hold the
50    /// graphics lock.
51    pub unsafe fn find_target_in(
52        &self,
53        root: *mut lvgl::lv_obj_t,
54    ) -> Option<NonNull<lvgl::lv_obj_t>> {
55        unsafe extern "C" fn visit(
56            object: *mut lvgl::lv_obj_t,
57            search: *mut c_void,
58        ) -> lvgl::lv_obj_tree_walk_res_t {
59            // SAFETY: `search` is the `Search` the walk below was given.
60            let search = unsafe { &mut *search.cast::<Search>() };
61            if object == search.target {
62                search.found = true;
63                lvgl::lv_obj_tree_walk_res_t_LV_OBJ_TREE_WALK_END
64            } else {
65                lvgl::lv_obj_tree_walk_res_t_LV_OBJ_TREE_WALK_NEXT
66            }
67        }
68
69        struct Search {
70            target: *mut lvgl::lv_obj_t,
71            found: bool,
72        }
73
74        let target = NonNull::new(self.target)?;
75        if root.is_null() {
76            return None;
77        }
78        let mut search = Search {
79            target: target.as_ptr(),
80            found: false,
81        };
82        // SAFETY: the caller guarantees that `root` is live and that it holds
83        // the graphics lock; `search` outlives the walk.
84        unsafe {
85            lvgl::lv_obj_tree_walk(root, Some(visit), (&raw mut search).cast());
86        }
87        search.found.then_some(target)
88    }
89
90    pub fn new(code: EventKind, target: *mut lvgl::lv_obj_t, key: Option<Key>) -> Self {
91        Self {
92            code,
93            target,
94            key,
95            pointer: None,
96        }
97    }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq)]
101#[repr(u16)]
102pub enum EventKind {
103    All = lvgl::lv_event_code_t_LV_EVENT_ALL as u16,
104    Pressed = lvgl::lv_event_code_t_LV_EVENT_PRESSED as u16,
105    Pressing = lvgl::lv_event_code_t_LV_EVENT_PRESSING as u16,
106    PressLost = lvgl::lv_event_code_t_LV_EVENT_PRESS_LOST as u16,
107    ShortClicked = lvgl::lv_event_code_t_LV_EVENT_SHORT_CLICKED as u16,
108    SingleClicked = lvgl::lv_event_code_t_LV_EVENT_SINGLE_CLICKED as u16,
109    DoubleClicked = lvgl::lv_event_code_t_LV_EVENT_DOUBLE_CLICKED as u16,
110    TripleClicked = lvgl::lv_event_code_t_LV_EVENT_TRIPLE_CLICKED as u16,
111    LongPressed = lvgl::lv_event_code_t_LV_EVENT_LONG_PRESSED as u16,
112    LongPressedRepeat = lvgl::lv_event_code_t_LV_EVENT_LONG_PRESSED_REPEAT as u16,
113    Clicked = lvgl::lv_event_code_t_LV_EVENT_CLICKED as u16,
114    Released = lvgl::lv_event_code_t_LV_EVENT_RELEASED as u16,
115    ScrollBegin = lvgl::lv_event_code_t_LV_EVENT_SCROLL_BEGIN as u16,
116    ScrollThrowBegin = lvgl::lv_event_code_t_LV_EVENT_SCROLL_THROW_BEGIN as u16,
117    ScrollEnd = lvgl::lv_event_code_t_LV_EVENT_SCROLL_END as u16,
118    Scroll = lvgl::lv_event_code_t_LV_EVENT_SCROLL as u16,
119    Gesture = lvgl::lv_event_code_t_LV_EVENT_GESTURE as u16,
120    Key = lvgl::lv_event_code_t_LV_EVENT_KEY as u16,
121    Rotary = lvgl::lv_event_code_t_LV_EVENT_ROTARY as u16,
122    Focused = lvgl::lv_event_code_t_LV_EVENT_FOCUSED as u16,
123    Defocused = lvgl::lv_event_code_t_LV_EVENT_DEFOCUSED as u16,
124    Leave = lvgl::lv_event_code_t_LV_EVENT_LEAVE as u16,
125    HitTest = lvgl::lv_event_code_t_LV_EVENT_HIT_TEST as u16,
126    InputDeviceReset = lvgl::lv_event_code_t_LV_EVENT_INDEV_RESET as u16,
127    HoverOver = lvgl::lv_event_code_t_LV_EVENT_HOVER_OVER as u16,
128    HoverLeave = lvgl::lv_event_code_t_LV_EVENT_HOVER_LEAVE as u16,
129    CoverCheck = lvgl::lv_event_code_t_LV_EVENT_COVER_CHECK as u16,
130    RefreshExtDrawSize = lvgl::lv_event_code_t_LV_EVENT_REFR_EXT_DRAW_SIZE as u16,
131    DrawMainBegin = lvgl::lv_event_code_t_LV_EVENT_DRAW_MAIN_BEGIN as u16,
132    DrawMain = lvgl::lv_event_code_t_LV_EVENT_DRAW_MAIN as u16,
133    DrawMainEnd = lvgl::lv_event_code_t_LV_EVENT_DRAW_MAIN_END as u16,
134    DrawPostBegin = lvgl::lv_event_code_t_LV_EVENT_DRAW_POST_BEGIN as u16,
135    DrawPost = lvgl::lv_event_code_t_LV_EVENT_DRAW_POST as u16,
136    DrawPostEnd = lvgl::lv_event_code_t_LV_EVENT_DRAW_POST_END as u16,
137    DrawTaskAdded = lvgl::lv_event_code_t_LV_EVENT_DRAW_TASK_ADDED as u16,
138    ValueChanged = lvgl::lv_event_code_t_LV_EVENT_VALUE_CHANGED as u16,
139    Insert = lvgl::lv_event_code_t_LV_EVENT_INSERT as u16,
140    Refresh = lvgl::lv_event_code_t_LV_EVENT_REFRESH as u16,
141    Ready = lvgl::lv_event_code_t_LV_EVENT_READY as u16,
142    Cancel = lvgl::lv_event_code_t_LV_EVENT_CANCEL as u16,
143    Create = lvgl::lv_event_code_t_LV_EVENT_CREATE as u16,
144    Delete = lvgl::lv_event_code_t_LV_EVENT_DELETE as u16,
145    ChildChanged = lvgl::lv_event_code_t_LV_EVENT_CHILD_CHANGED as u16,
146    ChildCreated = lvgl::lv_event_code_t_LV_EVENT_CHILD_CREATED as u16,
147    ChildDeleted = lvgl::lv_event_code_t_LV_EVENT_CHILD_DELETED as u16,
148    ScreenUnloadStart = lvgl::lv_event_code_t_LV_EVENT_SCREEN_UNLOAD_START as u16,
149    ScreenLoadStart = lvgl::lv_event_code_t_LV_EVENT_SCREEN_LOAD_START as u16,
150    ScreenLoaded = lvgl::lv_event_code_t_LV_EVENT_SCREEN_LOADED as u16,
151    ScreenUnloaded = lvgl::lv_event_code_t_LV_EVENT_SCREEN_UNLOADED as u16,
152    SizeChanged = lvgl::lv_event_code_t_LV_EVENT_SIZE_CHANGED as u16,
153    StyleChanged = lvgl::lv_event_code_t_LV_EVENT_STYLE_CHANGED as u16,
154    LayoutChanged = lvgl::lv_event_code_t_LV_EVENT_LAYOUT_CHANGED as u16,
155    GetSelfSize = lvgl::lv_event_code_t_LV_EVENT_GET_SELF_SIZE as u16,
156    InvalidateArea = lvgl::lv_event_code_t_LV_EVENT_INVALIDATE_AREA as u16,
157    ResolutionChanged = lvgl::lv_event_code_t_LV_EVENT_RESOLUTION_CHANGED as u16,
158    ColorFormatChanged = lvgl::lv_event_code_t_LV_EVENT_COLOR_FORMAT_CHANGED as u16,
159    RefreshRequest = lvgl::lv_event_code_t_LV_EVENT_REFR_REQUEST as u16,
160    RefreshStart = lvgl::lv_event_code_t_LV_EVENT_REFR_START as u16,
161    RefreshReady = lvgl::lv_event_code_t_LV_EVENT_REFR_READY as u16,
162    RenderStart = lvgl::lv_event_code_t_LV_EVENT_RENDER_START as u16,
163    RenderReady = lvgl::lv_event_code_t_LV_EVENT_RENDER_READY as u16,
164    FlushStart = lvgl::lv_event_code_t_LV_EVENT_FLUSH_START as u16,
165    FlushFinish = lvgl::lv_event_code_t_LV_EVENT_FLUSH_FINISH as u16,
166    FlushWaitStart = lvgl::lv_event_code_t_LV_EVENT_FLUSH_WAIT_START as u16,
167    FlushWaitFinish = lvgl::lv_event_code_t_LV_EVENT_FLUSH_WAIT_FINISH as u16,
168    VerticalSynchronization = lvgl::lv_event_code_t_LV_EVENT_VSYNC as u16,
169    Last = lvgl::lv_event_code_t_LV_EVENT_LAST as u16,
170    Custom1 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 1,
171    Custom2 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 2,
172    CloseRequested = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 3,
173    Custom4 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 4,
174    Custom5 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 5,
175    Custom6 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 6,
176    Custom7 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 7,
177    Custom8 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 8,
178    Custom9 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 9,
179    Custom10 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 10,
180    Custom11 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 11,
181    Custom12 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 12,
182    Custom13 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 13,
183    Custom14 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 14,
184    Custom15 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 15,
185    Custom16 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 16,
186    Custom17 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 17,
187    Custom18 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 18,
188    Custom19 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 19,
189    Custom20 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 20,
190    Custom21 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 21,
191    Custom22 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 22,
192    Custom23 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 23,
193    Custom24 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 24,
194    Custom25 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 25,
195    Custom26 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 26,
196    Custom27 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 27,
197    Custom28 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 28,
198    Custom29 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 29,
199    Custom30 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 30,
200    Custom31 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 31,
201    Custom32 = lvgl::lv_event_code_t_LV_EVENT_LAST as u16 + 32,
202    Preprocess = 0x8000,
203}
204
205impl EventKind {
206    pub const fn into_lvgl_code(self) -> lvgl::lv_event_code_t {
207        self as lvgl::lv_event_code_t
208    }
209
210    pub const fn from_lvgl_code(code: lvgl::lv_event_code_t) -> Self {
211        match code {
212            lvgl::lv_event_code_t_LV_EVENT_ALL => Self::All,
213            lvgl::lv_event_code_t_LV_EVENT_PRESSED => Self::Pressed,
214            lvgl::lv_event_code_t_LV_EVENT_PRESSING => Self::Pressing,
215            lvgl::lv_event_code_t_LV_EVENT_PRESS_LOST => Self::PressLost,
216            lvgl::lv_event_code_t_LV_EVENT_SHORT_CLICKED => Self::ShortClicked,
217            lvgl::lv_event_code_t_LV_EVENT_SINGLE_CLICKED => Self::SingleClicked,
218            lvgl::lv_event_code_t_LV_EVENT_DOUBLE_CLICKED => Self::DoubleClicked,
219            lvgl::lv_event_code_t_LV_EVENT_TRIPLE_CLICKED => Self::TripleClicked,
220            lvgl::lv_event_code_t_LV_EVENT_LONG_PRESSED => Self::LongPressed,
221            lvgl::lv_event_code_t_LV_EVENT_LONG_PRESSED_REPEAT => Self::LongPressedRepeat,
222            lvgl::lv_event_code_t_LV_EVENT_CLICKED => Self::Clicked,
223            lvgl::lv_event_code_t_LV_EVENT_RELEASED => Self::Released,
224            lvgl::lv_event_code_t_LV_EVENT_SCROLL_BEGIN => Self::ScrollBegin,
225            lvgl::lv_event_code_t_LV_EVENT_SCROLL_THROW_BEGIN => Self::ScrollThrowBegin,
226            lvgl::lv_event_code_t_LV_EVENT_SCROLL_END => Self::ScrollEnd,
227            lvgl::lv_event_code_t_LV_EVENT_SCROLL => Self::Scroll,
228            lvgl::lv_event_code_t_LV_EVENT_GESTURE => Self::Gesture,
229            lvgl::lv_event_code_t_LV_EVENT_KEY => Self::Key,
230            lvgl::lv_event_code_t_LV_EVENT_ROTARY => Self::Rotary,
231            lvgl::lv_event_code_t_LV_EVENT_FOCUSED => Self::Focused,
232            lvgl::lv_event_code_t_LV_EVENT_DEFOCUSED => Self::Defocused,
233            lvgl::lv_event_code_t_LV_EVENT_LEAVE => Self::Leave,
234            lvgl::lv_event_code_t_LV_EVENT_HIT_TEST => Self::HitTest,
235            lvgl::lv_event_code_t_LV_EVENT_INDEV_RESET => Self::InputDeviceReset,
236            lvgl::lv_event_code_t_LV_EVENT_HOVER_OVER => Self::HoverOver,
237            lvgl::lv_event_code_t_LV_EVENT_HOVER_LEAVE => Self::HoverLeave,
238            lvgl::lv_event_code_t_LV_EVENT_COVER_CHECK => Self::CoverCheck,
239            lvgl::lv_event_code_t_LV_EVENT_REFR_EXT_DRAW_SIZE => Self::RefreshExtDrawSize,
240            lvgl::lv_event_code_t_LV_EVENT_DRAW_MAIN_BEGIN => Self::DrawMainBegin,
241            lvgl::lv_event_code_t_LV_EVENT_DRAW_MAIN => Self::DrawMain,
242            lvgl::lv_event_code_t_LV_EVENT_DRAW_MAIN_END => Self::DrawMainEnd,
243            lvgl::lv_event_code_t_LV_EVENT_DRAW_POST_BEGIN => Self::DrawPostBegin,
244            lvgl::lv_event_code_t_LV_EVENT_DRAW_POST => Self::DrawPost,
245            lvgl::lv_event_code_t_LV_EVENT_DRAW_POST_END => Self::DrawPostEnd,
246            lvgl::lv_event_code_t_LV_EVENT_DRAW_TASK_ADDED => Self::DrawTaskAdded,
247            lvgl::lv_event_code_t_LV_EVENT_VALUE_CHANGED => Self::ValueChanged,
248            lvgl::lv_event_code_t_LV_EVENT_INSERT => Self::Insert,
249            lvgl::lv_event_code_t_LV_EVENT_REFRESH => Self::Refresh,
250            lvgl::lv_event_code_t_LV_EVENT_READY => Self::Ready,
251            lvgl::lv_event_code_t_LV_EVENT_CANCEL => Self::Cancel,
252            lvgl::lv_event_code_t_LV_EVENT_CREATE => Self::Create,
253            lvgl::lv_event_code_t_LV_EVENT_DELETE => Self::Delete,
254            lvgl::lv_event_code_t_LV_EVENT_CHILD_CHANGED => Self::ChildChanged,
255            lvgl::lv_event_code_t_LV_EVENT_CHILD_CREATED => Self::ChildCreated,
256            lvgl::lv_event_code_t_LV_EVENT_CHILD_DELETED => Self::ChildDeleted,
257            lvgl::lv_event_code_t_LV_EVENT_SCREEN_UNLOAD_START => Self::ScreenUnloadStart,
258            lvgl::lv_event_code_t_LV_EVENT_SCREEN_LOAD_START => Self::ScreenLoadStart,
259            lvgl::lv_event_code_t_LV_EVENT_SCREEN_LOADED => Self::ScreenLoaded,
260            lvgl::lv_event_code_t_LV_EVENT_SCREEN_UNLOADED => Self::ScreenUnloaded,
261            lvgl::lv_event_code_t_LV_EVENT_SIZE_CHANGED => Self::SizeChanged,
262            lvgl::lv_event_code_t_LV_EVENT_STYLE_CHANGED => Self::StyleChanged,
263            lvgl::lv_event_code_t_LV_EVENT_LAYOUT_CHANGED => Self::LayoutChanged,
264            lvgl::lv_event_code_t_LV_EVENT_GET_SELF_SIZE => Self::GetSelfSize,
265            lvgl::lv_event_code_t_LV_EVENT_INVALIDATE_AREA => Self::InvalidateArea,
266            lvgl::lv_event_code_t_LV_EVENT_RESOLUTION_CHANGED => Self::ResolutionChanged,
267            lvgl::lv_event_code_t_LV_EVENT_COLOR_FORMAT_CHANGED => Self::ColorFormatChanged,
268            lvgl::lv_event_code_t_LV_EVENT_REFR_REQUEST => Self::RefreshRequest,
269            lvgl::lv_event_code_t_LV_EVENT_REFR_START => Self::RefreshStart,
270            lvgl::lv_event_code_t_LV_EVENT_REFR_READY => Self::RefreshReady,
271            lvgl::lv_event_code_t_LV_EVENT_RENDER_START => Self::RenderStart,
272            lvgl::lv_event_code_t_LV_EVENT_RENDER_READY => Self::RenderReady,
273            lvgl::lv_event_code_t_LV_EVENT_FLUSH_START => Self::FlushStart,
274            lvgl::lv_event_code_t_LV_EVENT_FLUSH_FINISH => Self::FlushFinish,
275            lvgl::lv_event_code_t_LV_EVENT_FLUSH_WAIT_START => Self::FlushWaitStart,
276            lvgl::lv_event_code_t_LV_EVENT_FLUSH_WAIT_FINISH => Self::FlushWaitFinish,
277            lvgl::lv_event_code_t_LV_EVENT_VSYNC => Self::VerticalSynchronization,
278            lvgl::lv_event_code_t_LV_EVENT_LAST => Self::Last,
279            lvgl::lv_event_code_t_LV_EVENT_PREPROCESS => Self::Preprocess,
280            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 1 => Self::Custom1,
281            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 2 => Self::Custom2,
282            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 3 => Self::CloseRequested,
283            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 4 => Self::Custom4,
284            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 5 => Self::Custom5,
285            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 6 => Self::Custom6,
286            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 7 => Self::Custom7,
287            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 8 => Self::Custom8,
288            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 9 => Self::Custom9,
289            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 10 => Self::Custom10,
290            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 11 => Self::Custom11,
291            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 12 => Self::Custom12,
292            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 13 => Self::Custom13,
293            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 14 => Self::Custom14,
294            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 15 => Self::Custom15,
295            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 16 => Self::Custom16,
296            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 17 => Self::Custom17,
297            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 18 => Self::Custom18,
298            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 19 => Self::Custom19,
299            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 20 => Self::Custom20,
300            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 21 => Self::Custom21,
301            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 22 => Self::Custom22,
302            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 23 => Self::Custom23,
303            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 24 => Self::Custom24,
304            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 25 => Self::Custom25,
305            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 26 => Self::Custom26,
306            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 27 => Self::Custom27,
307            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 28 => Self::Custom28,
308            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 29 => Self::Custom29,
309            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 30 => Self::Custom30,
310            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 31 => Self::Custom31,
311            code if code == lvgl::lv_event_code_t_LV_EVENT_LAST + 32 => Self::Custom32,
312            _ => Self::Last,
313        }
314    }
315}
316
317impl From<EventKind> for lvgl::lv_event_code_t {
318    fn from(code: EventKind) -> Self {
319        code.into_lvgl_code()
320    }
321}
322
323impl From<lvgl::lv_event_code_t> for EventKind {
324    fn from(code: lvgl::lv_event_code_t) -> Self {
325        EventKind::from_lvgl_code(code)
326    }
327}
328
329#[cfg(test)]
330mod tests {
331    use super::{Event, EventKind};
332    use core::ptr;
333
334    #[test]
335    fn a_cleared_target_matches_no_candidate() {
336        let cleared = Event::new(EventKind::Pressed, ptr::null_mut(), None);
337        assert!(!cleared.is_target(ptr::null()));
338
339        let mut object = 0u8;
340        let address = (&raw mut object).cast();
341        assert!(!cleared.is_target(address));
342        assert!(Event::new(EventKind::Pressed, address, None).is_target(address));
343        // A cleared target is never searched for, even in an invalid tree.
344        assert_eq!(unsafe { cleared.find_target_in(address) }, None);
345    }
346}