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(int32_t value)102 void GridModelImpl::SetScrollBarMode(int32_t value)
103 {
104 auto displayMode = static_cast<DisplayMode>(value);
105 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
106 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
107 CHECK_NULL_VOID(grid);
108 grid->SetScrollBar(displayMode);
109 }
110
SetScrollBarColor(const std::string & value)111 void GridModelImpl::SetScrollBarColor(const std::string& value)
112 {
113 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
114 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
115 CHECK_NULL_VOID(grid);
116 grid->SetScrollBarColor(value);
117 }
118
SetScrollBarWidth(const std::string & value)119 void GridModelImpl::SetScrollBarWidth(const std::string& value)
120 {
121 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
122 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
123 CHECK_NULL_VOID(grid);
124 grid->SetScrollBarWidth(value);
125 }
126
SetCachedCount(int32_t value)127 void GridModelImpl::SetCachedCount(int32_t value)
128 {
129 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
130 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
131 CHECK_NULL_VOID(grid);
132 grid->SetCachedCount(value);
133 }
134
SetIsRTL(TextDirection direction)135 void GridModelImpl::SetIsRTL(TextDirection direction)
136 {
137 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
138 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
139 CHECK_NULL_VOID(grid);
140 bool isRtl;
141 switch (direction) {
142 case TextDirection::RTL:
143 isRtl = true;
144 break;
145 case TextDirection::LTR:
146 isRtl = false;
147 break;
148 default:
149 isRtl = AceApplicationInfo::GetInstance().IsRightToLeft();
150 }
151 grid->SetRightToLeft(isRtl);
152 }
153
SetLayoutDirection(FlexDirection value)154 void GridModelImpl::SetLayoutDirection(FlexDirection value)
155 {
156 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
157 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
158 CHECK_NULL_VOID(grid);
159 grid->SetDirection(value);
160 }
161
SetMaxCount(int32_t value)162 void GridModelImpl::SetMaxCount(int32_t value)
163 {
164 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
165 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
166 CHECK_NULL_VOID(grid);
167 grid->SetMaxCount(value);
168 }
169
SetMinCount(int32_t value)170 void GridModelImpl::SetMinCount(int32_t value)
171 {
172 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
173 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
174 CHECK_NULL_VOID(grid);
175 grid->SetMinCount(value);
176 }
177
SetCellLength(int32_t value)178 void GridModelImpl::SetCellLength(int32_t value)
179 {
180 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
181 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
182 CHECK_NULL_VOID(grid);
183 grid->SetCellLength(value);
184 }
185
SetEditable(bool value)186 void GridModelImpl::SetEditable(bool value)
187 {
188 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
189 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
190 CHECK_NULL_VOID(grid);
191 grid->SetEditMode(value);
192 }
193
SetMultiSelectable(bool value)194 void GridModelImpl::SetMultiSelectable(bool value)
195 {
196 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
197 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
198 CHECK_NULL_VOID(grid);
199 grid->SetMultiSelectable(value);
200 }
201
SetSupportAnimation(bool value)202 void GridModelImpl::SetSupportAnimation(bool value)
203 {
204 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
205 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
206 CHECK_NULL_VOID(grid);
207 grid->SetSupportAnimation(value);
208 }
209
SetSupportDragAnimation(bool value)210 void GridModelImpl::SetSupportDragAnimation(bool value)
211 {
212 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
213 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
214 CHECK_NULL_VOID(grid);
215 grid->SetDragAnimation(value);
216 }
217
SetEdgeEffect(EdgeEffect edgeEffect)218 void GridModelImpl::SetEdgeEffect(EdgeEffect edgeEffect)
219 {
220 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
221 auto grid = AceType::DynamicCast<GridLayoutComponent>(component);
222 CHECK_NULL_VOID(grid);
223 grid->SetEdgeEffect(edgeEffect);
224 }
225
SetOnScrollToIndex(std::function<void (const BaseEventInfo *)> && value)226 void GridModelImpl::SetOnScrollToIndex(std::function<void(const BaseEventInfo*)>&& value)
227 {
228 auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
229 CHECK_NULL_VOID(grid);
230 grid->SetScrolledEvent(EventMarker(std::move(value)));
231 }
232
SetOnScrollBarUpdate(std::function<std::pair<std::optional<float>,std::optional<float>> (int32_t,Dimension)> && value)233 void GridModelImpl::SetOnScrollBarUpdate(
234 std::function<std::pair<std::optional<float>, std::optional<float>>(int32_t, Dimension)>&& value)
235 {}
236
SetOnItemDragStart(std::function<void (const ItemDragInfo &,int32_t)> && value)237 void GridModelImpl::SetOnItemDragStart(std::function<void(const ItemDragInfo&, int32_t)>&& value)
238 {
239 auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
240 CHECK_NULL_VOID(grid);
241 auto onDragStart = [func = std::move(value)](const ItemDragInfo& dragInfo, int32_t index) -> RefPtr<Component> {
242 ScopedViewStackProcessor builderViewStackProcessor;
243 {
244 func(dragInfo, index);
245 }
246 return ViewStackProcessor::GetInstance()->Finish();
247 };
248 grid->SetOnGridDragStartId(onDragStart);
249 }
250
SetOnItemDragEnter(std::function<void (const ItemDragInfo &)> && value)251 void GridModelImpl::SetOnItemDragEnter(std::function<void(const ItemDragInfo&)>&& value)
252 {
253 auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
254 CHECK_NULL_VOID(grid);
255 grid->SetOnGridDragEnterId(value);
256 }
257
SetOnItemDragMove(std::function<void (const ItemDragInfo &,int32_t,int32_t)> && value)258 void GridModelImpl::SetOnItemDragMove(std::function<void(const ItemDragInfo&, int32_t, int32_t)>&& value)
259 {
260 auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
261 CHECK_NULL_VOID(grid);
262 grid->SetOnGridDragMoveId(value);
263 }
264
SetOnItemDragLeave(std::function<void (const ItemDragInfo &,int32_t)> && value)265 void GridModelImpl::SetOnItemDragLeave(std::function<void(const ItemDragInfo&, int32_t)>&& value)
266 {
267 auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
268 CHECK_NULL_VOID(grid);
269 grid->SetOnGridDragLeaveId(value);
270 }
271
SetOnItemDrop(std::function<void (const ItemDragInfo &,int32_t,int32_t,bool)> && value)272 void GridModelImpl::SetOnItemDrop(std::function<void(const ItemDragInfo&, int32_t, int32_t, bool)>&& value)
273 {
274 auto grid = AceType::DynamicCast<GridLayoutComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
275 CHECK_NULL_VOID(grid);
276 grid->SetOnGridDropId(value);
277 }
278
CreatePositionController()279 RefPtr<ScrollControllerBase> GridModelImpl::CreatePositionController()
280 {
281 return AceType::MakeRefPtr<V2::GridPositionController>();
282 }
283
CreateScrollBarProxy()284 RefPtr<ScrollProxy> GridModelImpl::CreateScrollBarProxy()
285 {
286 return AceType::MakeRefPtr<ScrollBarProxy>();
287 }
288
289 } // namespace OHOS::Ace::Framework
290