From 481b9ba7bc26cb036a10b7be3de4f4f56a93c3d2 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Tue, 14 Apr 2020 21:33:34 +0200 Subject: [PATCH] Change style using high level structures --- examples/demo/src/main.rs | 39 ++++++++++-------- lvgl/src/objx.rs | 85 ++++++++++++++++++++------------------- 2 files changed, 66 insertions(+), 58 deletions(-) diff --git a/examples/demo/src/main.rs b/examples/demo/src/main.rs index 83bcd31..688a76a 100644 --- a/examples/demo/src/main.rs +++ b/examples/demo/src/main.rs @@ -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(¬o_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(¬o_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::::uninit().assume_init(); - lvgl_sys::lv_style_copy(&mut native_style, &lvgl_sys::lv_style_pretty); - native_style.text.font = ¬o_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() } } diff --git a/lvgl/src/objx.rs b/lvgl/src/objx.rs index f112f18..8831428 100644 --- a/lvgl/src/objx.rs +++ b/lvgl/src/objx.rs @@ -9,7 +9,7 @@ pub trait NativeObject { pub struct ObjectX<'a> { raw: ptr::NonNull, - style: Option>, + 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::::uninit().assume_init(); - lvgl_sys::lv_style_copy(&mut native_style, &lvgl_sys::lv_style_pretty); + let mut native_style = mem::MaybeUninit::::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, +}