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 #include "common/input_method_manager.h"
17 
18 #include "components/ui_edit_text.h"
19 #include "gfx_utils/graphic_log.h"
20 #include "hal_tick.h"
21 
22 namespace OHOS {
GetInstance()23 InputMethodManager& InputMethodManager::GetInstance()
24 {
25     static InputMethodManager InputMethodManager;
26     return InputMethodManager;
27 }
28 
ShowInputMethod(UIView * view)29 void InputMethodManager::ShowInputMethod(UIView* view)
30 {
31     if (view == nullptr) {
32         return;
33     }
34 
35     if (inputMethodListener_ == nullptr) {
36         return;
37     }
38 
39     inputView_ = view;
40     UIViewType viewType = view->GetViewType();
41     if (viewType == UI_EDIT_TEXT) {
42         UIEditText* tmpView = reinterpret_cast<UIEditText*>(view);
43         InputMethodParam param;
44         param.view = view;
45         param.inputType = tmpView->GetInputType();
46 
47         if (tmpView->GetText() != nullptr) {
48             param.text = tmpView->GetText();
49         }
50 
51         inputMethodListener_->OnShow(param);
52     }
53 }
54 
HideInputMethod()55 void InputMethodManager::HideInputMethod()
56 {
57     if (inputMethodListener_ == nullptr) {
58         return;
59     }
60 
61     inputMethodListener_->OnHide();
62 }
63 
SetInputMethodListener(InputMethodListener * listener)64 void InputMethodManager::SetInputMethodListener(InputMethodListener* listener)
65 {
66     inputMethodListener_ = listener;
67 }
68 
InsertText(std::string text)69 void InputMethodManager::InsertText(std::string text)
70 {
71     if (inputView_ == nullptr) {
72         return;
73     }
74 
75     UIViewType viewType = inputView_->GetViewType();
76     if (viewType == UI_EDIT_TEXT) {
77         UIEditText* tmpView = reinterpret_cast<UIEditText*>(inputView_);
78         tmpView->InsertText(text);
79     }
80 }
81 
SetCursorIndex(uint16_t cursorIndex_)82 void InputMethodManager::SetCursorIndex(uint16_t cursorIndex_)
83 {
84     UIEditText* tmpView = reinterpret_cast<UIEditText*>(inputView_);
85     tmpView->SetCursorIndex(cursorIndex_);
86 }
87 
GetCursorIndex()88 uint16_t InputMethodManager::GetCursorIndex()
89 {
90     UIEditText* tmpView = reinterpret_cast<UIEditText*>(inputView_);
91     return tmpView->GetCursorIndex();
92 }
93 
DeleteBackward(uint16_t length)94 void InputMethodManager::DeleteBackward(uint16_t length)
95 {
96     if (inputView_ == nullptr) {
97         return;
98     }
99 
100     UIViewType viewType = inputView_->GetViewType();
101     if (viewType == UI_EDIT_TEXT) {
102         UIEditText* tmpView = reinterpret_cast<UIEditText*>(inputView_);
103         tmpView->DeleteBackward(length);
104     }
105 }
106 
SetInputType(InputType type)107 void InputMethodManager::SetInputType(InputType type)
108 {
109     if (inputView_ == nullptr) {
110         return;
111     }
112 
113     UIViewType viewType = inputView_->GetViewType();
114     if (viewType == UI_EDIT_TEXT) {
115         UIEditText* tmpView = reinterpret_cast<UIEditText*>(inputView_);
116         tmpView->SetInputType(type);
117     }
118 }
119 
OnKeyboardShow()120 void InputMethodManager::OnKeyboardShow() {}
121 
OnKeyboardHide()122 void InputMethodManager::OnKeyboardHide() {}
123 } // namespace OHOS
124