1 /* 2 * Copyright (c) 2022-2025 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_GRID_LAYOUT_GRID_CONTAINER_UTIL_CLASS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_GRID_LAYOUT_GRID_CONTAINER_UTIL_CLASS_H 18 19 #include <functional> 20 #include <sstream> 21 #include <utility> 22 23 #include "base/geometry/dimension.h" 24 #include "base/memory/ace_type.h" 25 #include "base/memory/referenced.h" 26 #include "core/common/resource/resource_object.h" 27 #include "core/components/common/layout/grid_container_info.h" 28 29 namespace OHOS::Ace::V2 { 30 31 constexpr int32_t DEFAULT_COLUMN_NUMBER = 12; 32 constexpr int32_t DEFAULT_XS_COLUMN = 2; 33 constexpr int32_t DEFAULT_SM_COLUMN = 4; 34 constexpr int32_t DEFAULT_MD_COLUMN = 8; 35 constexpr int32_t DEFAULT_LG_COLUMN = 12; 36 37 struct GridContainerSize : public Referenced { 38 GridContainerSize() = default; GridContainerSizeGridContainerSize39 explicit GridContainerSize(int32_t column) 40 { 41 xs = column; 42 sm = column; 43 md = column; 44 lg = column; 45 xl = column; 46 xxl = column; 47 }; 48 DEFINE_COPY_CONSTRUCTOR_AND_COPY_OPERATOR_AND_COMPARE_OPERATOR_WITH_PROPERTIES( 49 GridContainerSize, (xs)(sm)(md)(lg)(xl)(xxl)) 50 int32_t xs = DEFAULT_XS_COLUMN; 51 int32_t sm = DEFAULT_SM_COLUMN; 52 int32_t md = DEFAULT_MD_COLUMN; 53 int32_t lg = DEFAULT_LG_COLUMN; 54 int32_t xl = DEFAULT_LG_COLUMN; 55 int32_t xxl = DEFAULT_LG_COLUMN; 56 ToStringGridContainerSize57 std::string ToString() 58 { 59 std::stringstream ss; 60 ss << "GridContainerSize: {"; 61 ss << "xs: " << xs << ", "; 62 ss << "sm: " << sm << ", "; 63 ss << "md: " << md << ", "; 64 ss << "lg: " << lg << ", "; 65 ss << "xl: " << xl << ", "; 66 ss << "xxl: " << xxl; 67 ss << " }"; 68 return ss.str(); 69 } 70 }; 71 72 enum class BreakPointsReference { 73 WindowSize, 74 ComponentSize, 75 }; 76 77 enum class GridRowDirection { 78 Row, 79 RowReverse, 80 }; 81 82 enum class GridSizeType { 83 XS = 0, 84 SM = 1, 85 MD = 2, 86 LG = 3, 87 XL = 4, 88 XXL = 5, 89 UNDEFINED = 6, 90 }; 91 92 struct GridSizeInfo : public Referenced { 93 std::vector<Dimension> sizeInfo { 94 Dimension(320, DimensionUnit::VP), 95 Dimension(600, DimensionUnit::VP), 96 Dimension(840, DimensionUnit::VP), 97 }; 98 ResetGridSizeInfo99 void Reset() 100 { 101 sizeInfo.clear(); 102 } 103 }; 104 105 class Gutter : public AceType { 106 DECLARE_ACE_TYPE(Gutter, AceType); 107 108 public: 109 Gutter() = default; 110 DEFINE_COPY_CONSTRUCTOR_AND_COPY_OPERATOR_AND_COMPARE_OPERATOR_WITH_PROPERTIES( 111 Gutter, (xXs)(yXs)(xSm)(ySm)(xMd)(yMd)(xLg)(yLg)(xXl)(yXl)(xXXl)(yXXl)) Gutter(Dimension dimension)112 explicit Gutter(Dimension dimension) 113 : xXs(dimension), yXs(dimension), xSm(dimension), ySm(dimension), xMd(dimension), yMd(dimension), 114 xLg(dimension), yLg(dimension), xXl(dimension), yXl(dimension), xXXl(dimension), yXXl(dimension) {}; Gutter(Dimension xDimension,Dimension yDimension)115 Gutter(Dimension xDimension, Dimension yDimension) 116 : xXs(xDimension), yXs(yDimension), xSm(xDimension), ySm(yDimension), xMd(xDimension), yMd(yDimension), 117 xLg(xDimension), yLg(yDimension), xXl(xDimension), yXl(yDimension), xXXl(xDimension), yXXl(yDimension) {}; 118 SetYGutter(Dimension yDimension)119 void SetYGutter(Dimension yDimension) 120 { 121 yXs = yDimension; 122 ySm = yDimension; 123 yMd = yDimension; 124 yLg = yDimension; 125 yXl = yDimension; 126 yXXl = yDimension; 127 } 128 SetXGutter(Dimension xDimension)129 void SetXGutter(Dimension xDimension) 130 { 131 xXs = xDimension; 132 xSm = xDimension; 133 xMd = xDimension; 134 xLg = xDimension; 135 xXl = xDimension; 136 xXXl = xDimension; 137 } 138 Dimension xXs; 139 Dimension yXs; 140 Dimension xSm; 141 Dimension ySm; 142 Dimension xMd; 143 Dimension yMd; 144 Dimension xLg; 145 Dimension yLg; 146 Dimension xXl; 147 Dimension yXl; 148 Dimension xXXl; 149 Dimension yXXl; 150 struct resourceUpdater { 151 RefPtr<ResourceObject> resObj; 152 std::function<void(const RefPtr<ResourceObject>&, RefPtr<V2::Gutter>&)> updateFunc; 153 }; 154 std::unordered_map<std::string, resourceUpdater> resMap_; 155 ToString()156 std::string ToString() 157 { 158 std::stringstream ss; 159 ss << "Gutter: {"; 160 ss << "xXs: " << xXs.ToString().c_str() << ", "; 161 ss << "yXs: " << yXs.ToString().c_str() << ", "; 162 ss << "xSm: " << xSm.ToString().c_str() << ", "; 163 ss << "ySm: " << ySm.ToString().c_str() << ", "; 164 ss << "xMd: " << xMd.ToString().c_str() << ", "; 165 ss << "yMd: " << yMd.ToString().c_str() << ", "; 166 ss << "xLg: " << xLg.ToString().c_str() << ", "; 167 ss << "yLg: " << yLg.ToString().c_str() << ", "; 168 ss << "xXl: " << xXl.ToString().c_str() << ", "; 169 ss << "yXl: " << yXl.ToString().c_str() << ", "; 170 ss << "xXXl: " << xXXl.ToString().c_str() << ", "; 171 ss << "yXXl: " << yXXl.ToString().c_str(); 172 ss << " }"; 173 return ss.str(); 174 } 175 AddResource(const std::string & key,const RefPtr<ResourceObject> & resObj,std::function<void (const RefPtr<ResourceObject> &,RefPtr<V2::Gutter> &)> && updateFunc)176 void AddResource( 177 const std::string& key, 178 const RefPtr<ResourceObject>& resObj, 179 std::function<void(const RefPtr<ResourceObject>&, RefPtr<V2::Gutter>&)>&& updateFunc) 180 { 181 if (resObj == nullptr || !updateFunc) { 182 return; 183 } 184 resMap_[key] = {resObj, std::move(updateFunc)}; 185 } 186 ReloadResources(RefPtr<V2::Gutter> & gutter)187 void ReloadResources(RefPtr<V2::Gutter>& gutter) 188 { 189 for (const auto& [key, resourceUpdater] : resMap_) { 190 resourceUpdater.updateFunc(resourceUpdater.resObj, gutter); 191 } 192 } 193 }; 194 195 class BreakPoints : public AceType { 196 DECLARE_ACE_TYPE(BreakPoints, AceType); 197 198 public: 199 BreakPoints() = default; 200 DEFINE_COPY_CONSTRUCTOR_AND_COPY_OPERATOR_AND_COMPARE_OPERATOR_WITH_PROPERTIES( 201 BreakPoints, (reference)(breakpoints)(userDefine)) 202 BreakPointsReference reference = BreakPointsReference::WindowSize; 203 std::vector<std::string> breakpoints { "320vp", "600vp", "840vp" }; 204 bool userDefine = false; 205 ToString()206 std::string ToString() 207 { 208 std::stringstream ss; 209 ss << "BreakPoints: {"; 210 ss << "reference: " << static_cast<int32_t>(reference) << ", "; 211 ss << "breakpoints: ["; 212 for (const auto& breakpoint : breakpoints) { 213 ss << breakpoint << ", "; 214 } 215 ss << "]"; 216 ss << " }"; 217 return ss.str(); 218 } 219 }; 220 221 } // namespace OHOS::Ace::V2 222 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_GRID_LAYOUT_GRID_CONTAINER_UTIL_CLASS_H