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 GRAPHIC_LITE_INPUT_METHOD_MANAGER_H
17 #define GRAPHIC_LITE_INPUT_METHOD_MANAGER_H
18 
19 #include <string>
20 
21 #include "components/ui_view.h"
22 #include "gfx_utils/graphic_types.h"
23 
24 namespace OHOS {
25 class InputMethodManager : public HeapBase {
26 public:
27     struct InputMethodParam {
28         InputType inputType;
29         std::string text;
30         UIView* view; /* view param for mini system without Window feature */
31     };
32     /**
33      * @brief Defines a input method listener. You need to register this listener when you use keyboard to input text.
34      *        This listener will invoke when the edit text view get focused.
35      */
36     class InputMethodListener : public HeapBase {
37     public:
38         /**
39          * @brief A destructor used to delete the <b>InputMethodListener</b> instance.
40          */
~InputMethodListener()41         virtual ~InputMethodListener() {}
42 
43         /**
44          * @brief Invoke this method when edit text view get focused.
45          * @param param  the param passed, see InputMethodParam.
46          */
47         virtual void OnShow(InputMethodParam& param) = 0;
48 
49         /**
50          * @brief Invoke this method when edit text view get blured.
51          */
52         virtual void OnHide() = 0;
53     };
54 
55     /**
56      * @brief return InputMethodManager's singleton
57      * @return InputMethodManager*
58      */
59     static InputMethodManager& GetInstance();
60 
61     /**
62      * @brief Called to show input method when the edit view focuse
63      * @param UIView  the edit view
64      */
65     void ShowInputMethod(UIView* view);
66 
67     /**
68      * @brief Called to hide input method when the edit view blure
69      */
70     void HideInputMethod();
71 
72     /**
73      * @brief Sets a input method listener.
74      * @param listener the input method listener.
75      */
76     void SetInputMethodListener(InputMethodListener* listener);
77 
78     /**
79      * @brief Call to insert text when keyboard select new input text.
80      * @param text the input method listener.
81      */
82     void InsertText(std::string text);
83 
84     /**
85      * @brief Call to delete text when keyboard press delete button.
86      * @param length the length of charactor to delete
87      */
88     void DeleteBackward(uint16_t length);
89 
90     /**
91      * @brief Sets the input type.
92      * @param type the input type, see InputType
93      */
94     void SetInputType(InputType type);
95 
96     /**
97      * @brief Call function invoke after the keyboard showed.
98      */
99     void OnKeyboardShow();
100 
101     /**
102      * @brief Call function invoke after the keyboard hided.
103      */
104     void OnKeyboardHide();
105 
106     /**
107      * @brief Sets the cursor index.
108      */
109     void SetCursorIndex(uint16_t cursorIndex_);
110 
111     uint16_t GetCursorIndex();
112 
113 private:
InputMethodManager()114     InputMethodManager() {}
~InputMethodManager()115     ~InputMethodManager() {}
116 
117     InputMethodManager(const InputMethodManager&) = delete;
118     InputMethodManager& operator=(const InputMethodManager&) = delete;
119     InputMethodManager(InputMethodManager&&) = delete;
120     InputMethodManager& operator=(InputMethodManager&&) = delete;
121 
122     InputMethodListener* inputMethodListener_ = nullptr;
123     UIView* inputView_ = nullptr;
124 };
125 } // namespace OHOS
126 #endif // GRAPHIC_LITE_INPUT_METHOD_MANAGER_H
127