• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PATTERN_TEXT_INPUT_TEXT_INPUT_RESPONSE_AREA_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_INPUT_TEXT_INPUT_RESPONSE_AREA_H
18 
19 #include "base/geometry/ng/size_t.h"
20 #include "base/memory/ace_type.h"
21 #include "base/memory/referenced.h"
22 #include "core/components/text_field/textfield_theme.h"
23 #include "core/components_ng/pattern/pattern.h"
24 
25 namespace OHOS::Ace::NG {
26 class TextInputResponseArea : public virtual AceType {
27     DECLARE_ACE_TYPE(TextInputResponseArea, AceType);
28 
29 public:
TextInputResponseArea(const WeakPtr<Pattern> & hostPattern)30     TextInputResponseArea(const WeakPtr<Pattern>& hostPattern) : hostPattern_(hostPattern) {}
31     ~TextInputResponseArea() = default;
32 
33     virtual void InitResponseArea() = 0;
34 
35     virtual SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) = 0;
36 
37     virtual void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) = 0;
38 
Refresh()39     virtual void Refresh() {}
40 
ClearArea()41     virtual void ClearArea() {}
42 
43     virtual OffsetF GetChildOffset(SizeF parentSize, RectF contentRect, SizeF childSize, float nodeWidth);
44 
GetAreaRect()45     RectF GetAreaRect()
46     {
47         return areaRect_;
48     }
49 
50     virtual const RefPtr<FrameNode> GetFrameNode() = 0;
51 
52     SizeF GetFrameSize(bool withSafeArea = false);
53 
CreateIconRect(RoundRect & paintRect,bool isFocus)54     virtual void CreateIconRect(RoundRect& paintRect, bool isFocus) {}
55 
56     void SetHoverRect(RefPtr<FrameNode>& stackNode, RectF& rect, float iconSize,
57         float hoverRectHeight, bool isFocus);
58 
OnThemeScopeUpdate(const RefPtr<TextFieldTheme> & theme)59     virtual void OnThemeScopeUpdate(const RefPtr<TextFieldTheme>& theme) {}
60 
GetHoverIconPadding()61     virtual float GetHoverIconPadding() const
62     {
63         return 0.0f;
64     }
65 
66     void SetHotZoneRect(DimensionRect& hotZoneRegion, float iconSize, float hotZoneHeight);
67 
68 protected:
69     Alignment GetStackAlignment(const TextDirection& userDirection);
70     void LayoutChild(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth);
71     virtual RefPtr<FrameNode> CreateResponseAreaImageNode(const ImageSourceInfo& imageSourceInfo, ImageFit imageFit,
72         const CalcSize& userDefinedIdealSize);
73     WeakPtr<Pattern> hostPattern_;
74     RectF areaRect_;
75 };
76 
77 class PasswordResponseArea : public TextInputResponseArea {
78     DECLARE_ACE_TYPE(PasswordResponseArea, TextInputResponseArea);
79 
80 public:
PasswordResponseArea(const WeakPtr<Pattern> & hostPattern,bool isObscured)81     PasswordResponseArea(const WeakPtr<Pattern>& hostPattern, bool isObscured)
82         : TextInputResponseArea(hostPattern), isObscured_(isObscured) {}
83     ~PasswordResponseArea() = default;
84 
85     void InitResponseArea() override;
86 
87     SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) override;
88 
89     void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) override;
90 
91     void AddEvent(const RefPtr<FrameNode>& node);
92 
SetObscured(bool isObscured)93     void SetObscured(bool isObscured)
94     {
95         isObscured_ = isObscured;
96         ChangeObscuredState();
97     }
98 
IsObscured()99     bool IsObscured() const
100     {
101         return isObscured_;
102     }
103 
104     void Refresh() override;
105 
106     void ClearArea() override;
107 
108     const RefPtr<FrameNode> GetFrameNode() override;
109 
110     void OnPasswordIconClicked();
111     void UpdatePasswordIconColor(const Color& color);
112 
113     void CreateIconRect(RoundRect& paintRect, bool isFocus) override;
114 
115     void OnThemeScopeUpdate(const RefPtr<TextFieldTheme>& theme) override;
116 
GetHoverIconPadding()117     float GetHoverIconPadding() const override
118     {
119         return hoverIconPadding_;
120     }
121 
122 private:
123     void LoadImageSourceInfo();
124     void AddImageEventOnError();
125     void ChangeObscuredState();
126     ImageSourceInfo GetDefaultSourceInfo(bool isObscured);
127     void UpdateImageSource();
128     void UpdateSymbolSource();
129     void UpdateSymbolColor();
130     void InitSymbolEffectOptions();
131     bool IsShowSymbol();
132     bool IsSymbolIcon();
133     void ReplaceNode();
134     bool IsShowPasswordIcon();
135     float GetIconRightOffset();
136     float GetIconSize();
137     void AddIconHotZoneRect();
138     RefPtr<FrameNode> CreateNode();
GetCurrentSourceInfo()139     std::optional<ImageSourceInfo> GetCurrentSourceInfo()
140     {
141         return isObscured_ ? hideIcon_ : showIcon_;
142     }
143     bool isObscured_ = true;
144     std::optional<ImageSourceInfo> showIcon_;
145     std::optional<ImageSourceInfo> hideIcon_;
146     RefPtr<FrameNode> stackNode_;
147     WeakPtr<FrameNode> passwordNode_;
148     Color symbolColor_ = Color();
149     float passwordHoverSize_ = 0.0f;
150     float hoverIconPadding_ = 0.0f;
151 };
152 
153 class UnitResponseArea : public TextInputResponseArea {
154     DECLARE_ACE_TYPE(UnitResponseArea, TextInputResponseArea);
155 
156 public:
UnitResponseArea(const WeakPtr<Pattern> & hostPattern,const RefPtr<NG::UINode> & unitNode)157     UnitResponseArea(const WeakPtr<Pattern>& hostPattern, const RefPtr<NG::UINode>& unitNode)
158         : TextInputResponseArea(hostPattern), unitNode_(std::move(unitNode)) {}
159     ~UnitResponseArea() = default;
160 
161     void InitResponseArea() override;
162 
163     SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) override;
164 
165     void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) override;
166 
167     const RefPtr<FrameNode> GetFrameNode() override;
168 
169     void ClearArea() override;
170 
171 private:
172     bool IsShowUnit();
173     RefPtr<NG::UINode> unitNode_;
174 };
175 
176 class CleanNodeResponseArea : public TextInputResponseArea {
177     DECLARE_ACE_TYPE(CleanNodeResponseArea, TextInputResponseArea);
178 
179 public:
CleanNodeResponseArea(const WeakPtr<Pattern> & hostPattern)180     CleanNodeResponseArea(const WeakPtr<Pattern>& hostPattern) : TextInputResponseArea(hostPattern) {}
181     ~CleanNodeResponseArea() = default;
182 
183     void InitResponseArea() override;
184 
185     SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) override;
186 
187     void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) override;
188 
189     const RefPtr<FrameNode> GetFrameNode() override;
190 
191     void UpdateCleanNode(bool isShow);
192 
IsShow()193     bool IsShow() const
194     {
195         return isShow_;
196     }
197 
198     void ClearArea() override;
199 
200     void Refresh() override;
201 
GetIconSize()202     float GetIconSize()
203     {
204         return static_cast<float>(iconSize_.ConvertToPxDistribute(std::optional<float>(), std::optional<float>()));
205     }
206 
207     bool CheckUpdateCleanNode();
208 
209     void CreateIconRect(RoundRect& paintRect, bool isFocus) override;
210 
211     void OnThemeScopeUpdate(const RefPtr<TextFieldTheme>& theme) override;
212 
GetHoverIconPadding()213     float GetHoverIconPadding() const override
214     {
215         return hoverIconPadding_;
216     }
217 
218 private:
219     bool IsShowClean() const;
220     bool IsShowSymbol() const;
221     bool IsSymbolIcon() const;
222     void ReplaceNode();
223     void UpdateSymbolSource();
224     void InitClickEvent(const RefPtr<FrameNode>& frameNode);
225     void SetCancelSymbolIconSize();
226     CalcDimension GetSymbolDefaultSize();
227     void OnCleanNodeClicked();
228     RefPtr<FrameNode> CreateNode();
229     void LoadingImageProperty();
230     void LoadingCancelButtonColor();
231     void AddIconHotZoneRect();
232     ImageSourceInfo CreateImageSourceInfo();
233     RefPtr<FrameNode> cleanNode_;
234     CalcDimension iconSize_ = 0.0_px;
235     std::string iconSrc_;
236     std::string bundleName_;
237     std::string moduleName_;
238     Color iconColor_;
239     bool isShow_ = false;
240     float cancelHoverSize_ = 0.0f;
241     float hoverIconPadding_ = 0.0f;
242 };
243 } // namespace OHOS::Ace::NG
244 
245 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_INPUT_TEXT_INPUT_RESPONSE_AREA_H
246