Skip to main content

ohos_xcomponent_binding/
raw.rs

1use napi_ohos::{Error, Result};
2use ohos_xcomponent_sys::OH_NativeXComponent;
3use ohos_xcomponent_sys::{
4    OH_NativeXComponent_GetXComponentOffset, OH_NativeXComponent_GetXComponentSize,
5};
6use std::os::raw::c_void;
7
8use crate::{code::XComponentResultCode, XComponentOffset, XComponentSize};
9
10#[repr(transparent)]
11#[derive(Debug, Clone, Copy)]
12pub struct WindowRaw(pub *mut c_void);
13
14#[repr(transparent)]
15#[derive(Debug, Clone, Copy)]
16pub struct XComponentRaw(pub *mut OH_NativeXComponent);
17
18impl XComponentRaw {
19    /// Get the current XComponent's surface size
20    pub fn size(&self, win: WindowRaw) -> Result<XComponentSize> {
21        let mut width: u64 = 0;
22        let mut height: u64 = 0;
23        let ret: XComponentResultCode = unsafe {
24            OH_NativeXComponent_GetXComponentSize(self.0, win.0, &mut width, &mut height).into()
25        };
26        if ret != XComponentResultCode::Success {
27            return Err(Error::from_reason("XComponent get size failed"));
28        }
29        Ok(XComponentSize { width, height })
30    }
31
32    /// Get the offset of the surface held by the current XComponent.
33    pub fn offset(&self, win: WindowRaw) -> Result<XComponentOffset> {
34        let mut x: f64 = 0.0;
35        let mut y: f64 = 0.0;
36        let ret: XComponentResultCode = unsafe {
37            OH_NativeXComponent_GetXComponentOffset(self.0, win.0, &mut x, &mut y).into()
38        };
39        if ret != XComponentResultCode::Success {
40            return Err(Error::from_reason("XComponent get offset failed"));
41        }
42        Ok(XComponentOffset { x, y })
43    }
44}