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_ng/event/click_event.h"
23 #include "core/components_ng/layout/layout_wrapper.h"
24 #include "core/components_ng/pattern/pattern.h"
25 #include "core/image/image_source_info.h"
26 
27 namespace OHOS::Ace::NG {
28 class TextInputResponseArea : public virtual AceType {
29     DECLARE_ACE_TYPE(TextInputResponseArea, AceType);
30 
31 public:
TextInputResponseArea(const WeakPtr<Pattern> & hostPattern)32     TextInputResponseArea(const WeakPtr<Pattern>& hostPattern) : hostPattern_(hostPattern) {}
33     ~TextInputResponseArea() = default;
34 
35     virtual void InitResponseArea() = 0;
36 
37     virtual SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) = 0;
38 
39     virtual void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) = 0;
40 
Refresh()41     virtual void Refresh() {}
42 
ClearArea()43     virtual void ClearArea() {}
44 
45     virtual OffsetF GetChildOffset(SizeF parentSize, RectF contentRect, SizeF childSize, float nodeWidth);
46 
GetAreaRect()47     RectF GetAreaRect()
48     {
49         return areaRect_;
50     }
51 
52     virtual const RefPtr<FrameNode> GetFrameNode() = 0;
53 
54     SizeF GetFrameSize(bool withSafeArea = false);
55 
56 protected:
57     Alignment GetStackAlignment(const TextDirection& userDirection);
58     void LayoutChild(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth);
59     WeakPtr<Pattern> hostPattern_;
60     RectF areaRect_;
61 };
62 
63 class PasswordResponseArea : public TextInputResponseArea {
64     DECLARE_ACE_TYPE(PasswordResponseArea, TextInputResponseArea);
65 
66 public:
PasswordResponseArea(const WeakPtr<Pattern> & hostPattern,bool isObscured)67     PasswordResponseArea(const WeakPtr<Pattern>& hostPattern, bool isObscured)
68         : TextInputResponseArea(hostPattern), isObscured_(isObscured) {}
~PasswordResponseArea()69     ~PasswordResponseArea()
70     {
71         ClearArea();
72     }
73 
74     void InitResponseArea() override;
75 
76     SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) override;
77 
78     void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) override;
79 
80     void AddEvent(const RefPtr<FrameNode>& node);
81 
SetObscured(bool isObscured)82     void SetObscured(bool isObscured)
83     {
84         isObscured_ = isObscured;
85         ChangeObscuredState();
86     }
87 
IsObscured()88     bool IsObscured() const
89     {
90         return isObscured_;
91     }
92 
93     void Refresh() override;
94 
ClearArea()95     void ClearArea() override
96     {
97         auto hostPattern = hostPattern_.Upgrade();
98         CHECK_NULL_VOID(hostPattern);
99         auto host = hostPattern->GetHost();
100         CHECK_NULL_VOID(host);
101         CHECK_NULL_VOID(stackNode_);
102         host->RemoveChildAndReturnIndex(stackNode_);
103         passwordNode_.Reset();
104         areaRect_.Reset();
105     }
106 
107     const RefPtr<FrameNode> GetFrameNode() override;
108 
109     void OnPasswordIconClicked();
110 
111 private:
112     void LoadImageSourceInfo();
113     void ChangeObscuredState();
114     ImageSourceInfo GetDefaultSourceInfo(bool isObscured);
115     void UpdateImageSource();
116     bool IsShowPasswordIcon();
117     float GetIconRightOffset();
118     float GetIconSize();
119     RefPtr<FrameNode> CreateNode();
GetCurrentSourceInfo()120     std::optional<ImageSourceInfo> GetCurrentSourceInfo()
121     {
122         return isObscured_ ? hideIcon_ : showIcon_;
123     }
124     bool isObscured_ = true;
125     std::optional<ImageSourceInfo> showIcon_;
126     std::optional<ImageSourceInfo> hideIcon_;
127     RefPtr<FrameNode> stackNode_;
128     WeakPtr<FrameNode> passwordNode_;
129 };
130 
131 class UnitResponseArea : public TextInputResponseArea {
132     DECLARE_ACE_TYPE(UnitResponseArea, TextInputResponseArea);
133 
134 public:
UnitResponseArea(const WeakPtr<Pattern> & hostPattern,const RefPtr<NG::UINode> & unitNode)135     UnitResponseArea(const WeakPtr<Pattern>& hostPattern, const RefPtr<NG::UINode>& unitNode)
136         : TextInputResponseArea(hostPattern), unitNode_(std::move(unitNode)) {}
~UnitResponseArea()137     ~UnitResponseArea()
138     {
139         ClearArea();
140     }
141 
142     void InitResponseArea() override;
143 
144     SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) override;
145 
146     void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) override;
147 
148     const RefPtr<FrameNode> GetFrameNode() override;
149 
ClearArea()150     void ClearArea() override
151     {
152         auto hostPattern = hostPattern_.Upgrade();
153         CHECK_NULL_VOID(hostPattern);
154         auto host = hostPattern->GetHost();
155         CHECK_NULL_VOID(host);
156         CHECK_NULL_VOID(unitNode_);
157         host->RemoveChildAndReturnIndex(unitNode_);
158         areaRect_.Reset();
159     }
160 
161 private:
162     bool IsShowUnit();
163     RefPtr<NG::UINode> unitNode_;
164 };
165 
166 class CleanNodeResponseArea : public TextInputResponseArea {
167     DECLARE_ACE_TYPE(CleanNodeResponseArea, TextInputResponseArea);
168 
169 public:
CleanNodeResponseArea(const WeakPtr<Pattern> & hostPattern)170     CleanNodeResponseArea(const WeakPtr<Pattern>& hostPattern) : TextInputResponseArea(hostPattern) {}
171     ~CleanNodeResponseArea() = default;
172 
173     void InitResponseArea() override;
174 
175     SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) override;
176 
177     void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) override;
178 
179     const RefPtr<FrameNode> GetFrameNode() override;
180 
181     void UpdateCleanNode(bool isShow);
182 
IsShow()183     bool IsShow() const
184     {
185         return isShow_;
186     }
187 
188     void ClearArea() override;
189 
190     void Refresh() override;
191 
192 private:
193     bool IsShowClean();
194     void InitClickEvent(const RefPtr<FrameNode>& frameNode);
195     void OnCleanNodeClicked();
196     RefPtr<FrameNode> CreateNode();
197     void LoadingImageProperty();
198     void LoadingCancelButtonColor();
199     ImageSourceInfo CreateImageSourceInfo();
200     RefPtr<FrameNode> cleanNode_;
201     CalcDimension iconSize_ = 0.0_px;
202     std::string iconSrc_;
203     std::string bundleName_;
204     std::string moduleName_;
205     Color iconColor_;
206     bool isShow_ = false;
207 };
208 } // namespace OHOS::Ace::NG
209 
210 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_INPUT_TEXT_INPUT_RESPONSE_AREA_H
211