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
GetThemeStyle(const std::string & patternName,std::optional<std::string> bundleName,std::optional<std::string> moduleName)47 RefPtr<ThemeStyle> ThemeFactory::GetThemeStyle(const std::string& patternName,
48 std::optional<std::string> bundleName, std::optional<std::string> moduleName)
49 {
50 if (patternName.empty()) {
51 return nullptr;
52 }
53
54 auto themeManager = GetThemeManager();
55 CHECK_NULL_RETURN(themeManager, nullptr);
56
57 auto themeConstant = (bundleName.has_value() && moduleName.has_value()) ?
58 themeManager->GetThemeConstants(bundleName.value_or(""), moduleName.value_or("")) :
59 themeManager->GetThemeConstants();
60
61 return AceType::MakeRefPtr<ThemeStyleImpl>(themeConstant->GetPatternByName(patternName));
62 }
63
GetTheme(Ace::ThemeType type)64 RefPtr<Ace::Theme> ThemeFactory::GetTheme(Ace::ThemeType type)
65 {
66 auto themeManager = GetThemeManager();
67 CHECK_NULL_RETURN(themeManager, nullptr);
68 return themeManager->GetTheme(type);
69 }
70
71 } // namespace OHOS::Ace::Kit
72