Skip to main content

ohos_arkui_binding/api/node_custom_event/
geometry.rs

1//! Module api::node_custom_event::geometry wrappers and related types.
2
3use std::{os::raw::c_void, ptr::NonNull};
4
5use ohos_arkui_sys::{
6    ArkUI_DrawContext, ArkUI_IntOffset, ArkUI_IntSize, ArkUI_LayoutConstraint,
7    OH_ArkUI_DrawContext_GetCanvas, OH_ArkUI_DrawContext_GetSize,
8};
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11/// Integer offset used by node custom-event geometry APIs.
12pub struct IntOffset {
13    pub x: i32,
14    pub y: i32,
15}
16
17impl From<ArkUI_IntOffset> for IntOffset {
18    fn from(value: ArkUI_IntOffset) -> Self {
19        Self {
20            x: value.x,
21            y: value.y,
22        }
23    }
24}
25
26impl From<IntOffset> for ArkUI_IntOffset {
27    fn from(value: IntOffset) -> Self {
28        Self {
29            x: value.x,
30            y: value.y,
31        }
32    }
33}
34
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
36/// Integer size used by node custom-event geometry APIs.
37pub struct IntSize {
38    pub width: i32,
39    pub height: i32,
40}
41
42impl From<ArkUI_IntSize> for IntSize {
43    fn from(value: ArkUI_IntSize) -> Self {
44        Self {
45            width: value.width,
46            height: value.height,
47        }
48    }
49}
50
51impl From<IntSize> for ArkUI_IntSize {
52    fn from(value: IntSize) -> Self {
53        Self {
54            width: value.width,
55            height: value.height,
56        }
57    }
58}
59
60#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61/// Borrowed wrapper for `ArkUI_LayoutConstraint`.
62pub struct LayoutConstraintHandle {
63    raw: NonNull<ArkUI_LayoutConstraint>,
64}
65
66impl LayoutConstraintHandle {
67    pub(crate) fn from_raw(raw: *mut ArkUI_LayoutConstraint) -> Option<Self> {
68        NonNull::new(raw).map(|raw| Self { raw })
69    }
70
71    pub(crate) fn raw(&self) -> *mut ArkUI_LayoutConstraint {
72        self.raw.as_ptr()
73    }
74}
75
76#[derive(Clone, Copy, Debug, PartialEq, Eq)]
77/// Borrowed wrapper for `ArkUI_DrawContext`.
78pub struct DrawContext {
79    raw: NonNull<ArkUI_DrawContext>,
80}
81
82impl DrawContext {
83    pub(crate) fn from_raw(raw: *mut ArkUI_DrawContext) -> Option<Self> {
84        NonNull::new(raw).map(|raw| Self { raw })
85    }
86
87    pub(crate) fn raw(&self) -> *mut ArkUI_DrawContext {
88        self.raw.as_ptr()
89    }
90
91    /// Returns canvas handle exposed by ArkUI, if available.
92    pub fn canvas(&self) -> Option<NonNull<c_void>> {
93        let canvas = unsafe { OH_ArkUI_DrawContext_GetCanvas(self.raw()) };
94        NonNull::new(canvas)
95    }
96
97    /// Returns draw target size.
98    pub fn size(&self) -> IntSize {
99        let size = unsafe { OH_ArkUI_DrawContext_GetSize(self.raw()) };
100        size.into()
101    }
102}