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 IMF_ADAPTER_IMPL_H 17 #define IMF_ADAPTER_IMPL_H 18 19 #include "imf_adapter.h" 20 #include "input_method_controller.h" 21 22 namespace OHOS::NWeb { 23 24 class IMFAdapterFunctionKeyAdapterImpl : public IMFAdapterFunctionKeyAdapter { 25 public: 26 IMFAdapterFunctionKeyAdapterImpl() = default; 27 28 ~IMFAdapterFunctionKeyAdapterImpl() = default; 29 30 IMFAdapterEnterKeyType GetEnterKeyType() override 31 { 32 return enterKeyType; 33 } 34 SetEnterKeyType(IMFAdapterEnterKeyType keyType)35 void SetEnterKeyType(IMFAdapterEnterKeyType keyType) 36 { 37 enterKeyType = keyType; 38 } 39 40 private: 41 IMFAdapterEnterKeyType enterKeyType = IMFAdapterEnterKeyType::UNSPECIFIED; 42 }; 43 44 class IMFTextListenerAdapterImpl : public MiscServices::OnTextChangedListener { 45 public: 46 explicit IMFTextListenerAdapterImpl(const std::shared_ptr<IMFTextListenerAdapter>& listener); 47 48 ~IMFTextListenerAdapterImpl(); 49 50 void InsertText(const std::u16string& text) override; 51 52 void DeleteForward(int32_t length) override; 53 54 void DeleteBackward(int32_t length) override; 55 56 void SendKeyEventFromInputMethod(const MiscServices::KeyEvent& event) override; 57 58 void SendKeyboardStatus(const MiscServices::KeyboardStatus& keyboardStatus) override; 59 60 void SendFunctionKey(const MiscServices::FunctionKey& functionKey) override; 61 62 void SetKeyboardStatus(bool status) override; 63 64 void MoveCursor(const MiscServices::Direction direction) override; 65 66 void HandleSetSelection(int32_t start, int32_t end) override; 67 68 void HandleExtendAction(int32_t action) override; 69 70 void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override; 71 72 int32_t GetTextIndexAtCursor() override; 73 74 std::u16string GetLeftTextOfCursor(int32_t number) override; 75 76 std::u16string GetRightTextOfCursor(int32_t number) override; 77 78 int32_t SetPreviewText(const std::u16string& text, const MiscServices::Range& range) override; 79 80 void FinishTextPreview() override; 81 82 int32_t ReceivePrivateCommand( 83 const std::unordered_map<std::string, MiscServices::PrivateDataValue>& privateCommand) override; 84 85 private: 86 std::shared_ptr<IMFTextListenerAdapter> listener_ = nullptr; 87 const std::string PREVIEW_TEXT_STYLE_KEY = "previewTextStyle"; 88 const std::string PREVIEW_TEXT_STYLE_UNDERLINE = "underline"; 89 const std::string AUTO_FILL_PARAMS_USERNAME = "com.autofill.params.userName"; 90 const std::string AUTO_FILL_PARAMS_OTHERACCOUNT = "com.autofill.params.otherAccount"; 91 }; 92 93 class IMFAdapterImpl : public IMFAdapter { 94 public: 95 IMFAdapterImpl() = default; 96 97 ~IMFAdapterImpl() override = default; 98 99 bool Attach(std::shared_ptr<IMFTextListenerAdapter> listener, bool isShowKeyboard) override; 100 101 bool Attach(std::shared_ptr<IMFTextListenerAdapter> listener, bool isShowKeyboard, 102 const std::shared_ptr<IMFTextConfigAdapter> config, bool isResetListener) override; 103 104 void ShowCurrentInput(const IMFAdapterTextInputType& inputType) override; 105 106 void HideTextInput() override; 107 108 void Close() override; 109 110 void OnCursorUpdate(const std::shared_ptr<IMFCursorInfoAdapter> cursorInfo) override; 111 112 void OnSelectionChange(std::u16string text, int start, int end) override; 113 114 bool SendPrivateCommand(const std::string& commandKey, const std::string& commandValue) override; 115 116 private: 117 sptr<MiscServices::OnTextChangedListener> textListener_ = nullptr; 118 119 bool ParseFillContentJsonValue(const std::string& jsonStr, 120 std::unordered_map<std::string, std::variant<std::string, bool, int32_t>>& map); 121 }; 122 } // namespace OHOS::NWeb 123 124 #endif // IMF_ADAPTER_IMPL_H 125