Skip to main content

ohos_arkui_binding/component/attribute/
gesture.rs

1//! Module component::attribute::gesture wrappers and related types.
2
3#[cfg(feature = "api-18")]
4use crate::ARK_UI_NATIVE_GESTURE_API_2;
5use crate::{
6    ArkUIResult, Gesture, GestureInterruptInfoRef, GestureInterruptResult, GestureMask,
7    GesturePriority, GestureRecognizerRef, ParallelInnerGestureEventRef,
8    ARK_UI_NATIVE_GESTURE_API_1,
9};
10
11use super::ArkUIAttributeBasic;
12
13/// Gesture composition helpers shared by components.
14pub trait ArkUIGesture: ArkUIAttributeBasic {
15    fn add_gesture(
16        &self,
17        gesture: Gesture,
18        mode: Option<GesturePriority>,
19        mask: Option<GestureMask>,
20    ) -> ArkUIResult<()> {
21        self.add_gesture_ref(&gesture, mode, mask)
22    }
23
24    fn add_gesture_ref(
25        &self,
26        gesture: &Gesture,
27        mode: Option<GesturePriority>,
28        mask: Option<GestureMask>,
29    ) -> ArkUIResult<()> {
30        let mode = mode.unwrap_or(GesturePriority::Parallel);
31        let mask = mask.unwrap_or(GestureMask::NormalGestureMask);
32        let raw = *gesture.raw.borrow();
33        ARK_UI_NATIVE_GESTURE_API_1
34            .with(|api| api.add_gesture(raw, self.raw().raw(), mode, mask))?;
35        Ok(())
36    }
37
38    fn remove_gesture(&self, gesture: &Gesture) -> ArkUIResult<()> {
39        let raw = *gesture.raw.borrow();
40        ARK_UI_NATIVE_GESTURE_API_1.with(|api| api.remove_gesture(raw, self.raw().raw()))?;
41        Ok(())
42    }
43
44    fn set_gesture_interrupter<
45        T: Fn(GestureInterruptInfoRef) -> GestureInterruptResult + 'static,
46    >(
47        &self,
48        interrupter: T,
49    ) -> ArkUIResult<()> {
50        #[cfg(feature = "api-18")]
51        ARK_UI_NATIVE_GESTURE_API_2
52            .with(|api| api.set_gesture_interrupter_to_node(self.raw().raw(), interrupter))?;
53        #[cfg(not(feature = "api-18"))]
54        ARK_UI_NATIVE_GESTURE_API_1
55            .with(|api| api.set_gesture_interrupter_to_node(self.raw().raw(), interrupter))?;
56        Ok(())
57    }
58
59    fn clear_gesture_interrupter(&self) -> ArkUIResult<()> {
60        #[cfg(feature = "api-18")]
61        ARK_UI_NATIVE_GESTURE_API_2
62            .with(|api| api.clear_gesture_interrupter_to_node(self.raw().raw()))?;
63        #[cfg(not(feature = "api-18"))]
64        ARK_UI_NATIVE_GESTURE_API_1
65            .with(|api| api.clear_gesture_interrupter_to_node(self.raw().raw()))?;
66        Ok(())
67    }
68
69    fn set_inner_gesture_parallel_to<
70        T: Fn(ParallelInnerGestureEventRef) -> Option<GestureRecognizerRef> + 'static,
71    >(
72        &self,
73        parallel_inner_gesture: T,
74    ) -> ArkUIResult<()> {
75        ARK_UI_NATIVE_GESTURE_API_1.with(|api| {
76            api.set_inner_gesture_parallel_to(self.raw().raw(), parallel_inner_gesture)
77        })?;
78        Ok(())
79    }
80
81    fn clear_inner_gesture_parallel_to(&self) -> ArkUIResult<()> {
82        ARK_UI_NATIVE_GESTURE_API_1
83            .with(|api| api.clear_inner_gesture_parallel_to(self.raw().raw()))?;
84        Ok(())
85    }
86}