Support more styling options

This commit is contained in:
Rafael Caricio 2020-06-01 22:45:33 +02:00
parent 3faec0e2b1
commit 532cf3cf1b
4 changed files with 71 additions and 26 deletions

View file

@ -3,8 +3,8 @@ use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{ use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
}; };
use lvgl::widgets::{Bar, BarComponent, Label}; use lvgl::widgets::{Bar, BarComponent, Label, LabelAlign};
use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, Object, Style, UI}; use lvgl::{self, Align, Animation, Border, Color, DisplayDriver, Event, Object, Style, UI};
use lvgl_sys; use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex}; use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep; use std::thread::sleep;
@ -29,15 +29,15 @@ fn main() -> Result<(), String> {
let mut screen = ui.scr_act(); let mut screen = ui.scr_act();
let mut screen_style = Style::new(); let mut screen_style = Style::new();
screen_style.set_body_main_color(Color::from_rgb((0, 0, 0))); screen_style.set_body_main_color(Color::from_rgb((255, 255, 255)));
screen_style.set_body_grad_color(Color::from_rgb((0, 0, 0))); screen_style.set_body_grad_color(Color::from_rgb((255, 255, 255)));
screen_style.set_body_radius(0); screen_style.set_body_radius(0);
screen.set_style(screen_style); screen.set_style(screen_style);
// Create the bar object // Create the bar object
let mut bar = Bar::new(&mut screen); let mut bar = Bar::new(&mut screen);
bar.set_size(175, 50); bar.set_size(175, 50);
bar.set_align(&mut screen, Align::Center, 0, 0); bar.set_align(&mut screen, Align::Center, 0, 20);
bar.set_range(0, 100); bar.set_range(0, 100);
bar.set_value(0, Animation::OFF); bar.set_value(0, Animation::OFF);
@ -52,21 +52,17 @@ fn main() -> Result<(), String> {
bg_style.set_body_grad_color(Color::from_rgb((255, 255, 255))); 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_main_color(Color::from_rgb((255, 255, 255)));
bg_style.set_body_radius(0); 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); bar.set_bar_style(BarComponent::Background, bg_style);
let mut loading_lbl = Label::new(&mut screen); let mut loading_lbl = Label::new(&mut screen);
loading_lbl.set_text("Loading..."); loading_lbl.set_text("Loading...");
loading_lbl.set_align(&mut bar, Align::OutTopMid, 0, -10); loading_lbl.set_align(&mut bar, Align::OutTopMid, 0, -10);
loading_lbl.set_label_align(LabelAlign::Center);
loading_lbl.on_event(|mut this, event| {
this.set_text("Loaded!");
if let lvgl::Event::Clicked = event {
println!("Loaded!");
}
});
let mut loading_style = Style::new(); let mut loading_style = Style::new();
loading_style.set_text_color(Color::from_rgb((255, 255, 255))); loading_style.set_text_color(Color::from_rgb((0, 0, 0)));
loading_lbl.set_style(loading_style); loading_lbl.set_style(loading_style);
let threaded_ui = Arc::new(Mutex::new(ui)); let threaded_ui = Arc::new(Mutex::new(ui));

View file

@ -15,3 +15,4 @@ lvgl-sys = { path = "../lvgl-sys", version = "0.2.0" }
cty = "0.2.1" cty = "0.2.1"
embedded-graphics = "0.6.2" embedded-graphics = "0.6.2"
cstr_core = { version = "0.2.0", default-features = false, features = ["alloc"] } cstr_core = { version = "0.2.0", default-features = false, features = ["alloc"] }
bitflags = "1.2.1"

View file

@ -1,6 +1,8 @@
#![no_std] #![no_std]
extern crate alloc; extern crate alloc;
#[macro_use]
extern crate bitflags;
mod display; mod display;
mod global; mod global;

View file

@ -216,6 +216,17 @@ pub enum Themes {
Pretty, Pretty,
} }
bitflags! {
pub struct Border: u32 {
const NONE = lvgl_sys::LV_BORDER_NONE;
const BOTTOM = lvgl_sys::LV_BORDER_BOTTOM;
const TOP = lvgl_sys::LV_BORDER_TOP;
const LEFT = lvgl_sys::LV_BORDER_LEFT;
const RIGHT = lvgl_sys::LV_BORDER_RIGHT;
const FULL = lvgl_sys::LV_BORDER_FULL;
}
}
pub struct Style { pub struct Style {
pub(crate) raw: lvgl_sys::lv_style_t, pub(crate) raw: lvgl_sys::lv_style_t,
} }
@ -230,7 +241,7 @@ impl Style {
Self { raw } Self { raw }
} }
/// Object's main background color. /// Object's main background color
pub fn set_body_main_color(&mut self, color: Color) { pub fn set_body_main_color(&mut self, color: Color) {
self.raw.body.main_color = color.raw; self.raw.body.main_color = color.raw;
} }
@ -240,25 +251,60 @@ impl Style {
self.raw.body.grad_color = color.raw; self.raw.body.grad_color = color.raw;
} }
/// Text color. /// Body radius for rounded corners
pub fn set_text_color(&mut self, color: Color) {
self.raw.text.color = color.raw;
}
/// Font used for displaying the text.
pub fn set_text_font(&mut self, font: &lvgl_sys::lv_font_t) {
self.raw.text.font = font;
}
/// Body radius for rounded corners.
pub fn set_body_radius(&mut self, radius: i16) { pub fn set_body_radius(&mut self, radius: i16) {
self.raw.body.radius = radius; self.raw.body.radius = radius;
} }
/// Border color. /// Border color
pub fn set_body_border_color(&mut self, color: Color) { pub fn set_body_border_color(&mut self, color: Color) {
self.raw.body.border.color = color.raw; self.raw.body.border.color = color.raw;
} }
/// Border opacity
pub fn set_body_border_opa(&mut self, opa: u8) {
self.raw.body.border.opa = opa;
}
/// Border width
pub fn set_body_border_width(&mut self, width: i16) {
self.raw.body.border.width = width;
}
/// Which borders to draw
pub fn set_body_border_part(&mut self, part: Border) {
self.raw.body.border.part = part.bits as u8;
}
/// Text color
pub fn set_text_color(&mut self, color: Color) {
self.raw.text.color = color.raw;
}
/// Font used for displaying the text
pub fn set_text_font(&mut self, font: &lvgl_sys::lv_font_t) {
self.raw.text.font = font;
}
/// Space between letters
pub fn set_text_letter_space(&mut self, space: i16) {
self.raw.text.letter_space = space;
}
/// Space between lines (vertical)
pub fn set_text_line_space(&mut self, space: i16) {
self.raw.text.line_space = space;
}
/// Text opacity
pub fn set_text_opa(&mut self, opa: u8) {
self.raw.text.opa = opa;
}
/// Text selection background color
pub fn set_text_sel_color(&mut self, color: Color) {
self.raw.text.sel_color = color.raw;
}
} }
impl Clone for Style { impl Clone for Style {