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_column_info.h"
17 
18 #include "core/components/common/layout/grid_system_manager.h"
19 
20 namespace OHOS::Ace {
21 
22 /* set offset by grid column number */
SetOffset(int32_t offset,GridSizeType type)23 void GridColumnInfo::Builder::SetOffset(int32_t offset, GridSizeType type)
24 {
25     columnInfo_->hasColumnOffset_ = true;
26     if (IsValid(type)) {
27         columnInfo_->offsets_[type] = offset;
28     }
29 }
30 
GetWidth() const31 double GridColumnInfo::GetWidth() const
32 {
33     if (!parent_) {
34         LOGE("no parent info");
35         return 0.0;
36     }
37     auto sizeType = parent_->GetSizeType();
38     uint32_t columns = IsValid(sizeType) ? columns_[sizeType] : 0;
39     columns = columns > 0 ? columns : columns_[GridSizeType::UNDEFINED];
40     return (columns <= 0) ? 0.0 : GetWidth(columns);
41 }
42 
GetWidth(uint32_t columns) const43 double GridColumnInfo::GetWidth(uint32_t columns) const
44 {
45     if (!parent_) {
46         LOGE("no parent info");
47         return 0.0;
48     }
49     double dipScale = GridSystemManager::GetInstance().GetDipScale();
50     return columns == 0 ? 0.0
51                         : (columns * parent_->GetColumnWidth()) +
52                               ((columns - 1) * parent_->GetGutterWidth().ConvertToPx(dipScale));
53 }
54 
GetMaxWidth() const55 double GridColumnInfo::GetMaxWidth() const
56 {
57     if (!parent_) {
58         LOGE("no parent info");
59         return 0.0;
60     }
61 
62     uint32_t columns = 0;
63     auto sizeType = parent_->GetSizeType();
64     if (IsValid(sizeType)) {
65         columns = maxColumns_[sizeType];
66         if (columns == 0) {
67             columns = columns_[sizeType];
68         }
69     }
70     columns = columns > 0 ? columns : columns_[GridSizeType::UNDEFINED];
71     return GetWidth(columns);
72 }
73 
GetOffset() const74 Dimension GridColumnInfo::GetOffset() const
75 {
76     if (!parent_) {
77         LOGE("no parent info");
78         return UNDEFINED_DIMENSION;
79     }
80 
81     /* ace1.0 obsolete logic since 6 */
82     auto sizeType = parent_->GetSizeType();
83     if (!hasColumnOffset_) {
84         return IsValid(sizeType) ? dimOffsets_[sizeType] : UNDEFINED_DIMENSION;
85     }
86 
87     /* ace2.0 */
88     int32_t offset = INVALID_OFFSET;
89     if (IsValid(sizeType)) {
90         offset = offsets_[sizeType];
91     }
92 
93     if (offset == INVALID_OFFSET) {
94         offset = offsets_[GridSizeType::UNDEFINED]; // use common offset
95     }
96     if (offset == INVALID_OFFSET) {
97         return UNDEFINED_DIMENSION;
98     }
99     double dipScale = GridSystemManager::GetInstance().GetDipScale();
100     double offsetVp = offset * (parent_->GetColumnWidth() + parent_->GetGutterWidth().ConvertToPx(dipScale));
101     return Dimension(offsetVp, DimensionUnit::PX);
102 }
103 
104 } // namespace OHOS::Ace
105