Skip to main content

graphics/
lvgl.rs

1use core::ffi::c_char;
2
3pub use lvgl_rust_sys::*;
4
5use crate::Point;
6
7pub const LV_SIZE_CONTENT: i32 = (LV_COORD_MAX | LV_COORD_TYPE_SPEC) as i32;
8
9pub const LV_STATE_DEFAULT: u32 = 0;
10pub const LV_STATE_CHECKED: lv_state_t = lv_state_t_LV_STATE_CHECKED;
11pub const LV_STATE_PRESSED: lv_state_t = lv_state_t_LV_STATE_PRESSED;
12
13pub const LV_PART_MAIN: u32 = 0;
14pub const LV_PART_INDICATOR: u32 = 0x020000;
15pub const LV_PART_KNOB: u32 = 0x030000;
16pub const LV_PART_ITEMS: u32 = 0x050000;
17
18pub const LV_OPA_TRANSP: u8 = 0;
19pub const LV_OPA_0: u8 = 0;
20pub const LV_OPA_50: u8 = 127;
21pub const LV_OPA_COVER: u8 = 255;
22
23// `lvgl_rust_sys` omits this optional LVGL declaration. Keep the raw
24// function private to this module and expose it only to the graphics wrapper.
25unsafe extern "C" {
26    #[link_name = "lv_snapshot_take"]
27    fn lv_snapshot_take_raw(
28        object: *mut lv_obj_t,
29        color_format: lv_color_format_t,
30    ) -> *mut lv_draw_buf_t;
31}
32
33pub(crate) unsafe fn snapshot_take(
34    object: *mut lv_obj_t,
35    color_format: lv_color_format_t,
36) -> *mut lv_draw_buf_t {
37    unsafe { lv_snapshot_take_raw(object, color_format) }
38}
39
40/// Set the padding of an object on all sides
41///
42/// # Arguments
43///
44/// * `Object` - The object to set the padding of.
45/// * `Padding` - The padding to set.
46/// * `Selector` - The selector to set the padding for.
47///
48/// # Safety
49///
50/// This function is unsafe because it may dereference raw pointers (e.g. `Object`).
51pub unsafe fn lv_obj_set_style_pad_all(
52    object: *mut lv_obj_t,
53    padding: i32,
54    selector: lv_style_selector_t,
55) {
56    unsafe {
57        lv_obj_set_style_pad_top(object, padding, selector);
58        lv_obj_set_style_pad_bottom(object, padding, selector);
59        lv_obj_set_style_pad_left(object, padding, selector);
60        lv_obj_set_style_pad_right(object, padding, selector);
61    }
62}
63
64/// Set the padding of an object on the top side
65///
66/// # Arguments
67///
68/// * `Object` - The object to set the padding of.
69/// * `Padding` - The padding to set.
70/// * `Selector` - The selector to set the padding for.
71///
72/// # Safety
73///
74/// This function is unsafe because it may dereference raw pointers (e.g. `Object`).
75///
76pub unsafe fn lv_obj_move_foreground(object: *mut lv_obj_t) {
77    unsafe {
78        lv_obj_move_to_index(object, -1);
79    }
80}
81
82/// Get the size of an object as a Point
83///
84/// # Arguments
85/// * `Object` - The object to get the size of.
86///
87///  # Safety
88/// This function is unsafe because it may dereference raw pointers (e.g. `Object`).
89///
90pub unsafe fn lv_obj_get_size(object: *mut lv_obj_t) -> Point {
91    unsafe {
92        let width = lv_obj_get_width(object) as i16;
93        let height = lv_obj_get_height(object) as i16;
94
95        Point::new(width, height)
96    }
97}
98
99/// Add a tab to a tabview and adjust the tab button size
100///
101/// # Arguments
102///
103/// * `tabview` - The tabview to add the tab to.
104/// * `name` - The name of the tab.
105///
106/// # Safety
107///
108/// This function is unsafe because it may dereference raw pointers (e.g. `tabview`).
109pub unsafe fn lv_tabview_add_tab(tabview: *mut lv_obj_t, name: *const c_char) -> *mut lv_obj_t {
110    unsafe {
111        let page = lvgl_rust_sys::lv_tabview_add_tab(tabview, name);
112
113        let bar = lv_tabview_get_tab_bar(tabview);
114
115        lv_obj_set_size(bar, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
116
117        // get latest tab button
118        let tab_count = lv_obj_get_child_count(bar);
119        let button = lv_obj_get_child(bar, (tab_count - 1) as _);
120
121        // don't make it grow
122        lv_obj_set_flex_grow(button, 0);
123        lv_obj_set_size(button, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
124
125        page
126    }
127}
128
129unsafe extern "C" fn radio_event_handler(event: *mut lv_event_t) {
130    unsafe {
131        let code = lvgl_rust_sys::lv_event_get_code(event);
132        let target = lvgl_rust_sys::lv_event_get_target(event) as *mut lv_obj_t;
133        let user_data = lvgl_rust_sys::lv_event_get_user_data(event) as *mut lv_obj_t;
134
135        if code == lv_event_code_t_LV_EVENT_CLICKED {
136            let parent = user_data as *mut lv_obj_t;
137            let child_count = lvgl_rust_sys::lv_obj_get_child_count(parent);
138
139            for i in 0..child_count {
140                let child = lvgl_rust_sys::lv_obj_get_child(parent, i as _);
141                if child != target {
142                    lvgl_rust_sys::lv_obj_remove_state(child, LV_STATE_CHECKED as _);
143                }
144            }
145
146            lvgl_rust_sys::lv_obj_add_state(target, LV_STATE_CHECKED as _);
147        }
148    }
149}
150
151/// Create a radio button (a checkbox that behaves like a radio button)
152///
153/// # Arguments
154///
155/// * `parent` - The parent object of the radio button.
156///
157/// # Safety
158/// This function is unsafe because it may dereference raw pointers (e.g. `parent`).
159pub unsafe fn lv_radiobox_create(parent: *mut lv_obj_t) -> *mut lv_obj_t {
160    unsafe {
161        let checkbox = lvgl_rust_sys::lv_checkbox_create(parent);
162
163        lvgl_rust_sys::lv_obj_add_event_cb(
164            checkbox,
165            Some(radio_event_handler),
166            lvgl_rust_sys::lv_event_code_t_LV_EVENT_CLICKED,
167            parent as _,
168        );
169
170        checkbox
171    }
172}