Update Bar example
This commit is contained in:
parent
c494d8fac0
commit
52e31496c6
5 changed files with 54 additions and 100 deletions
|
@ -3,8 +3,9 @@ use embedded_graphics::prelude::*;
|
|||
use embedded_graphics_simulator::{
|
||||
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
|
||||
};
|
||||
use lvgl::widgets::{Bar, BarComponent, Label, LabelAlign};
|
||||
use lvgl::{self, Align, Animation, Border, Color, DisplayDriver, Event, Object, Style, UI};
|
||||
use lvgl::style::{State, Style};
|
||||
use lvgl::widgets::{Bar, BarPart, Label, LabelAlign};
|
||||
use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, ObjPart, Object, UI};
|
||||
use lvgl_sys;
|
||||
use std::sync::{mpsc, Arc, Mutex};
|
||||
use std::thread::sleep;
|
||||
|
@ -29,32 +30,20 @@ fn main() -> Result<(), String> {
|
|||
let mut screen = ui.scr_act();
|
||||
|
||||
let mut screen_style = Style::default();
|
||||
screen_style.set_body_main_color(Color::from_rgb((255, 255, 255)));
|
||||
screen_style.set_body_grad_color(Color::from_rgb((255, 255, 255)));
|
||||
screen_style.set_body_radius(0);
|
||||
screen.set_style(screen_style);
|
||||
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
|
||||
screen_style.set_radius(State::DEFAULT, 0);
|
||||
screen.add_style(ObjPart::Main, screen_style);
|
||||
|
||||
// Create the bar object
|
||||
let mut bar = Bar::new(&mut screen);
|
||||
bar.set_size(175, 50);
|
||||
bar.set_align(&mut screen, Align::Center, 0, 20);
|
||||
bar.set_size(175, 20);
|
||||
bar.set_align(&mut screen, Align::Center, 0, 10);
|
||||
bar.set_range(0, 100);
|
||||
bar.set_value(0, Animation::OFF);
|
||||
|
||||
// Set the indicator style for the bar object
|
||||
// // Set the indicator style for the bar object
|
||||
let mut ind_style = Style::default();
|
||||
ind_style.set_body_main_color(Color::from_rgb((100, 245, 0)));
|
||||
ind_style.set_body_grad_color(Color::from_rgb((100, 245, 0)));
|
||||
bar.set_bar_style(BarComponent::Indicator, ind_style);
|
||||
|
||||
// Set the background style for the bar object
|
||||
let mut bg_style = Style::default();
|
||||
bg_style.set_body_grad_color(Color::from_rgb((255, 255, 255)));
|
||||
bg_style.set_body_main_color(Color::from_rgb((255, 255, 255)));
|
||||
bg_style.set_body_radius(0);
|
||||
bg_style.set_body_border_part(Border::TOP);
|
||||
bg_style.set_body_border_width(1);
|
||||
bar.set_bar_style(BarComponent::Background, bg_style);
|
||||
ind_style.set_bg_color(State::DEFAULT, Color::from_rgb((100, 245, 100)));
|
||||
bar.add_style(BarPart::Indicator, ind_style);
|
||||
|
||||
let mut loading_lbl = Label::new(&mut screen);
|
||||
loading_lbl.set_text("Loading...");
|
||||
|
@ -62,8 +51,8 @@ fn main() -> Result<(), String> {
|
|||
loading_lbl.set_label_align(LabelAlign::Center);
|
||||
|
||||
let mut loading_style = Style::default();
|
||||
loading_style.set_text_color(Color::from_rgb((0, 0, 0)));
|
||||
loading_lbl.set_style(loading_style);
|
||||
loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
|
||||
loading_lbl.add_style(ObjPart::Main, loading_style);
|
||||
|
||||
let threaded_ui = Arc::new(Mutex::new(ui));
|
||||
|
||||
|
|
|
@ -1,3 +1 @@
|
|||
pub mod style;
|
||||
|
||||
use style::*;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::Color;
|
||||
use alloc::boxed::Box;
|
||||
use core::borrow::BorrowMut;
|
||||
use core::mem;
|
||||
use cstr_core::CString;
|
||||
use lvgl_sys;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::lv_core::style::Style;
|
||||
use alloc::boxed::Box;
|
||||
use core::convert::{TryFrom, TryInto};
|
||||
use core::mem;
|
||||
use core::ptr;
|
||||
use core::ptr::NonNull;
|
||||
use embedded_graphics::pixelcolor::{Rgb565, Rgb888};
|
||||
|
@ -32,6 +31,7 @@ impl NativeObject for GenericObject {
|
|||
/// A wrapper for all LittlevGL common operations on generic objects.
|
||||
pub trait Object: NativeObject {
|
||||
type SpecialEvent;
|
||||
type Part: Into<u8>;
|
||||
|
||||
/// Construct an instance of the object from a raw pointer.
|
||||
///
|
||||
|
@ -40,6 +40,12 @@ pub trait Object: NativeObject {
|
|||
///
|
||||
unsafe fn from_raw(raw_pointer: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self;
|
||||
|
||||
fn add_style(&mut self, part: Self::Part, style: Style) {
|
||||
unsafe {
|
||||
lvgl_sys::lv_obj_add_style(self.raw().as_mut(), part.into(), Box::into_raw(style.raw));
|
||||
};
|
||||
}
|
||||
|
||||
fn set_pos(&mut self, x: i16, y: i16) {
|
||||
unsafe {
|
||||
lvgl_sys::lv_obj_set_pos(
|
||||
|
@ -109,26 +115,31 @@ pub trait Object: NativeObject {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn add_style(&mut self, style: Style) {
|
||||
unsafe {
|
||||
lvgl_sys::lv_obj_add_style(
|
||||
self.raw().as_mut(),
|
||||
lvgl_sys::LV_OBJ_PART_MAIN as u8,
|
||||
Box::into_raw(style.raw),
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Object for GenericObject {
|
||||
type SpecialEvent = ();
|
||||
type Part = ObjPart;
|
||||
|
||||
unsafe fn from_raw(raw: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
|
||||
Self { raw: raw.as_ptr() }
|
||||
}
|
||||
}
|
||||
|
||||
pub enum ObjPart {
|
||||
Main,
|
||||
All,
|
||||
}
|
||||
|
||||
impl Into<u8> for ObjPart {
|
||||
fn into(self) -> u8 {
|
||||
match self {
|
||||
ObjPart::Main => lvgl_sys::LV_OBJ_PART_MAIN as u8,
|
||||
ObjPart::All => lvgl_sys::LV_OBJ_PART_ALL as u8,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GenericObject {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
@ -139,48 +150,18 @@ impl Default for GenericObject {
|
|||
|
||||
macro_rules! define_object {
|
||||
($item:ident) => {
|
||||
pub struct $item {
|
||||
core: $crate::support::GenericObject,
|
||||
}
|
||||
|
||||
impl $item {
|
||||
pub fn on_event<F>(&mut self, f: F)
|
||||
where
|
||||
F: FnMut(
|
||||
Self,
|
||||
$crate::support::Event<<Self as $crate::support::Object>::SpecialEvent>,
|
||||
),
|
||||
{
|
||||
unsafe {
|
||||
let mut raw = self.raw();
|
||||
let obj = raw.as_mut();
|
||||
let user_closure = alloc::boxed::Box::new(f);
|
||||
obj.user_data = alloc::boxed::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>),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl $crate::support::NativeObject for $item {
|
||||
fn raw(&self) -> core::ptr::NonNull<lvgl_sys::lv_obj_t> {
|
||||
self.core.raw()
|
||||
}
|
||||
}
|
||||
|
||||
impl $crate::support::Object for $item {
|
||||
type SpecialEvent = ();
|
||||
|
||||
unsafe fn from_raw(raw_pointer: core::ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
|
||||
Self {
|
||||
core: $crate::support::GenericObject::from_raw(raw_pointer),
|
||||
}
|
||||
}
|
||||
}
|
||||
define_object!($item, event = (), part = $crate::support::ObjPart);
|
||||
};
|
||||
($item:ident, $event_type:ident) => {
|
||||
($item:ident, event = $event_type:ty) => {
|
||||
define_object!($item, event = $event_type, part = $crate::support::ObjPart);
|
||||
};
|
||||
($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::support::GenericObject,
|
||||
}
|
||||
|
@ -214,6 +195,7 @@ macro_rules! define_object {
|
|||
|
||||
impl $crate::support::Object 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 {
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
use crate::style::Style;
|
||||
use crate::support::{Animation, GenericObject, NativeObject};
|
||||
use crate::Object;
|
||||
use alloc::boxed::Box;
|
||||
use core::ptr;
|
||||
|
||||
define_object!(Bar);
|
||||
define_object!(Bar, part = BarPart);
|
||||
|
||||
impl Bar {
|
||||
pub fn new<C>(parent: &mut C) -> Self
|
||||
|
@ -32,22 +30,10 @@ impl Bar {
|
|||
lvgl_sys::lv_bar_set_value(self.core.raw().as_mut(), value, anim.into());
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the style, for the given `BarComponent`
|
||||
pub fn set_bar_style(&mut self, style: Style) {
|
||||
let boxed = style.raw;
|
||||
unsafe {
|
||||
lvgl_sys::lv_obj_add_style(
|
||||
self.core.raw().as_mut(),
|
||||
lvgl_sys::LV_OBJ_PART_MAIN as u8,
|
||||
Box::into_raw(boxed),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The different components, of a bar object.
|
||||
pub enum BarComponent {
|
||||
/// The different parts, of a bar object.
|
||||
pub enum BarPart {
|
||||
/// The background of the bar.
|
||||
Background,
|
||||
/// The indicator of the bar.
|
||||
|
@ -55,11 +41,11 @@ pub enum BarComponent {
|
|||
Indicator,
|
||||
}
|
||||
|
||||
impl From<BarComponent> for lvgl_sys::lv_bar_style_t {
|
||||
fn from(component: BarComponent) -> Self {
|
||||
impl From<BarPart> for u8 {
|
||||
fn from(component: BarPart) -> Self {
|
||||
match component {
|
||||
BarComponent::Background => lvgl_sys::LV_BAR_STYLE_BG as u8,
|
||||
BarComponent::Indicator => lvgl_sys::LV_BAR_STYLE_INDIC as u8,
|
||||
BarPart::Background => lvgl_sys::LV_BAR_PART_BG as u8,
|
||||
BarPart::Indicator => lvgl_sys::LV_BAR_PART_INDIC as u8,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue