• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "imf_adapter_impl.h"
17 
18 #include "nweb_log.h"
19 
20 namespace OHOS::NWeb {
IMFTextListenerAdapterImpl(const std::shared_ptr<IMFTextListenerAdapter> & listener)21 IMFTextListenerAdapterImpl::IMFTextListenerAdapterImpl(const std::shared_ptr<IMFTextListenerAdapter> &listener)
22     : listener_(listener) {};
23 
~IMFTextListenerAdapterImpl()24 IMFTextListenerAdapterImpl::~IMFTextListenerAdapterImpl()
25 {
26     listener_ = nullptr;
27 }
28 
InsertText(const std::u16string & text)29 void IMFTextListenerAdapterImpl::InsertText(const std::u16string &text)
30 {
31     if (listener_) {
32         listener_->InsertText(text);
33     }
34 }
35 
DeleteForward(int32_t length)36 void IMFTextListenerAdapterImpl::DeleteForward(int32_t length)
37 {
38     if (listener_) {
39         listener_->DeleteForward(length);
40     }
41 }
42 
DeleteBackward(int32_t length)43 void IMFTextListenerAdapterImpl::DeleteBackward(int32_t length)
44 {
45     if (listener_) {
46         listener_->DeleteBackward(length);
47     }
48 }
49 
SendKeyEventFromInputMethod(const MiscServices::KeyEvent & event)50 void IMFTextListenerAdapterImpl::SendKeyEventFromInputMethod(const MiscServices::KeyEvent &event)
51 {
52     (void)event;
53     if (listener_) {
54         listener_->SendKeyEventFromInputMethod();
55     }
56 }
57 
SendKeyboardInfo(const MiscServices::KeyboardInfo & info)58 void IMFTextListenerAdapterImpl::SendKeyboardInfo(const MiscServices::KeyboardInfo &info)
59 {
60     if (listener_) {
61         IMFAdapterKeyboardInfo adapterInfo;
62         if (info.GetKeyboardStatus() == MiscServices::KeyboardStatus::SHOW) {
63             adapterInfo.keyboardStatus = IMFAdapterKeyboardStatus::SHOW;
64         } else if (info.GetKeyboardStatus() == MiscServices::KeyboardStatus::HIDE) {
65             adapterInfo.keyboardStatus = IMFAdapterKeyboardStatus::HIDE;
66         }
67         if (info.GetFunctionKey() == MiscServices::FunctionKey::CONFIRM) {
68             adapterInfo.functionKey = IMFAdapterFunctionKey::CONFIRM;
69         }
70         listener_->SendKeyboardInfo(adapterInfo);
71     }
72 }
73 
SetKeyboardStatus(bool status)74 void IMFTextListenerAdapterImpl::SetKeyboardStatus(bool status)
75 {
76     if (listener_) {
77         listener_->SetKeyboardStatus(status);
78     }
79 }
80 
MoveCursor(const MiscServices::Direction direction)81 void IMFTextListenerAdapterImpl::MoveCursor(const MiscServices::Direction direction)
82 {
83     if (listener_) {
84         IMFAdapterDirection adapterDirection;
85         switch (direction) {
86             case MiscServices::Direction::UP: {
87                 adapterDirection = IMFAdapterDirection::UP;
88                 break;
89             }
90             case MiscServices::Direction::DOWN: {
91                 adapterDirection = IMFAdapterDirection::DOWN;
92                 break;
93             }
94             case MiscServices::Direction::LEFT: {
95                 adapterDirection = IMFAdapterDirection::LEFT;
96                 break;
97             }
98             case MiscServices::Direction::RIGHT: {
99                 adapterDirection = IMFAdapterDirection::RIGHT;
100                 break;
101             }
102             default: {
103                 adapterDirection = IMFAdapterDirection::NONE;
104             }
105         }
106         listener_->MoveCursor(adapterDirection);
107     }
108 }
109 
HandleSetSelection(int32_t start,int32_t end)110 void IMFTextListenerAdapterImpl::HandleSetSelection(int32_t start, int32_t end)
111 {
112     if (listener_) {
113         listener_->HandleSetSelection(start, end);
114     }
115 }
116 
HandleExtendAction(int32_t action)117 void IMFTextListenerAdapterImpl::HandleExtendAction(int32_t action)
118 {
119     if (listener_) {
120         listener_->HandleExtendAction(action);
121     }
122 }
123 
HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)124 void IMFTextListenerAdapterImpl::HandleSelect(int32_t keyCode, int32_t cursorMoveSkip)
125 {
126     if (listener_) {
127         listener_->HandleSelect(keyCode, cursorMoveSkip);
128     }
129 }
130 
Attach(std::shared_ptr<IMFTextListenerAdapter> listener,bool isShowKeyboard)131 bool IMFAdapterImpl::Attach(std::shared_ptr<IMFTextListenerAdapter> listener, bool isShowKeyboard)
132 {
133     if (!listener) {
134         WVLOG_E("the listener is nullptr");
135         return false;
136     }
137     if (!textListener_) {
138         textListener_ = new (std::nothrow) IMFTextListenerAdapterImpl(listener);
139         if (!textListener_) {
140             WVLOG_E("new textListener failed");
141             return false;
142         }
143     }
144     MiscServices::InputMethodController::GetInstance()->Attach(textListener_, isShowKeyboard);
145     return true;
146 }
147 
ShowCurrentInput(const IMFAdapterTextInputType & inputType)148 void IMFAdapterImpl::ShowCurrentInput(const IMFAdapterTextInputType &inputType)
149 {
150     MiscServices::Configuration config;
151     if (inputType == IMFAdapterTextInputType::NUMBER) {
152         config.SetTextInputType(MiscServices::TextInputType::NUMBER);
153     } else {
154         config.SetTextInputType(MiscServices::TextInputType::TEXT);
155     }
156     MiscServices::InputMethodController::GetInstance()->OnConfigurationChange(config);
157     MiscServices::InputMethodController::GetInstance()->ShowCurrentInput();
158 }
159 
HideTextInput()160 void IMFAdapterImpl::HideTextInput()
161 {
162     MiscServices::InputMethodController::GetInstance()->HideTextInput();
163 }
164 
Close()165 void IMFAdapterImpl::Close()
166 {
167     MiscServices::InputMethodController::GetInstance()->Close();
168 }
169 
OnCursorUpdate(IMFAdapterCursorInfo cursorInfo)170 void IMFAdapterImpl::OnCursorUpdate(IMFAdapterCursorInfo cursorInfo)
171 {
172     MiscServices::CursorInfo imfInfo = {
173         .left = cursorInfo.left,
174         .top = cursorInfo.top,
175         .width = cursorInfo.width,
176         .height = cursorInfo.height
177     };
178     MiscServices::InputMethodController::GetInstance()->OnCursorUpdate(imfInfo);
179 }
180 
OnSelectionChange(std::u16string text,int start,int end)181 void IMFAdapterImpl::OnSelectionChange(std::u16string text, int start, int end)
182 {
183     MiscServices::InputMethodController::GetInstance()->OnSelectionChange(text, start, end);
184 }
185 } // namespace OHOS::NWeb
186