use core::marker::PhantomData;
use oxivgl_sys::*;
use crate::widgets::{AsLvHandle, WidgetError};
pub struct Group {
ptr: *mut lv_group_t,
_not_send: PhantomData<*const ()>,
}
impl core::fmt::Debug for Group {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Group").finish_non_exhaustive()
}
}
impl Group {
pub fn new() -> Result<Self, WidgetError> {
let ptr = unsafe { lv_group_create() };
if ptr.is_null() {
return Err(WidgetError::LvglNullPointer);
}
Ok(Self { ptr, _not_send: PhantomData })
}
pub fn set_default(&self) -> &Self {
unsafe { lv_group_set_default(self.ptr) };
self
}
pub fn add_obj(&self, obj: &impl AsLvHandle) -> &Self {
unsafe { lv_group_add_obj(self.ptr, obj.lv_handle()) };
self
}
pub fn focus_obj(&self, obj: &impl AsLvHandle) -> &Self {
unsafe { lv_group_focus_obj(obj.lv_handle()) };
self
}
pub fn focus_next(&self) -> &Self {
unsafe { lv_group_focus_next(self.ptr) };
self
}
pub fn assign_to_keyboard_indevs(&self) -> &Self {
unsafe {
let mut indev = lv_indev_get_next(core::ptr::null_mut());
while !indev.is_null() {
let kind = lv_indev_get_type(indev);
if kind == lv_indev_type_t_LV_INDEV_TYPE_KEYPAD
|| kind == lv_indev_type_t_LV_INDEV_TYPE_ENCODER
{
lv_indev_set_group(indev, self.ptr);
}
indev = lv_indev_get_next(indev);
}
}
self
}
}
impl Drop for Group {
fn drop(&mut self) {
unsafe { lv_group_delete(self.ptr) };
}
}
pub struct GroupRef {
ptr: *mut lv_group_t,
_not_send: PhantomData<*const ()>,
}
impl GroupRef {
pub fn add_obj(&self, obj: &impl AsLvHandle) -> &Self {
unsafe { lv_group_add_obj(self.ptr, obj.lv_handle()) };
self
}
pub fn focus_obj(&self, obj: &impl AsLvHandle) -> &Self {
unsafe { lv_group_focus_obj(obj.lv_handle()) };
self
}
pub fn focus_next(&self) -> &Self {
unsafe { lv_group_focus_next(self.ptr) };
self
}
}
pub fn group_get_default() -> Option<GroupRef> {
let ptr = unsafe { lv_group_get_default() };
if ptr.is_null() {
None
} else {
Some(GroupRef { ptr, _not_send: PhantomData })
}
}
pub fn group_remove_obj(obj: &impl AsLvHandle) {
unsafe { lv_group_remove_obj(obj.lv_handle()) };
}