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 #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 dimension value from platform constants. 74 * Dimension with 0.0 will be returned if not found or value is not Dimension. 75 * @param[in] key Target dimension key. 76 * @return Dimension corresponding to the key. 77 */ 78 Dimension GetDimension(uint32_t key) const; 79 80 /* 81 * Get int32_t value from platform constants. 82 * NOTE: -1 will be returned if not found or value is not int32_t. 83 * @param[in] key Target key. 84 * @return Int value corresponding to the key. 85 */ 86 int32_t GetInt(uint32_t key) const; 87 88 /* 89 * Get double value from platform constants. 90 * NOTE: 0.0 will be returned if not found or value is not double. 91 * @param[in] key Target key. 92 * @return Double value corresponding to the key. 93 */ 94 double GetDouble(uint32_t key) const; 95 96 /* 97 * Get string value from platform constants. 98 * NOTE: empty string will be returned if not found or value is not string. 99 * @param[in] key Target key. 100 * @return String value corresponding to the key. 101 */ 102 std::string GetString(uint32_t key) const; 103 104 /* 105 * Get plural string value from platform constants. 106 * NOTE: empty string will be returned if not found. 107 * @param[in] key Target key, count Target plural string quantity. 108 * @return plural string value corresponding to the key and count. 109 */ 110 std::string GetPluralString(uint32_t key, int count) const; 111 112 /* 113 * Get bool value from platform constants. 114 * NOTE: false will be returned if not found or value is not boolean. 115 * @param[in] key Target key. 116 * @return bool value corresponding to the key. 117 */ 118 bool GetBoolean(uint32_t key) const; 119 120 /* 121 * Get int array value from platform constants. 122 * NOTE: empty array will be returned if not found or value is not boolean. 123 * @param[in] key Target key. 124 * @return int array value corresponding to the key. 125 */ 126 std::vector<uint32_t> GetIntArray(uint32_t key) const; 127 128 /* 129 * Get ResourceId from platform constants. 130 * NOTE: ResourceId::NO_ID will be returned if not found or value is not ResourceId. 131 */ 132 InternalResource::ResourceId GetResourceId(uint32_t key) const; 133 134 /* 135 * Get string array value from platform constants. 136 * NOTE: empty array will be returned if not found or value is not boolean. 137 * @param[in] key Target key. 138 * @return string array value corresponding to the key. 139 */ 140 std::vector<std::string> GetStringArray(uint32_t key) const; 141 142 /* 143 * Get media path value from platform constants. 144 * NOTE: empty string will be returned if not found. 145 * @param[in] key Target key. 146 * @return media path value corresponding to the key. 147 */ 148 std::string GetMediaPath(uint32_t key) const; 149 150 /* 151 * Get rawfile path. 152 * NOTE: empty string will be returned if not found. 153 * @param[in] fileName Target file name. 154 * @return rawfile path value corresponding to file name. 155 */ 156 std::string GetRawfile(const std::string& fileName) const; 157 158 template<class T> GetMediaResource(T & resId,std::ostream & dest)159 bool GetMediaResource(T& resId, std::ostream& dest) const 160 { 161 if (!resAdapter_) { 162 return false; 163 } 164 return resAdapter_->GetResource(resId, dest); 165 } 166 167 bool GetResourceIdByName(const std::string& resName, const std::string& resType, uint32_t& resId) const; 168 169 void LoadCustomStyle(const RefPtr<AssetManager>& assetManager); 170 171 /* 172 * Load theme from system resource. 173 */ 174 void LoadTheme(int32_t themeId); 175 GetThemeStyle()176 RefPtr<ThemeStyle> GetThemeStyle() const 177 { 178 return currentThemeStyle_; 179 } 180 181 void SetColorScheme(ColorScheme colorScheme); 182 HasCustomStyle(uint32_t key)183 bool HasCustomStyle(uint32_t key) const 184 { 185 return customStyleMap_.find(key) != customStyleMap_.end(); 186 } 187 188 private: 189 static const ResValueWrapper* GetPlatformConstants(uint32_t key); 190 static const ResValueWrapper* styleMapDefault[]; 191 static uint32_t DefaultMapCount; 192 #ifdef WEARABLE_PRODUCT 193 static const ResValueWrapper* styleMapWatch[]; 194 static uint32_t WatchMapCount; 195 #else 196 static const ResValueWrapper* styleMapTv[]; 197 static uint32_t TvMapCount; 198 #endif 199 200 ResValueWrapper GetValue(uint32_t key) const; 201 double GetBlendAlpha(const BlendAlpha& blendAlpha) const; 202 void ParseCustomStyle(const std::string& content); 203 void LoadFile(const RefPtr<Asset>& asset); 204 205 RefPtr<ResourceAdapter> resAdapter_; 206 RefPtr<ThemeStyle> currentThemeStyle_; 207 ThemeConstantsMap customStyleMap_; 208 209 ACE_DISALLOW_COPY_AND_MOVE(ThemeConstants); 210 }; 211 212 } // namespace OHOS::Ace 213 214 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_CONSTANTS_H 215