Use traits to abstract behaviour

This commit is contained in:
Rafael Caricio 2020-04-13 16:15:50 +02:00
parent f8f84002c6
commit 79c7b23bed
2 changed files with 37 additions and 74 deletions

View file

@ -1,8 +1,8 @@
use crate::objx::Object;
use crate::objx::ObjectX;
use core::ptr;
pub fn get_active_screen() -> Object {
pub fn get_active_screen() -> ObjectX {
let raw =
unsafe { ptr::NonNull::new_unchecked(lvgl_sys::lv_disp_get_scr_act(ptr::null_mut())) };
Object::new(raw)
ObjectX::new(raw)
}

View file

@ -6,16 +6,24 @@ pub trait Container {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t>;
}
pub struct Object {
pub struct ObjectX {
raw: ptr::NonNull<lvgl_sys::lv_obj_t>,
}
impl Object {
impl ObjectX {
pub(crate) fn new(raw: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
Self { raw }
}
}
pub fn set_pos(&mut self, x: i16, y: i16) {
impl Container for ObjectX {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
unsafe { ptr::NonNull::new_unchecked(self.raw.as_ptr()) }
}
}
pub trait Object: Container {
fn set_pos(&mut self, x: i16, y: i16) {
unsafe {
lvgl_sys::lv_obj_set_pos(
self.raw().as_mut(),
@ -25,7 +33,7 @@ impl Object {
}
}
pub fn set_size(&mut self, w: i16, h: i16) {
fn set_size(&mut self, w: i16, h: i16) {
unsafe {
lvgl_sys::lv_obj_set_size(
self.raw().as_mut(),
@ -35,26 +43,26 @@ impl Object {
}
}
pub fn set_width(&mut self, w: u32) {
fn set_width(&mut self, w: u32) {
unsafe {
lvgl_sys::lv_obj_set_width(
self.raw().as_mut(),
w as lvgl_sys::lv_coord_t,
);
lvgl_sys::lv_obj_set_width(self.raw().as_mut(), w as lvgl_sys::lv_coord_t);
}
}
pub fn set_height(&mut self, h: u32) {
fn set_height(&mut self, h: u32) {
unsafe {
lvgl_sys::lv_obj_set_height(
self.raw().as_mut(),
h as lvgl_sys::lv_coord_t,
);
lvgl_sys::lv_obj_set_height(self.raw().as_mut(), h as lvgl_sys::lv_coord_t);
}
}
pub fn set_object_align(&mut self, base: &mut dyn Container, align: ObjectAlign, x_mod: i32, y_mod: i32) {
let align= match align {
fn set_object_align(
&mut self,
base: &mut dyn Container,
align: ObjectAlign,
x_mod: i32,
y_mod: i32,
) {
let align = match align {
ObjectAlign::Center => lvgl_sys::LV_ALIGN_CENTER,
ObjectAlign::InTopLeft => lvgl_sys::LV_ALIGN_IN_TOP_LEFT,
ObjectAlign::InTopMid => lvgl_sys::LV_ALIGN_IN_TOP_MID,
@ -83,18 +91,12 @@ impl Object {
base.raw().as_mut(),
align,
x_mod as lvgl_sys::lv_coord_t,
y_mod as lvgl_sys::lv_coord_t
y_mod as lvgl_sys::lv_coord_t,
);
}
}
}
impl Container for Object {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
unsafe { ptr::NonNull::new_unchecked(self.raw.as_ptr()) }
}
}
pub enum ObjectAlign {
Center,
InTopLeft,
@ -120,7 +122,7 @@ pub enum ObjectAlign {
}
pub struct Button {
core: Object,
core: ObjectX,
}
impl Button {
@ -129,29 +131,9 @@ impl Button {
let ptr = lvgl_sys::lv_btn_create(parent.raw().as_mut(), ptr::null_mut());
ptr::NonNull::new_unchecked(ptr)
};
let core = Object::new(raw);
let core = ObjectX::new(raw);
Self { core }
}
pub fn set_pos(&mut self, x: i16, y: i16) {
self.core.set_pos(x, y)
}
pub fn set_size(&mut self, w: i16, h: i16) {
self.core.set_size(w, h)
}
pub fn set_width(&mut self, w: u32) {
self.core.set_width(w)
}
pub fn set_height(&mut self, h: u32) {
self.core.set_height(h)
}
pub fn set_object_align(&mut self, base: &mut dyn Container, align: ObjectAlign, x_mod: i32, y_mod: i32) {
self.core.set_object_align(base, align, x_mod, y_mod);
}
}
impl Container for Button {
@ -160,6 +142,8 @@ impl Container for Button {
}
}
impl Object for Button {}
pub enum LabelAlign {
Left,
Center,
@ -168,7 +152,7 @@ pub enum LabelAlign {
}
pub struct Label {
core: Object,
core: ObjectX,
}
impl Label {
@ -177,7 +161,7 @@ impl Label {
let ptr = lvgl_sys::lv_label_create(parent.raw().as_mut(), ptr::null_mut());
ptr::NonNull::new_unchecked(ptr)
};
let core = Object::new(raw);
let core = ObjectX::new(raw);
Self { core }
}
@ -198,32 +182,9 @@ impl Label {
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
);
lvgl_sys::lv_label_set_align(self.core.raw().as_mut(), align);
}
}
pub fn set_pos(&mut self, x: i16, y: i16) {
self.core.set_pos(x, y)
}
pub fn set_size(&mut self, w: i16, h: i16) {
self.core.set_size(w, h)
}
pub fn set_width(&mut self, w: u32) {
self.core.set_width(w)
}
pub fn set_height(&mut self, h: u32) {
self.core.set_height(h)
}
pub fn set_object_align(&mut self, base: &mut dyn Container, align: ObjectAlign, x_mod: i32, y_mod: i32) {
self.core.set_object_align(base, align, x_mod, y_mod);
}
}
impl Container for Label {
@ -231,3 +192,5 @@ impl Container for Label {
self.core.raw()
}
}
impl Object for Label {}