• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
LoadSystemTheme(int32_t themeId)42     void LoadSystemTheme(int32_t themeId) override
43     {
44         currentThemeId_ = themeId;
45         themeConstants_->LoadTheme(themeId);
46     }
47 
GetSystemTheme()48     int32_t GetSystemTheme() override
49     {
50         return currentThemeId_;
51     }
52 
ParseSystemTheme()53     void ParseSystemTheme() override
54     {
55         themeConstants_->ParseTheme();
56     }
57 
LoadCustomTheme(const RefPtr<AssetManager> & assetManager)58     void LoadCustomTheme(const RefPtr<AssetManager>& assetManager) override
59     {
60         themeConstants_->LoadCustomStyle(assetManager);
61     }
62 
63     /*
64      * Color scheme of the whole window, app bg color will be change in transparent scheme.
65      */
SetColorScheme(ColorScheme colorScheme)66     void SetColorScheme(ColorScheme colorScheme) override
67     {
68         themeConstants_->SetColorScheme(colorScheme);
69     }
70 
71     /*
72      * Get color value from AppTheme (if exists) or system theme style.
73      * Prebuilt background color will be returned if AppTheme and system theme style both not exists.
74      * @return App background color.
75      */
76     Color GetBackgroundColor() const override;
77 
GetThemeConstants(const std::string & bundleName,const std::string & moduleName)78     RefPtr<ThemeConstants> GetThemeConstants(
79         const std::string& bundleName, const std::string& moduleName) const override
80     {
81         themeConstants_->UpdateThemeConstants(bundleName, moduleName);
82         return themeConstants_;
83     }
84 
GetThemeConstants()85     RefPtr<ThemeConstants> GetThemeConstants() const override
86     {
87         return GetThemeConstants("", "");
88     }
89 
90     /*
91      * Get target theme, this function will cause construction of the theme if it not exists.
92      * @return Target component theme.
93      */
94     RefPtr<Theme> GetTheme(ThemeType type) override;
95 
96     template<typename T>
GetTheme()97     RefPtr<T> GetTheme()
98     {
99         return AceType::DynamicCast<T>(GetTheme(T::TypeId()));
100     }
101 
102     /*
103      * Get the theme and update it according to the TokenTheme, that given in param.
104      * @return Target component theme.
105      */
106     RefPtr<Theme> GetTheme(ThemeType type, int32_t themeScopeId) override;
107 
108     template<typename T>
GetTheme(int32_t themeScopeId)109     RefPtr<T> GetTheme(int32_t themeScopeId)
110     {
111         return AceType::DynamicCast<T>(GetTheme(T::TypeId()), themeScopeId);
112     }
113 
114     void LoadResourceThemes() override;
115 
GetResourceLimitKeys()116     uint32_t GetResourceLimitKeys() const override
117     {
118         return themeConstants_->GetResourceLimitKeys();
119     }
120 
121     void RegisterThemeKit(ThemeType type, Ace::Kit::BuildFunc func) override;
122 
123     RefPtr<Theme> GetThemeOrigin(ThemeType type);
124 
125     RefPtr<Theme> GetThemeKit(ThemeType type);
126 
127 private:
128     using ThemeWrappers = std::unordered_map<ThemeType, RefPtr<NG::TokenThemeWrapper>>;
129     std::unordered_map<ThemeType, RefPtr<Theme>> themes_;
130     ThemeWrappers themeWrappersLight_;
131     ThemeWrappers themeWrappersDark_;
132 
133     RefPtr<ThemeConstants> themeConstants_;
134     int32_t currentThemeId_ = -1;
135 
136     ACE_DISALLOW_COPY_AND_MOVE(ThemeManagerImpl);
137 
138     ThemeWrappers& GetThemeWrappers(ColorMode mode);
139     ColorMode GetCurrentColorMode() const;
140 };
141 } // namespace OHOS::Ace
142 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_MANAGER_H
143