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