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