1 /* 2 * Copyright (c) 2021-2024 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_THEME_THEME_CONSTANTS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_CONSTANTS_H 18 19 #include <unordered_map> 20 21 #include "base/geometry/dimension.h" 22 #include "base/image/pixel_map.h" 23 #include "base/resource/asset_manager.h" 24 #include "base/resource/internal_resource.h" 25 #include "base/utils/macros.h" 26 #include "core/components/common/layout/constants.h" 27 #include "core/components/common/properties/color.h" 28 #include "core/components/common/properties/edge.h" 29 #include "core/components/common/properties/radius.h" 30 #include "core/components/common/properties/text_style.h" 31 #include "core/components/theme/resource_adapter.h" 32 #include "core/components/theme/theme_attributes.h" 33 #include "core/components/theme/theme_style.h" 34 namespace OHOS::Ace { 35 36 class ACE_FORCE_EXPORT ThemeConstants : public AceType { 37 DECLARE_ACE_TYPE(ThemeConstants, AceType); 38 39 public: ThemeConstants(RefPtr<ResourceAdapter> resourceAdapter)40 explicit ThemeConstants(RefPtr<ResourceAdapter> resourceAdapter) : resAdapter_(resourceAdapter) {} 41 ~ThemeConstants() override = default; 42 43 /* 44 * Init properties at platform. 45 */ 46 static void InitDeviceType(); 47 InitResource(const ResourceInfo & resourceInfo)48 void InitResource(const ResourceInfo& resourceInfo) 49 { 50 if (resAdapter_) { 51 resAdapter_->Init(resourceInfo); 52 } 53 } 54 UpdateConfig(const ResourceConfiguration & config)55 void UpdateConfig(const ResourceConfiguration& config) 56 { 57 if (resAdapter_) { 58 resAdapter_->UpdateConfig(config); 59 } 60 } 61 62 void ParseTheme(); 63 64 /* 65 * Get color value from platform constants. 66 * Color::BLACK will be returned if value not found or corresponding value is not Color. 67 * @param[in] key Target color key. 68 * @return Color corresponding to the key. 69 */ 70 Color GetColor(uint32_t key) const; 71 72 /* 73 * Get color value from platform constants. 74 * Color::BLACK will be returned if value not found or corresponding value is not Color. 75 * @param[in] key Target color key. 76 * @return Color corresponding to the name. 77 */ 78 Color GetColorByName(const std::string& resName) const; 79 80 /* 81 * Get dimension value from platform constants. 82 * Dimension with 0.0 will be returned if not found or value is not Dimension. 83 * @param[in] key Target dimension key. 84 * @return Dimension corresponding to the key. 85 */ 86 Dimension GetDimension(uint32_t key) const; 87 88 /* 89 * Get dimension value from platform constants. 90 * Dimension with 0.0 will be returned if not found or value is not Dimension. 91 * @param[in] name Target dimension name. 92 * @return Dimension corresponding to the name. 93 */ 94 Dimension GetDimensionByName(const std::string& resName) const; 95 96 /* 97 * Get int32_t value from platform constants. 98 * NOTE: -1 will be returned if not found or value is not int32_t. 99 * @param[in] key Target key. 100 * @return Int value corresponding to the key. 101 */ 102 int32_t GetInt(uint32_t key) const; 103 104 /* 105 * Get int32_t value from platform constants. 106 * NOTE: -1 will be returned if not found or value is not int32_t. 107 * @param[in] key Target key. 108 * @return Int value corresponding to the key. 109 */ 110 int32_t GetIntByName(const std::string& resName) const; 111 112 /* 113 * Get double value from platform constants. 114 * NOTE: 0.0 will be returned if not found or value is not double. 115 * @param[in] key Target key. 116 * @return Double value corresponding to the key. 117 */ 118 double GetDouble(uint32_t key) const; 119 120 /* 121 * Get double value from platform constants. 122 * NOTE: 0.0 will be returned if not found or value is not double. 123 * @param[in] key Target key. 124 * @return Double value corresponding to the key. 125 */ 126 double GetDoubleByName(const std::string& resName) const; 127 128 /* 129 * Get string value from platform constants. 130 * NOTE: empty string will be returned if not found or value is not string. 131 * @param[in] key Target key. 132 * @return String value corresponding to the key. 133 */ 134 std::string GetString(uint32_t key) const; 135 136 /* 137 * Get string value from platform constants. 138 * NOTE: empty string will be returned if not found or value is not string. 139 * @param[in] key Target key. 140 * @return String value corresponding to the key. 141 */ 142 std::string GetStringByName(const std::string& resName) const; 143 144 /* 145 * Get plural string value from platform constants. 146 * NOTE: empty string will be returned if not found. 147 * @param[in] key Target key, count Target plural string quantity. 148 * @return plural string value corresponding to the key and count. 149 */ 150 std::string GetPluralString(uint32_t key, int count) const; 151 152 /* 153 * Get plural string value from platform constants. 154 * NOTE: empty string will be returned if not found. 155 * @param[in] key Target key, count Target plural string quantity. 156 * @return plural string value corresponding to the name and count. 157 */ 158 std::string GetPluralStringByName(const std::string& resName, int count) const; 159 160 /* 161 * Get bool value from platform constants. 162 * NOTE: false will be returned if not found or value is not boolean. 163 * @param[in] key Target key. 164 * @return bool value corresponding to the key. 165 */ 166 bool GetBoolean(uint32_t key) const; 167 168 /* 169 * Get bool value from platform constants. 170 * NOTE: false will be returned if not found or value is not boolean. 171 * @param[in] key Target key. 172 * @return bool value corresponding to the name. 173 */ 174 bool GetBooleanByName(const std::string& resName) const; 175 176 /* 177 * Get int value from platform constants. 178 * NOTE: false will be returned if not found or value is not uint32_t. 179 * @param[in] key Target key. 180 * @return uint32_t value corresponding to the name. 181 */ 182 uint32_t GetSymbolByName(const char *name) const; 183 184 /* 185 * Get int value from platform constants. 186 * NOTE: 0 will be returned if not found or value is not uint32_t. 187 * @param[in] key Target key. 188 * @return uint32_t value corresponding to the name. 189 */ 190 uint32_t GetSymbolById(uint32_t resId) const; 191 192 /* 193 * Get int array value from platform constants. 194 * NOTE: empty array will be returned if not found or value is not boolean. 195 * @param[in] key Target key. 196 * @return int array value corresponding to the key. 197 */ 198 std::vector<uint32_t> GetIntArray(uint32_t key) const; 199 200 /* 201 * Get int array value from platform constants. 202 * NOTE: empty array will be returned if not found or value is not boolean. 203 * @param[in] key Target key. 204 * @return int array value corresponding to the name. 205 */ 206 std::vector<uint32_t> GetIntArrayByName(const std::string& resName) const; 207 208 /* 209 * Get ResourceId from platform constants. 210 * NOTE: ResourceId::NO_ID will be returned if not found or value is not ResourceId. 211 */ 212 InternalResource::ResourceId GetResourceId(uint32_t key) const; 213 214 /* 215 * Get PixelMap from platform constants. 216 * NOTE: nullptr will be returned if getPixelMap failed. 217 */ 218 std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t key) const; 219 220 /* 221 * Get string array value from platform constants. 222 * NOTE: empty array will be returned if not found or value is not boolean. 223 * @param[in] key Target key. 224 * @return string array value corresponding to the key. 225 */ 226 std::vector<std::string> GetStringArray(uint32_t key) const; 227 228 /* 229 * Get string array value from platform constants. 230 * NOTE: empty array will be returned if not found or value is not boolean. 231 * @param[in] key Target key. 232 * @return string array value corresponding to the name. 233 */ 234 std::vector<std::string> GetStringArrayByName(const std::string& resName) const; 235 236 /* 237 * Get media path value from platform constants. 238 * NOTE: empty string will be returned if not found. 239 * @param[in] key Target key. 240 * @return media path value corresponding to the key. 241 */ 242 std::string GetMediaPath(uint32_t key) const; 243 244 /* 245 * Get media path value from platform constants. 246 * NOTE: empty string will be returned if not found. 247 * @param[in] key Target key. 248 * @return media path value corresponding to the name. 249 */ 250 std::string GetMediaPathByName(const std::string& resName) const; 251 252 /* 253 * Get rawfile path. 254 * NOTE: empty string will be returned if not found. 255 * @param[in] fileName Target file name. 256 * @return rawfile path value corresponding to file name. 257 */ 258 std::string GetRawfile(const std::string& fileName) const; 259 260 /* 261 * Get rawfile file description. 262 * NOTE: false value will be returned if not found. 263 * @param[in] rawfileName Target rawfile, rawfileDescription Target file info. 264 * @return success or not to get file info. 265 */ 266 bool GetRawFileDescription(const std::string& rawfileName, RawfileDescription& rawfileDescription) const; 267 268 /* 269 * Close rawfile file description. 270 * NOTE: false value will be returned if not found. 271 * @param[in] rawfileName Target rawfile. 272 * @return success or not to close file info. 273 */ 274 bool CloseRawFileDescription(const std::string& rawfileName) const; 275 276 /* 277 * Get rawfile file description which will not be cached. 278 * NOTE: false value will be returned if not found. 279 * @param[in] rawfileName Target rawfile. 280 * @return success or not to close file info. 281 */ 282 bool GetRawFD(const std::string& rawfileName, RawfileDescription& rawfileDescription) const; 283 284 /* 285 * Get resource media file path. 286 * NOTE: false value will be returned if not found. 287 * @param[in] resId Target resource id, mediaPath Target media path. 288 * @return success or not to get media path. 289 */ 290 bool GetMediaById(const int32_t& resId, std::string& mediaPath) const; 291 292 template<class T> GetMediaResource(T & resId,std::ostream & dest)293 bool GetMediaResource(T& resId, std::ostream& dest) const 294 { 295 if (!resAdapter_) { 296 return false; 297 } 298 return resAdapter_->GetResource(resId, dest); 299 } 300 301 template<class T> GetMediaData(T & resId,size_t & len,std::unique_ptr<uint8_t[]> & dest)302 bool GetMediaData(T& resId, size_t& len, std::unique_ptr<uint8_t[]>& dest) 303 { 304 if (!resAdapter_) { 305 return false; 306 } 307 return resAdapter_->GetMediaData(resId, len, dest); 308 } 309 310 template<class T> GetMediaData(T & resId,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)311 bool GetMediaData(T& resId, size_t& len, std::unique_ptr<uint8_t[]>& dest, const std::string& bundleName, 312 const std::string& moduleName) 313 { 314 if (!resAdapter_) { 315 return false; 316 } 317 return resAdapter_->GetMediaData(resId, len, dest, bundleName, moduleName); 318 } 319 GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest)320 bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest) 321 { 322 if (!resAdapter_) { 323 return false; 324 } 325 return resAdapter_->GetRawFileData(rawFile, len, dest); 326 } 327 GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest,const std::string & bundleName,const std::string & moduleName)328 bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest, 329 const std::string& bundleName, const std::string& moduleName) 330 { 331 if (!resAdapter_) { 332 return false; 333 } 334 return resAdapter_->GetRawFileData(rawFile, len, dest, bundleName, moduleName); 335 } 336 337 bool GetResourceIdByName(const std::string& resName, const std::string& resType, uint32_t& resId) const; 338 339 void LoadCustomStyle(const RefPtr<AssetManager>& assetManager); 340 341 /* 342 * Load theme from system resource. 343 */ 344 void LoadTheme(int32_t themeId); 345 GetThemeStyle()346 RefPtr<ThemeStyle> GetThemeStyle() const 347 { 348 return currentThemeStyle_; 349 } 350 351 void SetColorScheme(ColorScheme colorScheme); 352 HasCustomStyle(uint32_t key)353 bool HasCustomStyle(uint32_t key) const 354 { 355 return customStyleMap_.find(key) != customStyleMap_.end(); 356 } 357 UpdateThemeConstants(const std::string & bundleName,const std::string & moduleName)358 void UpdateThemeConstants(const std::string& bundleName, const std::string& moduleName) 359 { 360 if (resAdapter_) { 361 resAdapter_->UpdateResourceManager(bundleName, moduleName); 362 } 363 } 364 UpdateResourceAdapter(const RefPtr<ResourceAdapter> & adapter)365 void UpdateResourceAdapter(const RefPtr<ResourceAdapter>& adapter) 366 { 367 resAdapter_ = adapter; 368 } 369 GetResourceLimitKeys()370 uint32_t GetResourceLimitKeys() const 371 { 372 CHECK_NULL_RETURN(resAdapter_, 0); 373 return resAdapter_->GetResourceLimitKeys(); 374 } 375 GetResourceAdapter()376 RefPtr<ResourceAdapter> GetResourceAdapter() 377 { 378 return resAdapter_; 379 } 380 381 RefPtr<ThemeStyle> GetPatternByName(const std::string& patternName); 382 private: 383 static const ResValueWrapper* GetPlatformConstants(uint32_t key); 384 static const ResValueWrapper* styleMapDefault[]; 385 static uint32_t DefaultMapCount; 386 #ifdef WEARABLE_PRODUCT 387 static const ResValueWrapper* styleMapWatch[]; 388 static uint32_t WatchMapCount; 389 #else 390 static const ResValueWrapper* styleMapTv[]; 391 static uint32_t TvMapCount; 392 #endif 393 394 ResValueWrapper GetValue(uint32_t key) const; 395 double GetBlendAlpha(const BlendAlpha& blendAlpha) const; 396 void ParseCustomStyle(const std::string& content); 397 void LoadFile(const RefPtr<Asset>& asset); 398 399 RefPtr<ResourceAdapter> resAdapter_; 400 RefPtr<ThemeStyle> currentThemeStyle_; 401 ThemeConstantsMap customStyleMap_; 402 403 ACE_DISALLOW_COPY_AND_MOVE(ThemeConstants); 404 }; 405 406 } // namespace OHOS::Ace 407 408 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_CONSTANTS_H 409