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
9/// Set the padding of an object on all sides
10///
11/// # Arguments
12///
13/// * `Object` - The object to set the padding of.
14/// * `Padding` - The padding to set.
15/// * `Selector` - The selector to set the padding for.
16///
17/// # Safety
18///
19/// This function is unsafe because it may dereference raw pointers (e.g. `Object`).
20pub unsafe fn lv_obj_set_style_pad_all(
21    object: *mut lv_obj_t,
22    padding: i32,
23    selector: lv_style_selector_t,
24) {
25    unsafe {
26        lv_obj_set_style_pad_top(object, padding, selector);
27        lv_obj_set_style_pad_bottom(object, padding, selector);
28        lv_obj_set_style_pad_left(object, padding, selector);
29        lv_obj_set_style_pad_right(object, padding, selector);
30    }
31}
32
33/// Set the padding of an object on the top side
34///
35/// # Arguments
36///
37/// * `Object` - The object to set the padding of.
38/// * `Padding` - The padding to set.
39/// * `Selector` - The selector to set the padding for.
40///
41/// # Safety
42///
43/// This function is unsafe because it may dereference raw pointers (e.g. `Object`).
44///
45pub unsafe fn lv_obj_move_foreground(object: *mut lv_obj_t) {
46    unsafe {
47        lv_obj_move_to_index(object, -1);
48    }
49}
50
51/// Get the size of an object as a Point
52///
53/// # Arguments
54/// * `Object` - The object to get the size of.
55///
56///  # Safety
57/// This function is unsafe because it may dereference raw pointers (e.g. `Object`).
58///
59pub unsafe fn lv_obj_get_size(object: *mut lv_obj_t) -> Point {
60    unsafe {
61        let width = lv_obj_get_width(object) as i16;
62        let height = lv_obj_get_height(object) as i16;
63
64        Point::new(width, height)
65    }
66}
67
68/// Add a tab to a tabview and adjust the tab button size
69///
70/// # Arguments
71///
72/// * `tabview` - The tabview to add the tab to.
73/// * `name` - The name of the tab.
74///
75/// # Safety
76///
77/// This function is unsafe because it may dereference raw pointers (e.g. `tabview`).
78pub unsafe fn lv_tabview_add_tab(tabview: *mut lv_obj_t, name: *const c_char) -> *mut lv_obj_t {
79    unsafe {
80        let page = lvgl_rust_sys::lv_tabview_add_tab(tabview, name);
81
82        let bar = lv_tabview_get_tab_bar(tabview);
83
84        lv_obj_set_size(bar, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
85
86        // get latest tab button
87        let tab_count = lv_obj_get_child_count(bar);
88        let button = lv_obj_get_child(bar, (tab_count - 1) as _);
89
90        // don't make it grow
91        lv_obj_set_flex_grow(button, 0);
92        lv_obj_set_size(button, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
93
94        page
95    }
96}
97
98unsafe extern "C" fn radio_event_handler(event: *mut lv_event_t) {
99    unsafe {
100        let code = lvgl_rust_sys::lv_event_get_code(event);
101        let target = lvgl_rust_sys::lv_event_get_target(event) as *mut lv_obj_t;
102        let user_data = lvgl_rust_sys::lv_event_get_user_data(event) as *mut lv_obj_t;
103
104        if code == lv_event_code_t_LV_EVENT_CLICKED {
105            let parent = user_data as *mut lv_obj_t;
106            let child_count = lvgl_rust_sys::lv_obj_get_child_count(parent);
107
108            for i in 0..child_count {
109                let child = lvgl_rust_sys::lv_obj_get_child(parent, i as _);
110                if child != target {
111                    lvgl_rust_sys::lv_obj_remove_state(child, lvgl_rust_sys::LV_STATE_CHECKED as _);
112                }
113            }
114
115            lvgl_rust_sys::lv_obj_add_state(target, lvgl_rust_sys::LV_STATE_CHECKED as _);
116        }
117    }
118}
119
120/// Create a radio button (a checkbox that behaves like a radio button)
121///
122/// # Arguments
123///
124/// * `parent` - The parent object of the radio button.
125///
126/// # Safety
127/// This function is unsafe because it may dereference raw pointers (e.g. `parent`).
128pub unsafe fn lv_radiobox_create(parent: *mut lv_obj_t) -> *mut lv_obj_t {
129    unsafe {
130        let checkbox = lvgl_rust_sys::lv_checkbox_create(parent);
131
132        lvgl_rust_sys::lv_obj_add_event_cb(
133            checkbox,
134            Some(radio_event_handler),
135            lvgl_rust_sys::lv_event_code_t_LV_EVENT_CLICKED,
136            parent as _,
137        );
138
139        checkbox
140    }
141}