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 struct LaterAvoidInfo { 39 bool laterAvoid = false; 40 Rect keyboardArea; 41 double positionY = 0.0; 42 double avoidHeight = 0.0; 43 int32_t orientation = -1; 44 }; 45 46 class ACE_EXPORT TextFieldManagerNG : public ManagerInterface { 47 DECLARE_ACE_TYPE(TextFieldManagerNG, ManagerInterface); 48 49 public: 50 TextFieldManagerNG() = default; 51 ~TextFieldManagerNG() override; 52 53 void SetClickPosition(const Offset& position) override; GetClickPosition()54 const Offset& GetClickPosition() override 55 { 56 return position_; 57 } 58 GetOptionalClickPosition()59 const std::optional<Offset>& GetOptionalClickPosition() 60 { 61 return optionalPosition_; 62 } 63 ResetOptionalClickPosition()64 void ResetOptionalClickPosition() { 65 optionalPosition_ = std::nullopt; 66 } 67 68 RectF GetFocusedNodeCaretRect(); 69 70 void TriggerAvoidOnCaretChange(); 71 72 void AvoidKeyboardInSheet(const RefPtr<FrameNode>& textField); 73 MovePage(int32_t pageId,const Offset & rootRect,double offsetHeight)74 void MovePage(int32_t pageId, const Offset& rootRect, double offsetHeight) override {} RemovePageId(int32_t pageId)75 void RemovePageId(int32_t pageId) override {} 76 GetOnFocusTextField()77 WeakPtr<Pattern>& GetOnFocusTextField() 78 { 79 return onFocusTextField_; 80 } 81 82 void SetOnFocusTextField(const WeakPtr<Pattern>& onFocusTextField); 83 84 void GetOnFocusTextFieldInfo(const WeakPtr<Pattern>& onFocusTextField); 85 IsScrollableChild()86 bool IsScrollableChild() 87 { 88 return isScrollableChild_; 89 } 90 91 bool ScrollTextFieldToSafeArea(); 92 93 void ClearOnFocusTextField(); 94 95 void ClearOnFocusTextField(int32_t id); 96 97 bool ResetSlidingPanelParentHeight(); 98 99 bool UpdatePanelForVirtualKeyboard(double offsetY, double fullHeight); 100 void SetHeight(float height); 101 GetHeight()102 float GetHeight() const 103 { 104 return height_; 105 } 106 107 bool OnBackPressed(); 108 109 void UpdateScrollableParentViewPort(const RefPtr<FrameNode>& node); 110 111 bool GetImeShow() const override; 112 SetImeShow(bool imeShow)113 void SetImeShow(bool imeShow) 114 { 115 imeShow_ = imeShow; 116 imeAttachCalled_ = false; 117 } 118 SetImeAttached(bool imeAttached)119 void SetImeAttached(bool imeAttached) 120 { 121 imeAttachCalled_ = imeAttached; 122 } 123 UsingCustomKeyboardAvoid()124 bool UsingCustomKeyboardAvoid() { 125 return usingCustomKeyboardAvoid_; 126 } 127 128 void TriggerCustomKeyboardAvoid(); 129 SetUsingCustomKeyboardAvoid(bool usingCustomKeyboardAvoid)130 void SetUsingCustomKeyboardAvoid(bool usingCustomKeyboardAvoid) { 131 usingCustomKeyboardAvoid_ = usingCustomKeyboardAvoid; 132 } 133 SetUIExtensionImeShow(bool imeShow)134 void SetUIExtensionImeShow(bool imeShow) override 135 { 136 uiExtensionImeShow_ = imeShow; 137 } 138 PrevHasTextFieldPattern()139 bool PrevHasTextFieldPattern() const 140 { 141 return prevHasTextFieldPattern_; 142 } 143 UpdatePrevHasTextFieldPattern()144 void UpdatePrevHasTextFieldPattern() 145 { 146 prevHasTextFieldPattern_ = onFocusTextField_.Upgrade(); 147 } 148 HasKeyboard()149 bool HasKeyboard() const override 150 { 151 return imeShow_ || uiExtensionImeShow_; 152 } 153 154 void AvoidKeyBoardInNavigation(); 155 156 void SetNavContentAvoidKeyboardOffset(const RefPtr<FrameNode>& navNode, float avoidKeyboardOffset); 157 SetNeedToRequestKeyboard(bool val)158 void SetNeedToRequestKeyboard(bool val) override 159 { 160 needToRequestKeyboard_ = val; 161 } 162 GetNeedToRequestKeyboard()163 bool GetNeedToRequestKeyboard() override 164 { 165 return needToRequestKeyboard_; 166 } 167 GetIfFocusTextFieldIsInline()168 bool GetIfFocusTextFieldIsInline() { 169 return focusFieldIsInline_; 170 } 171 SetIfFocusTextFieldIsInline(bool isinline)172 void SetIfFocusTextFieldIsInline(bool isinline) { 173 focusFieldIsInline_ = isinline; 174 } 175 GetInlineTextFieldAvoidPositionYAndHeight(double & positionY,double & height)176 void GetInlineTextFieldAvoidPositionYAndHeight(double& positionY, double& height) { 177 positionY = inlinePositionY_; 178 height = inlineHeight_; 179 } 180 SetInlineTextFieldAvoidPositionYAndHeight(double positionY,double height)181 void SetInlineTextFieldAvoidPositionYAndHeight(double positionY, double height) { 182 inlinePositionY_ = positionY; 183 inlineHeight_ = height; 184 } 185 SetLastAvoidFieldId(int32_t lastAvoidFieldId)186 void SetLastAvoidFieldId(int32_t lastAvoidFieldId) { 187 lastAvoidFieldId_ = lastAvoidFieldId; 188 } 189 GetLastAvoidFieldId()190 int32_t GetLastAvoidFieldId() { 191 return lastAvoidFieldId_; 192 } 193 GetOnFocusTextFieldId()194 int32_t GetOnFocusTextFieldId() { 195 return onFocusTextFieldId_; 196 } 197 GetLaterAvoid()198 bool GetLaterAvoid() const 199 { 200 return laterAvoidInfo_.laterAvoid; 201 } 202 SetLaterAvoid(bool laterAvoid)203 void SetLaterAvoid(bool laterAvoid) 204 { 205 laterAvoidInfo_.laterAvoid = laterAvoid; 206 } 207 SetLaterAvoidArgs(LaterAvoidInfo laterAvoidInfo)208 void SetLaterAvoidArgs(LaterAvoidInfo laterAvoidInfo) 209 { 210 laterAvoidInfo_ = laterAvoidInfo; 211 } 212 GetLaterAvoidKeyboardRect()213 Rect GetLaterAvoidKeyboardRect() 214 { 215 return laterAvoidInfo_.keyboardArea; 216 } 217 GetLaterAvoidPositionY()218 double GetLaterAvoidPositionY() 219 { 220 return laterAvoidInfo_.positionY; 221 } 222 GetLaterAvoidHeight()223 double GetLaterAvoidHeight() 224 { 225 return laterAvoidInfo_.avoidHeight; 226 } 227 GetLaterOrientation()228 int32_t GetLaterOrientation() 229 { 230 return laterAvoidInfo_.orientation; 231 } 232 SetLastRequestKeyboardId(int32_t lastRequestKeyboardId)233 void SetLastRequestKeyboardId(int32_t lastRequestKeyboardId) { 234 lastRequestKeyboardId_ = lastRequestKeyboardId; 235 } 236 GetLastRequestKeyboardId()237 int32_t GetLastRequestKeyboardId() { 238 return lastRequestKeyboardId_; 239 } 240 SetClickPositionOffset(float clickPositionOffset)241 void SetClickPositionOffset(float clickPositionOffset) 242 { 243 clickPositionOffset_ = clickPositionOffset; 244 } 245 GetClickPositionOffset()246 float GetClickPositionOffset() 247 { 248 return clickPositionOffset_; 249 } 250 251 RefPtr<FrameNode> FindScrollableOfFocusedTextField(const RefPtr<FrameNode>& textField); 252 void AddTextFieldInfo(const TextFieldInfo& textFieldInfo); 253 void RemoveTextFieldInfo(const int32_t& autoFillContainerNodeId, const int32_t& nodeId); 254 void UpdateTextFieldInfo(const TextFieldInfo& textFieldInfo); 255 bool HasAutoFillPasswordNodeInContainer(const int32_t& autoFillContainerNodeId, const int32_t& nodeId); 256 257 GetFocusFieldAlreadyTriggerWsCallback()258 bool GetFocusFieldAlreadyTriggerWsCallback() const 259 { 260 return focusFieldAlreadyTriggerWsCallback_; 261 } 262 SetFocusFieldAlreadyTriggerWsCallback(bool focusFieldAlreadyTriggerWsCallback)263 void SetFocusFieldAlreadyTriggerWsCallback(bool focusFieldAlreadyTriggerWsCallback) { 264 focusFieldAlreadyTriggerWsCallback_ = focusFieldAlreadyTriggerWsCallback; 265 } 266 GetFocusFieldOrientation()267 int32_t GetFocusFieldOrientation() const 268 { 269 return focusFieldOrientation_; 270 } 271 SetFocusFieldOrientation(int32_t focusFieldOrientation)272 void SetFocusFieldOrientation(int32_t focusFieldOrientation) 273 { 274 focusFieldOrientation_ = focusFieldOrientation; 275 } 276 SetIsImeAttached(bool isImeAttached)277 void SetIsImeAttached(bool isImeAttached) 278 { 279 isImeAttached_ = isImeAttached; 280 } 281 GetIsImeAttached()282 bool GetIsImeAttached() const override 283 { 284 return isImeAttached_; 285 } 286 287 void AddAvoidKeyboardCallback(int32_t id, bool isCustomKeyboard, const std::function<void()>&& callback); 288 RemoveAvoidKeyboardCallback(int32_t id)289 void RemoveAvoidKeyboardCallback(int32_t id) 290 { 291 avoidCustomKeyboardCallbacks_.erase(id); 292 avoidSystemKeyboardCallbacks_.erase(id); 293 } 294 295 void OnAfterAvoidKeyboard(bool isCustomKeyboard); 296 297 private: 298 bool ScrollToSafeAreaHelper(const SafeAreaInsets::Inset& bottomInset, bool isShowKeyboard); 299 RefPtr<FrameNode> FindNavNode(const RefPtr<FrameNode>& textField); 300 bool IsAutoFillPasswordType(const TextFieldInfo& textFieldInfo); 301 302 bool focusFieldIsInline_ = false; 303 double inlinePositionY_ = 0.0f; 304 double inlineHeight_ = 0.0f; 305 bool hasMove_ = false; 306 bool imeShow_ = false; 307 bool usingCustomKeyboardAvoid_ = false; 308 bool uiExtensionImeShow_ = false; 309 bool prevHasTextFieldPattern_ = true; 310 bool focusFieldAlreadyTriggerWsCallback_ = false; 311 int32_t focusFieldOrientation_ = -1; 312 Offset position_; 313 float clickPositionOffset_ = 0.0f; 314 std::optional<Offset> optionalPosition_; 315 float height_ = 0.0f; 316 WeakPtr<Pattern> onFocusTextField_; 317 WeakPtr<FrameNode> weakNavNode_; 318 int32_t onFocusTextFieldId_ = -1; 319 int32_t lastAvoidFieldId_ = -1; 320 int32_t lastRequestKeyboardId_ = -1; 321 bool imeAttachCalled_ = false; 322 bool needToRequestKeyboard_ = true; 323 std::unordered_map<int32_t, std::unordered_map<int32_t, TextFieldInfo>> textFieldInfoMap_; 324 LaterAvoidInfo laterAvoidInfo_; 325 bool isScrollableChild_ = false; 326 bool isImeAttached_ = false; 327 std::unordered_map<int32_t, std::function<void()>> avoidSystemKeyboardCallbacks_; 328 std::unordered_map<int32_t, std::function<void()>> avoidCustomKeyboardCallbacks_; 329 float lastKeyboardOffset_ = 0.0f; 330 }; 331 332 } // namespace OHOS::Ace::NG 333 334 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_MANAGER_H 335