1 /* 2 * Copyright (c) 2022-2023 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_MANAGER_IMPL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_MANAGER_IMPL_H 18 19 #include "core/components/theme/resource_adapter.h" 20 #include "core/components/theme/theme_manager.h" 21 #include "core/components_ng/token_theme/token_theme_wrapper.h" 22 23 namespace OHOS::Ace { 24 class ACE_EXPORT ThemeManagerImpl : public ThemeManager { 25 DECLARE_ACE_TYPE(ThemeManagerImpl, ThemeManager); 26 27 public: 28 ThemeManagerImpl(); 29 explicit ThemeManagerImpl(RefPtr<ResourceAdapter>& resourceAdapter); 30 ~ThemeManagerImpl() override = default; 31 InitResource(const ResourceInfo & resourceInfo)32 void InitResource(const ResourceInfo& resourceInfo) override 33 { 34 themeConstants_->InitResource(resourceInfo); 35 } 36 UpdateConfig(const ResourceConfiguration & config)37 void UpdateConfig(const ResourceConfiguration& config) override 38 { 39 themeConstants_->UpdateConfig(config); 40 } 41 SetSystemThemeId(int32_t themeId)42 void SetSystemThemeId(int32_t themeId) override 43 { 44 currentThemeId_ = themeId; 45 } 46 LoadSystemTheme(int32_t themeId)47 void LoadSystemTheme(int32_t themeId) override 48 { 49 currentThemeId_ = themeId; 50 themeConstants_->LoadTheme(themeId); 51 } 52 GetSystemTheme()53 int32_t GetSystemTheme() override 54 { 55 return currentThemeId_; 56 } 57 ParseSystemTheme()58 void ParseSystemTheme() override 59 { 60 themeConstants_->ParseTheme(); 61 } 62 LoadCustomTheme(const RefPtr<AssetManager> & assetManager)63 void LoadCustomTheme(const RefPtr<AssetManager>& assetManager) override 64 { 65 themeConstants_->LoadCustomStyle(assetManager); 66 } 67 68 /* 69 * Color scheme of the whole window, app bg color will be change in transparent scheme. 70 */ SetColorScheme(ColorScheme colorScheme)71 void SetColorScheme(ColorScheme colorScheme) override 72 { 73 themeConstants_->SetColorScheme(colorScheme); 74 } 75 76 /* 77 * Get color value from AppTheme (if exists) or system theme style. 78 * Prebuilt background color will be returned if AppTheme and system theme style both not exists. 79 * @return App background color. 80 */ 81 Color GetBackgroundColor() const override; 82 GetThemeConstants(const std::string & bundleName,const std::string & moduleName)83 RefPtr<ThemeConstants> GetThemeConstants( 84 const std::string& bundleName, const std::string& moduleName) const override 85 { 86 themeConstants_->UpdateThemeConstants(bundleName, moduleName); 87 return themeConstants_; 88 } 89 GetThemeConstants()90 RefPtr<ThemeConstants> GetThemeConstants() const override 91 { 92 return GetThemeConstants("", ""); 93 } 94 95 /* 96 * Get target theme, this function will cause construction of the theme if it not exists. 97 * @return Target component theme. 98 */ 99 RefPtr<Theme> GetTheme(ThemeType type) override; 100 101 template<typename T> GetTheme()102 RefPtr<T> GetTheme() 103 { 104 return AceType::DynamicCast<T>(GetTheme(T::TypeId())); 105 } 106 107 /* 108 * Get the theme and update it according to the TokenTheme, that given in param. 109 * @return Target component theme. 110 */ 111 RefPtr<Theme> GetTheme(ThemeType type, int32_t themeScopeId) override; 112 113 template<typename T> GetTheme(int32_t themeScopeId)114 RefPtr<T> GetTheme(int32_t themeScopeId) 115 { 116 return AceType::DynamicCast<T>(GetTheme(T::TypeId()), themeScopeId); 117 } 118 119 void LoadResourceThemes() override; 120 GetResourceLimitKeys()121 uint32_t GetResourceLimitKeys() const override 122 { 123 return themeConstants_->GetResourceLimitKeys(); 124 } 125 126 void RegisterThemeKit(ThemeType type, Ace::Kit::BuildFunc func) override; 127 128 RefPtr<Theme> GetThemeOrigin(ThemeType type); 129 130 RefPtr<Theme> GetThemeKit(ThemeType type); 131 132 void RegisterCustomThemeKit(ThemeType type, Ace::Kit::BuildThemeWrapperFunc func) override; 133 134 RefPtr<Theme> GetThemeOrigin(ThemeType type, int32_t themeScopeId); 135 136 RefPtr<Theme> GetThemeKit(ThemeType type, int32_t themeScopeId); 137 138 private: 139 using ThemeWrappers = std::unordered_map<ThemeType, RefPtr<TokenThemeWrapper>>; 140 std::unordered_map<ThemeType, RefPtr<Theme>> themes_; 141 ThemeWrappers themeWrappersLight_; 142 ThemeWrappers themeWrappersDark_; 143 144 RefPtr<ThemeConstants> themeConstants_; 145 int32_t currentThemeId_ = -1; 146 147 ACE_DISALLOW_COPY_AND_MOVE(ThemeManagerImpl); 148 149 ThemeWrappers& GetThemeWrappers(ColorMode mode); 150 ColorMode GetCurrentColorMode() const; 151 }; 152 } // namespace OHOS::Ace 153 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_MANAGER_H 154