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_NG_PATTERNS_TEXT_FIELD_TEXT_FIELD_CONTENT_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_FIELD_TEXT_FIELD_CONTENT_MODIFIER_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components_ng/base/modifier.h"
22 #include "core/components_ng/pattern/pattern.h"
23 #include "core/components_ng/pattern/text_field/text_field_paint_property.h"
24 #include "core/components_ng/property/property.h"
25 #include "core/components_ng/render/animation_utils.h"
26 #include "core/components_ng/render/drawing.h"
27 
28 namespace OHOS::Ace::NG {
29 class TextFieldContentModifier : public ContentModifier {
30     DECLARE_ACE_TYPE(TextFieldContentModifier, ContentModifier);
31 
32 public:
33     explicit TextFieldContentModifier(const WeakPtr<OHOS::Ace::NG::Pattern>& pattern);
34     ~TextFieldContentModifier() override = default;
35 
36     void onDraw(DrawingContext& context) override;
37     void ModifyTextStyle(TextStyle& textStyle);
38     void SetDefaultAnimatablePropertyValue();
39 
40     void SetFontFamilies(const std::vector<std::string>& value);
41     void SetFontSize(const Dimension& value);
42     void SetAdaptMinFontSize(const Dimension& value);
43     void SetAdaptMaxFontSize(const Dimension& value);
44     void SetFontWeight(const FontWeight& value);
45     void SetTextColor(const Color& value);
46     void SetFontStyle(const OHOS::Ace::FontStyle& value);
47     void SetContentOffset(OffsetF& value);
48     float GetContentOffsetY();
49     void SetContentSize(SizeF& value);
50     void SetTextValue(std::string& value);
51     void SetErrorTextValue(const std::string& value);
52     void SetPlaceholderValue(std::string&& value);
53     void SetTextRectY(const float value);
54     float GetTextRectX();
55     void SetTextObscured(bool value);
56     void ChangeDragStatus();
57     void SetTextRectX(const float value);
58     float GetTextRectY();
59     void SetTextAlign(const TextAlign value);
60     bool NeedMeasureUpdate(PropertyChangeFlag& flag);
61     void SetShowErrorState(bool value);
62     void SetShowUnderlineState(bool value);
63     void SetFontReady(bool value);
64     void SetTextOverflow(const TextOverflow value);
65     void SetTextDecoration(const TextDecoration& value, const Color& color, const TextDecorationStyle& style);
66     void ContentChange();
67 
68 private:
69     void SetDefaultFontSize(const TextStyle& textStyle);
70     void SetDefaultAdaptMinFontSize(const TextStyle& textStyle);
71     void SetDefaultAdaptMaxFontSize(const TextStyle& textStyle);
72     void SetDefaultFontWeight(const TextStyle& textStyle);
73     void SetDefaultTextColor(const TextStyle& textStyle);
74     void SetDefaultFontStyle(const TextStyle& textStyle);
75     void SetDefaultTextOverflow(const TextStyle& textStyle);
76     void SetDefaultTextDecoration(const TextStyle& textStyle);
77     void SetDefaultPropertyValue();
78     void GetFrameRectClip(RSRect& clipRect, std::vector<RSPoint>& clipRadius);
79     void ProcessErrorParagraph(DrawingContext& context, float errorMargin);
80     void ModifyDecorationInTextStyle(TextStyle& textStyle);
81     void UpdateTextDecorationMeasureFlag(PropertyChangeFlag& flag);
82 
83     WeakPtr<Pattern> pattern_;
84     RefPtr<PropertyString> fontFamilyString_;
85 
86     std::optional<Dimension> fontSize_;
87     RefPtr<AnimatablePropertyFloat> fontSizeFloat_;
88 
89     std::optional<Dimension> adaptMinFontSize_;
90     RefPtr<AnimatablePropertyFloat> adaptMinFontSizeFloat_;
91 
92     std::optional<Dimension> adaptMaxFontSize_;
93     RefPtr<AnimatablePropertyFloat> adaptMaxFontSizeFloat_;
94 
95     std::optional<FontWeight> fontWeight_;
96     RefPtr<AnimatablePropertyFloat> fontWeightFloat_;
97 
98     std::optional<Color> textColor_;
99     RefPtr<AnimatablePropertyColor> animatableTextColor_;
100 
101     float oldColorAlpha_ { 0.0f };
102     std::optional<TextDecoration> textDecoration_;
103     std::optional<TextDecorationStyle> textDecorationStyle_;
104     std::optional<Color> textDecorationColor_;
105     RefPtr<AnimatablePropertyFloat> textDecorationColorAlpha_;
106     bool textDecorationAnimatable_ { false };
107 
108     RefPtr<PropertyFloat> textRectX_;
109     RefPtr<PropertyFloat> textRectY_;
110     RefPtr<PropertyOffsetF> contentOffset_;
111     RefPtr<PropertySizeF> contentSize_;
112     RefPtr<PropertyString> textValue_;
113     RefPtr<PropertyString> errorTextValue_;
114     RefPtr<PropertyString> placeholderValue_;
115     RefPtr<PropertyBool> textObscured_;
116     RefPtr<PropertyBool> dragStatus_;
117     RefPtr<PropertyInt> textAlign_;
118     RefPtr<PropertyBool> showErrorState_;
119     RefPtr<PropertyBool> showUnderline_;
120     RefPtr<PropertyBool> contentChange_;
121     RefPtr<PropertyInt> fontStyle_;
122     RefPtr<PropertyBool> fontReady_;
123     RefPtr<PropertyInt> textOverflow_;
124 
125     ACE_DISALLOW_COPY_AND_MOVE(TextFieldContentModifier);
126 };
127 } // namespace OHOS::Ace::NG
128 
129 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_FIELD_TEXT_FIELD_CONTENT_MODIFIER_H
130