• 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 #include "ui/base/utils/utils.h"
17 #include "ui/view/theme/theme_factory.h"
18 #include "ui/view/theme/theme_style.h"
19 #include "interfaces/inner_api/ace_kit/src/view/theme/theme_style_impl.h"
20 
21 #include "core/common/container.h"
22 #include "core/components/theme/theme_manager.h"
23 #include "core/pipeline/pipeline_base.h"
24 
25 namespace OHOS::Ace::Kit {
GetThemeManager()26 static RefPtr<Ace::ThemeManager> GetThemeManager()
27 {
28     auto container = Ace::Container::Current();
29     CHECK_NULL_RETURN(container, nullptr);
30 
31     auto pipelineContext = container->GetPipelineContext();
32     CHECK_NULL_RETURN(pipelineContext, nullptr);
33 
34     auto themeManager = pipelineContext->GetThemeManager();
35     CHECK_NULL_RETURN(themeManager, nullptr);
36     return themeManager;
37 }
38 
CreateTheme(Ace::ThemeType type,BuildFunc func)39 bool ThemeFactory::CreateTheme(Ace::ThemeType type, BuildFunc func)
40 {
41     auto themeManager = GetThemeManager();
42     CHECK_NULL_RETURN(themeManager, false);
43     themeManager->RegisterThemeKit(type, func);
44     return true;
45 }
46 
CreateCustomTheme(Ace::ThemeType type,BuildThemeWrapperFunc func)47 bool ThemeFactory::CreateCustomTheme(Ace::ThemeType type, BuildThemeWrapperFunc func)
48 {
49     auto themeManager = GetThemeManager();
50     CHECK_NULL_RETURN(themeManager, false);
51     themeManager->RegisterCustomThemeKit(type, func);
52     return true;
53 }
54 
GetThemeStyle(const std::string & patternName,std::optional<std::string> bundleName,std::optional<std::string> moduleName)55 RefPtr<ThemeStyle> ThemeFactory::GetThemeStyle(const std::string& patternName,
56     std::optional<std::string> bundleName, std::optional<std::string> moduleName)
57 {
58     if (patternName.empty()) {
59         return nullptr;
60     }
61 
62     auto themeManager = GetThemeManager();
63     CHECK_NULL_RETURN(themeManager, nullptr);
64 
65     auto themeConstant = (bundleName.has_value() && moduleName.has_value()) ?
66         themeManager->GetThemeConstants(bundleName.value_or(""), moduleName.value_or("")) :
67         themeManager->GetThemeConstants();
68 
69     return AceType::MakeRefPtr<ThemeStyleImpl>(themeConstant->GetPatternByName(patternName));
70 }
71 
GetTheme(Ace::ThemeType type)72 RefPtr<Ace::Theme> ThemeFactory::GetTheme(Ace::ThemeType type)
73 {
74     auto themeManager = GetThemeManager();
75     CHECK_NULL_RETURN(themeManager, nullptr);
76     return themeManager->GetTheme(type);
77 }
78 
GetTheme(Ace::ThemeType type,int32_t themeScopeId)79 RefPtr<Ace::Theme> ThemeFactory::GetTheme(Ace::ThemeType type, int32_t themeScopeId)
80 {
81     auto themeManager = GetThemeManager();
82     CHECK_NULL_RETURN(themeManager, nullptr);
83     return themeManager->GetTheme(type, themeScopeId);
84 }
85 
GetThemeScopeId(RefPtr<FrameNode> & node)86 int32_t ThemeFactory::GetThemeScopeId(RefPtr<FrameNode>& node)
87 {
88     auto ui_node = reinterpret_cast<NG::UINode*>(node->GetHandle());
89     CHECK_NULL_RETURN(ui_node, 0);
90     return ui_node->GetThemeScopeId();
91 }
92 
93 } // namespace OHOS::Ace::Kit
94