use oxivgl_sys::*;
use crate::widgets::Part;
use super::area::Area;
use super::layer::Layer;
pub struct DrawTask {
ptr: *mut lv_draw_task_t,
}
impl core::fmt::Debug for DrawTask {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("DrawTask").finish_non_exhaustive()
}
}
impl DrawTask {
pub(crate) fn from_raw(ptr: *mut lv_draw_task_t) -> Self {
Self { ptr }
}
pub fn base(&self) -> DrawDscBase {
let dsc = unsafe { lv_draw_task_get_draw_dsc(self.ptr) };
let base = unsafe { &*(dsc as *const lv_draw_dsc_base_t) };
DrawDscBase { part: Part::from_raw(base.part), id1: base.id1, id2: base.id2 }
}
pub fn label_dsc(&self) -> Option<DrawLabelDsc> {
let dsc = unsafe { lv_draw_task_get_label_dsc(self.ptr) };
if dsc.is_null() { None } else { Some(DrawLabelDsc { ptr: dsc }) }
}
pub fn area(&self) -> Area {
let task = unsafe { &*self.ptr };
task.area.into()
}
pub fn set_area(&self, area: Area) {
unsafe { (*self.ptr).area = area.into() };
}
pub fn fill_dsc(&self) -> Option<DrawFillDsc> {
let dsc = unsafe { lv_draw_task_get_fill_dsc(self.ptr) };
if dsc.is_null() { None } else { Some(DrawFillDsc { ptr: dsc }) }
}
pub fn with_fill_dsc<R>(&self, f: impl FnOnce(&DrawFillDsc) -> R) -> Option<R> {
self.fill_dsc().map(|dsc| f(&dsc))
}
pub fn with_label_dsc<R>(&self, f: impl FnOnce(&DrawLabelDsc) -> R) -> Option<R> {
self.label_dsc().map(|dsc| f(&dsc))
}
pub fn box_shadow_dsc(&self) -> Option<DrawBoxShadowDsc> {
let dsc = unsafe { lv_draw_task_get_box_shadow_dsc(self.ptr) };
if dsc.is_null() { None } else { Some(DrawBoxShadowDsc { ptr: dsc }) }
}
pub fn with_box_shadow_dsc<R>(&self, f: impl FnOnce(&DrawBoxShadowDsc) -> R) -> Option<R> {
self.box_shadow_dsc().map(|dsc| f(&dsc))
}
pub fn task_type(&self) -> u32 {
unsafe { lv_draw_task_get_type(self.ptr) as u32 }
}
pub fn layer(&self) -> Option<Layer> {
let dsc = unsafe { lv_draw_task_get_draw_dsc(self.ptr) };
if dsc.is_null() {
return None;
}
let base = unsafe { &*(dsc as *const lv_draw_dsc_base_t) };
if base.layer.is_null() { None } else { Some(Layer::from_raw(base.layer)) }
}
}
#[derive(Clone, Copy, Debug)]
pub struct DrawDscBase {
pub part: Part,
pub id1: u32,
pub id2: u32,
}
pub struct DrawLabelDsc {
ptr: *mut lv_draw_label_dsc_t,
}
impl core::fmt::Debug for DrawLabelDsc {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("DrawLabelDsc").finish_non_exhaustive()
}
}
impl DrawLabelDsc {
pub fn color(&self) -> lv_color_t {
unsafe { (*self.ptr).color }
}
pub fn set_color(&self, color: lv_color_t) {
unsafe { (*self.ptr).color = color };
}
pub fn text(&self) -> Option<&str> {
let text_ptr = unsafe { (*self.ptr).text };
if text_ptr.is_null() {
return None;
}
let cstr = unsafe { core::ffi::CStr::from_ptr(text_ptr) };
cstr.to_str().ok()
}
pub fn set_text(&self, text: &str) {
let dsc = unsafe { &mut *self.ptr };
if dsc.text_local() != 0 {
unsafe { lv_free(dsc.text as *mut core::ffi::c_void) };
}
let mut buf = [0u8; 32];
let len = text.len().min(buf.len() - 1);
buf[..len].copy_from_slice(&text.as_bytes()[..len]);
buf[len] = 0;
dsc.text = unsafe { lv_strdup(buf.as_ptr() as *const core::ffi::c_char) };
dsc.set_text_local(1);
}
pub fn set_align(&self, align: crate::widgets::TextAlign) {
unsafe { (*self.ptr).align = align as lv_text_align_t };
}
pub fn opa(&self) -> u8 {
unsafe { (*self.ptr).opa }
}
pub fn set_opa(&self, opa: u8) {
unsafe { (*self.ptr).opa = opa };
}
pub fn font(&self) -> *const lv_font_t {
unsafe { (*self.ptr).font }
}
pub fn text_size(&self, text: &str) -> (i32, i32) {
let mut buf = [0u8; 32];
let len = text.len().min(buf.len() - 1);
buf[..len].copy_from_slice(&text.as_bytes()[..len]);
buf[len] = 0;
let mut size: lv_point_t = unsafe { core::mem::zeroed() };
unsafe {
lv_text_get_size(
&mut size,
buf.as_ptr() as *const core::ffi::c_char,
(*self.ptr).font,
0, 0, 1000, lv_text_flag_t_LV_TEXT_FLAG_NONE,
);
}
(size.x, size.y)
}
}
pub struct DrawFillDsc {
ptr: *mut lv_draw_fill_dsc_t,
}
impl core::fmt::Debug for DrawFillDsc {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("DrawFillDsc").finish_non_exhaustive()
}
}
impl DrawFillDsc {
pub fn color(&self) -> lv_color_t {
unsafe { (*self.ptr).color }
}
pub fn set_color(&self, color: lv_color_t) {
unsafe { (*self.ptr).color = color };
}
pub fn opa(&self) -> u8 {
unsafe { (*self.ptr).opa }
}
pub fn set_opa(&self, opa: u8) {
unsafe { (*self.ptr).opa = opa };
}
pub fn radius(&self) -> i32 {
unsafe { (*self.ptr).radius }
}
pub fn set_radius(&self, radius: i32) {
unsafe { (*self.ptr).radius = radius };
}
}
pub struct DrawBoxShadowDsc {
ptr: *mut lv_draw_box_shadow_dsc_t,
}
impl core::fmt::Debug for DrawBoxShadowDsc {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("DrawBoxShadowDsc").finish_non_exhaustive()
}
}
impl DrawBoxShadowDsc {
pub fn set_width(&self, width: i32) {
unsafe { (*self.ptr).width = width };
}
pub fn set_ofs_x(&self, x: i32) {
unsafe { (*self.ptr).ofs_x = x };
}
pub fn set_ofs_y(&self, y: i32) {
unsafe { (*self.ptr).ofs_y = y };
}
pub fn set_radius(&self, radius: i32) {
unsafe { (*self.ptr).radius = radius };
}
}