• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 FRAMEWORKS_CORE_COMPONENTS_NG_TOKEN_THEME_TOKEN_THEME_STORAGE_H
17 #define FRAMEWORKS_CORE_COMPONENTS_NG_TOKEN_THEME_TOKEN_THEME_STORAGE_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <unordered_map>
23 
24 #include "base/memory/referenced.h"
25 #include "core/components_ng/token_theme/token_theme.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 class ACE_EXPORT TokenThemeStorage final {
30 public:
31     ACE_FORCE_EXPORT static TokenThemeStorage* GetInstance();
32     ~TokenThemeStorage() = default;
33 
34     // theme map (key: themeScopeId - value: ark theme instance)
35     void StoreThemeScope(TokenThemeScopeId themeScopeId, int32_t themeId);
36     void RemoveThemeScope(TokenThemeScopeId themeScopeId, bool removeToken = false);
37     const RefPtr<TokenTheme>& GetTheme(TokenThemeScopeId themeScopeId);
38 
39     // default theme
40     void SetDefaultTheme(const RefPtr<TokenTheme>& theme, ColorMode colorMode);
41     const RefPtr<TokenTheme>& GetDefaultTheme();
42     void UpdateDefaultThemeBySystemTheme(ColorMode colorMode);
43 
44     // cache (key: theme id - value: ark theme instance)
45     void CacheClear();
46     void CacheResetColor();
47     void CacheSet(const RefPtr<TokenTheme>& theme);
48     const RefPtr<TokenTheme>& CacheGet(int32_t themeId);
49     void CacheRemove(int32_t themeId);
50 
51     RefPtr<TokenTheme> ObtainSystemTheme();
52 
53     void SetIsThemeColorAvailable(bool isDark, int32_t index, bool isColorAvailable);
54 
55     void SetIsThemeColorSetByUser(int32_t themeId, bool isDark, int32_t index, bool isColorSetByUser);
56 
57 private:
58     static constexpr int32_t SYSTEM_THEME_LIGHT_ID = -1;
59     static constexpr int32_t SYSTEM_THEME_DARK_ID = -2;
60     bool systemTokenThemeCreated_[3] = {false, false, false}; // 3 means color modes: light, dark, undefined
61 
62     TokenThemeStorage();
63     RefPtr<TokenTheme> CreateSystemTokenTheme(ColorMode colorMode);
64     ColorMode CheckLocalAndSystemColorMode();
65     void ResetThemeColor(int32_t themeId, RefPtr<TokenTheme>& theme, RefPtr<TokenTheme>& defaultTheme,
66         ColorMode& colorMode);
67 
68     // key: scope id, value: theme id
69     std::unordered_map<TokenThemeScopeId, int32_t> themeScopeMap_;
70 
71     // key: theme id, value: theme instance
72     std::map<int32_t, RefPtr<TokenTheme>> themeCache_;
73     std::mutex themeCacheMutex_;
74 
75     std::vector<bool> darkThemeColorsAvailable_ = std::vector<bool> (TokenColors::TOTAL_NUMBER, false);
76     std::vector<bool> lightThemeColorsAvailable_ = std::vector<bool> (TokenColors::TOTAL_NUMBER, false);
77 
78     std::unordered_map<int32_t, std::map<bool, std::vector<bool>>> themeColorSetByUser_;
79 
80     inline static RefPtr<TokenTheme> defaultLightTheme_ = nullptr;
81     inline static RefPtr<TokenTheme> defaultDarkTheme_ = nullptr;
82 };
83 
84 } // namespace
85 #endif
86