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 /** 26 * TextTheme defines color and styles of ThemeComponent. TextTheme should be built 27 * using TextTheme::Builder. 28 */ 29 class TextTheme : public virtual Theme { 30 DECLARE_ACE_TYPE(TextTheme, Theme); 31 32 public: 33 class Builder { 34 public: 35 Builder() = default; 36 ~Builder() = default; 37 Build(const RefPtr<ThemeConstants> & themeConstants)38 RefPtr<TextTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 39 { 40 RefPtr<TextTheme> theme = AceType::Claim(new TextTheme()); 41 if (!themeConstants) { 42 return theme; 43 } 44 // Styles below do not need to get from ThemeConstants, directly set at here. 45 theme->textStyle_.SetFontStyle(FontStyle::NORMAL); 46 theme->textStyle_.SetFontWeight(FontWeight::NORMAL); 47 theme->textStyle_.SetTextDecoration(TextDecoration::NONE); 48 ParsePattern(themeConstants, theme); 49 return theme; 50 } 51 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<TextTheme> & theme)52 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<TextTheme>& theme) const 53 { 54 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_TEXT); 55 if (!pattern) { 56 LOGW("find pattern of text fail"); 57 return; 58 } 59 theme->textStyle_.SetTextColor(pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK) 60 .BlendOpacity(pattern->GetAttr<double>(PATTERN_TEXT_COLOR_ALPHA, 0.9))); 61 theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>("text_font_size", 0.0_vp)); 62 theme->selectedColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_SELECTED, Color(0x33007dff)); 63 auto draggable = pattern->GetAttr<std::string>("draggable", "0"); 64 theme->draggable_ = StringUtils::StringToInt(draggable); 65 auto dragBackgroundColor = pattern->GetAttr<Color>("drag_background_color", Color::WHITE); 66 if (SystemProperties::GetColorMode() == ColorMode::DARK) { 67 dragBackgroundColor = dragBackgroundColor.ChangeOpacity(DRAG_BACKGROUND_OPACITY); 68 } 69 theme->dragBackgroundColor_ = dragBackgroundColor; 70 constexpr double childMinSize = 20.0; 71 theme->linearSplitChildMinSize_ = pattern->GetAttr<double>(LINEAR_SPLIT_CHILD_MIN_SIZE, childMinSize); 72 auto textShowHandle = pattern->GetAttr<std::string>("text_show_handle", "0"); 73 theme->isShowHandle_ = StringUtils::StringToInt(textShowHandle); 74 } 75 }; 76 77 ~TextTheme() override = default; 78 GetTextStyle()79 const TextStyle& GetTextStyle() const 80 { 81 return textStyle_; 82 } 83 GetSelectedColor()84 const Color& GetSelectedColor() const 85 { 86 return selectedColor_; 87 } 88 GetDraggable()89 bool GetDraggable() const 90 { 91 return draggable_; 92 } 93 GetLinearSplitChildMinSize()94 double GetLinearSplitChildMinSize() const 95 { 96 return linearSplitChildMinSize_; 97 } 98 IsShowHandle()99 bool IsShowHandle() const 100 { 101 return isShowHandle_; 102 } 103 GetDragBackgroundColor()104 const Color& GetDragBackgroundColor() const 105 { 106 return dragBackgroundColor_; 107 } 108 109 protected: 110 TextTheme() = default; 111 112 private: 113 TextStyle textStyle_; 114 Color selectedColor_; 115 Color dragBackgroundColor_ = Color::WHITE; 116 bool draggable_ = false; 117 double linearSplitChildMinSize_ = 20.0; 118 bool isShowHandle_ = false; 119 }; 120 121 } // namespace OHOS::Ace 122 123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_TEXT_THEME_H 124