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 { 22 constexpr int32_t INVALID_OFFSET = -1; 23 } 24 25 namespace OHOS::Ace { 26 27 /* set offset by grid column number */ SetOffset(int32_t offset,GridSizeType type)28void GridColumnInfo::Builder::SetOffset(int32_t offset, GridSizeType type) 29 { 30 if (!columnInfo_->hasColumnOffset_) { 31 for (uint8_t i = 0; i < sizeof(columnInfo_->offsets_) / sizeof(int32_t); i++) { 32 columnInfo_->offsets_[i] = INVALID_OFFSET; 33 } 34 columnInfo_->hasColumnOffset_ = true; 35 } 36 37 const int32_t arraySize = static_cast<const int32_t>(GridSizeType::XL); 38 int32_t typeVal = static_cast<int32_t>(type); 39 if (typeVal < arraySize) { 40 columnInfo_->offsets_[typeVal] = offset; 41 } 42 } 43 GetWidth() const44double GridColumnInfo::GetWidth() const 45 { 46 if (!parent_) { 47 LOGE("no parent info"); 48 return 0.0; 49 } 50 51 uint32_t columns = columns_; 52 auto sizeType = parent_->GetSizeType(); 53 switch (sizeType) { 54 case GridSizeType::XS: 55 columns = xsSizeColumn_ > 0 ? xsSizeColumn_ : columns_; 56 break; 57 case GridSizeType::SM: 58 columns = smSizeColumn_ > 0 ? smSizeColumn_ : columns_; 59 break; 60 case GridSizeType::MD: 61 columns = mdSizeColumn_ > 0 ? mdSizeColumn_ : columns_; 62 break; 63 case GridSizeType::LG: 64 columns = lgSizeColumn_ > 0 ? lgSizeColumn_ : columns_; 65 break; 66 case GridSizeType::XL: 67 columns = lgSizeColumn_ > 0 ? lgSizeColumn_ : columns_; 68 break; 69 default: 70 break; 71 } 72 return (columns == 0) ? 0.0 : GetWidth(columns); 73 } 74 GetWidth(uint32_t columns) const75double GridColumnInfo::GetWidth(uint32_t columns) const 76 { 77 if (!parent_) { 78 LOGE("no parent info"); 79 return 0.0; 80 } 81 double dipScale = GridSystemManager::GetInstance().GetDipScale(); 82 return columns == 0 ? 0.0 83 : (columns * parent_->GetColumnWidth()) + 84 ((columns - 1) * parent_->GetGutterWidth().ConvertToPx(dipScale)); 85 } 86 GetMaxWidth() const87double GridColumnInfo::GetMaxWidth() const 88 { 89 if (!parent_) { 90 LOGE("no parent info"); 91 return 0.0; 92 } 93 94 uint32_t columns = 0; 95 auto sizeType = parent_->GetSizeType(); 96 switch (sizeType) { 97 case GridSizeType::XS: 98 columns = xsSizeColumn_ > 0 ? xsSizeColumn_ : columns_; 99 break; 100 case GridSizeType::SM: 101 columns = smSizeMaxColumn_ > 0 ? smSizeMaxColumn_ : smSizeColumn_ > 0 ? smSizeColumn_ : columns_; 102 break; 103 case GridSizeType::MD: 104 columns = mdSizeMaxColumn_ > 0 ? mdSizeMaxColumn_ : mdSizeColumn_ > 0 ? mdSizeColumn_ : columns_; 105 break; 106 case GridSizeType::LG: 107 columns = lgSizeMaxColumn_ > 0 ? lgSizeMaxColumn_ : lgSizeColumn_ > 0 ? lgSizeColumn_ : columns_; 108 break; 109 default: 110 break; 111 } 112 113 return GetWidth(columns); 114 } 115 GetOffset() const116Dimension GridColumnInfo::GetOffset() const 117 { 118 if (!parent_) { 119 LOGE("no parent info"); 120 return UNDEFINED_DIMENSION; 121 } 122 123 /* ace1.0 obsolete logic since 6 */ 124 if (!hasColumnOffset_) { 125 Dimension dim = UNDEFINED_DIMENSION; 126 switch (parent_->GetSizeType()) { 127 case GridSizeType::XS: 128 dim = xsSizeOffset_; 129 break; 130 case GridSizeType::SM: 131 dim = smSizeOffset_; 132 break; 133 case GridSizeType::MD: 134 dim = mdSizeOffset_; 135 break; 136 case GridSizeType::LG: 137 dim = lgSizeOffset_; 138 break; 139 default: 140 break; 141 } 142 return dim; 143 } 144 145 /* ace2.0 */ 146 int32_t sizeType = static_cast<int32_t>(parent_->GetSizeType()); 147 int32_t offset = INVALID_OFFSET; 148 if (sizeType < static_cast<int32_t>(GridSizeType::XL)) { 149 offset = offsets_[sizeType]; 150 } 151 152 if (offset == INVALID_OFFSET) { 153 offset = offsets_[static_cast<int32_t>(GridSizeType::UNDEFINED)]; // use common offset 154 } 155 if (offset == INVALID_OFFSET) { 156 return UNDEFINED_DIMENSION; 157 } 158 double dipScale = GridSystemManager::GetInstance().GetDipScale(); 159 double offsetVp = offset * (parent_->GetColumnWidth() + parent_->GetGutterWidth().ConvertToPx(dipScale)); 160 return Dimension(offsetVp, DimensionUnit::PX); 161 } 162 163 } // namespace OHOS::Ace 164