Skip to main content

ohos_arkui_binding/api/node_custom_event/
span.rs

1//! Module api::node_custom_event::span wrappers and related types.
2
3use std::ptr::NonNull;
4
5use ohos_arkui_input_binding::ArkUIErrorCode;
6use ohos_arkui_sys::{
7    ArkUI_CustomSpanDrawInfo, ArkUI_CustomSpanMeasureInfo, ArkUI_CustomSpanMetrics,
8    OH_ArkUI_CustomSpanDrawInfo_Create, OH_ArkUI_CustomSpanDrawInfo_Dispose,
9    OH_ArkUI_CustomSpanDrawInfo_GetBaseline, OH_ArkUI_CustomSpanDrawInfo_GetLineBottom,
10    OH_ArkUI_CustomSpanDrawInfo_GetLineTop, OH_ArkUI_CustomSpanDrawInfo_GetXOffset,
11    OH_ArkUI_CustomSpanMeasureInfo_Create, OH_ArkUI_CustomSpanMeasureInfo_Dispose,
12    OH_ArkUI_CustomSpanMeasureInfo_GetFontSize, OH_ArkUI_CustomSpanMetrics_Create,
13    OH_ArkUI_CustomSpanMetrics_Dispose, OH_ArkUI_CustomSpanMetrics_SetHeight,
14    OH_ArkUI_CustomSpanMetrics_SetWidth,
15};
16
17use crate::{check_arkui_status, ArkUIError, ArkUIResult};
18
19/// Measurement input for custom text span callbacks.
20pub struct CustomSpanMeasureInfo {
21    raw: NonNull<ArkUI_CustomSpanMeasureInfo>,
22}
23
24impl CustomSpanMeasureInfo {
25    /// Allocates a new measure-info object.
26    pub fn new() -> ArkUIResult<Self> {
27        let info = unsafe { OH_ArkUI_CustomSpanMeasureInfo_Create() };
28        let info = NonNull::new(info).ok_or_else(|| {
29            ArkUIError::new(
30                ArkUIErrorCode::ParamInvalid,
31                "OH_ArkUI_CustomSpanMeasureInfo_Create returned null",
32            )
33        })?;
34        Ok(Self::from_non_null(info))
35    }
36
37    fn from_non_null(raw: NonNull<ArkUI_CustomSpanMeasureInfo>) -> Self {
38        Self { raw }
39    }
40
41    pub(crate) fn from_raw(raw: *mut ArkUI_CustomSpanMeasureInfo) -> Self {
42        Self::from_non_null(
43            NonNull::new(raw)
44                .unwrap_or_else(|| panic!("ArkUI_CustomSpanMeasureInfo pointer is null")),
45        )
46    }
47
48    pub(crate) fn raw(&self) -> *mut ArkUI_CustomSpanMeasureInfo {
49        self.raw.as_ptr()
50    }
51
52    /// Releases the underlying native object.
53    pub fn dispose(self) {
54        unsafe { OH_ArkUI_CustomSpanMeasureInfo_Dispose(self.raw()) }
55    }
56
57    /// Returns current font size provided by layout engine.
58    pub fn font_size(&self) -> f32 {
59        unsafe { OH_ArkUI_CustomSpanMeasureInfo_GetFontSize(self.raw()) }
60    }
61}
62
63/// Output metrics for custom text span measurement.
64pub struct CustomSpanMetrics {
65    raw: NonNull<ArkUI_CustomSpanMetrics>,
66}
67
68impl CustomSpanMetrics {
69    /// Allocates a new custom-span metrics object.
70    pub fn new() -> ArkUIResult<Self> {
71        let metrics = unsafe { OH_ArkUI_CustomSpanMetrics_Create() };
72        let metrics = NonNull::new(metrics).ok_or_else(|| {
73            ArkUIError::new(
74                ArkUIErrorCode::ParamInvalid,
75                "OH_ArkUI_CustomSpanMetrics_Create returned null",
76            )
77        })?;
78        Ok(Self::from_non_null(metrics))
79    }
80
81    fn from_non_null(raw: NonNull<ArkUI_CustomSpanMetrics>) -> Self {
82        Self { raw }
83    }
84
85    pub(crate) fn from_raw(raw: *mut ArkUI_CustomSpanMetrics) -> Self {
86        Self::from_non_null(
87            NonNull::new(raw).unwrap_or_else(|| panic!("ArkUI_CustomSpanMetrics pointer is null")),
88        )
89    }
90
91    pub(crate) fn raw(&self) -> *mut ArkUI_CustomSpanMetrics {
92        self.raw.as_ptr()
93    }
94
95    /// Releases the underlying native object.
96    pub fn dispose(self) {
97        unsafe { OH_ArkUI_CustomSpanMetrics_Dispose(self.raw()) }
98    }
99
100    /// Sets measured span width.
101    pub fn set_width(&mut self, width: f32) -> ArkUIResult<()> {
102        unsafe { check_arkui_status!(OH_ArkUI_CustomSpanMetrics_SetWidth(self.raw(), width)) }
103    }
104
105    /// Sets measured span height.
106    pub fn set_height(&mut self, height: f32) -> ArkUIResult<()> {
107        unsafe { check_arkui_status!(OH_ArkUI_CustomSpanMetrics_SetHeight(self.raw(), height)) }
108    }
109}
110
111/// Drawing info for custom text span rendering callbacks.
112pub struct CustomSpanDrawInfo {
113    raw: NonNull<ArkUI_CustomSpanDrawInfo>,
114}
115
116impl CustomSpanDrawInfo {
117    /// Allocates a new draw-info object.
118    pub fn new() -> ArkUIResult<Self> {
119        let info = unsafe { OH_ArkUI_CustomSpanDrawInfo_Create() };
120        let info = NonNull::new(info).ok_or_else(|| {
121            ArkUIError::new(
122                ArkUIErrorCode::ParamInvalid,
123                "OH_ArkUI_CustomSpanDrawInfo_Create returned null",
124            )
125        })?;
126        Ok(Self::from_non_null(info))
127    }
128
129    fn from_non_null(raw: NonNull<ArkUI_CustomSpanDrawInfo>) -> Self {
130        Self { raw }
131    }
132
133    pub(crate) fn from_raw(raw: *mut ArkUI_CustomSpanDrawInfo) -> Self {
134        Self::from_non_null(
135            NonNull::new(raw).unwrap_or_else(|| panic!("ArkUI_CustomSpanDrawInfo pointer is null")),
136        )
137    }
138
139    pub(crate) fn raw(&self) -> *mut ArkUI_CustomSpanDrawInfo {
140        self.raw.as_ptr()
141    }
142
143    /// Releases the underlying native object.
144    pub fn dispose(self) {
145        unsafe { OH_ArkUI_CustomSpanDrawInfo_Dispose(self.raw()) }
146    }
147
148    /// Horizontal offset where this span should be drawn.
149    pub fn x_offset(&self) -> f32 {
150        unsafe { OH_ArkUI_CustomSpanDrawInfo_GetXOffset(self.raw()) }
151    }
152
153    /// Top line boundary for the span.
154    pub fn line_top(&self) -> f32 {
155        unsafe { OH_ArkUI_CustomSpanDrawInfo_GetLineTop(self.raw()) }
156    }
157
158    /// Bottom line boundary for the span.
159    pub fn line_bottom(&self) -> f32 {
160        unsafe { OH_ArkUI_CustomSpanDrawInfo_GetLineBottom(self.raw()) }
161    }
162
163    /// Baseline position for text alignment.
164    pub fn baseline(&self) -> f32 {
165        unsafe { OH_ArkUI_CustomSpanDrawInfo_GetBaseline(self.raw()) }
166    }
167}