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  #include "core/components_ng/render/paragraph.h"
27  #include "core/components_ng/pattern/text_field/text_field_model.h"
28  
29  namespace OHOS::Ace {
30  
31  constexpr uint32_t KEY_NULL = 0;
32  constexpr uint32_t KEY_ALT = 1 << 0;
33  constexpr uint32_t KEY_SHIFT = 1 << 1;
34  constexpr uint32_t KEY_CTRL = 1 << 2;
35  constexpr uint32_t KEY_META = 1 << 3;
36  
37  constexpr int32_t INVALID_VALUE = -1;
38  const std::string PRIVATE_DATA_KEY = "previewTextStyle";
39  
40  enum class CaretMoveIntent {
41      Left,
42      Right,
43      Up,
44      Down,
45      LeftWord,
46      RightWord,
47      ParagraghBegin,
48      ParagraghEnd,
49      LineBegin,
50      LineEnd,
51      Home,
52      End,
53  };
54  
55  struct PreviewRange {
56      int32_t start = INVALID_VALUE;
57      int32_t end = INVALID_VALUE;
58      bool operator==(const PreviewRange &range) const
59      {
60          return start == range.start && end == range.end;
61      }
SetPreviewRange62      void Set(int32_t startValue, int32_t endValue)
63      {
64          start = startValue;
65          end = endValue;
66      }
67  };
68  
69  struct KeyComb final {
70      KeyCode code;
71      int32_t modKeyFlags;
72  
73      KeyComb(KeyCode code, int32_t modKeyFlags = KEY_NULL) : code(code), modKeyFlags(modKeyFlags) {};
74  
75      bool operator<(const KeyComb& other) const
76      {
77          return code == other.code ? modKeyFlags < other.modKeyFlags : code < other.code;
78      }
79  };
80  
81  class TextInputClient : public virtual AceType {
82      DECLARE_ACE_TYPE(TextInputClient, AceType);
83  
84  public:
85      // Requests that this client update its editing state to the given value.
86      virtual void UpdateEditingValue(
87          const std::shared_ptr<TextEditingValue>& value, bool needFireChangeEvent = true) = 0;
88  
89      // Requests that this client perform the given action.
90      virtual void PerformAction(TextInputAction action, bool forceCloseKeyboard = false) = 0;
91  
92      virtual void InsertValue(const std::string& insertValue, bool isIME = false) {};
DeleteBackward(int32_t length)93      virtual void DeleteBackward(int32_t length) {};
DeleteForward(int32_t length)94      virtual void DeleteForward(int32_t length) {};
SetInputMethodStatus(bool keyboardShown)95      virtual void SetInputMethodStatus(bool keyboardShown) {}
NotifyKeyboardClosedByUser()96      virtual void NotifyKeyboardClosedByUser() {}
NotifyKeyboardClosed()97      virtual void NotifyKeyboardClosed() {}
98      virtual void NotifyKeyboardHeight(uint32_t height);
GetLeftTextOfCursor(int32_t number)99      virtual std::u16string GetLeftTextOfCursor(int32_t number)
100      {
101          return StringUtils::DEFAULT_USTRING;
102      }
103  
GetRightTextOfCursor(int32_t number)104      virtual std::u16string GetRightTextOfCursor(int32_t number)
105      {
106          return StringUtils::DEFAULT_USTRING;
107      }
GetTextIndexAtCursor()108      virtual int32_t GetTextIndexAtCursor()
109      {
110          return -1;
111      }
112  
113      virtual void HandleSetSelection(int32_t start, int32_t end, bool showHandle = true) {}
HandleExtendAction(int32_t action)114      virtual void HandleExtendAction(int32_t action) {}
115  
116  #if defined(IOS_PLATFORM)
GetInputEditingValue()117      virtual const TextEditingValue& GetInputEditingValue() const
118      {
119          static TextEditingValue value;
120          return value;
121      };
122  #endif
UpdateInputFilterErrorText(const std::string & errorText)123      virtual void UpdateInputFilterErrorText(const std::string& errorText) {};
ResetTouchAtLeftOffsetFlag()124      virtual void ResetTouchAtLeftOffsetFlag() {}
125  
126      // Requests that this client Y point.
GetEditingBoxY()127      virtual double GetEditingBoxY() const
128      {
129          return 0.0;
130      };
GetEditingBoxTopY()131      virtual double GetEditingBoxTopY() const
132      {
133          return 0.0;
134      };
GetEditingBoxModel()135      virtual bool GetEditingBoxModel() const
136      {
137          return false;
138      };
139  
GetInstanceId()140      virtual int32_t GetInstanceId() const
141      {
142          return instanceId_;
143      }
144  
SetInstanceId(int32_t instanceId)145      void SetInstanceId(int32_t instanceId)
146      {
147          instanceId_ = instanceId;
148      }
149  
150      bool HandleKeyEvent(const KeyEvent& keyEvent);
151  
HandleOnEscape()152      virtual bool HandleOnEscape()
153      {
154          return false;
155      }
156  
HandleOnTab(bool backward)157      virtual bool HandleOnTab(bool backward)
158      {
159          return false;
160      }
161  
CursorMove(CaretMoveIntent direction)162      virtual void CursorMove(CaretMoveIntent direction) {}
163  
HandleSelect(CaretMoveIntent direction)164      virtual void HandleSelect(CaretMoveIntent direction) {}
165  
HandleSelectFontStyle(KeyCode code)166      virtual void HandleSelectFontStyle(KeyCode code) {}
167  
HandleOnSelectAll()168      virtual void HandleOnSelectAll() {}
169  
HandleOnEnter()170      virtual void HandleOnEnter() {}
171  
HandleOnShowMenu()172      virtual void HandleOnShowMenu() {}
173  
174      virtual void HandleOnCopy(bool isUsingExternalKeyboard = false) {}
175  
HandleOnCut()176      virtual void HandleOnCut() {}
177  
HandleOnPaste()178      virtual void HandleOnPaste() {}
179  
HandleOnUndoAction()180      virtual void HandleOnUndoAction() {}
181  
HandleOnRedoAction()182      virtual void HandleOnRedoAction() {}
183  
HandleOnDelete(bool backward)184      virtual void HandleOnDelete(bool backward) {}
185  
HandleOnDeleteComb(bool backward)186      virtual bool HandleOnDeleteComb(bool backward)
187      {
188          return false;
189      }
190  
SetPreviewText(const std::string & previewValue,const PreviewRange range)191      virtual int32_t SetPreviewText(const std::string& previewValue, const PreviewRange range)
192      {
193          return 0;
194      }
195  
FinishTextPreview()196      virtual void FinishTextPreview() {}
ReceivePreviewTextStyle(const std::string & style)197      virtual void ReceivePreviewTextStyle (const std::string& style) {}
198  
CheckPreviewTextValidate(const std::string & previewValue,const PreviewRange range)199      virtual int32_t CheckPreviewTextValidate(const std::string& previewValue, const PreviewRange range)
200      {
201          return 0;
202      }
203  
204      static std::map<KeyComb, std::function<bool(TextInputClient*)>> functionKeys_;
205  
206      static std::map<KeyComb, std::function<void(TextInputClient*)>> keyboardShortCuts_;
207  
SetCaretOffset(int32_t caretPosition)208      virtual bool SetCaretOffset(int32_t caretPosition)
209      {
210          return false;
211      }
212  
213      virtual void SetSelection(int32_t start, int32_t end,
214          const std::optional<SelectionOptions>& options = std::nullopt, bool isForward = false) {}
215  
InsertOrDeleteSpace(int32_t index)216      virtual bool InsertOrDeleteSpace(int32_t index)
217      {
218          return false;
219      }
DeleteRange(int32_t start,int32_t end)220      virtual void DeleteRange(int32_t start, int32_t end) {}
221  protected:
222      int32_t instanceId_ = -1;
223  };
224  
225  } // namespace OHOS::Ace
226  
227  #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CLIENT_H
228