1 /* 2 * Copyright (c) 2021-2025 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 "ui/base/utils/utils.h" 20 #include "core/common/container.h" 21 #include "core/components/common/properties/text_style.h" 22 #include "core/components/theme/theme.h" 23 #include "core/components/theme/theme_constants.h" 24 25 namespace OHOS::Ace { 26 namespace { 27 constexpr float DRAG_BACKGROUND_OPACITY = 0.95f; 28 constexpr float URL_DISA_OPACITY = 0.4f; 29 } // namespace 30 31 /** 32 * TextTheme defines color and styles of ThemeComponent. TextTheme should be built 33 * using TextTheme::Builder. 34 */ 35 class TextTheme : public virtual Theme { 36 DECLARE_ACE_TYPE(TextTheme, Theme); 37 38 public: 39 class Builder { 40 public: 41 Builder() = default; 42 ~Builder() = default; 43 Build(const RefPtr<ThemeConstants> & themeConstants)44 RefPtr<TextTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 45 { 46 RefPtr<TextTheme> theme = AceType::MakeRefPtr<TextTheme>(); 47 if (!themeConstants) { 48 return theme; 49 } 50 InitThemeDefaults(theme); 51 ParsePattern(themeConstants, theme); 52 return theme; 53 } 54 protected: InitThemeDefaults(const RefPtr<TextTheme> & theme)55 void InitThemeDefaults(const RefPtr<TextTheme>& theme) const 56 { 57 // Styles below do not need to get from ThemeConstants, directly set at here. 58 theme->textStyle_.SetFontStyle(FontStyle::NORMAL); 59 theme->textStyle_.SetFontWeight(FontWeight::NORMAL); 60 theme->textStyle_.SetTextDecoration(TextDecoration::NONE); 61 theme->textStyle_.SetLineThicknessScale(1.0f); 62 } 63 ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<TextTheme> & theme)64 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<TextTheme>& theme) const 65 { 66 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_TEXT); 67 if (!pattern) { 68 LOGW("find pattern of text fail"); 69 return; 70 } 71 theme->textStyle_.SetTextColor(pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK) 72 .BlendOpacity(pattern->GetAttr<double>(PATTERN_TEXT_COLOR_ALPHA, 0.9))); 73 theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>("text_font_size", 0.0_vp)); 74 theme->caretColor_ = pattern->GetAttr<Color>("text_caret_color", Color(0xff006cde)); 75 theme->textStyle_.SetLineSpacing(pattern->GetAttr<Dimension>("text_line_spacing", 0.0_vp)); 76 theme->textStyle_.SetFontWeight(static_cast<FontWeight>(pattern->GetAttr<double>("text_font_weight", 0.0))); 77 theme->textStyle_.SetTextAlign(static_cast<TextAlign>(pattern->GetAttr<double>("text_align", 0.0))); 78 theme->selectedColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_SELECTED, Color(0x33007dff)); 79 auto draggable = pattern->GetAttr<std::string>("draggable", "0"); 80 theme->draggable_ = StringUtils::StringToInt(draggable); 81 auto dragBackgroundColor = pattern->GetAttr<Color>("drag_background_color", Color::WHITE); 82 if (Container::CurrentColorMode() == ColorMode::DARK) { 83 dragBackgroundColor = dragBackgroundColor.ChangeOpacity(DRAG_BACKGROUND_OPACITY); 84 } 85 theme->dragBackgroundColor_ = dragBackgroundColor; 86 constexpr double childMinSize = 20.0; 87 theme->linearSplitChildMinSize_ = pattern->GetAttr<double>(LINEAR_SPLIT_CHILD_MIN_SIZE, childMinSize); 88 auto textShowHandle = pattern->GetAttr<std::string>("text_show_handle", "0"); 89 theme->isShowHandle_ = StringUtils::StringToInt(textShowHandle); 90 auto textShowTranslate = pattern->GetAttr<std::string>("menu_translate_is_support", "0"); 91 theme->isShowTranslate_ = StringUtils::StringToInt(textShowTranslate); 92 auto textShowSearch = pattern->GetAttr<std::string>("text_menu_search_is_support", "0"); 93 theme->isShowSearch_ = StringUtils::StringToInt(textShowSearch); 94 auto disabledOpacity = pattern->GetAttr<double>("interactive_disable", URL_DISA_OPACITY); 95 theme->urlDefaultColor_ = pattern->GetAttr<Color>("font_emphasize", Color(0xff007dff)); 96 theme->urlDisabledColor_ = theme->urlDefaultColor_.BlendOpacity(disabledOpacity); 97 theme->urlHoverColor_ = pattern->GetAttr<Color>("interactive_hover", Color(0x33007dff)); 98 theme->urlPressColor_ = pattern->GetAttr<Color>("interactive_pressed", Color(0x19182431)); 99 theme->isTextFadeout_ = pattern->GetAttr<std::string>("text_fadeout_enable", "") == "true"; 100 theme->fadeoutWidth_ = pattern->GetAttr<Dimension>("text_fadeout_width", 16.0_vp); 101 theme->marqueeStartPolicy_ = static_cast<MarqueeStartPolicy>(static_cast<int32_t>( 102 pattern->GetAttr<double>("text_marquee_start_policy", 0.0))); 103 auto textSupportCeliaAsk = pattern->GetAttr<std::string>("menu_celia_ask_is_support", "0"); 104 theme->isSupportAskCelia_ = StringUtils::StringToInt(textSupportCeliaAsk); 105 } 106 }; 107 108 ~TextTheme() override = default; 109 GetTextStyle()110 const TextStyle& GetTextStyle() const 111 { 112 return textStyle_; 113 } 114 GetTextDecoration()115 TextDecoration GetTextDecoration() const 116 { 117 return textStyle_.GetTextDecoration().size() > 0 ? 118 textStyle_.GetTextDecoration()[0] : TextDecoration::NONE; 119 } 120 GetCaretColor()121 const Color& GetCaretColor() const 122 { 123 return caretColor_; 124 } 125 GetSelectedColor()126 const Color& GetSelectedColor() const 127 { 128 return selectedColor_; 129 } 130 GetDraggable()131 bool GetDraggable() const 132 { 133 return draggable_; 134 } 135 GetLinearSplitChildMinSize()136 double GetLinearSplitChildMinSize() const 137 { 138 return linearSplitChildMinSize_; 139 } 140 GetIsTextFadeout()141 bool GetIsTextFadeout() const 142 { 143 return isTextFadeout_; 144 } 145 GetFadeoutWidth()146 const Dimension& GetFadeoutWidth() const 147 { 148 return fadeoutWidth_; 149 } 150 IsShowHandle()151 bool IsShowHandle() const 152 { 153 return isShowHandle_; 154 } 155 IsShowTranslate()156 bool IsShowTranslate() const 157 { 158 return isShowTranslate_; 159 } 160 IsShowSearch()161 bool IsShowSearch() const 162 { 163 return isShowSearch_; 164 } 165 GetDragBackgroundColor()166 const Color& GetDragBackgroundColor() const 167 { 168 return dragBackgroundColor_; 169 } 170 GetUrlDisabledColor()171 const Color& GetUrlDisabledColor() const 172 { 173 return urlDisabledColor_; 174 } 175 GetUrlDefaultColor()176 const Color& GetUrlDefaultColor() const 177 { 178 return urlDefaultColor_; 179 } 180 GetUrlHoverColor()181 const Color& GetUrlHoverColor() const 182 { 183 return urlHoverColor_; 184 } 185 GetUrlPressColor()186 const Color& GetUrlPressColor() const 187 { 188 return urlPressColor_; 189 } 190 GetMarqueeStartPolicy()191 MarqueeStartPolicy GetMarqueeStartPolicy() const 192 { 193 return marqueeStartPolicy_; 194 } 195 IsSupportAskCelia()196 bool IsSupportAskCelia() const 197 { 198 return isSupportAskCelia_; 199 } 200 protected: 201 TextTheme() = default; 202 TextStyle textStyle_; 203 204 private: 205 Color caretColor_; 206 Color selectedColor_; 207 Color dragBackgroundColor_ = Color::WHITE; 208 bool draggable_ = false; 209 double linearSplitChildMinSize_ = 20.0; 210 bool isShowHandle_ = false; 211 bool isShowTranslate_ = false; 212 bool isShowSearch_ = false; 213 bool isTextFadeout_ = false; 214 Dimension fadeoutWidth_; 215 MarqueeStartPolicy marqueeStartPolicy_ = MarqueeStartPolicy::DEFAULT; 216 Color urlDisabledColor_; 217 Color urlDefaultColor_; 218 Color urlHoverColor_; 219 Color urlPressColor_; 220 bool isSupportAskCelia_; 221 }; 222 223 } // namespace OHOS::Ace 224 225 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_TEXT_THEME_H 226