• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "core/interfaces/native/node/scroller_modifier.h"
16 
17 #include "bridge/common/utils/utils.h"
18 #include "core/components_ng/pattern/scroll/scroll_model_ng.h"
19 #include "core/components_ng/pattern/scrollable/scrollable_controller.h"
20 
21 namespace OHOS::Ace::NG {
22 
GetScroller(ArkUINodeHandle node,ArkUINodeHandle proxy)23 ArkUINodeHandle GetScroller(ArkUINodeHandle node, ArkUINodeHandle proxy)
24 {
25     auto* frameNode = reinterpret_cast<FrameNode*>(node);
26     CHECK_NULL_RETURN(frameNode, nullptr);
27     auto controller = ScrollModelNG::GetOrCreateController(frameNode);
28     return reinterpret_cast<ArkUINodeHandle>(AceType::RawPtr(controller));
29 }
30 
GetScrollerProxy(ArkUINodeHandle node)31 ArkUINodeHandle GetScrollerProxy(ArkUINodeHandle node)
32 {
33     TAG_LOGE(AceLogTag::ACE_SCROLL, "GetScrollerProxy not implemented");
34     return node;
35 }
36 
SetScrollToIndex(ArkUINodeHandle node,ArkUI_Int32 index,ArkUI_Bool smooth,ArkUIScrollAlign align)37 void SetScrollToIndex(ArkUINodeHandle node, ArkUI_Int32 index, ArkUI_Bool smooth,
38     ArkUIScrollAlign align)
39 {
40     auto* scrollController = reinterpret_cast<NG::ScrollableController*>(node);
41     CHECK_NULL_VOID(scrollController);
42     scrollController->ScrollToIndex(index, smooth, static_cast<ScrollAlign>(align), std::nullopt);
43 }
44 
ScrollBy(ArkUINodeHandle node,ArkUI_Float32 * values,ArkUI_Int32 * units)45 void ScrollBy(ArkUINodeHandle node, ArkUI_Float32* values, ArkUI_Int32* units)
46 {
47     auto* scrollController = reinterpret_cast<NG::ScrollableController*>(node);
48     CHECK_NULL_VOID(scrollController);
49     Dimension deltaX(values[0], static_cast<OHOS::Ace::DimensionUnit>(units[0]));
50     Dimension deltaY(values[1], static_cast<OHOS::Ace::DimensionUnit>(units[1]));
51     scrollController->ScrollBy(deltaX.Value(), deltaY.Value(), false);
52 }
53 
GetCurrentOffset(ArkUINodeHandle node,ArkUI_Float32 * out)54 void GetCurrentOffset(ArkUINodeHandle node, ArkUI_Float32* out)
55 {
56     auto* scrollController = reinterpret_cast<NG::ScrollableController*>(node);
57     CHECK_NULL_VOID(scrollController);
58     auto offset = scrollController->GetCurrentOffset();
59     out[0] = offset.GetX();
60     out[1] = offset.GetY();
61 }
62 
ScrollTo(ArkUINodeHandle node,ArkUI_Float32 * xOffsetArray,ArkUI_Float32 * yOffsetArray,ArkUI_Bool animation,const struct ArkUIScrollerAnimationType * animationType)63 void ScrollTo(ArkUINodeHandle node, ArkUI_Float32* xOffsetArray, ArkUI_Float32* yOffsetArray,
64     ArkUI_Bool animation, const struct ArkUIScrollerAnimationType* animationType)
65 {
66     auto* controller = reinterpret_cast<NG::ScrollableController*>(node);
67     CHECK_NULL_VOID(controller);
68     Dimension xOffset(xOffsetArray[0], static_cast<OHOS::Ace::DimensionUnit>(xOffsetArray[1]));
69     Dimension yOffset(yOffsetArray[0], static_cast<OHOS::Ace::DimensionUnit>(yOffsetArray[1]));
70     float duration = animationType->duration;
71     RefPtr<Curve> curve = Framework::CreateCurve(Framework::CurveIntToString(animationType->curve), false);
72     auto direction = controller->GetScrollDirection();
73     auto position = direction == Axis::VERTICAL ? yOffset : xOffset;
74     controller->AnimateTo(position, duration, curve, animation);
75 }
76 
77 namespace NodeModifier {
GetScrollerModifier()78 const ArkUIScrollerModifier* GetScrollerModifier()
79 {
80     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
81     static const ArkUIScrollerModifier modifier = {
82         .getScroller = GetScroller,
83         .getScrollerProxy = GetScrollerProxy,
84         .scrollToIndex = SetScrollToIndex,
85         .scrollBy = ScrollBy,
86         .getCurrentOffset = GetCurrentOffset,
87         .scrollTo = ScrollTo,
88     };
89     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
90 
91     return &modifier;
92 }
93 
GetCJUIScrollerModifier()94 const CJUIScrollerModifier* GetCJUIScrollerModifier()
95 {
96     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
97     static const CJUIScrollerModifier modifier = {
98         .getScroller = GetScroller,
99         .getScrollerProxy = GetScrollerProxy,
100         .scrollToIndex = SetScrollToIndex,
101         .scrollBy = ScrollBy,
102         .getCurrentOffset = GetCurrentOffset,
103         .scrollTo = ScrollTo,
104     };
105     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
106 
107     return &modifier;
108 }
109 }
110 }
111