Change style using high level structures

This commit is contained in:
Rafael Caricio 2020-04-14 21:33:34 +02:00
parent cd0e9e7f5f
commit 481b9ba7bc
2 changed files with 66 additions and 58 deletions

View file

@ -66,26 +66,31 @@ fn main() -> Result<(), String> {
// label.set_text("Hello Mundo!\0");
let mut time = lvgl::Label::new(&mut screen);
let mut style_time = Style::default();
style_time.text.font = unsafe {
Some(&noto_sans_numeric_80)
};
time.set_style(&mut style_time);
time.set_align(&mut screen, lvgl::Align::InLeftMid, 20, 0);
time.set_text("20:46\0");
time.set_width(240);
time.set_height(200);
time.set_height(240);
// let mut style_time = Style::default();
// style_time.text.font = unsafe {
// Some(&noto_sans_numeric_80)
// };
//time.set_style(style_time);
let mut bt = lvgl::Label::new(&mut screen);
bt.set_width(50);
bt.set_height(80);
bt.set_recolor(true);
bt.set_text("#5794f2 \u{F293}#\0");
bt.set_label_align(lvgl::LabelAlign::Left);
bt.set_align(&mut screen, lvgl::Align::InTopLeft, 0, 0);
let mut native_style: lvgl_sys::lv_style_t;
unsafe {
native_style = MaybeUninit::<lvgl_sys::lv_style_t>::uninit().assume_init();
lvgl_sys::lv_style_copy(&mut native_style, &lvgl_sys::lv_style_pretty);
native_style.text.font = &noto_sans_numeric_80;
}
time.set_style(&mut native_style);
time.set_label_align(lvgl::LabelAlign::Center);
time.set_align(&mut screen, lvgl::Align::Center, 0, -30);
let mut power = lvgl::Label::new(&mut screen);
power.set_recolor(true);
power.set_width(80);
power.set_height(20);
power.set_text("#fade2a 20%#\0");
power.set_label_align(lvgl::LabelAlign::Right);
power.set_align(&mut screen, lvgl::Align::InTopRight, 0, 0);
let mut event_pump = sdl_context.event_pump()?;
'running: loop {
@ -164,7 +169,7 @@ where
}
}
fn get_active_screen(&mut self) -> lvgl::ObjectX {
fn get_active_screen(&mut self) -> lvgl::ObjectX<'static> {
lvgl::display::get_active_screen()
}
}

View file

@ -9,7 +9,7 @@ pub trait NativeObject {
pub struct ObjectX<'a> {
raw: ptr::NonNull<lvgl_sys::lv_obj_t>,
style: Option<Style<'a>>,
style: Option<&'a mut Style<'a>>,
}
impl<'a> ObjectX<'a> {
@ -24,7 +24,7 @@ impl<'a> NativeObject for ObjectX<'a> {
}
}
pub trait Object: NativeObject {
pub trait Object<'a>: NativeObject {
fn set_pos(&mut self, x: i16, y: i16) {
unsafe {
lvgl_sys::lv_obj_set_pos(
@ -98,8 +98,7 @@ pub trait Object: NativeObject {
}
}
//fn set_style(&mut self, style: Style<'static>);
fn set_style(&mut self, style: &mut lvgl_sys::lv_style_t);
fn set_style(&mut self, style: &'a mut Style<'a>);
}
macro_rules! define_object {
@ -114,41 +113,17 @@ macro_rules! define_object {
}
}
impl<'a> Object for $item<'a> {
fn set_style(&mut self, style: &mut lvgl_sys::lv_style_t) {
unsafe {
lvgl_sys::lv_obj_set_style(self.raw().as_mut(), style);
};
impl<'a> Object<'a> for $item<'a> {
fn set_style(&mut self, style: &'a mut Style<'a>) {
//self.core.style = Some(style);
unsafe {
lvgl_sys::lv_obj_set_style(self.raw().as_mut(), style.raw());
};
}
}
}
}
pub enum Align {
Center,
InTopLeft,
InTopMid,
InTopRight,
InBottomLeft,
InBottomMid,
InBottomRight,
InLeftMid,
InRightMid,
OutTopLeft,
OutTopMid,
OutTopRight,
OutBottomLeft,
OutBottomMid,
OutBottomRight,
OutLeftTop,
OutLeftMid,
OutLeftBottom,
OutRightTop,
OutRightMid,
OutRightBottom,
}
define_object!(Button);
impl<'a> Button<'a> {
@ -201,6 +176,12 @@ impl<'a> Label<'a> {
lvgl_sys::lv_label_set_align(self.core.raw().as_mut(), align);
}
}
pub fn set_recolor(&mut self, recolor: bool) {
unsafe {
lvgl_sys::lv_label_set_recolor(self.core.raw().as_mut(), recolor);
}
}
}
pub enum Themes {
@ -221,20 +202,42 @@ pub struct TextStyle<'a> {
impl<'a> Style<'a> {
fn raw(&mut self) -> *const lvgl_sys::lv_style_t {
match self.raw {
Some(mut native_pointer) => unsafe {
Some(mut native_pointer) => {
&mut native_pointer
}
},
None => unsafe {
// TODO: Create the native struct and save to self
let mut native_style = mem::MaybeUninit::<lvgl_sys::lv_style_t>::uninit().assume_init();
lvgl_sys::lv_style_copy(&mut native_style, &lvgl_sys::lv_style_pretty);
let mut native_style = mem::MaybeUninit::<lvgl_sys::lv_style_t>::uninit();
lvgl_sys::lv_style_copy(native_style.as_mut_ptr(), &lvgl_sys::lv_style_pretty);
self.raw = Some(native_style.assume_init());
if let Some(text_font) = self.text.font {
native_style.text.font = text_font;
self.raw.as_mut().unwrap().text.font = text_font as *const lvgl_sys::lv_font_t;
}
self.raw = Some(native_style);
&mut self.raw.unwrap()
self.raw.as_mut().unwrap()
}
}
}
}
pub enum Align {
Center,
InTopLeft,
InTopMid,
InTopRight,
InBottomLeft,
InBottomMid,
InBottomRight,
InLeftMid,
InRightMid,
OutTopLeft,
OutTopMid,
OutTopRight,
OutBottomLeft,
OutBottomMid,
OutBottomRight,
OutLeftTop,
OutLeftMid,
OutLeftBottom,
OutRightTop,
OutRightMid,
OutRightBottom,
}