1 /*
2 * Copyright (c) 2021 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 "core/components/grid_layout/grid_layout_component.h"
17
18 #include "core/components/grid_layout/grid_layout_element.h"
19 #include "core/components/grid_layout/render_grid_layout.h"
20 #include "core/components_v2/grid/grid_element.h"
21 #include "core/components_v2/grid/render_grid_scroll.h"
22 #include "core/pipeline/base/multi_composed_component.h"
23
24 namespace OHOS::Ace {
25
CreateElement()26 RefPtr<Element> GridLayoutComponent::CreateElement()
27 {
28 // judge to create GridLayoutElement for dynamic grid
29 if (UseNonProxiedCodePath()) {
30 return AceType::MakeRefPtr<GridLayoutElement>();
31 }
32 if (isDeclarative_ && useScroll_ && (rowsArgs_.empty() || columnsArgs_.empty())) {
33 return AceType::MakeRefPtr<V2::GridElement>();
34 }
35 return AceType::MakeRefPtr<GridLayoutElement>();
36 }
37
CreateRenderNode()38 RefPtr<RenderNode> GridLayoutComponent::CreateRenderNode()
39 {
40 // judge to create RenderGridLayout for dynamic grid
41 if (UseNonProxiedCodePath()) {
42 return RenderGridLayout::Create();
43 }
44 if (isDeclarative_ && useScroll_ && (rowsArgs_.empty() || columnsArgs_.empty())) {
45 return V2::RenderGridScroll::Create();
46 }
47
48 return RenderGridLayout::Create();
49 }
50
SetDirection(FlexDirection direction)51 void GridLayoutComponent::SetDirection(FlexDirection direction)
52 {
53 if (direction < FlexDirection::ROW || direction > FlexDirection::COLUMN_REVERSE) {
54 LOGW("Invalid direction %{public}d", direction);
55 return;
56 }
57 direction_ = direction;
58 }
59
SetFlexAlign(FlexAlign flexAlign)60 void GridLayoutComponent::SetFlexAlign(FlexAlign flexAlign)
61 {
62 if (flexAlign < FlexAlign::FLEX_START || flexAlign > FlexAlign::STRETCH) {
63 LOGW("Invalid flexAlign %{public}d", flexAlign);
64 return;
65 }
66 flexAlign_ = flexAlign;
67 }
68
SetColumnCount(int32_t count)69 void GridLayoutComponent::SetColumnCount(int32_t count)
70 {
71 if (count <= 0) {
72 LOGW("Invalid ColumnCount %{public}d", count);
73 return;
74 }
75 columnCount_ = count;
76 }
77
SetRowCount(int32_t count)78 void GridLayoutComponent::SetRowCount(int32_t count)
79 {
80 if (count <= 0) {
81 LOGW("Invalid RowCount %{public}d", count);
82 return;
83 }
84 rowCount_ = count;
85 }
86
SetWidth(double width)87 void GridLayoutComponent::SetWidth(double width)
88 {
89 if (width <= 0.0) {
90 LOGW("Invalid Width %{public}lf", width);
91 return;
92 }
93 width_ = width;
94 }
95
SetHeight(double height)96 void GridLayoutComponent::SetHeight(double height)
97 {
98 if (height <= 0.0) {
99 LOGW("Invalid Height %{public}lf", height);
100 return;
101 }
102 height_ = height;
103 }
104
SetColumnsArgs(const std::string & columnsArgs)105 void GridLayoutComponent::SetColumnsArgs(const std::string& columnsArgs)
106 {
107 columnsArgs_ = columnsArgs;
108 }
109
SetRowsArgs(const std::string & rowsArgs)110 void GridLayoutComponent::SetRowsArgs(const std::string& rowsArgs)
111 {
112 rowsArgs_ = rowsArgs;
113 }
114
SetColumnGap(const Dimension & columnGap)115 void GridLayoutComponent::SetColumnGap(const Dimension& columnGap)
116 {
117 if (columnGap.Value() < 0.0) {
118 LOGW("Invalid RowGap, use 0px");
119 columnGap_ = 0.0_px;
120 return;
121 }
122 columnGap_ = columnGap;
123 }
124
SetRowGap(const Dimension & rowGap)125 void GridLayoutComponent::SetRowGap(const Dimension& rowGap)
126 {
127 if (rowGap.Value() < 0.0) {
128 LOGW("Invalid RowGap, use 0px");
129 rowGap_ = 0.0_px;
130 return;
131 }
132 rowGap_ = rowGap;
133 }
134
SetRightToLeft(bool rightToLeft)135 void GridLayoutComponent::SetRightToLeft(bool rightToLeft)
136 {
137 rightToLeft_ = rightToLeft;
138 }
139
SetScrollBarColor(const std::string & color)140 void GridLayoutComponent::SetScrollBarColor(const std::string& color)
141 {
142 scrollBarColor_ = color;
143 }
144
SetScrollBarWidth(const std::string & width)145 void GridLayoutComponent::SetScrollBarWidth(const std::string& width)
146 {
147 scrollBarWidth_ = width;
148 }
149
SetScrollBar(DisplayMode displayMode)150 void GridLayoutComponent::SetScrollBar(DisplayMode displayMode)
151 {
152 displayMode_ = displayMode;
153 }
154
SetOnGridDragEnterId(const OnGridDragEnterFunc & onGridDragEnterId)155 void GridLayoutComponent::SetOnGridDragEnterId(const OnGridDragEnterFunc& onGridDragEnterId)
156 {
157 onGridDragEnterId_ = onGridDragEnterId;
158 }
159
SetOnGridDragMoveId(const OnGridDragMoveFunc & onGridDragMoveId)160 void GridLayoutComponent::SetOnGridDragMoveId(const OnGridDragMoveFunc& onGridDragMoveId)
161 {
162 onGridDragMoveId_ = onGridDragMoveId;
163 }
164
SetOnGridDragLeaveId(const OnGridDragLeaveFunc & onGridDragLeaveId)165 void GridLayoutComponent::SetOnGridDragLeaveId(const OnGridDragLeaveFunc& onGridDragLeaveId)
166 {
167 onGridDragLeaveId_ = onGridDragLeaveId;
168 }
169
SetOnGridDragStartId(const OnGridDragStartFunc & onGridDragStartId)170 void GridLayoutComponent::SetOnGridDragStartId(const OnGridDragStartFunc& onGridDragStartId)
171 {
172 onGridDragStartId_ = onGridDragStartId;
173 }
174
SetOnGridDropId(const OnGridDropFunc & onGridDropId)175 void GridLayoutComponent::SetOnGridDropId(const OnGridDropFunc& onGridDropId)
176 {
177 onGridDropId_ = onGridDropId;
178 }
179
GetOnGridDragEnterId() const180 const OnGridDragEnterFunc& GridLayoutComponent::GetOnGridDragEnterId() const
181 {
182 return onGridDragEnterId_;
183 }
184
GetOnGridDragMoveId() const185 const OnGridDragMoveFunc& GridLayoutComponent::GetOnGridDragMoveId() const
186 {
187 return onGridDragMoveId_;
188 }
189
GetOnGridDragLeaveId() const190 const OnGridDragLeaveFunc& GridLayoutComponent::GetOnGridDragLeaveId() const
191 {
192 return onGridDragLeaveId_;
193 }
194
GetOnGridDragStartId() const195 const OnGridDragStartFunc& GridLayoutComponent::GetOnGridDragStartId() const
196 {
197 return onGridDragStartId_;
198 }
199
GetOnGridDropId() const200 const OnGridDropFunc& GridLayoutComponent::GetOnGridDropId() const
201 {
202 return onGridDropId_;
203 }
204
205 } // namespace OHOS::Ace
206