1 /* 2 * Copyright (c) 2021-2022 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/resource/asset_manager.h" 23 #include "base/resource/internal_resource.h" 24 #include "base/utils/macros.h" 25 #include "core/components/common/layout/constants.h" 26 #include "core/components/common/properties/color.h" 27 #include "core/components/common/properties/edge.h" 28 #include "core/components/common/properties/radius.h" 29 #include "core/components/common/properties/text_style.h" 30 #include "core/components/theme/resource_adapter.h" 31 #include "core/components/theme/theme_attributes.h" 32 #include "core/components/theme/theme_style.h" 33 34 namespace OHOS::Ace { 35 36 class ACE_FORCE_EXPORT_WITH_PREVIEW 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 array value from platform constants. 178 * NOTE: empty array will be returned if not found or value is not boolean. 179 * @param[in] key Target key. 180 * @return int array value corresponding to the key. 181 */ 182 std::vector<uint32_t> GetIntArray(uint32_t key) const; 183 184 /* 185 * Get int array value from platform constants. 186 * NOTE: empty array will be returned if not found or value is not boolean. 187 * @param[in] key Target key. 188 * @return int array value corresponding to the name. 189 */ 190 std::vector<uint32_t> GetIntArrayByName(const std::string& resName) const; 191 192 /* 193 * Get ResourceId from platform constants. 194 * NOTE: ResourceId::NO_ID will be returned if not found or value is not ResourceId. 195 */ 196 InternalResource::ResourceId GetResourceId(uint32_t key) const; 197 198 /* 199 * Get string array value from platform constants. 200 * NOTE: empty array will be returned if not found or value is not boolean. 201 * @param[in] key Target key. 202 * @return string array value corresponding to the key. 203 */ 204 std::vector<std::string> GetStringArray(uint32_t key) const; 205 206 /* 207 * Get string array value from platform constants. 208 * NOTE: empty array will be returned if not found or value is not boolean. 209 * @param[in] key Target key. 210 * @return string array value corresponding to the name. 211 */ 212 std::vector<std::string> GetStringArrayByName(const std::string& resName) const; 213 214 /* 215 * Get media path value from platform constants. 216 * NOTE: empty string will be returned if not found. 217 * @param[in] key Target key. 218 * @return media path value corresponding to the key. 219 */ 220 std::string GetMediaPath(uint32_t key) const; 221 222 /* 223 * Get media path value from platform constants. 224 * NOTE: empty string will be returned if not found. 225 * @param[in] key Target key. 226 * @return media path value corresponding to the name. 227 */ 228 std::string GetMediaPathByName(const std::string& resName) const; 229 230 /* 231 * Get rawfile path. 232 * NOTE: empty string will be returned if not found. 233 * @param[in] fileName Target file name. 234 * @return rawfile path value corresponding to file name. 235 */ 236 std::string GetRawfile(const std::string& fileName) const; 237 238 template<class T> GetMediaResource(T & resId,std::ostream & dest)239 bool GetMediaResource(T& resId, std::ostream& dest) const 240 { 241 if (!resAdapter_) { 242 return false; 243 } 244 return resAdapter_->GetResource(resId, dest); 245 } 246 247 template<class T> GetMediaData(T & resId,size_t & len,std::unique_ptr<uint8_t[]> & dest)248 bool GetMediaData(T& resId, size_t& len, std::unique_ptr<uint8_t[]> &dest) 249 { 250 if (!resAdapter_) { 251 return false; 252 } 253 return resAdapter_->GetMediaData(resId, len, dest); 254 } 255 GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest)256 bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]> &dest) 257 { 258 if (!resAdapter_) { 259 return false; 260 } 261 return resAdapter_->GetRawFileData(rawFile, len, dest); 262 } 263 264 bool GetResourceIdByName(const std::string& resName, const std::string& resType, uint32_t& resId) const; 265 266 void LoadCustomStyle(const RefPtr<AssetManager>& assetManager); 267 268 /* 269 * Load theme from system resource. 270 */ 271 void LoadTheme(int32_t themeId); 272 GetThemeStyle()273 RefPtr<ThemeStyle> GetThemeStyle() const 274 { 275 return currentThemeStyle_; 276 } 277 278 void SetColorScheme(ColorScheme colorScheme); 279 HasCustomStyle(uint32_t key)280 bool HasCustomStyle(uint32_t key) const 281 { 282 return customStyleMap_.find(key) != customStyleMap_.end(); 283 } 284 UpdateThemeConstants(const std::string & bundleName,const std::string & moduleName)285 void UpdateThemeConstants(const std::string& bundleName, const std::string& moduleName) 286 { 287 if (resAdapter_) { 288 resAdapter_->UpdateResourceManager(bundleName, moduleName); 289 } 290 } 291 ReloadResource()292 void ReloadResource() 293 { 294 if (resAdapter_) { 295 resAdapter_->Reload(); 296 } 297 } 298 private: 299 static const ResValueWrapper* GetPlatformConstants(uint32_t key); 300 static const ResValueWrapper* styleMapDefault[]; 301 static uint32_t DefaultMapCount; 302 #ifdef WEARABLE_PRODUCT 303 static const ResValueWrapper* styleMapWatch[]; 304 static uint32_t WatchMapCount; 305 #else 306 static const ResValueWrapper* styleMapTv[]; 307 static uint32_t TvMapCount; 308 #endif 309 310 ResValueWrapper GetValue(uint32_t key) const; 311 double GetBlendAlpha(const BlendAlpha& blendAlpha) const; 312 void ParseCustomStyle(const std::string& content); 313 void LoadFile(const RefPtr<Asset>& asset); 314 315 RefPtr<ResourceAdapter> resAdapter_; 316 RefPtr<ThemeStyle> currentThemeStyle_; 317 ThemeConstantsMap customStyleMap_; 318 319 ACE_DISALLOW_COPY_AND_MOVE(ThemeConstants); 320 }; 321 322 } // namespace OHOS::Ace 323 324 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_CONSTANTS_H 325