@@ -9,23 +9,24 @@ use crate::lv_core::style::Style;
9
9
use crate :: { Align , LvError , LvResult } ;
10
10
use core:: fmt:: { self , Debug } ;
11
11
use core:: marker:: PhantomData ;
12
- use core:: ptr;
12
+ use core:: ptr:: { self , NonNull } ;
13
13
14
14
/// Represents a native LVGL object.
15
15
pub trait NativeObject {
16
16
/// Provide common way to access to the underlying native object pointer.
17
- fn raw ( & self ) -> LvResult < ptr :: NonNull < lvgl_sys:: lv_obj_t > > ;
17
+ fn raw ( & self ) -> NonNull < lvgl_sys:: lv_obj_t > ;
18
18
}
19
19
20
20
/// Generic LVGL object.
21
21
///
22
- /// This is the parent object of all widget types. It stores the native LVGL raw pointer.
22
+ /// This is the parent object of all widget types. It stores the native LVGL
23
+ /// raw pointer.
23
24
pub struct Obj < ' a > {
24
- // We use a raw pointer here because we do not control this memory address, it is controlled
25
- // by LVGL's global state.
26
- raw : * mut lvgl_sys:: lv_obj_t ,
27
- // This is to ensure safety for style memory; it has no runtime impact
28
- styles_used : PhantomData < & ' a lvgl_sys :: lv_style_t > ,
25
+ // We use a raw pointer here because we do not control this memory address,
26
+ // it is controlled by LVGL's global state.
27
+ raw : NonNull < lvgl_sys:: lv_obj_t > ,
28
+ // This is to ensure safety for children memory; it has no runtime impact
29
+ dependents : PhantomData < & ' a isize > ,
29
30
}
30
31
31
32
impl Debug for Obj < ' _ > {
@@ -38,15 +39,15 @@ impl Debug for Obj<'_> {
38
39
39
40
// We need to manually impl methods on Obj since widget codegen is defined in
40
41
// terms of Obj
41
- impl Obj < ' _ > {
42
- pub fn create ( parent : & mut impl NativeObject ) -> LvResult < Self > {
42
+ impl < ' a > Obj < ' a > {
43
+ pub fn create ( parent : & ' a mut impl NativeObject ) -> LvResult < Self > {
43
44
unsafe {
44
- let ptr = lvgl_sys:: lv_obj_create ( parent. raw ( ) ? . as_mut ( ) ) ;
45
- if ptr:: NonNull :: new ( ptr) . is_some ( ) {
45
+ let ptr = lvgl_sys:: lv_obj_create ( parent. raw ( ) . as_mut ( ) ) ;
46
+ if let Some ( nn_ptr ) = ptr:: NonNull :: new ( ptr) {
46
47
//(*ptr).user_data = Box::new(UserDataObj::empty()).into_raw() as *mut _;
47
48
Ok ( Self {
48
- raw : ptr ,
49
- styles_used : PhantomData ,
49
+ raw : nn_ptr ,
50
+ dependents : PhantomData :: < & ' a _ > ,
50
51
} )
51
52
} else {
52
53
Err ( LvError :: InvalidReference )
@@ -56,17 +57,23 @@ impl Obj<'_> {
56
57
57
58
pub fn new ( ) -> crate :: LvResult < Self > {
58
59
let mut parent = crate :: display:: get_scr_act ( ) ?;
59
- Self :: create ( & mut parent)
60
+ Self :: create ( unsafe { & mut * ( & mut parent as * mut _ ) } )
61
+ }
62
+
63
+ pub fn blank ( ) -> LvResult < Self > {
64
+ match NonNull :: new ( unsafe { lvgl_sys:: lv_obj_create ( ptr:: null_mut ( ) ) } ) {
65
+ Some ( raw) => Ok ( Self {
66
+ raw,
67
+ dependents : PhantomData ,
68
+ } ) ,
69
+ None => Err ( LvError :: LvOOMemory ) ,
70
+ }
60
71
}
61
72
}
62
73
63
74
impl NativeObject for Obj < ' _ > {
64
- fn raw ( & self ) -> LvResult < ptr:: NonNull < lvgl_sys:: lv_obj_t > > {
65
- if let Some ( non_null_ptr) = ptr:: NonNull :: new ( self . raw ) {
66
- Ok ( non_null_ptr)
67
- } else {
68
- Err ( LvError :: InvalidReference )
69
- }
75
+ fn raw ( & self ) -> ptr:: NonNull < lvgl_sys:: lv_obj_t > {
76
+ self . raw
70
77
}
71
78
}
72
79
@@ -85,92 +92,77 @@ pub trait Widget<'a>: NativeObject + Sized + 'a {
85
92
unsafe fn from_raw ( raw_pointer : ptr:: NonNull < lvgl_sys:: lv_obj_t > ) -> Option < Self > ;
86
93
87
94
/// Adds a `Style` to a given widget.
88
- fn add_style ( & mut self , part : Self :: Part , style : & ' a mut Style ) -> LvResult < ( ) > {
95
+ fn add_style ( & mut self , part : Self :: Part , style : & ' a mut Style ) {
89
96
unsafe {
90
97
lvgl_sys:: lv_obj_add_style (
91
- self . raw ( ) ? . as_mut ( ) ,
98
+ self . raw ( ) . as_mut ( ) ,
92
99
style. raw . as_mut ( ) as * mut _ ,
93
100
part. into ( ) ,
94
101
) ;
95
102
} ;
96
- Ok ( ( ) )
97
103
}
98
104
99
105
/// Sets a widget's position relative to its parent.
100
- fn set_pos ( & mut self , x : i16 , y : i16 ) -> LvResult < ( ) > {
106
+ fn set_pos ( & mut self , x : i16 , y : i16 ) {
101
107
unsafe {
102
108
lvgl_sys:: lv_obj_set_pos (
103
- self . raw ( ) ? . as_mut ( ) ,
109
+ self . raw ( ) . as_mut ( ) ,
104
110
x as lvgl_sys:: lv_coord_t ,
105
111
y as lvgl_sys:: lv_coord_t ,
106
112
) ;
107
113
}
108
- Ok ( ( ) )
109
114
}
110
115
111
116
/// Sets a widget's size. Alternatively, use `set_width()` and `set_height()`.
112
- fn set_size ( & mut self , w : i16 , h : i16 ) -> LvResult < ( ) > {
117
+ fn set_size ( & mut self , w : i16 , h : i16 ) {
113
118
unsafe {
114
119
lvgl_sys:: lv_obj_set_size (
115
- self . raw ( ) ? . as_mut ( ) ,
120
+ self . raw ( ) . as_mut ( ) ,
116
121
w as lvgl_sys:: lv_coord_t ,
117
122
h as lvgl_sys:: lv_coord_t ,
118
123
) ;
119
124
}
120
- Ok ( ( ) )
121
125
}
122
126
123
127
/// Sets a widget's width. Alternatively, use `set_size()`.
124
- fn set_width ( & mut self , w : u32 ) -> LvResult < ( ) > {
128
+ fn set_width ( & mut self , w : u32 ) {
125
129
unsafe {
126
- lvgl_sys:: lv_obj_set_width ( self . raw ( ) ? . as_mut ( ) , w as lvgl_sys:: lv_coord_t ) ;
130
+ lvgl_sys:: lv_obj_set_width ( self . raw ( ) . as_mut ( ) , w as lvgl_sys:: lv_coord_t ) ;
127
131
}
128
- Ok ( ( ) )
129
132
}
130
133
131
134
/// Sets a widget's height. Alternatively, use `set_size()`.
132
- fn set_height ( & mut self , h : u32 ) -> LvResult < ( ) > {
135
+ fn set_height ( & mut self , h : u32 ) {
133
136
unsafe {
134
- lvgl_sys:: lv_obj_set_height ( self . raw ( ) ? . as_mut ( ) , h as lvgl_sys:: lv_coord_t ) ;
137
+ lvgl_sys:: lv_obj_set_height ( self . raw ( ) . as_mut ( ) , h as lvgl_sys:: lv_coord_t ) ;
135
138
}
136
- Ok ( ( ) )
137
139
}
138
140
139
141
/// Sets a widget's align relative to its parent along with an offset.
140
- fn set_align ( & mut self , align : Align , x_mod : i32 , y_mod : i32 ) -> LvResult < ( ) > {
142
+ fn set_align ( & mut self , align : Align , x_mod : i32 , y_mod : i32 ) {
141
143
unsafe {
142
144
lvgl_sys:: lv_obj_align (
143
- self . raw ( ) ? . as_mut ( ) ,
145
+ self . raw ( ) . as_mut ( ) ,
144
146
align. into ( ) ,
145
147
x_mod as lvgl_sys:: lv_coord_t ,
146
148
y_mod as lvgl_sys:: lv_coord_t ,
147
149
) ;
148
150
}
149
- Ok ( ( ) )
150
151
}
151
152
}
152
153
153
154
impl < ' a > Widget < ' a > for Obj < ' a > {
154
155
type SpecialEvent = u32 ;
155
156
type Part = Part ;
156
157
157
- unsafe fn from_raw ( raw : ptr :: NonNull < lvgl_sys:: lv_obj_t > ) -> Option < Self > {
158
+ unsafe fn from_raw ( raw : NonNull < lvgl_sys:: lv_obj_t > ) -> Option < Self > {
158
159
Some ( Self {
159
- raw : raw . as_ptr ( ) ,
160
- styles_used : PhantomData ,
160
+ raw,
161
+ dependents : PhantomData ,
161
162
} )
162
163
}
163
164
}
164
165
165
- impl Default for Obj < ' _ > {
166
- fn default ( ) -> Self {
167
- Self {
168
- raw : unsafe { lvgl_sys:: lv_obj_create ( ptr:: null_mut ( ) ) } ,
169
- styles_used : PhantomData ,
170
- }
171
- }
172
- }
173
-
174
166
macro_rules! define_object {
175
167
( $item: ident) => {
176
168
define_object!( $item, event = ( ) , part = $crate:: Part ) ;
@@ -197,7 +189,7 @@ macro_rules! define_object {
197
189
{
198
190
use $crate:: NativeObject ;
199
191
unsafe {
200
- let obj = self . raw( ) ? . as_mut( ) ;
192
+ let obj = self . raw( ) . as_mut( ) ;
201
193
obj. user_data = $crate:: Box :: into_raw( $crate:: Box :: new( f) ) as * mut _;
202
194
lvgl_sys:: lv_obj_add_event_cb(
203
195
obj,
@@ -213,7 +205,7 @@ macro_rules! define_object {
213
205
}
214
206
215
207
impl $crate:: NativeObject for $item<' _> {
216
- fn raw( & self ) -> $crate :: LvResult < core:: ptr:: NonNull <lvgl_sys:: lv_obj_t> > {
208
+ fn raw( & self ) -> core:: ptr:: NonNull <lvgl_sys:: lv_obj_t> {
217
209
self . core. raw( )
218
210
}
219
211
}
0 commit comments