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