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 
22 namespace OHOS::Ace {
23 class ACE_EXPORT ThemeManagerImpl : public ThemeManager {
24     DECLARE_ACE_TYPE(ThemeManagerImpl, ThemeManager);
25 
26 public:
27     ThemeManagerImpl();
28     explicit ThemeManagerImpl(RefPtr<ResourceAdapter>& resourceAdapter);
29     ~ThemeManagerImpl() override = default;
30 
InitResource(const ResourceInfo & resourceInfo)31     void InitResource(const ResourceInfo& resourceInfo) override
32     {
33         themeConstants_->InitResource(resourceInfo);
34     }
35 
UpdateConfig(const ResourceConfiguration & config)36     void UpdateConfig(const ResourceConfiguration& config) override
37     {
38         themeConstants_->UpdateConfig(config);
39     }
40 
LoadSystemTheme(int32_t themeId)41     void LoadSystemTheme(int32_t themeId) override
42     {
43         currentThemeId_ = themeId;
44         themeConstants_->LoadTheme(themeId);
45     }
46 
ParseSystemTheme()47     void ParseSystemTheme() override
48     {
49         themeConstants_->ParseTheme();
50     }
51 
LoadCustomTheme(const RefPtr<AssetManager> & assetManager)52     void LoadCustomTheme(const RefPtr<AssetManager>& assetManager) override
53     {
54         themeConstants_->LoadCustomStyle(assetManager);
55     }
56 
57     /*
58      * Color scheme of the whole window, app bg color will be change in transparent scheme.
59      */
SetColorScheme(ColorScheme colorScheme)60     void SetColorScheme(ColorScheme colorScheme) override
61     {
62         themeConstants_->SetColorScheme(colorScheme);
63     }
64 
65     /*
66      * Get color value from AppTheme (if exists) or system theme style.
67      * Prebuilt background color will be returned if AppTheme and system theme style both not exists.
68      * @return App background color.
69      */
70     Color GetBackgroundColor() const override;
71 
GetThemeConstants(const std::string & bundleName,const std::string & moduleName)72     RefPtr<ThemeConstants> GetThemeConstants(
73         const std::string& bundleName, const std::string& moduleName) const override
74     {
75         themeConstants_->UpdateThemeConstants(bundleName, moduleName);
76         return themeConstants_;
77     }
78 
GetThemeConstants()79     RefPtr<ThemeConstants> GetThemeConstants() const override
80     {
81         return GetThemeConstants("", "");
82     }
83 
84     /*
85      * Get target theme, this function will cause construction of the theme if it not exists.
86      * @return Target component theme.
87      */
88     RefPtr<Theme> GetTheme(ThemeType type) override;
89 
90     template<typename T>
GetTheme()91     RefPtr<T> GetTheme()
92     {
93         return AceType::DynamicCast<T>(GetTheme(T::TypeId()));
94     }
95 
96     void LoadResourceThemes() override;
97 
GetResourceLimitKeys()98     uint32_t GetResourceLimitKeys() const override
99     {
100         return themeConstants_->GetResourceLimitKeys();
101     }
102 
103 private:
104     std::unordered_map<ThemeType, RefPtr<Theme>> themes_;
105     RefPtr<ThemeConstants> themeConstants_;
106     int32_t currentThemeId_ = -1;
107 
108     ACE_DISALLOW_COPY_AND_MOVE(ThemeManagerImpl);
109 };
110 } // namespace OHOS::Ace
111 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_MANAGER_H
112