#![allow(clippy::missing_safety_doc)]
use napi_ohos::{
bindgen_prelude::{check_status, JsObjectValue, Object},
Env, JsValue, Result,
};
use napi_sys_ohos as sys;
use ohos_xcomponent_sys::{
OH_NativeXComponent, OH_NativeXComponent_Callback, OH_NATIVE_XCOMPONENT_OBJ,
};
use std::{os::raw::c_void, ptr};
use crate::{
native_xcomponent::NativeXComponent, tool::resolve_id, TouchEventData, WindowRaw,
XComponentOffset, XComponentRaw, XComponentSize,
};
#[repr(transparent)]
pub struct XComponent(NativeXComponent);
impl XComponent {
pub fn init(env: Env, exports: Object<'_>) -> Result<Self> {
let xcomponent_obj_name: &str = unsafe {
std::str::from_utf8_unchecked(
&OH_NATIVE_XCOMPONENT_OBJ[..OH_NATIVE_XCOMPONENT_OBJ.len() - 1],
)
};
let export_instance: Object<'_> = exports.get_named_property(xcomponent_obj_name)?;
let mut instance = ptr::null_mut();
check_status!(
unsafe {
sys::napi_unwrap(
env.raw(),
export_instance.raw(),
&mut instance as *mut *mut OH_NativeXComponent as *mut *mut c_void,
)
},
"Get OH_NativeXComponent failed."
)?;
let id = resolve_id(instance);
Ok(XComponent(NativeXComponent {
raw: XComponentRaw(instance),
id,
}))
}
pub fn id(&self) -> Result<String> {
self.0.id()
}
pub fn raw(&self) -> *mut OH_NativeXComponent {
self.0.raw()
}
pub fn set_frame_rate(&self, min: i32, max: i32, expected: i32) -> Result<()> {
self.0.set_frame_rate(min, max, expected)
}
#[cfg(feature = "callbacks")]
pub fn register_callback(&self) -> Result<()> {
self.0.register_callback()
}
pub unsafe fn register_native_callback(
&self,
callbacks: Box<OH_NativeXComponent_Callback>,
) -> Result<()> {
self.0.register_native_callback(callbacks)
}
pub fn size(&self, window: WindowRaw) -> Result<XComponentSize> {
self.0.size(window)
}
pub fn offset(&self, window: WindowRaw) -> Result<XComponentOffset> {
self.0.offset(window)
}
pub fn on_frame_callback(&self, cb: fn(XComponentRaw, u64, u64) -> Result<()>) -> Result<()> {
self.0.on_frame_callback(cb)
}
pub fn on_surface_changed(&self, cb: fn(XComponentRaw, WindowRaw) -> Result<()>) {
self.0.on_surface_changed(cb)
}
pub fn on_surface_created(&self, cb: fn(XComponentRaw, WindowRaw) -> Result<()>) {
self.0.on_surface_created(cb)
}
pub fn on_surface_destroyed(&self, cb: fn(XComponentRaw, WindowRaw) -> Result<()>) {
self.0.on_surface_destroyed(cb)
}
pub fn on_touch_event(&self, cb: fn(XComponentRaw, WindowRaw, TouchEventData) -> Result<()>) {
self.0.on_touch_event(cb)
}
}