-
Notifications
You must be signed in to change notification settings - Fork 64
/
obj.rs
216 lines (192 loc) · 6.19 KB
/
obj.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::lv_core::style::Style;
use crate::mem::Box;
use crate::{Align, LvError, LvResult};
use core::ptr;
/// Represents a native LittlevGL object
pub trait NativeObject {
/// Provide common way to access to the underlying native object pointer.
fn raw(&self) -> LvResult<ptr::NonNull<lvgl_sys::lv_obj_t>>;
}
/// Generic LVGL object.
///
/// This is the parent object of all widget types. It stores the native LVGL raw pointer.
pub struct Obj {
// We use a raw pointer here because we do not control this memory address, it is controlled
// by LVGL's global state.
raw: *mut lvgl_sys::lv_obj_t,
}
impl NativeObject for Obj {
fn raw(&self) -> LvResult<ptr::NonNull<lvgl_sys::lv_obj_t>> {
if let Some(non_null_ptr) = ptr::NonNull::new(self.raw) {
Ok(non_null_ptr)
} else {
Err(LvError::InvalidReference)
}
}
}
/// A wrapper for all LittlevGL common operations on generic objects.
pub trait Widget: NativeObject {
type SpecialEvent;
type Part: Into<u8>;
/// Construct an instance of the object from a raw pointer.
///
/// # Safety
/// Provided the LVGL library can allocate memory this should be safe.
///
unsafe fn from_raw(raw_pointer: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self;
fn add_style(&self, part: Self::Part, style: Style) -> LvResult<()> {
unsafe {
lvgl_sys::lv_obj_add_style(self.raw()?.as_mut(), part.into(), Box::into_raw(style.raw));
};
Ok(())
}
fn set_pos(&mut self, x: i16, y: i16) -> LvResult<()> {
unsafe {
lvgl_sys::lv_obj_set_pos(
self.raw()?.as_mut(),
x as lvgl_sys::lv_coord_t,
y as lvgl_sys::lv_coord_t,
);
}
Ok(())
}
fn set_size(&mut self, w: i16, h: i16) -> LvResult<()> {
unsafe {
lvgl_sys::lv_obj_set_size(
self.raw()?.as_mut(),
w as lvgl_sys::lv_coord_t,
h as lvgl_sys::lv_coord_t,
);
}
Ok(())
}
fn set_width(&mut self, w: u32) -> LvResult<()> {
unsafe {
lvgl_sys::lv_obj_set_width(self.raw()?.as_mut(), w as lvgl_sys::lv_coord_t);
}
Ok(())
}
fn set_height(&mut self, h: u32) -> LvResult<()> {
unsafe {
lvgl_sys::lv_obj_set_height(self.raw()?.as_mut(), h as lvgl_sys::lv_coord_t);
}
Ok(())
}
fn set_align<C>(&mut self, base: &mut C, align: Align, x_mod: i32, y_mod: i32) -> LvResult<()>
where
C: NativeObject,
{
unsafe {
lvgl_sys::lv_obj_align(
self.raw()?.as_mut(),
base.raw()?.as_mut(),
align.into(),
x_mod as lvgl_sys::lv_coord_t,
y_mod as lvgl_sys::lv_coord_t,
);
}
Ok(())
}
}
impl Widget for Obj {
type SpecialEvent = ();
type Part = Part;
unsafe fn from_raw(raw: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
Self { raw: raw.as_ptr() }
}
}
impl Default for Obj {
fn default() -> Self {
Self {
raw: unsafe { lvgl_sys::lv_obj_create(ptr::null_mut(), ptr::null_mut()) },
}
}
}
macro_rules! define_object {
($item:ident) => {
define_object!($item, event = (), part = $crate::Part);
};
($item:ident, event = $event_type:ty) => {
define_object!($item, event = $event_type, part = $crate::Part);
};
($item:ident, part = $part_type:ty) => {
define_object!($item, event = (), part = $part_type);
};
($item:ident, part = $part_type:ty, event = $event_type:ty) => {
define_object!($item, event = $event_type, part = $part_type);
};
($item:ident, event = $event_type:ty, part = $part_type:ty) => {
pub struct $item {
core: $crate::Obj,
}
unsafe impl Send for $item {}
impl $item {
pub fn on_event<F>(&mut self, f: F) -> $crate::LvResult<()>
where
F: FnMut(Self, $crate::support::Event<<Self as $crate::Widget>::SpecialEvent>),
{
use $crate::NativeObject;
unsafe {
let mut raw = self.raw()?;
let obj = raw.as_mut();
let user_closure = $crate::mem::Box::new(f)?;
obj.user_data = $crate::mem::Box::into_raw(user_closure) as *mut cty::c_void;
lvgl_sys::lv_obj_set_event_cb(
obj,
lvgl_sys::lv_event_cb_t::Some($crate::support::event_callback::<Self, F>),
);
}
Ok(())
}
}
impl $crate::NativeObject for $item {
fn raw(&self) -> $crate::LvResult<core::ptr::NonNull<lvgl_sys::lv_obj_t>> {
self.core.raw()
}
}
impl $crate::Widget for $item {
type SpecialEvent = $event_type;
type Part = $part_type;
unsafe fn from_raw(raw_pointer: core::ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
Self {
core: $crate::Obj::from_raw(raw_pointer),
}
}
}
};
}
bitflags! {
pub struct State: u32 {
/// Normal, released
const DEFAULT = lvgl_sys::LV_STATE_DEFAULT;
/// Toggled or checked
const CHECKED = lvgl_sys::LV_STATE_CHECKED;
/// Focused via keypad or encoder or clicked via touchpad/mouse
const FOCUSED = lvgl_sys::LV_STATE_FOCUSED;
/// Edit by an encoder
const EDITED = lvgl_sys::LV_STATE_EDITED;
/// Hovered by mouse (not supported now)
const HOVERED = lvgl_sys::LV_STATE_HOVERED;
/// Pressed
const PRESSED = lvgl_sys::LV_STATE_PRESSED;
/// Disabled or inactive
const DISABLED = lvgl_sys::LV_STATE_DISABLED;
}
}
impl State {
pub(crate) fn get_bits(&self) -> u32 {
self.bits
}
}
pub enum Part {
Main,
All,
}
impl Into<u8> for Part {
fn into(self) -> u8 {
match self {
Part::Main => lvgl_sys::LV_OBJ_PART_MAIN as u8,
Part::All => lvgl_sys::LV_OBJ_PART_ALL as u8,
}
}
}