• 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 
23 namespace OHOS::Ace {
24 constexpr float DRAG_BACKGROUND_OPACITY = 0.95f;
25 constexpr float URL_DISA_OPACITY = 0.4f;
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             // Styles below do not need to get from ThemeConstants, directly set at here.
46             theme->textStyle_.SetFontStyle(FontStyle::NORMAL);
47             theme->textStyle_.SetFontWeight(FontWeight::NORMAL);
48             theme->textStyle_.SetTextDecoration(TextDecoration::NONE);
49             ParsePattern(themeConstants, theme);
50             return theme;
51         }
52     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<TextTheme> & theme)53         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<TextTheme>& theme) const
54         {
55             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_TEXT);
56             if (!pattern) {
57                 LOGW("find pattern of text fail");
58                 return;
59             }
60             theme->textStyle_.SetTextColor(pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK)
61                                                .BlendOpacity(pattern->GetAttr<double>(PATTERN_TEXT_COLOR_ALPHA, 0.9)));
62             theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>("text_font_size", 0.0_vp));
63             theme->caretColor_ = pattern->GetAttr<Color>("text_caret_color", Color(0xff006cde));
64             theme->selectedColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_SELECTED, Color(0x33007dff));
65             auto draggable = pattern->GetAttr<std::string>("draggable", "0");
66             theme->draggable_ = StringUtils::StringToInt(draggable);
67             auto dragBackgroundColor = pattern->GetAttr<Color>("drag_background_color", Color::WHITE);
68             if (SystemProperties::GetColorMode() == ColorMode::DARK) {
69                 dragBackgroundColor = dragBackgroundColor.ChangeOpacity(DRAG_BACKGROUND_OPACITY);
70             }
71             theme->dragBackgroundColor_ = dragBackgroundColor;
72             constexpr double childMinSize = 20.0;
73             theme->linearSplitChildMinSize_ = pattern->GetAttr<double>(LINEAR_SPLIT_CHILD_MIN_SIZE, childMinSize);
74             auto textShowHandle = pattern->GetAttr<std::string>("text_show_handle", "0");
75             theme->isShowHandle_ = StringUtils::StringToInt(textShowHandle);
76             auto textShowTranslate = pattern->GetAttr<std::string>("menu_translate_is_support", "0");
77             theme->isShowTranslate_ = StringUtils::StringToInt(textShowTranslate);
78             theme->urlDisabledColor_ = pattern->GetAttr<Color>(
79                 "text_url_disabled_color", Color(0xff007dff)).BlendOpacity(URL_DISA_OPACITY);
80             theme->urlDefaultColor_ = pattern->GetAttr<Color>("text_url_default_color", Color(0xff007dff));
81             theme->urlHoverColor_ = pattern->GetAttr<Color>("text_url_hover_color", Color(0x33007dff));
82             theme->urlPressColor_ = pattern->GetAttr<Color>("text_url_press_color", Color(0x19182431));
83         }
84     };
85 
86     ~TextTheme() override = default;
87 
GetTextStyle()88     const TextStyle& GetTextStyle() const
89     {
90         return textStyle_;
91     }
92 
GetCaretColor()93     const Color& GetCaretColor() const
94     {
95         return caretColor_;
96     }
97 
GetSelectedColor()98     const Color& GetSelectedColor() const
99     {
100         return selectedColor_;
101     }
102 
GetDraggable()103     bool GetDraggable() const
104     {
105         return draggable_;
106     }
107 
GetLinearSplitChildMinSize()108     double GetLinearSplitChildMinSize() const
109     {
110         return linearSplitChildMinSize_;
111     }
112 
IsShowHandle()113     bool IsShowHandle() const
114     {
115         return isShowHandle_;
116     }
117 
IsShowTranslate()118     bool IsShowTranslate() const
119     {
120         return isShowTranslate_;
121     }
122 
GetDragBackgroundColor()123     const Color& GetDragBackgroundColor() const
124     {
125         return dragBackgroundColor_;
126     }
127 
GetUrlDisabledColor()128     const Color& GetUrlDisabledColor() const
129     {
130         return urlDisabledColor_;
131     }
132 
GetUrlDefaultColor()133     const Color& GetUrlDefaultColor() const
134     {
135         return urlDefaultColor_;
136     }
137 
GetUrlHoverColor()138     const Color& GetUrlHoverColor() const
139     {
140         return urlHoverColor_;
141     }
142 
GetUrlPressColor()143     const Color& GetUrlPressColor() const
144     {
145         return urlPressColor_;
146     }
147 
148 protected:
149     TextTheme() = default;
150 
151 private:
152     TextStyle textStyle_;
153     Color caretColor_;
154     Color selectedColor_;
155     Color dragBackgroundColor_ = Color::WHITE;
156     bool draggable_ = false;
157     double linearSplitChildMinSize_ = 20.0;
158     bool isShowHandle_ = false;
159     bool isShowTranslate_ = false;
160     Color urlDisabledColor_;
161     Color urlDefaultColor_;
162     Color urlHoverColor_;
163     Color urlPressColor_;
164 };
165 
166 } // namespace OHOS::Ace
167 
168 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_TEXT_THEME_H
169