1 /*
2  * Copyright (c) 2022 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 
16 #include "bridge/declarative_frontend/jsview/models/grid_model_impl.h"
17 
18 #include "base/memory/ace_type.h"
19 #include "base/utils/utils.h"
20 #include "bridge/declarative_frontend/jsview/js_container_base.h"
21 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
22 #include "bridge/declarative_frontend/jsview/js_utils.h"
23 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
24 #include "bridge/declarative_frontend/view_stack_processor.h"
25 #include "core/common/ace_application_info.h"
26 #include "core/components/common/layout/constants.h"
27 #include "core/components_ng/base/view_abstract_model.h"
28 
29 namespace OHOS::Ace::Framework {
30 
Create(const RefPtr<ScrollControllerBase> & positionController,const RefPtr<ScrollProxy> & scrollProxy)31 void GridModelImpl::Create(
32     const RefPtr<ScrollControllerBase>& positionController, const RefPtr<ScrollProxy>& scrollProxy)
33 {
34     auto controller = AceType::DynamicCast<V2::GridPositionController>(positionController);
35     std::list<RefPtr<OHOS::Ace::Component>> componentChildren;
36     RefPtr<OHOS::Ace::GridLayoutComponent> gridComponent = AceType::MakeRefPtr<GridLayoutComponent>(componentChildren);
37     ViewStackProcessor::GetInstance()->ClaimElementId(gridComponent);
38     gridComponent->SetDeclarative();
39     gridComponent->SetNeedShrink(true);
40     if (controller) {
41         gridComponent->SetController(controller);
42     }
43     auto scrollBarProxy = AceType::DynamicCast<ScrollBarProxy>(scrollProxy);
44     if (scrollBarProxy) {
45         gridComponent->SetScrollBarProxy(scrollBarProxy);
46     }
47 
48     if (Container::IsCurrentUsePartialUpdate()) {
49         ViewStackProcessor::GetInstance()->PushGrid(gridComponent);
50     } else {
51         ViewStackProcessor::GetInstance()->Push(gridComponent);
52     }
53 }
54 
Pop()55 void GridModelImpl::Pop()
56 {
57     ViewStackProcessor::GetInstance()->PopGrid();
58 }
59 
SetColumnsTemplate(const std::string & value)60 void GridModelImpl::SetColumnsTemplate(const std::string& value)
61 {
62     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
63     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
64     CHECK_NULL_VOID(grid);
65     grid->SetColumnsArgs(value);
66 }
67 
SetRowsTemplate(const std::string & value)68 void GridModelImpl::SetRowsTemplate(const std::string& value)
69 {
70     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
71     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
72     CHECK_NULL_VOID(grid);
73     grid->SetRowsArgs(value);
74 }
75 
SetColumnsGap(const Dimension & value)76 void GridModelImpl::SetColumnsGap(const Dimension& value)
77 {
78     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
79     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
80     CHECK_NULL_VOID(grid);
81     grid->SetColumnGap(value);
82 }
83 
SetRowsGap(const Dimension & value)84 void GridModelImpl::SetRowsGap(const Dimension& value)
85 {
86     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
87     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
88     CHECK_NULL_VOID(grid);
89     grid->SetRowGap(value);
90 }
91 
SetGridHeight(const Dimension & value)92 void GridModelImpl::SetGridHeight(const Dimension& value)
93 {
94     ViewAbstractModel::GetInstance()->SetHeight(value);
95     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
96     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
97     if (grid && value.IsValid()) {
98         grid->SetNeedShrink(false);
99     }
100 }
101 
SetScrollBarMode(DisplayMode value)102 void GridModelImpl::SetScrollBarMode(DisplayMode value)
103 {
104     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
105     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
106     CHECK_NULL_VOID(grid);
107     grid->SetScrollBar(value);
108 }
109 
SetScrollBarColor(const std::string & value)110 void GridModelImpl::SetScrollBarColor(const std::string& value)
111 {
112     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
113     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
114     CHECK_NULL_VOID(grid);
115     grid->SetScrollBarColor(value);
116 }
117 
SetScrollBarWidth(const std::string & value)118 void GridModelImpl::SetScrollBarWidth(const std::string& value)
119 {
120     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
121     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
122     CHECK_NULL_VOID(grid);
123     grid->SetScrollBarWidth(value);
124 }
125 
SetCachedCount(int32_t value,bool show)126 void GridModelImpl::SetCachedCount(int32_t value, bool show)
127 {
128     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
129     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
130     CHECK_NULL_VOID(grid);
131     grid->SetCachedCount(value);
132 }
133 
SetIsRTL(TextDirection direction)134 void GridModelImpl::SetIsRTL(TextDirection direction)
135 {
136     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
137     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
138     CHECK_NULL_VOID(grid);
139     bool isRtl;
140     switch (direction) {
141         case TextDirection::RTL:
142             isRtl = true;
143             break;
144         case TextDirection::LTR:
145             isRtl = false;
146             break;
147         default:
148             isRtl = AceApplicationInfo::GetInstance().IsRightToLeft();
149     }
150     grid->SetRightToLeft(isRtl);
151 }
152 
SetLayoutDirection(FlexDirection value)153 void GridModelImpl::SetLayoutDirection(FlexDirection value)
154 {
155     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
156     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
157     CHECK_NULL_VOID(grid);
158     grid->SetDirection(value);
159 }
160 
SetMaxCount(int32_t value)161 void GridModelImpl::SetMaxCount(int32_t value)
162 {
163     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
164     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
165     CHECK_NULL_VOID(grid);
166     grid->SetMaxCount(value);
167 }
168 
SetMinCount(int32_t value)169 void GridModelImpl::SetMinCount(int32_t value)
170 {
171     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
172     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
173     CHECK_NULL_VOID(grid);
174     grid->SetMinCount(value);
175 }
176 
SetCellLength(int32_t value)177 void GridModelImpl::SetCellLength(int32_t value)
178 {
179     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
180     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
181     CHECK_NULL_VOID(grid);
182     grid->SetCellLength(value);
183 }
184 
SetEditable(bool value)185 void GridModelImpl::SetEditable(bool value)
186 {
187     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
188     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
189     CHECK_NULL_VOID(grid);
190     grid->SetEditMode(value);
191 }
192 
SetMultiSelectable(bool value)193 void GridModelImpl::SetMultiSelectable(bool value)
194 {
195     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
196     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
197     CHECK_NULL_VOID(grid);
198     grid->SetMultiSelectable(value);
199 }
200 
SetSupportAnimation(bool value)201 void GridModelImpl::SetSupportAnimation(bool value)
202 {
203     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
204     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
205     CHECK_NULL_VOID(grid);
206     grid->SetSupportAnimation(value);
207 }
208 
SetSupportDragAnimation(bool value)209 void GridModelImpl::SetSupportDragAnimation(bool value)
210 {
211     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
212     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
213     CHECK_NULL_VOID(grid);
214     grid->SetDragAnimation(value);
215 }
216 
SetEdgeEffect(EdgeEffect edgeEffect,bool alwaysEnabled)217 void GridModelImpl::SetEdgeEffect(EdgeEffect edgeEffect, bool alwaysEnabled)
218 {
219     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
220     auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
221     CHECK_NULL_VOID(grid);
222     grid->SetEdgeEffect(edgeEffect);
223 }
224 
SetOnScrollToIndex(std::function<void (const BaseEventInfo *)> && value)225 void GridModelImpl::SetOnScrollToIndex(std::function<void(const BaseEventInfo*)>&& value)
226 {
227     auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
228     CHECK_NULL_VOID(grid);
229     grid->SetScrolledEvent(EventMarker(std::move(value)));
230 }
231 
SetOnScrollBarUpdate(std::function<std::pair<std::optional<float>,std::optional<float>> (int32_t,Dimension)> && value)232 void GridModelImpl::SetOnScrollBarUpdate(
233     std::function<std::pair<std::optional<float>, std::optional<float>>(int32_t, Dimension)>&& value)
234 {}
235 
SetOnItemDragStart(std::function<void (const ItemDragInfo &,int32_t)> && value)236 void GridModelImpl::SetOnItemDragStart(std::function<void(const ItemDragInfo&, int32_t)>&& value)
237 {
238     auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
239     CHECK_NULL_VOID(grid);
240     auto onDragStart = [func = std::move(value)](const ItemDragInfo& dragInfo, int32_t index) -> RefPtr<Component> {
241         ScopedViewStackProcessor builderViewStackProcessor;
242         {
243             func(dragInfo, index);
244         }
245         return ViewStackProcessor::GetInstance()->Finish();
246     };
247     grid->SetOnGridDragStartId(onDragStart);
248 }
249 
SetOnItemDragEnter(std::function<void (const ItemDragInfo &)> && value)250 void GridModelImpl::SetOnItemDragEnter(std::function<void(const ItemDragInfo&)>&& value)
251 {
252     auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
253     CHECK_NULL_VOID(grid);
254     grid->SetOnGridDragEnterId(value);
255 }
256 
SetOnItemDragMove(std::function<void (const ItemDragInfo &,int32_t,int32_t)> && value)257 void GridModelImpl::SetOnItemDragMove(std::function<void(const ItemDragInfo&, int32_t, int32_t)>&& value)
258 {
259     auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
260     CHECK_NULL_VOID(grid);
261     grid->SetOnGridDragMoveId(value);
262 }
263 
SetOnItemDragLeave(std::function<void (const ItemDragInfo &,int32_t)> && value)264 void GridModelImpl::SetOnItemDragLeave(std::function<void(const ItemDragInfo&, int32_t)>&& value)
265 {
266     auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
267     CHECK_NULL_VOID(grid);
268     grid->SetOnGridDragLeaveId(value);
269 }
270 
SetOnItemDrop(std::function<void (const ItemDragInfo &,int32_t,int32_t,bool)> && value)271 void GridModelImpl::SetOnItemDrop(std::function<void(const ItemDragInfo&, int32_t, int32_t, bool)>&& value)
272 {
273     auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
274     CHECK_NULL_VOID(grid);
275     grid->SetOnGridDropId(value);
276 }
277 
CreatePositionController()278 RefPtr<ScrollControllerBase> GridModelImpl::CreatePositionController()
279 {
280     return AceType::MakeRefPtr<V2::GridPositionController>();
281 }
282 
CreateScrollBarProxy()283 RefPtr<ScrollProxy> GridModelImpl::CreateScrollBarProxy()
284 {
285     return AceType::MakeRefPtr<ScrollBarProxy>();
286 }
287 
288 } // namespace OHOS::Ace::Framework
289