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_DECLARATION_TEXT_TEXT_DECLARATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_TEXT_TEXT_DECLARATION_H 18 19 #include "core/components/declaration/common/declaration.h" 20 #include "frameworks/bridge/common/dom/dom_type.h" 21 22 namespace OHOS::Ace { 23 24 struct TextSpecializedAttribute : Attribute { 25 std::string data; 26 }; 27 28 struct TextSpecializedStyle : Style { 29 TextStyle textStyle; 30 Color focusColor; 31 bool isMaxWidthLayout = false; 32 bool autoMaxLines = false; 33 }; 34 35 class TextDeclaration : public Declaration { 36 DECLARE_ACE_TYPE(TextDeclaration, Declaration); 37 38 public: 39 TextDeclaration() = default; 40 ~TextDeclaration() override = default; 41 42 void InitializeStyle() override; 43 GetData()44 const std::string& GetData() const 45 { 46 auto& attribute = static_cast<TextSpecializedAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 47 return attribute.data; 48 } 49 SetData(const std::string & data)50 void SetData(const std::string& data) 51 { 52 auto& attribute = MaybeResetAttribute<TextSpecializedAttribute>(AttributeTag::SPECIALIZED_ATTR); 53 CheckIsChanged(attribute.data, data); 54 attribute.data = data; 55 } 56 GetTextStyle()57 const TextStyle& GetTextStyle() const 58 { 59 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 60 return style.textStyle; 61 } 62 SetTextStyle(const TextStyle & textStyle)63 void SetTextStyle(const TextStyle& textStyle) 64 { 65 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 66 CheckIsChanged(style.textStyle, textStyle); 67 style.textStyle = textStyle; 68 } 69 GetFocusColor()70 const Color& GetFocusColor() const 71 { 72 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 73 return style.focusColor; 74 } 75 SetFocusColor(const Color & focusColor)76 void SetFocusColor(const Color& focusColor) 77 { 78 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 79 CheckIsChanged(style.focusColor, focusColor); 80 style.focusColor = focusColor; 81 } 82 IsMaxWidthLayout()83 bool IsMaxWidthLayout() const 84 { 85 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 86 return style.isMaxWidthLayout; 87 } 88 SetIsMaxWidthLayout(bool isMaxWidthLayout)89 void SetIsMaxWidthLayout(bool isMaxWidthLayout) 90 { 91 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 92 CheckIsChanged(style.isMaxWidthLayout, isMaxWidthLayout); 93 style.isMaxWidthLayout = isMaxWidthLayout; 94 } 95 GetAutoMaxLines()96 bool GetAutoMaxLines() const 97 { 98 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 99 return style.autoMaxLines; 100 } 101 SetAutoMaxLines(bool autoMaxLines)102 void SetAutoMaxLines(bool autoMaxLines) 103 { 104 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 105 CheckIsChanged(style.autoMaxLines, autoMaxLines); 106 style.autoMaxLines = autoMaxLines; 107 } 108 IsChanged()109 bool IsChanged() const 110 { 111 return isChanged_; 112 } 113 SetIsChanged(bool isChanged)114 void SetIsChanged(bool isChanged) 115 { 116 isChanged_ = isChanged; 117 } 118 HasSetTextFontSize()119 bool HasSetTextFontSize() const 120 { 121 return hasSetTextFontSize_; 122 } 123 HasSetTextColor()124 bool HasSetTextColor() const 125 { 126 return hasSetTextColor_; 127 } 128 129 protected: 130 void InitSpecialized() override; 131 bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override; 132 bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override; 133 134 private: 135 template<class T> CheckIsChanged(const T & update,const T & val)136 void CheckIsChanged(const T& update, const T& val) 137 { 138 if (update != val) { 139 isChanged_ = true; 140 } 141 } 142 143 bool isChanged_ = false; 144 bool hasSetTextColor_ = false; 145 bool hasSetTextFontSize_ = false; 146 }; 147 148 } // namespace OHOS::Ace 149 150 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_TEXT_TEXT_DECLARATION_H 151