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