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