use core::{ops::Deref, ptr::null_mut};
use oxivgl_sys::*;
use super::{
WidgetError,
obj::{AsLvHandle, Obj},
};
#[derive(Debug)]
pub struct Switch<'p> {
obj: Obj<'p>,
}
impl<'p> AsLvHandle for Switch<'p> {
fn lv_handle(&self) -> *mut lv_obj_t {
self.obj.lv_handle()
}
}
impl<'p> Deref for Switch<'p> {
type Target = Obj<'p>;
fn deref(&self) -> &Obj<'p> {
&self.obj
}
}
#[repr(u32)]
#[derive(Clone, Copy, Debug)]
pub enum SwitchOrientation {
Auto = lv_switch_orientation_t_LV_SWITCH_ORIENTATION_AUTO,
Horizontal = lv_switch_orientation_t_LV_SWITCH_ORIENTATION_HORIZONTAL,
Vertical = lv_switch_orientation_t_LV_SWITCH_ORIENTATION_VERTICAL,
}
impl<'p> Switch<'p> {
pub fn new(parent: &impl AsLvHandle) -> Result<Self, WidgetError> {
let parent_ptr = parent.lv_handle();
assert_ne!(parent_ptr, null_mut(), "Parent widget cannot be null");
let handle = unsafe { lv_switch_create(parent_ptr) };
if handle.is_null() { Err(WidgetError::LvglNullPointer) } else { Ok(Switch { obj: Obj::from_raw(handle) }) }
}
pub fn set_orientation(&self, orientation: SwitchOrientation) -> &Self {
unsafe { lv_switch_set_orientation(self.lv_handle(), orientation as lv_switch_orientation_t) };
self
}
pub fn get_orientation(&self) -> SwitchOrientation {
let raw = unsafe { lv_switch_get_orientation(self.lv_handle()) };
match raw {
x if x == lv_switch_orientation_t_LV_SWITCH_ORIENTATION_HORIZONTAL => SwitchOrientation::Horizontal,
x if x == lv_switch_orientation_t_LV_SWITCH_ORIENTATION_VERTICAL => SwitchOrientation::Vertical,
_ => SwitchOrientation::Auto,
}
}
}