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 "ohos_adapter/bridge/ark_imfadapter_wrapper.h"
17
18 #include "ohos_adapter/bridge/ark_imf_cursor_info_adapter_impl.h"
19 #include "ohos_adapter/bridge/ark_imf_text_config_adapter_impl.h"
20 #include "ohos_adapter/bridge/ark_imf_text_listener_adapter_impl.h"
21
22 #include "base/bridge/ark_web_bridge_macros.h"
23
24 namespace OHOS::ArkWeb {
25
ArkIMFAdapterWrapper(ArkWebRefPtr<ArkIMFAdapter> ref)26 ArkIMFAdapterWrapper::ArkIMFAdapterWrapper(ArkWebRefPtr<ArkIMFAdapter> ref) : ctocpp_(ref) {}
27
Attach(std::shared_ptr<OHOS::NWeb::IMFTextListenerAdapter> listener,bool isShowKeyboard)28 bool ArkIMFAdapterWrapper::Attach(std::shared_ptr<OHOS::NWeb::IMFTextListenerAdapter> listener, bool isShowKeyboard)
29 {
30 if (CHECK_SHARED_PTR_IS_NULL(listener)) {
31 return ctocpp_->Attach(nullptr, isShowKeyboard);
32 }
33
34 return ctocpp_->Attach(new ArkIMFTextListenerAdapterImpl(listener), isShowKeyboard);
35 }
36
Attach(std::shared_ptr<OHOS::NWeb::IMFTextListenerAdapter> listener,bool isShowKeyboard,const std::shared_ptr<OHOS::NWeb::IMFTextConfigAdapter> config,bool isResetListener)37 bool ArkIMFAdapterWrapper::Attach(std::shared_ptr<OHOS::NWeb::IMFTextListenerAdapter> listener, bool isShowKeyboard,
38 const std::shared_ptr<OHOS::NWeb::IMFTextConfigAdapter> config, bool isResetListener)
39 {
40 if (!CHECK_SHARED_PTR_IS_NULL(listener) && !CHECK_SHARED_PTR_IS_NULL(config)) {
41 return ctocpp_->Attach(new ArkIMFTextListenerAdapterImpl(listener), isShowKeyboard,
42 new ArkIMFTextConfigAdapterImpl(config), isResetListener);
43 } else if (CHECK_SHARED_PTR_IS_NULL(listener) && CHECK_SHARED_PTR_IS_NULL(config)) {
44 return ctocpp_->Attach(
45 nullptr, isShowKeyboard, nullptr, isResetListener);
46 } else if (CHECK_SHARED_PTR_IS_NULL(listener)) {
47 return ctocpp_->Attach(
48 nullptr, isShowKeyboard, new ArkIMFTextConfigAdapterImpl(config), isResetListener);
49 } else {
50 return ctocpp_->Attach(new ArkIMFTextListenerAdapterImpl(listener), isShowKeyboard,
51 nullptr, isResetListener);
52 }
53 }
54
AttachWithRequestKeyboardReason(std::shared_ptr<OHOS::NWeb::IMFTextListenerAdapter> listener,bool isShowKeyboard,const std::shared_ptr<OHOS::NWeb::IMFTextConfigAdapter> config,bool isResetListener,int32_t requestKeyboardReason)55 bool ArkIMFAdapterWrapper::AttachWithRequestKeyboardReason(std::shared_ptr<OHOS::NWeb::IMFTextListenerAdapter> listener,
56 bool isShowKeyboard, const std::shared_ptr<OHOS::NWeb::IMFTextConfigAdapter> config, bool isResetListener,
57 int32_t requestKeyboardReason)
58 {
59 if (!CHECK_SHARED_PTR_IS_NULL(listener) && !CHECK_SHARED_PTR_IS_NULL(config)) {
60 return ctocpp_->AttachWithRequestKeyboardReason(new ArkIMFTextListenerAdapterImpl(listener), isShowKeyboard,
61 new ArkIMFTextConfigAdapterImpl(config), isResetListener, requestKeyboardReason);
62 } else if (CHECK_SHARED_PTR_IS_NULL(listener) && CHECK_SHARED_PTR_IS_NULL(config)) {
63 return ctocpp_->AttachWithRequestKeyboardReason(
64 nullptr, isShowKeyboard, nullptr, isResetListener, requestKeyboardReason);
65 } else if (CHECK_SHARED_PTR_IS_NULL(listener)) {
66 return ctocpp_->AttachWithRequestKeyboardReason(
67 nullptr, isShowKeyboard, new ArkIMFTextConfigAdapterImpl(config), isResetListener, requestKeyboardReason);
68 } else {
69 return ctocpp_->AttachWithRequestKeyboardReason(new ArkIMFTextListenerAdapterImpl(listener), isShowKeyboard,
70 nullptr, isResetListener, requestKeyboardReason);
71 }
72 }
73
ShowCurrentInput(const OHOS::NWeb::IMFAdapterTextInputType & inputType)74 void ArkIMFAdapterWrapper::ShowCurrentInput(const OHOS::NWeb::IMFAdapterTextInputType& inputType)
75 {
76 return ctocpp_->ShowCurrentInput((int32_t)inputType);
77 }
78
HideTextInput()79 void ArkIMFAdapterWrapper::HideTextInput()
80 {
81 return ctocpp_->HideTextInput();
82 }
83
Close()84 void ArkIMFAdapterWrapper::Close()
85 {
86 return ctocpp_->Close();
87 }
88
OnCursorUpdate(const std::shared_ptr<OHOS::NWeb::IMFCursorInfoAdapter> cursorInfo)89 void ArkIMFAdapterWrapper::OnCursorUpdate(const std::shared_ptr<OHOS::NWeb::IMFCursorInfoAdapter> cursorInfo)
90 {
91 if (CHECK_SHARED_PTR_IS_NULL(cursorInfo)) {
92 return ctocpp_->OnCursorUpdate(nullptr);
93 }
94 return ctocpp_->OnCursorUpdate(new ArkIMFCursorInfoAdapterImpl(cursorInfo));
95 }
96
OnSelectionChange(std::u16string text,int start,int end)97 void ArkIMFAdapterWrapper::OnSelectionChange(std::u16string text, int start, int end)
98 {
99 ArkWebU16String str = ArkWebU16StringClassToStruct(text);
100 ctocpp_->OnSelectionChange(str, start, end);
101
102 ArkWebU16StringStructRelease(str);
103 }
104
SendPrivateCommand(const std::string & commandKey,const std::string & commandValue)105 bool ArkIMFAdapterWrapper::SendPrivateCommand(const std::string& commandKey, const std::string& commandValue)
106 {
107 ArkWebString keyStr = ArkWebStringClassToStruct(commandKey);
108 ArkWebString valueStr = ArkWebStringClassToStruct(commandValue);
109 auto result = ctocpp_->SendPrivateCommand(keyStr, valueStr);
110
111 ArkWebStringStructRelease(keyStr);
112 ArkWebStringStructRelease(valueStr);
113
114 return result;
115 }
116
117 } // namespace OHOS::ArkWeb
118