use crate::types::EventType;
pub struct Event {
pub(crate) inner: *mut nappgui_sys::Event,
}
impl Event {
pub(crate) fn new(ptr: *mut nappgui_sys::Event) -> Self {
if ptr.is_null() {
panic!("ptr is null");
}
Self { inner: ptr }
}
pub fn type_(&self) -> EventType {
let event_type = unsafe { nappgui_sys::event_type(self.inner as _) } as i32;
EventType::try_from(event_type).unwrap()
}
pub fn params<T>(&self) -> Option<T>
where
T: NappGUIEventParams,
{
let tp = T::type_();
let tp = std::ffi::CString::new(tp).unwrap();
let params = unsafe { nappgui_sys::event_params_imp(self.inner, tp.as_ptr()) };
T::from_ptr(params)
}
pub unsafe fn result<T>(&self, value: T)
where
T: NappGUIEventResult,
{
let tp = T::type_();
let tp = std::ffi::CString::new(tp).unwrap();
let result = unsafe { nappgui_sys::event_result_imp(self.inner, tp.as_ptr()) }
as *mut <T as NappGUIEventResult>::CrossType;
let value = value.to_cross_type();
*result = value;
}
}
pub trait NappGUIEventParams {
fn type_() -> &'static str;
fn from_ptr(ptr: *mut std::ffi::c_void) -> Option<Self>
where
Self: Sized;
}
pub trait NappGUIEventResult {
type CrossType;
fn type_() -> &'static str {
""
}
fn to_cross_type(&self) -> Self::CrossType;
}