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