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->GetThemeStyle(), theme); 49 return theme; 50 } 51 52 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<HyperlinkTheme> & theme)53 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<HyperlinkTheme>& theme) const 54 { 55 if (!themeStyle) { 56 LOGI("hyperlink theme style is null"); 57 return; 58 } 59 auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_HYPERLINK, nullptr); 60 if (!pattern) { 61 LOGW("find pattern of hyperlink fail"); 62 return; 63 } 64 theme->textColor_ = pattern->GetAttr<Color>("text_color", Color(0xff007dff)); 65 theme->textLinkedColor_ = pattern->GetAttr<Color>("text_linked_opacity", Color(0x19182431)); 66 theme->textDisabledColor_ = pattern->GetAttr<Color>("text_color", Color(0xff007dff)) 67 .BlendOpacity(pattern->GetAttr<double>("text_disabled_opacity", 0.0)); 68 theme->textFocusedColor_ = pattern->GetAttr<Color>("text_focused_color", Color(0xff007dff)); 69 auto draggable = pattern->GetAttr<std::string>("draggable", "0"); 70 theme->draggable_ = StringUtils::StringToInt(draggable); 71 } 72 }; 73 74 ~HyperlinkTheme() override = default; 75 GetDraggable()76 bool GetDraggable() const 77 { 78 return draggable_; 79 } 80 GetTextColor()81 const Color& GetTextColor() const 82 { 83 return textColor_; 84 } 85 GetTextLinkedColor()86 const Color& GetTextLinkedColor() const 87 { 88 return textLinkedColor_; 89 } 90 GetTextDisabledColor()91 const Color& GetTextDisabledColor() const 92 { 93 return textDisabledColor_; 94 } 95 GetTextFocusedColor()96 const Color& GetTextFocusedColor() const 97 { 98 return textFocusedColor_; 99 } 100 GetTextSelectedDecoration()101 TextDecoration GetTextSelectedDecoration() const 102 { 103 return textSelectedDecoration_; 104 } 105 GetTextUnSelectedDecoration()106 TextDecoration GetTextUnSelectedDecoration() const 107 { 108 return textUnSelectedDecoration_; 109 } 110 protected: 111 HyperlinkTheme() = default; 112 113 private: 114 bool draggable_ = false; 115 Color textColor_; 116 Color textLinkedColor_; 117 Color textDisabledColor_; 118 Color textFocusedColor_; 119 TextDecoration textSelectedDecoration_; 120 TextDecoration textUnSelectedDecoration_; 121 }; 122 } // namespace OHOS::Ace 123 124 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_HYPERLINK_HYPERLINK_THEME_H 125