• 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_TEXT_TEXT_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_TEXT_THEME_H
18 
19 #include "core/components/common/properties/text_style.h"
20 #include "core/components/theme/theme.h"
21 #include "core/components/theme/theme_constants.h"
22 #include "core/components/theme/theme_constants_defines.h"
23 
24 namespace OHOS::Ace {
25 
26 /**
27  * TextTheme defines color and styles of ThemeComponent. TextTheme should be built
28  * using TextTheme::Builder.
29  */
30 class TextTheme : public virtual Theme {
31     DECLARE_ACE_TYPE(TextTheme, Theme);
32 
33 public:
34     class Builder {
35     public:
36         Builder() = default;
37         ~Builder() = default;
38 
Build(const RefPtr<ThemeConstants> & themeConstants)39         RefPtr<TextTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
40         {
41             RefPtr<TextTheme> theme = AceType::Claim(new TextTheme());
42             if (!themeConstants) {
43                 return theme;
44             }
45             theme->textStyle_.SetTextColor(themeConstants->GetColor(THEME_OHOS_COLOR_TEXT_PRIMARY));
46             theme->textStyle_.SetFontSize(themeConstants->GetDimension(THEME_TEXT_SIZE));
47             // Styles below do not need to get from ThemeConstants, directly set at here.
48             theme->textStyle_.SetFontStyle(FontStyle::NORMAL);
49             theme->textStyle_.SetFontWeight(FontWeight::NORMAL);
50             theme->textStyle_.SetTextDecoration(TextDecoration::NONE);
51             ParsePattern(themeConstants->GetThemeStyle(), theme);
52             return theme;
53         }
54     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<TextTheme> & theme)55         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<TextTheme>& theme) const
56         {
57             if (!themeStyle) {
58                 return;
59             }
60             auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_TEXT, nullptr);
61             if (!pattern) {
62                 LOGW("find pattern of text fail");
63                 return;
64             }
65             theme->textStyle_.SetTextColor(pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK)
66                                                .BlendOpacity(pattern->GetAttr<double>(PATTERN_TEXT_COLOR_ALPHA, 0.9)));
67             theme->selectedColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_SELECTED, Color(0x33007dff));
68             auto draggable = pattern->GetAttr<std::string>("draggable", "0");
69             theme->draggable_ = StringUtils::StringToInt(draggable);
70             constexpr double childMinSize = 20.0;
71             theme->linearSplitChildMinSize_ = pattern->GetAttr<double>(LINEAR_SPLIT_CHILD_MIN_SIZE, childMinSize);
72         }
73     };
74 
75     ~TextTheme() override = default;
76 
GetTextStyle()77     TextStyle GetTextStyle() const
78     {
79         return textStyle_;
80     }
81 
GetSelectedColor()82     const Color& GetSelectedColor() const
83     {
84         return selectedColor_;
85     }
86 
GetDraggable()87     bool GetDraggable() const
88     {
89         return draggable_;
90     }
91 
GetLinearSplitChildMinSize()92     double GetLinearSplitChildMinSize() const
93     {
94         return linearSplitChildMinSize_;
95     }
96 
97 protected:
98     TextTheme() = default;
99 
100 private:
101     TextStyle textStyle_;
102     Color selectedColor_;
103     bool draggable_ = false;
104     double linearSplitChildMinSize_ = 20.0;
105 };
106 
107 } // namespace OHOS::Ace
108 
109 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_TEXT_THEME_H
110