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