diff --git a/examples/bar.rs b/examples/bar.rs index 4d14c29..0bc6d77 100644 --- a/examples/bar.rs +++ b/examples/bar.rs @@ -3,7 +3,8 @@ use embedded_graphics::prelude::*; use embedded_graphics_simulator::{ OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, }; -use lvgl::{self, widgets::Bar, Align, Animation, Color, DisplayDriver, Label, Object, Style, UI}; +use lvgl::widgets::{Bar, Label}; +use lvgl::{self, Align, Animation, Color, DisplayDriver, Object, Style, UI}; use lvgl_sys; use std::sync::{mpsc, Arc, Mutex}; use std::thread::sleep; diff --git a/examples/demo.rs b/examples/demo.rs index 6e3db84..517c68e 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -4,6 +4,7 @@ use embedded_graphics_simulator::{ OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, }; use lvgl; +use lvgl::widgets::{Label, LabelAlign}; use lvgl::{Object, UI}; use lvgl_sys; use std::sync::{mpsc, Arc, Mutex}; @@ -37,7 +38,7 @@ fn main() -> Result<(), String> { screen_style.set_body_radius(0); screen.set_style(screen_style); - let mut time = lvgl::Label::new(&mut screen); + let mut time = Label::new(&mut screen); let mut style_time = lvgl::Style::new(); style_time.set_text_font(font_noto_sans_numeric_28); style_time.set_text_color(lvgl::Color::from_rgb((255, 255, 255))); @@ -47,7 +48,7 @@ fn main() -> Result<(), String> { time.set_width(240); time.set_height(240); - let mut bt = lvgl::Label::new(&mut screen); + let mut bt = Label::new(&mut screen); let mut style_bt = lvgl::Style::new(); style_bt.set_text_font(font_roboto_28); let style_power = style_bt.clone(); @@ -56,16 +57,16 @@ fn main() -> Result<(), String> { bt.set_height(80); bt.set_recolor(true); bt.set_text("#5794f2 \u{F293}#"); - bt.set_label_align(lvgl::LabelAlign::Left); + bt.set_label_align(LabelAlign::Left); bt.set_align(&mut screen, lvgl::Align::InTopLeft, 0, 0); - let mut power = lvgl::Label::new(&mut screen); + let mut power = Label::new(&mut screen); power.set_style(style_power); power.set_recolor(true); power.set_width(80); power.set_height(20); power.set_text("#fade2a 20%#"); - power.set_label_align(lvgl::LabelAlign::Right); + power.set_label_align(LabelAlign::Right); power.set_align(&mut screen, lvgl::Align::InTopRight, 0, 0); let threaded_ui = Arc::new(Mutex::new(ui)); diff --git a/lvgl/src/support.rs b/lvgl/src/support.rs index 980d7af..f488ea5 100644 --- a/lvgl/src/support.rs +++ b/lvgl/src/support.rs @@ -1,7 +1,6 @@ use alloc::boxed::Box; use core::mem; use core::ptr; -use cstr_core::CString; use embedded_graphics::pixelcolor::{Rgb565, Rgb888}; use lvgl_sys; @@ -134,70 +133,6 @@ macro_rules! define_object { }; } -define_object!(Button); - -impl Button { - pub fn new(parent: &mut C) -> Self - where - C: NativeObject, - { - let raw = unsafe { - let ptr = lvgl_sys::lv_btn_create(parent.raw().as_mut(), ptr::null_mut()); - ptr::NonNull::new_unchecked(ptr) - }; - let core = ObjectX::from_raw(raw); - Self { core } - } -} - -pub enum LabelAlign { - Left, - Center, - Right, - Auto, -} - -define_object!(Label); - -impl Label { - pub fn new(parent: &mut C) -> Self - where - C: NativeObject, - { - let raw = unsafe { - let ptr = lvgl_sys::lv_label_create(parent.raw().as_mut(), ptr::null_mut()); - ptr::NonNull::new_unchecked(ptr) - }; - let core = ObjectX::from_raw(raw); - Self { core } - } - - pub fn set_text(&mut self, text: &str) { - let text = CString::new(text).unwrap(); - unsafe { - lvgl_sys::lv_label_set_text(self.core.raw().as_mut(), text.as_ptr()); - } - } - - pub fn set_label_align(&mut self, align: LabelAlign) { - let align = match align { - LabelAlign::Left => lvgl_sys::LV_LABEL_ALIGN_LEFT, - LabelAlign::Center => lvgl_sys::LV_LABEL_ALIGN_CENTER, - LabelAlign::Right => lvgl_sys::LV_LABEL_ALIGN_RIGHT, - LabelAlign::Auto => lvgl_sys::LV_LABEL_ALIGN_AUTO, - } as lvgl_sys::lv_label_align_t; - unsafe { - 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 { Pretty, } diff --git a/lvgl/src/widgets/button.rs b/lvgl/src/widgets/button.rs new file mode 100644 index 0000000..00ceed9 --- /dev/null +++ b/lvgl/src/widgets/button.rs @@ -0,0 +1,18 @@ +use crate::{NativeObject, ObjectX}; +use core::ptr; + +define_object!(Button); + +impl Button { + pub fn new(parent: &mut C) -> Self + where + C: NativeObject, + { + let raw = unsafe { + let ptr = lvgl_sys::lv_btn_create(parent.raw().as_mut(), ptr::null_mut()); + ptr::NonNull::new_unchecked(ptr) + }; + let core = ObjectX::from_raw(raw); + Self { core } + } +} diff --git a/lvgl/src/widgets/label.rs b/lvgl/src/widgets/label.rs new file mode 100644 index 0000000..c8c573e --- /dev/null +++ b/lvgl/src/widgets/label.rs @@ -0,0 +1,51 @@ +use crate::{NativeObject, ObjectX}; +use core::ptr; +use cstr_core::CString; + +pub enum LabelAlign { + Left, + Center, + Right, + Auto, +} + +define_object!(Label); + +impl Label { + pub fn new(parent: &mut C) -> Self + where + C: NativeObject, + { + let raw = unsafe { + let ptr = lvgl_sys::lv_label_create(parent.raw().as_mut(), ptr::null_mut()); + ptr::NonNull::new_unchecked(ptr) + }; + let core = ObjectX::from_raw(raw); + Self { core } + } + + pub fn set_text(&mut self, text: &str) { + let text = CString::new(text).unwrap(); + unsafe { + lvgl_sys::lv_label_set_text(self.core.raw().as_mut(), text.as_ptr()); + } + } + + pub fn set_label_align(&mut self, align: LabelAlign) { + let align = match align { + LabelAlign::Left => lvgl_sys::LV_LABEL_ALIGN_LEFT, + LabelAlign::Center => lvgl_sys::LV_LABEL_ALIGN_CENTER, + LabelAlign::Right => lvgl_sys::LV_LABEL_ALIGN_RIGHT, + LabelAlign::Auto => lvgl_sys::LV_LABEL_ALIGN_AUTO, + } as lvgl_sys::lv_label_align_t; + unsafe { + 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); + } + } +} diff --git a/lvgl/src/widgets/mod.rs b/lvgl/src/widgets/mod.rs index af83335..4a01bb4 100644 --- a/lvgl/src/widgets/mod.rs +++ b/lvgl/src/widgets/mod.rs @@ -1,3 +1,7 @@ mod bar; +mod button; +mod label; -pub use self::bar::Bar; +pub use self::bar::*; +pub use self::button::*; +pub use self::label::*;