• 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_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) = 0;
46 
GetAreaRect()47     RectF GetAreaRect()
48     {
49         return areaRect_;
50     }
51 
52     virtual const RefPtr<FrameNode> GetFrameNode() = 0;
53 protected:
54     void LayoutChild(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth);
55     WeakPtr<Pattern> hostPattern_;
56     RectF areaRect_;
57 };
58 
59 class PasswordResponseArea : public TextInputResponseArea {
60     DECLARE_ACE_TYPE(PasswordResponseArea, TextInputResponseArea);
61 
62 public:
PasswordResponseArea(const WeakPtr<Pattern> & hostPattern,bool isObscured)63     PasswordResponseArea(const WeakPtr<Pattern>& hostPattern, bool isObscured)
64         : TextInputResponseArea(hostPattern), isObscured_(isObscured) {}
65     ~PasswordResponseArea() = default;
66 
67     void InitResponseArea() override;
68 
69     SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) override;
70 
71     void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) override;
72 
73     OffsetF GetChildOffset(SizeF parentSize, RectF contentRect, SizeF childSize, float nodeWidth) override;
74 
75     void AddEvent(const RefPtr<FrameNode>& node);
76 
SetObscured(bool isObscured)77     void SetObscured(bool isObscured)
78     {
79         isObscured_ = isObscured;
80     }
81 
82     void Refresh() override;
83 
ClearArea()84     void ClearArea() override
85     {
86         auto hostPattern = hostPattern_.Upgrade();
87         CHECK_NULL_VOID(hostPattern);
88         auto host = hostPattern->GetHost();
89         CHECK_NULL_VOID(host);
90         CHECK_NULL_VOID(stackNode_);
91         host->RemoveChildAndReturnIndex(stackNode_);
92         passwordNode_.Reset();
93     }
94 
95     const RefPtr<FrameNode> GetFrameNode() override;
96 
97     void OnPasswordIconClicked();
98 
99 private:
100     void LoadImageSourceInfo();
101     ImageSourceInfo GetDefaultSourceInfo(bool isObscured);
102     void UpdateImageSource();
103     bool IsShowPasswordIcon();
104     float GetIconRightOffset();
105     float GetIconSize();
106     RefPtr<FrameNode> CreateNode();
GetCurrentSourceInfo()107     std::optional<ImageSourceInfo> GetCurrentSourceInfo()
108     {
109         return isObscured_ ? hideIcon_ : showIcon_;
110     }
111     bool isObscured_ = true;
112     std::optional<ImageSourceInfo> showIcon_;
113     std::optional<ImageSourceInfo> hideIcon_;
114     RefPtr<FrameNode> stackNode_;
115     WeakPtr<FrameNode> passwordNode_;
116 };
117 
118 class UnitResponseArea : public TextInputResponseArea {
119     DECLARE_ACE_TYPE(UnitResponseArea, TextInputResponseArea);
120 
121 public:
UnitResponseArea(const WeakPtr<Pattern> & hostPattern,const RefPtr<NG::UINode> & unitNode)122     UnitResponseArea(const WeakPtr<Pattern>& hostPattern, const RefPtr<NG::UINode>& unitNode)
123         : TextInputResponseArea(hostPattern), unitNode_(std::move(unitNode)) {}
~UnitResponseArea()124     ~UnitResponseArea()
125     {
126         ClearArea();
127     }
128 
129     void InitResponseArea() override;
130 
131     SizeF Measure(LayoutWrapper* layoutWrapper, int32_t index) override;
132 
133     void Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth) override;
134 
135     OffsetF GetChildOffset(SizeF parentSize, RectF contentRect, SizeF childSize, float nodeWidth) override;
136 
137     const RefPtr<FrameNode> GetFrameNode() override;
138 
ClearArea()139     void ClearArea() override
140     {
141         auto hostPattern = hostPattern_.Upgrade();
142         CHECK_NULL_VOID(hostPattern);
143         auto host = hostPattern->GetHost();
144         CHECK_NULL_VOID(host);
145         CHECK_NULL_VOID(unitNode_);
146         host->RemoveChildAndReturnIndex(unitNode_);
147     }
148 
149 private:
150     bool IsShowUnit();
151     RefPtr<NG::UINode> unitNode_;
152 };
153 
154 class CleanNodeResponseArea : public TextInputResponseArea {
155     DECLARE_ACE_TYPE(CleanNodeResponseArea, TextInputResponseArea);
156 
157 public:
CleanNodeResponseArea(const WeakPtr<Pattern> & hostPattern)158     CleanNodeResponseArea(const WeakPtr<Pattern>& hostPattern) : TextInputResponseArea(hostPattern) {}
159     ~CleanNodeResponseArea() = 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     OffsetF GetChildOffset(SizeF parentSize, RectF contentRect, SizeF childSize, float nodeWidth) override;
168 
169     const RefPtr<FrameNode> GetFrameNode() override;
170 
171     void UpdateCleanNode(bool isShow);
172 
IsShow()173     bool IsShow() const
174     {
175         return isShow_;
176     }
177 
178     void ClearArea() override;
179 
180     void Refresh() override;
181 
182 private:
183     void InitClickEvent(const RefPtr<FrameNode>& frameNode);
184     void OnCleanNodeClicked();
185     RefPtr<FrameNode> CreateNode();
186     void LoadingImageProperty();
187     ImageSourceInfo CreateImageSourceInfo();
188     RefPtr<FrameNode> cleanNode_;
189     CalcDimension iconSize_ = 0.0_px;
190     std::string iconSrc_;
191     Color iconColor_;
192     bool isShow_ = false;
193 };
194 } // namespace OHOS::Ace::NG
195 
196 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_INPUT_TEXT_INPUT_RESPONSE_AREA_H
197