• 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_PATTERN_TEXT_TEXT_THEME_WRAPPER_H
17 #define FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_THEME_WRAPPER_H
18 
19 #include <memory>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/components/text/text_theme.h"
23 #include "core/components_ng/token_theme/token_theme_wrapper.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 /**
28  * TextThemeWrapper defines color and styles of TextComponent basing on TokenTheme's data.
29  * TextThemeWrapper should be built using TextThemeWrapper::Builder.
30  */
31 class TextThemeWrapper : public TextTheme, public TokenThemeWrapper {
32     DECLARE_ACE_TYPE(TextThemeWrapper, TextTheme);
33 
34 public:
35     class WrapperBuilder : public Builder {
36     public:
37         WrapperBuilder() = default;
38         ~WrapperBuilder() = default;
39 
BuildWrapper(const RefPtr<ThemeConstants> & themeConstants)40         RefPtr<TokenThemeWrapper> BuildWrapper(const RefPtr<ThemeConstants>& themeConstants) const
41         {
42             auto wrapper = AceType::MakeRefPtr<TextThemeWrapper>();
43             auto theme = AceType::DynamicCast<TextTheme>(wrapper);
44             if (!themeConstants) {
45                 return wrapper;
46             }
47             InitThemeDefaults(theme);
48             ParsePattern(themeConstants, theme);
49             return wrapper;
50         }
51     };
52 
53     ~TextThemeWrapper() override = default;
54 
ApplyTokenTheme(const TokenTheme & theme)55     void ApplyTokenTheme(const TokenTheme& theme) override
56     {
57         if (const auto& colors = theme.Colors(); colors) {
58             textStyle_.SetTextColor(colors->FontPrimary());
59         }
60     }
61 
62 protected:
63     TextThemeWrapper() = default;
64 };
65 
66 } // namespace
67 #endif // FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_THEME_WRAPPER_H