1 /*
2  * Copyright (c) 2023 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_HYPERLINK_HYPERLINK_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_HYPERLINK_HYPERLINK_THEME_H
18 
19 #include "base/utils/string_utils.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 #include "core/components/theme/theme_manager.h"
24 #include "frameworks/bridge/common/utils/utils.h"
25 
26 namespace OHOS::Ace {
27 /**
28  * HyperlinkTheme defines styles of Hyperlink. HyperlinkTheme should be built
29  * using HyperlinkTheme::Builder.
30  */
31 class HyperlinkTheme : public virtual Theme {
32     DECLARE_ACE_TYPE(HyperlinkTheme, Theme);
33 
34 public:
35     class Builder {
36     public:
37         Builder() = default;
38         ~Builder() = default;
39 
Build(const RefPtr<ThemeConstants> & themeConstants)40         RefPtr<HyperlinkTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
41         {
42             RefPtr<HyperlinkTheme> theme = AceType::Claim(new HyperlinkTheme());
43             if (!themeConstants) {
44                 return theme;
45             }
46             theme->textSelectedDecoration_ = TextDecoration::UNDERLINE;
47             theme->textUnSelectedDecoration_ = TextDecoration::NONE;
48             ParsePattern(themeConstants, theme);
49             return theme;
50         }
51 
52     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<HyperlinkTheme> & theme)53         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<HyperlinkTheme>& theme) const
54         {
55             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_HYPERLINK);
56             if (!pattern) {
57                 LOGW("find pattern of hyperlink fail");
58                 return;
59             }
60             theme->textColor_ = pattern->GetAttr<Color>("text_color", Color(0xff007dff));
61             theme->textTouchedColor_ = pattern->GetAttr<Color>("text_touched_color", Color(0x19182431));
62             theme->textLinkedColor_ = pattern->GetAttr<Color>("text_linked_color", Color(0x66182431));
63             theme->opacity_ = pattern->GetAttr<double>("text_disabled_opacity", 0.0);
64             theme->textDisabledColor_ = pattern->GetAttr<Color>("text_color", Color(0xff007dff))
65                 .BlendOpacity(theme->opacity_);
66             theme->textFocusedColor_ = pattern->GetAttr<Color>("text_focused_color", Color(0xff007dff));
67             auto draggable = pattern->GetAttr<std::string>("draggable", "0");
68             theme->draggable_ = StringUtils::StringToInt(draggable);
69         }
70     };
71 
72     ~HyperlinkTheme() override = default;
73 
GetOpacity()74     double GetOpacity() const
75     {
76         return opacity_;
77     }
78 
GetDraggable()79     bool GetDraggable() const
80     {
81         return draggable_;
82     }
83 
GetTextColor()84     const Color& GetTextColor() const
85     {
86         return textColor_;
87     }
88 
GetTextTouchedColor()89     const Color& GetTextTouchedColor() const
90     {
91         return textTouchedColor_;
92     }
93 
GetTextLinkedColor()94     const Color& GetTextLinkedColor() const
95     {
96         return textLinkedColor_;
97     }
98 
GetTextDisabledColor()99     const Color& GetTextDisabledColor() const
100     {
101         return textDisabledColor_;
102     }
103 
GetTextFocusedColor()104     const Color& GetTextFocusedColor() const
105     {
106         return textFocusedColor_;
107     }
108 
GetTextSelectedDecoration()109     TextDecoration GetTextSelectedDecoration() const
110     {
111         return textSelectedDecoration_;
112     }
113 
GetTextUnSelectedDecoration()114     TextDecoration GetTextUnSelectedDecoration() const
115     {
116         return textUnSelectedDecoration_;
117     }
118 protected:
119     HyperlinkTheme() = default;
120 
121 private:
122     bool draggable_ = false;
123     double opacity_;
124     Color textColor_;
125     Color textTouchedColor_;
126     Color textLinkedColor_;
127     Color textDisabledColor_;
128     Color textFocusedColor_;
129     TextDecoration textSelectedDecoration_;
130     TextDecoration textUnSelectedDecoration_;
131 };
132 } // namespace OHOS::Ace
133 
134 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_HYPERLINK_HYPERLINK_THEME_H
135