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