ohos_arkui_binding/animate/options/
animate_option.rs1use std::{cell::RefCell, ptr::NonNull, rc::Rc};
4
5use ohos_arkui_sys::{
6 ArkUI_AnimateOption, OH_ArkUI_AnimateOption_Create, OH_ArkUI_AnimateOption_Dispose,
7 OH_ArkUI_AnimateOption_GetCurve, OH_ArkUI_AnimateOption_GetDelay,
8 OH_ArkUI_AnimateOption_GetDuration, OH_ArkUI_AnimateOption_GetExpectedFrameRateRange,
9 OH_ArkUI_AnimateOption_GetICurve, OH_ArkUI_AnimateOption_GetIterations,
10 OH_ArkUI_AnimateOption_GetPlayMode, OH_ArkUI_AnimateOption_GetTempo,
11 OH_ArkUI_AnimateOption_SetCurve, OH_ArkUI_AnimateOption_SetDelay,
12 OH_ArkUI_AnimateOption_SetDuration, OH_ArkUI_AnimateOption_SetExpectedFrameRateRange,
13 OH_ArkUI_AnimateOption_SetICurve, OH_ArkUI_AnimateOption_SetIterations,
14 OH_ArkUI_AnimateOption_SetPlayMode, OH_ArkUI_AnimateOption_SetTempo,
15};
16
17use crate::api::ARK_UI_NATIVE_ANIMATE_API_1;
18use crate::{AnimationFinishCallbackType, AnimationMode, ArkUIContext, ArkUIResult, Curve};
19
20use super::AnimationFrameRateRange;
21use crate::animate::context::{AnimationFinishContext, AnimationUpdateContext};
22use crate::animate::curve::CurveHandle;
23
24pub struct Animation {
26 pub(crate) raw: Rc<RefCell<NonNull<ArkUI_AnimateOption>>>,
27 pub(crate) update_ctx: Rc<RefCell<AnimationUpdateContext>>,
28 pub(crate) finish_ctx: Rc<RefCell<AnimationFinishContext>>,
29}
30
31impl Animation {
32 pub fn new() -> Self {
34 let raw = unsafe { OH_ArkUI_AnimateOption_Create() };
35 let raw = NonNull::new(raw).unwrap_or_else(|| {
36 panic!("OH_ArkUI_AnimateOption_Create returned null");
37 });
38 Self {
39 raw: Rc::new(RefCell::new(raw)),
40 update_ctx: Default::default(),
41 finish_ctx: Default::default(),
42 }
43 }
44
45 pub fn duration(&self, duration: i32) {
47 unsafe { OH_ArkUI_AnimateOption_SetDuration(self.raw(), duration) };
48 }
49
50 pub fn get_duration(&self) -> u32 {
51 unsafe { OH_ArkUI_AnimateOption_GetDuration(self.raw()) }
52 }
53
54 pub fn tempo(&self, tempo: f32) {
56 unsafe { OH_ArkUI_AnimateOption_SetTempo(self.raw(), tempo) };
57 }
58
59 pub fn get_tempo(&self) -> f32 {
60 unsafe { OH_ArkUI_AnimateOption_GetTempo(self.raw()) }
61 }
62
63 pub fn delay(&self, delay: i32) {
65 unsafe { OH_ArkUI_AnimateOption_SetDelay(self.raw(), delay) };
66 }
67
68 pub fn get_delay(&self) -> i32 {
69 unsafe { OH_ArkUI_AnimateOption_GetDelay(self.raw()) }
70 }
71
72 pub fn iterations(&self, iterations: i32) {
74 unsafe { OH_ArkUI_AnimateOption_SetIterations(self.raw(), iterations) };
75 }
76
77 pub fn get_iterations(&self) -> i32 {
78 unsafe { OH_ArkUI_AnimateOption_GetIterations(self.raw()) }
79 }
80
81 pub fn curve(&self, curve: Curve) {
83 unsafe { OH_ArkUI_AnimateOption_SetCurve(self.raw(), curve.into()) };
84 }
85
86 pub fn get_curve(&self) -> Option<Curve> {
87 let curve = unsafe { OH_ArkUI_AnimateOption_GetCurve(self.raw()) };
88 Curve::try_from_raw(curve)
89 }
90
91 pub fn i_curve(&self, curve: &CurveHandle) {
93 unsafe { OH_ArkUI_AnimateOption_SetICurve(self.raw(), curve.as_raw()) };
94 }
95
96 pub fn get_i_curve(&self) -> Option<CurveHandle> {
97 let curve = unsafe { OH_ArkUI_AnimateOption_GetICurve(self.raw()) };
98 CurveHandle::from_raw_borrowed(curve)
99 }
100
101 pub fn mode(&self, mode: AnimationMode) {
103 unsafe { OH_ArkUI_AnimateOption_SetPlayMode(self.raw(), mode.into()) };
104 }
105
106 pub fn get_mode(&self) -> Option<AnimationMode> {
107 let mode = unsafe { OH_ArkUI_AnimateOption_GetPlayMode(self.raw()) };
108 AnimationMode::try_from_raw(mode)
109 }
110
111 pub fn rate_range(&self, range: AnimationFrameRateRange) {
113 let mut raw_range = range.raw();
114 unsafe { OH_ArkUI_AnimateOption_SetExpectedFrameRateRange(self.raw(), &mut raw_range) };
115 }
116
117 pub fn get_rate_range(&self) -> AnimationFrameRateRange {
118 let range_ptr = unsafe { OH_ArkUI_AnimateOption_GetExpectedFrameRateRange(self.raw()) };
119 if range_ptr.is_null() {
120 return AnimationFrameRateRange::new();
121 }
122
123 let range = unsafe { *range_ptr };
124 AnimationFrameRateRange::from_raw(range)
125 }
126
127 pub(crate) fn raw(&self) -> *mut ArkUI_AnimateOption {
128 self.raw.borrow().as_ptr()
129 }
130
131 pub fn update<T: Fn() + 'static>(&self, callback: T) {
133 let ctx = self.update_ctx.borrow();
134 ctx.callback(callback);
135 }
136
137 pub fn finish<T: Fn() + 'static>(
139 &self,
140 callback_type: AnimationFinishCallbackType,
141 callback: T,
142 ) {
143 let ctx = self.finish_ctx.borrow();
144 ctx.callback(callback);
145 ctx.callback_type(callback_type);
146 }
147
148 #[cfg(feature = "napi")]
149 pub fn animate_to(&self, ctx: ArkUIContext) -> ArkUIResult<()> {
150 let update_ctx_raw = self.update_ctx.borrow().raw();
151 let finish_ctx_raw = self.finish_ctx.borrow().raw();
152 ARK_UI_NATIVE_ANIMATE_API_1
153 .with(|api| api.animate_to(ctx.raw(), self.raw(), update_ctx_raw, finish_ctx_raw))?;
154 Ok(())
155 }
156}
157
158impl Default for Animation {
159 fn default() -> Self {
160 Self::new()
161 }
162}
163
164impl Drop for Animation {
165 fn drop(&mut self) {
166 unsafe { OH_ArkUI_AnimateOption_Dispose(self.raw()) };
167 }
168}