• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_FIELD_TEXT_FIELD_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_MANAGER_H
18 
19 #include "base/geometry/offset.h"
20 #include "base/memory/ace_type.h"
21 #include "base/utils/macros.h"
22 #include "core/common/ime/text_input_type.h"
23 #include "core/common/manager_interface.h"
24 #include "core/components_ng/pattern/pattern.h"
25 #include "core/components_ng/pattern/text_field/text_content_type.h"
26 #include "core/components_ng/property/safe_area_insets.h"
27 
28 namespace OHOS::Ace::NG {
29 
30 struct TextFieldInfo {
31     int32_t nodeId = -1;
32     TextInputType inputType;
33     TextContentType contentType;
34     int32_t autoFillContainerNodeId = -1;
35     bool enableAutoFill = true;
36 };
37 
38 class ACE_EXPORT TextFieldManagerNG : public ManagerInterface {
39     DECLARE_ACE_TYPE(TextFieldManagerNG, ManagerInterface);
40 
41 public:
42     TextFieldManagerNG() = default;
43     ~TextFieldManagerNG() override;
44 
45     void SetClickPosition(const Offset& position) override;
46 
GetClickPosition()47     const Offset& GetClickPosition() override
48     {
49         return position_;
50     }
51 
GetOptionalClickPosition()52     const std::optional<Offset>& GetOptionalClickPosition()
53     {
54         return optionalPosition_;
55     }
56 
ResetOptionalClickPosition()57     void ResetOptionalClickPosition() {
58         optionalPosition_ = std::nullopt;
59     }
60 
61     void AvoidKeyboardInSheet(const RefPtr<FrameNode>& textField);
62 
MovePage(int32_t pageId,const Offset & rootRect,double offsetHeight)63     void MovePage(int32_t pageId, const Offset& rootRect, double offsetHeight) override {}
RemovePageId(int32_t pageId)64     void RemovePageId(int32_t pageId) override {}
65 
GetOnFocusTextField()66     WeakPtr<Pattern>& GetOnFocusTextField()
67     {
68         return onFocusTextField_;
69     }
70 
SetOnFocusTextField(const WeakPtr<Pattern> & onFocusTextField)71     void SetOnFocusTextField(const WeakPtr<Pattern>& onFocusTextField)
72     {
73         const auto& pattern = onFocusTextField.Upgrade();
74         if (pattern && pattern->GetHost()) {
75             onFocusTextFieldId = pattern->GetHost()->GetId();
76         }
77         if (onFocusTextField_ != onFocusTextField) {
78             SetImeAttached(false);
79         }
80         onFocusTextField_ = onFocusTextField;
81     }
82 
83     bool ScrollTextFieldToSafeArea();
84 
85     void ClearOnFocusTextField();
86 
87     void ClearOnFocusTextField(int32_t id);
88 
89     bool ResetSlidingPanelParentHeight();
90 
91     bool UpdatePanelForVirtualKeyboard(double offsetY, double fullHeight);
92     void SetHeight(float height);
93 
GetHeight()94     float GetHeight() const
95     {
96         return height_;
97     }
98 
99     bool OnBackPressed();
100 
101     void UpdateScrollableParentViewPort(const RefPtr<FrameNode>& node);
102 
GetImeShow()103     bool GetImeShow() const override
104     {
105         if (!imeShow_ && imeAttachCalled_) {
106             TAG_LOGI(ACE_KEYBOARD, "imeNotShown but attach called, still consider that as shown");
107         }
108         return imeShow_ || imeAttachCalled_;
109     }
110 
SetImeShow(bool imeShow)111     void SetImeShow(bool imeShow)
112     {
113         imeShow_ = imeShow;
114         imeAttachCalled_ = false;
115     }
116 
SetImeAttached(bool imeAttached)117     void SetImeAttached(bool imeAttached)
118     {
119         imeAttachCalled_ = imeAttached;
120     }
121 
UsingCustomKeyboardAvoid()122     bool UsingCustomKeyboardAvoid() {
123         return usingCustomKeyboardAvoid_;
124     }
125 
SetUsingCustomKeyboardAvoid(bool usingCustomKeyboardAvoid)126     void SetUsingCustomKeyboardAvoid(bool usingCustomKeyboardAvoid) {
127         usingCustomKeyboardAvoid_ = usingCustomKeyboardAvoid;
128     }
129 
SetUIExtensionImeShow(bool imeShow)130     void SetUIExtensionImeShow(bool imeShow) override
131     {
132         uiExtensionImeShow_ = imeShow;
133     }
134 
PrevHasTextFieldPattern()135     bool PrevHasTextFieldPattern() const
136     {
137         return prevHasTextFieldPattern_;
138     }
139 
UpdatePrevHasTextFieldPattern()140     void UpdatePrevHasTextFieldPattern()
141     {
142         if (onFocusTextField_.Upgrade()) {
143             prevHasTextFieldPattern_ = true;
144         } else {
145             prevHasTextFieldPattern_ = false;
146         }
147     }
148 
149     void AvoidKeyBoardInNavigation();
150 
151     void SetNavContentAvoidKeyboardOffset(RefPtr<FrameNode> navNode, float avoidKeyboardOffset);
152 
SetNeedToRequestKeyboard(bool val)153     void SetNeedToRequestKeyboard(bool val) override
154     {
155         needToRequestKeyboard_ = val;
156     }
157 
GetNeedToRequestKeyboard()158     bool GetNeedToRequestKeyboard() override
159     {
160         return needToRequestKeyboard_;
161     }
162 
GetIfFocusTextFieldIsInline()163     bool GetIfFocusTextFieldIsInline() {
164         return focusFieldIsInline;
165     }
166 
SetIfFocusTextFieldIsInline(bool isinline)167     void SetIfFocusTextFieldIsInline(bool isinline) {
168         focusFieldIsInline = isinline;
169     }
170 
GetInlineTextFieldAvoidPositionYAndHeight(double & positionY,double & height)171     void GetInlineTextFieldAvoidPositionYAndHeight(double& positionY, double& height) {
172         positionY = inlinePositionY_;
173         height = inlineHeight_;
174     }
175 
SetInlineTextFieldAvoidPositionYAndHeight(double positionY,double height)176     void SetInlineTextFieldAvoidPositionYAndHeight(double positionY, double height) {
177         inlinePositionY_ = positionY;
178         inlineHeight_ = height;
179     }
180 
SetLastAvoidFieldId(int32_t lastAvoidFieldId)181     void SetLastAvoidFieldId(int32_t lastAvoidFieldId) {
182         lastAvoidFieldId_ = lastAvoidFieldId;
183     }
184 
GetLastAvoidFieldId()185     int32_t GetLastAvoidFieldId() {
186         return lastAvoidFieldId_;
187     }
188 
GetOnFocusTextFieldId()189     int32_t GetOnFocusTextFieldId() {
190         return onFocusTextFieldId;
191     }
192 
GetLaterAvoid()193     bool GetLaterAvoid() const
194     {
195         return laterAvoid_;
196     }
197 
SetLaterAvoid(bool laterAvoid)198     void SetLaterAvoid(bool laterAvoid)
199     {
200         laterAvoid_ = laterAvoid;
201     }
202 
SetLaterAvoidArgs(Rect keyboardArea,double positionY,double height,int32_t orientation)203     void SetLaterAvoidArgs(Rect keyboardArea, double positionY, double height, int32_t orientation)
204     {
205         laterAvoid_ = true;
206         laterAvoidKeyboardArea_ = keyboardArea;
207         laterAvoidPositionY_ = positionY;
208         laterAvoidHeight_ = height;
209         laterOrientation_ = orientation;
210     }
211 
GetLaterAvoidKeyboardRect()212     Rect GetLaterAvoidKeyboardRect()
213     {
214         return laterAvoidKeyboardArea_;
215     }
216 
GetLaterAvoidPositionY()217     double GetLaterAvoidPositionY()
218     {
219         return laterAvoidPositionY_;
220     }
221 
GetLaterAvoidHeight()222     double GetLaterAvoidHeight()
223     {
224         return laterAvoidHeight_;
225     }
226 
GetLaterOrientation()227     int32_t GetLaterOrientation()
228     {
229         return laterOrientation_;
230     }
231 
SetLastRequestKeyboardId(int32_t lastRequestKeyboardId)232     void SetLastRequestKeyboardId(int32_t lastRequestKeyboardId) {
233         lastRequestKeyboardId_ = lastRequestKeyboardId;
234     }
235 
GetLastRequestKeyboardId()236     int32_t GetLastRequestKeyboardId() {
237         return lastRequestKeyboardId_;
238     }
239 
SetClickPositionOffset(float clickPositionOffset)240     void SetClickPositionOffset(float clickPositionOffset)
241     {
242         clickPositionOffset_ = clickPositionOffset;
243     }
244 
GetClickPositionOffset()245     float GetClickPositionOffset()
246     {
247         return clickPositionOffset_;
248     }
249 
250     RefPtr<FrameNode> FindScrollableOfFocusedTextField(const RefPtr<FrameNode>& textField);
251     void AddTextFieldInfo(const TextFieldInfo& textFieldInfo);
252     void RemoveTextFieldInfo(const int32_t& autoFillContainerNodeId, const int32_t& nodeId);
253     void UpdateTextFieldInfo(const TextFieldInfo& textFieldInfo);
254     bool HasAutoFillPasswordNodeInContainer(const int32_t& autoFillContainerNodeId, const int32_t& nodeId);
255 
SetIsImeAttached(bool isImeAttached)256     void SetIsImeAttached(bool isImeAttached)
257     {
258         isImeAttached_ = isImeAttached;
259     }
260 
GetIsImeAttached()261     bool GetIsImeAttached() const override
262     {
263         return isImeAttached_;
264     }
265 
266 private:
267     bool ScrollToSafeAreaHelper(const SafeAreaInsets::Inset& bottomInset, bool isShowKeyboard);
268     RefPtr<FrameNode> FindNavNode(const RefPtr<FrameNode>& textField);
269     bool IsAutoFillPasswordType(const TextFieldInfo& textFieldInfo);
270 
271     bool focusFieldIsInline = false;
272     double inlinePositionY_ = 0.0f;
273     double inlineHeight_ = 0.0f;
274     bool hasMove_ = false;
275     bool imeShow_ = false;
276     bool usingCustomKeyboardAvoid_ = false;
277     bool uiExtensionImeShow_ = false;
278     bool prevHasTextFieldPattern_ = true;
279     Offset position_;
280     float clickPositionOffset_ = 0.0f;
281     std::optional<Offset> optionalPosition_;
282     float height_ = 0.0f;
283     WeakPtr<Pattern> onFocusTextField_;
284     WeakPtr<FrameNode> weakNavNode_;
285     int32_t onFocusTextFieldId = -1;
286     int32_t lastAvoidFieldId_ = -1;
287     int32_t lastRequestKeyboardId_ = -1;
288     bool imeAttachCalled_ = false;
289     bool needToRequestKeyboard_ = true;
290     std::unordered_map<int32_t, std::unordered_map<int32_t, TextFieldInfo>> textFieldInfoMap_;
291     bool laterAvoid_ = false;
292     Rect laterAvoidKeyboardArea_;
293     double laterAvoidPositionY_ = 0.0;
294     double laterAvoidHeight_ = 0.0;
295     int32_t laterOrientation_ = -1;
296     bool isImeAttached_ = false;
297 };
298 
299 } // namespace OHOS::Ace::NG
300 
301 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_MANAGER_H
302