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/common/layout/grid_container_info.h"
17
18 #include "base/log/log.h"
19 #include "base/utils/utils.h"
20 #include "core/components/common/layout/grid_layout_info.h"
21 #include "core/components/common/layout/grid_system_manager.h"
22
23 namespace OHOS::Ace {
24
25 template<typename T>
GetValue(const T & current,const T & system,const T & defaultValue)26 const T& GetValue(const T& current, const T& system, const T& defaultValue)
27 {
28 if (current == defaultValue) {
29 return system;
30 }
31 return current;
32 }
33
BuildColumnWidth()34 void GridContainerInfo::BuildColumnWidth()
35 {
36 BuildColumnWidth(GridSystemManager::GetInstance().GetScreenWidth());
37 }
38
BuildColumnWidth(double width)39 void GridContainerInfo::BuildColumnWidth(double width)
40 {
41 SystemGridInfo systemGridInfo;
42 if (sizeType_ != GridSizeType::UNDEFINED && currentSizeType_ == GridSizeType::UNDEFINED) {
43 systemGridInfo = GridSystemManager::GetInstance().GetSystemGridInfo(sizeType_);
44 // using fix size type
45 currentSizeType_ = sizeType_;
46 } else {
47 systemGridInfo = GridSystemManager::GetInstance().GetSystemGridInfo(templateType_, width);
48 if (currentSizeType_ != systemGridInfo.sizeType) {
49 // system size changed
50 currentSizeType_ = systemGridInfo.sizeType;
51 } else {
52 if (NearEqual(containerWidth_, width)) {
53 LOGW("container width not changed.");
54 return;
55 }
56 }
57 }
58 containerWidth_ = width;
59 double dipScale = GridSystemManager::GetInstance().GetDipScale();
60 // if not define the prop, use system grid define
61 int32_t columns = GetValue(columns_, systemGridInfo.columns, UNDEFINED_INT);
62 if (columns == 0) {
63 return;
64 }
65 double gutterWidth = GetValue(gutterWidth_, systemGridInfo.gutter, UNDEFINED_DIMENSION).ConvertToPx(dipScale);
66 double marginLeft = GetValue(marginLeft_, systemGridInfo.margin, UNDEFINED_DIMENSION).ConvertToPx(dipScale);
67 double marginRight = GetValue(marginRight_, systemGridInfo.margin, UNDEFINED_DIMENSION).ConvertToPx(dipScale);
68 double padding = GetValue(paddingLeft_, Dimension(), UNDEFINED_DIMENSION).ConvertToPx(dipScale) +
69 GetValue(paddingRight_, Dimension(), UNDEFINED_DIMENSION).ConvertToPx(dipScale);
70
71 columnWidth_ = (width - marginLeft - marginRight - padding - (columns - 1) * gutterWidth) / columns;
72 LOGI("width = %{public}f, columns: %{public}d columnWidth: %{public}f gutterWidth: %{public}f marginLeft: "
73 "%{public}f marginRight: %{public}f padding: %{public}f",
74 width, columns, columnWidth_, gutterWidth, marginLeft, marginRight, padding);
75 }
76
GetSizeType() const77 GridSizeType GridContainerInfo::GetSizeType() const
78 {
79 // if container don't want to use fix size type, use system current size type default
80 return sizeType_ != GridSizeType::UNDEFINED ? sizeType_ : currentSizeType_;
81 }
82
GetColumns() const83 int32_t GridContainerInfo::GetColumns() const
84 {
85 if (columns_ == UNDEFINED_INT) {
86 return GridSystemManager::GetInstance().GetSystemGridInfo(templateType_, containerWidth_).columns;
87 }
88 return columns_;
89 }
90
GetGutterWidth() const91 Dimension GridContainerInfo::GetGutterWidth() const
92 {
93 if (gutterWidth_ == UNDEFINED_DIMENSION) {
94 return GridSystemManager::GetInstance().GetSystemGridInfo(templateType_, containerWidth_).gutter;
95 }
96 return gutterWidth_;
97 }
98
GetMarginLeft() const99 Dimension GridContainerInfo::GetMarginLeft() const
100 {
101 if (marginLeft_ == UNDEFINED_DIMENSION) {
102 return GridSystemManager::GetInstance().GetSystemGridInfo(templateType_, containerWidth_).margin;
103 }
104 return marginLeft_;
105 }
106
GetMarginRight() const107 Dimension GridContainerInfo::GetMarginRight() const
108 {
109 if (marginRight_ == UNDEFINED_DIMENSION) {
110 return GridSystemManager::GetInstance().GetSystemGridInfo(templateType_, containerWidth_).margin;
111 }
112 return marginRight_;
113 }
114
115 } // namespace OHOS::Ace
116