Generate code for styles properties

This commit is contained in:
Rafael Caricio 2020-06-04 02:00:31 +02:00 committed by Rafael Carício
parent 93a7fd4316
commit c494d8fac0
11 changed files with 1325 additions and 131 deletions

View file

@ -2,5 +2,6 @@
members = [
"lvgl",
"lvgl-sys",
"lvgl-codegen",
"examples"
]

View file

@ -94,35 +94,36 @@ List of LVGL features that impacts the library usage in general.
- [ ] Arc (lv_arc)
- [x] Bar (lv_bar)
- [x] Button (lv_btn)
- [ ] Button matrix (lv_btnm)
- [ ] Button matrix (lv_btnmatrix)
- [ ] Calendar (lv_calendar)
- [ ] Canvas (lv_canvas)
- [ ] Checkbox (lv_cb)
- [ ] Chart (lv_chart)
- [ ] Container (lv_cont)
- [ ] Color picker (lv_cpicker)
- [ ] Drop-down list (lv_ddlist)
- [ ] Drop-down list (lv_dropdown)
- [ ] Gauge (lv_gauge)
- [ ] Image (lv_img)
- [ ] Image button (lv_imgbtn)
- [ ] Keyboard (lv_kb)
- [ ] Keyboard (lv_keyboard)
- [x] Label (lv_label)
- [ ] LED (lv_led)
- [ ] Line (lv_line)
- [ ] List (lv_list)
- [ ] Line meter (lv_lmeter)
- [ ] Message box (lv_mbox)
- [ ] Message box (lv_msdbox)
- [ ] Object mask (lv_objmask)
- [ ] Page (lv_page)
- [ ] Preloader (lv_preload)
- [ ] Roller (lv_roller)
- [ ] Slider (lv_slider)
- [ ] Spinbox (lv_spinbox)
- [ ] Switch (lv_sw)
- [ ] Spinner (lv_spinner)
- [ ] Switch (lv_switch)
- [ ] Table (lv_table)
- [ ] Tabview (lv_tabview)
- [ ] Text area (lv_ta)
- [ ] Text area (lv_textarea)
- [ ] Tile view (lv_tileview)
- [ ] Window (lv_win)
- [ ] Window (lv_wn- [ ]
Widgets currently implemented might have some missing features. If the widget you want to use is not exposed or
is missing a feature you want to make use, please send a Pull Request or open an issue.

11
lvgl-codegen/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "lvgl-codegen"
version = "0.1.0"
authors = ["Rafael Caricio <rafael@caricio.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tera = "1.3.0"
regex = "1.3.9"

35
lvgl-codegen/src/main.rs Normal file
View file

@ -0,0 +1,35 @@
use regex::Regex;
use tera::{Context, Tera};
fn main() {
let re = Regex::new(r"\((?P<prop_name>[^,]+), (?P<func_name>[^,]+), (?P<value_type>[^,]+), (?P<style_type>[^)]+), [a-z]+\)").unwrap();
let input = include_str!("../../lvgl-sys/vendor/lvgl/src/lv_core/lv_obj_style_dec.h");
let mut tera = Tera::default();
tera.add_raw_template("styles.rs", include_str!("../templates/style.rs.j2"))
.unwrap();
for line in input.lines() {
if !line.starts_with("_LV_OBJ_STYLE_SET_GET_DECLARE") {
continue;
}
if let Some(cap) = re.captures(line) {
let style_type = cap.get(4).unwrap().as_str().to_string();
if style_type.eq("_ptr") {
// Just a few, we will take care of this manually.
continue;
}
let value_type = cap.get(3).unwrap().as_str().to_string();
let mut ctx = Context::new();
ctx.insert("prop_name", cap.get(1).unwrap().as_str());
ctx.insert("func_name", cap.get(2).unwrap().as_str());
ctx.insert("value_type", value_type.as_str());
ctx.insert("style_type", style_type.as_str());
println!("{}", tera.render("styles.rs", &ctx).unwrap());
}
}
}

View file

@ -0,0 +1,33 @@
pub fn set_{{func_name}}(&mut self, state: State,
{% if style_type == "_color" %}
value: Color
{% elif style_type == "_int" %}
value: i16
{% elif style_type == "_ptr" %}
value: Any
{% elif style_type == "_opa" %}
value: u8
{% endif %}
) {
let native_state: u32 = state.bits;
unsafe {
lvgl_sys::_lv_style_set{{style_type}}(
self.raw.as_mut(),
(lvgl_sys::LV_STYLE_{{prop_name}}
| (native_state << lvgl_sys::LV_STYLE_STATE_POS as u32)) as u16,
{% if style_type == "_color" %}
value.raw,
{% elif style_type == "_int" %}
value
{% elif style_type == "_opa" %}
value
{% elif style_type == "_ptr" %}
value.into()
{% endif %}
);
}
}

View file

@ -47,11 +47,11 @@ fn main() {
add_c_files(&mut cfg, vendor_src.join("lv_core"));
add_c_files(&mut cfg, vendor_src.join("lv_draw"));
add_c_files(&mut cfg, vendor_src.join("lv_font"));
add_c_files(&mut cfg, vendor_src.join("lv_gpu"));
add_c_files(&mut cfg, vendor_src.join("lv_hal"));
add_c_files(&mut cfg, vendor_src.join("lv_misc"));
add_c_files(&mut cfg, vendor_src.join("lv_objx"));
add_c_files(&mut cfg, vendor_src.join("lv_themes"));
add_c_files(&mut cfg, vendor_src.join("lv_themes"));
add_c_files(&mut cfg, vendor_src.join("lv_widgets"));
add_c_files(&mut cfg, &lv_config_dir);
add_c_files(&mut cfg, &shims_dir);

View file

@ -8,8 +8,10 @@ mod display;
mod global;
#[macro_use]
mod support;
mod lv_core;
pub mod widgets;
pub use display::DisplayDriver;
pub use global::{LvError, UI};
pub use lv_core::*;
pub use support::*;

3
lvgl/src/lv_core/mod.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod style;
use style::*;

1215
lvgl/src/lv_core/style.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,4 @@
use crate::lv_core::style::Style;
use alloc::boxed::Box;
use core::convert::{TryFrom, TryInto};
use core::mem;
@ -109,10 +110,13 @@ pub trait Object: NativeObject {
}
}
fn set_style(&mut self, style: Style) {
fn add_style(&mut self, style: Style) {
unsafe {
let boxed = Box::new(style.raw);
lvgl_sys::lv_obj_set_style(self.raw().as_mut(), Box::into_raw(boxed));
lvgl_sys::lv_obj_add_style(
self.raw().as_mut(),
lvgl_sys::LV_OBJ_PART_MAIN as u8,
Box::into_raw(style.raw),
);
};
}
}
@ -220,121 +224,9 @@ macro_rules! define_object {
};
}
pub enum Themes {
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(crate) raw: lvgl_sys::lv_style_t,
}
impl Style {
/// Object's main background color
pub fn set_body_main_color(&mut self, color: Color) {
self.raw.body.main_color = color.raw;
}
/// Second color. If not equal to `set_body_main_color` a gradient will be drawn for the background.
pub fn set_body_grad_color(&mut self, color: Color) {
self.raw.body.grad_color = color.raw;
}
/// Body radius for rounded corners
pub fn set_body_radius(&mut self, radius: i16) {
self.raw.body.radius = radius;
}
/// Border color
pub fn set_body_border_color(&mut self, color: Color) {
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 Default for Style {
fn default() -> Self {
let raw = unsafe {
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);
native_style.assume_init()
};
Self { raw }
}
}
impl Clone for Style {
fn clone(&self) -> Self {
let mut native_style = mem::MaybeUninit::<lvgl_sys::lv_style_t>::uninit();
unsafe {
lvgl_sys::lv_style_copy(
native_style.as_mut_ptr(),
&self.raw as *const lvgl_sys::lv_style_t,
);
Self {
raw: native_style.assume_init(),
}
}
}
}
#[derive(Clone)]
pub struct Color {
raw: lvgl_sys::lv_color_t,
pub(crate) raw: lvgl_sys::lv_color_t,
}
impl Color {

View file

@ -1,4 +1,5 @@
use crate::support::{Animation, GenericObject, NativeObject, Style};
use crate::style::Style;
use crate::support::{Animation, GenericObject, NativeObject};
use crate::Object;
use alloc::boxed::Box;
use core::ptr;
@ -33,12 +34,12 @@ impl Bar {
}
/// Set the style, for the given `BarComponent`
pub fn set_bar_style(&mut self, component: BarComponent, style: Style) {
let boxed = Box::new(style.raw);
pub fn set_bar_style(&mut self, style: Style) {
let boxed = style.raw;
unsafe {
lvgl_sys::lv_bar_set_style(
lvgl_sys::lv_obj_add_style(
self.core.raw().as_mut(),
component.into(),
lvgl_sys::LV_OBJ_PART_MAIN as u8,
Box::into_raw(boxed),
);
}