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