use std::{ffi::CStr, os::raw::c_char};
#[cfg(feature = "napi")]
use crate::napi::*;
include!(concat!(env!("OUT_DIR"), "/xcomponent_sys.rs"));
#[cfg(feature = "napi")]
pub fn get_x_component(env: napi_env, exports: napi_value) -> Option<*mut OH_NativeXComponent> {
use std::os::raw::c_void;
if env.is_null() || exports.is_null() {
return None;
}
unsafe {
let mut instance: napi_value = std::ptr::null_mut();
if napi_get_named_property(
env,
exports,
OH_NATIVE_XCOMPONENT_OBJ.as_ptr() as *const c_char,
&mut instance,
) != napi_status::napi_ok
{
return None;
}
let mut component: *mut c_void = std::ptr::null_mut();
if napi_unwrap(env, instance, &mut component) != napi_status::napi_ok {
return None;
}
Some(component as *mut OH_NativeXComponent)
}
}
#[cfg(feature = "napi")]
pub fn get_x_component_from_callback(
env: napi_env,
callback: napi_callback_info,
) -> Option<*mut OH_NativeXComponent> {
if env.is_null() || callback.is_null() {
return None;
}
let mut this: napi_value = std::ptr::null_mut();
unsafe {
if napi_get_cb_info(
env,
callback,
std::ptr::null_mut(),
std::ptr::null_mut(),
&mut this,
std::ptr::null_mut(),
) != napi_status::napi_ok
{
return None;
}
}
get_x_component(env, this)
}
pub fn get_x_component_id(component: *mut OH_NativeXComponent) -> String {
let mut id = [0 as c_char; (OH_XCOMPONENT_ID_LEN_MAX + 1) as usize];
let mut size: u64 = (OH_XCOMPONENT_ID_LEN_MAX + 1).into();
unsafe {
OH_NativeXComponent_GetXComponentId(component, id.as_mut_ptr(), &mut size as *mut u64);
String::from_utf8_lossy(CStr::from_ptr(id.as_ptr()).to_bytes()).to_string()
}
}