1 /* 2 * Copyright (c) 2021 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_COMMON_IME_TEXT_INPUT_CLIENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CLIENT_H 18 19 #include <cstdint> 20 21 #include "base/memory/ace_type.h" 22 #include "base/utils/string_utils.h" 23 #include "core/common/ime/text_editing_value.h" 24 #include "core/common/ime/text_input_action.h" 25 #include "core/event/key_event.h" 26 27 namespace OHOS::Ace { 28 29 constexpr uint32_t KEY_NULL = 0; 30 constexpr uint32_t KEY_ALT = 1 << 0; 31 constexpr uint32_t KEY_SHIFT = 1 << 1; 32 constexpr uint32_t KEY_CTRL = 1 << 2; 33 constexpr uint32_t KEY_META = 1 << 3; 34 35 enum class CaretMoveIntent { 36 Left, 37 Right, 38 Up, 39 Down, 40 LeftWord, 41 RightWord, 42 ParagraghBegin, 43 ParagraghEnd, 44 LineBegin, 45 LineEnd, 46 Home, 47 End, 48 }; 49 50 struct KeyComb final { 51 KeyCode code; 52 int32_t modKeyFlags; 53 codefinal54 KeyComb(KeyCode code, int32_t modKeyFlags = KEY_NULL) : code(code), modKeyFlags(modKeyFlags) {}; 55 56 bool operator<(const KeyComb& other) const 57 { 58 return code == other.code ? modKeyFlags < other.modKeyFlags : code < other.code; 59 } 60 }; 61 62 class TextInputClient : public virtual AceType { 63 DECLARE_ACE_TYPE(TextInputClient, AceType); 64 65 public: 66 // Requests that this client update its editing state to the given value. 67 virtual void UpdateEditingValue( 68 const std::shared_ptr<TextEditingValue>& value, bool needFireChangeEvent = true) = 0; 69 70 // Requests that this client perform the given action. 71 virtual void PerformAction(TextInputAction action, bool forceCloseKeyboard = false) = 0; 72 InsertValue(const std::string & insertValue)73 virtual void InsertValue(const std::string& insertValue) {}; DeleteBackward(int32_t length)74 virtual void DeleteBackward(int32_t length) {}; DeleteForward(int32_t length)75 virtual void DeleteForward(int32_t length) {}; SetInputMethodStatus(bool keyboardShown)76 virtual void SetInputMethodStatus(bool keyboardShown) {} NotifyKeyboardClosedByUser()77 virtual void NotifyKeyboardClosedByUser() {} GetLeftTextOfCursor(int32_t number)78 virtual std::u16string GetLeftTextOfCursor(int32_t number) 79 { 80 return StringUtils::DEFAULT_USTRING; 81 } 82 GetRightTextOfCursor(int32_t number)83 virtual std::u16string GetRightTextOfCursor(int32_t number) 84 { 85 return StringUtils::DEFAULT_USTRING; 86 } GetTextIndexAtCursor()87 virtual int32_t GetTextIndexAtCursor() 88 { 89 return -1; 90 } 91 92 virtual void HandleSetSelection(int32_t start, int32_t end, bool showHandle = true) {} HandleExtendAction(int32_t action)93 virtual void HandleExtendAction(int32_t action) {} 94 95 #if defined(IOS_PLATFORM) GetInputEditingValue()96 virtual const TextEditingValue& GetInputEditingValue() const 97 { 98 static TextEditingValue value; 99 return value; 100 }; 101 #endif UpdateInputFilterErrorText(const std::string & errorText)102 virtual void UpdateInputFilterErrorText(const std::string& errorText) {}; ResetTouchAtLeftOffsetFlag()103 virtual void ResetTouchAtLeftOffsetFlag() {} 104 105 // Requests that this client Y point. GetEditingBoxY()106 virtual double GetEditingBoxY() const 107 { 108 return 0.0; 109 }; GetEditingBoxTopY()110 virtual double GetEditingBoxTopY() const 111 { 112 return 0.0; 113 }; GetEditingBoxModel()114 virtual bool GetEditingBoxModel() const 115 { 116 return false; 117 }; 118 GetInstanceId()119 virtual int32_t GetInstanceId() const 120 { 121 return instanceId_; 122 } 123 SetInstanceId(int32_t instanceId)124 void SetInstanceId(int32_t instanceId) 125 { 126 instanceId_ = instanceId; 127 } 128 129 bool HandleKeyEvent(const KeyEvent& keyEvent); 130 HandleOnEscape()131 virtual bool HandleOnEscape() 132 { 133 return false; 134 } 135 HandleOnTab(bool backward)136 virtual bool HandleOnTab(bool backward) 137 { 138 return false; 139 } 140 CursorMove(CaretMoveIntent direction)141 virtual void CursorMove(CaretMoveIntent direction) {} 142 HandleSelect(CaretMoveIntent direction)143 virtual void HandleSelect(CaretMoveIntent direction) {} 144 HandleOnSelectAll()145 virtual void HandleOnSelectAll() {} 146 HandleOnEnter()147 virtual void HandleOnEnter() {} 148 HandleOnShowMenu()149 virtual void HandleOnShowMenu() {} 150 151 virtual void HandleOnCopy(bool isUsingExternalKeyboard = false) {} 152 HandleOnCut()153 virtual void HandleOnCut() {} 154 HandleOnPaste()155 virtual void HandleOnPaste() {} 156 HandleOnUndoAction()157 virtual void HandleOnUndoAction() {} 158 HandleOnRedoAction()159 virtual void HandleOnRedoAction() {} 160 HandleOnDelete(bool backward)161 virtual void HandleOnDelete(bool backward) {} 162 163 static std::map<KeyComb, std::function<bool(TextInputClient*)>> functionKeys_; 164 165 static std::map<KeyComb, std::function<void(TextInputClient*)>> keyboardShortCuts_; 166 167 protected: 168 int32_t instanceId_ = -1; 169 }; 170 171 } // namespace OHOS::Ace 172 173 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CLIENT_H 174