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