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(nullptr, isShowKeyboard, nullptr, isResetListener);
45     } else if (CHECK_SHARED_PTR_IS_NULL(listener)) {
46         return ctocpp_->Attach(nullptr, isShowKeyboard, new ArkIMFTextConfigAdapterImpl(config), isResetListener);
47     } else {
48         return ctocpp_->Attach(new ArkIMFTextListenerAdapterImpl(listener), isShowKeyboard, nullptr, isResetListener);
49     }
50 }
51 
ShowCurrentInput(const OHOS::NWeb::IMFAdapterTextInputType & inputType)52 void ArkIMFAdapterWrapper::ShowCurrentInput(const OHOS::NWeb::IMFAdapterTextInputType& inputType)
53 {
54     return ctocpp_->ShowCurrentInput((int32_t)inputType);
55 }
56 
HideTextInput()57 void ArkIMFAdapterWrapper::HideTextInput()
58 {
59     return ctocpp_->HideTextInput();
60 }
61 
Close()62 void ArkIMFAdapterWrapper::Close()
63 {
64     return ctocpp_->Close();
65 }
66 
OnCursorUpdate(const std::shared_ptr<OHOS::NWeb::IMFCursorInfoAdapter> cursorInfo)67 void ArkIMFAdapterWrapper::OnCursorUpdate(const std::shared_ptr<OHOS::NWeb::IMFCursorInfoAdapter> cursorInfo)
68 {
69     if (CHECK_SHARED_PTR_IS_NULL(cursorInfo)) {
70         return ctocpp_->OnCursorUpdate(nullptr);
71     }
72     return ctocpp_->OnCursorUpdate(new ArkIMFCursorInfoAdapterImpl(cursorInfo));
73 }
74 
OnSelectionChange(std::u16string text,int start,int end)75 void ArkIMFAdapterWrapper::OnSelectionChange(std::u16string text, int start, int end)
76 {
77     ArkWebU16String str = ArkWebU16StringClassToStruct(text);
78     ctocpp_->OnSelectionChange(str, start, end);
79 
80     ArkWebU16StringStructRelease(str);
81 }
82 
SendPrivateCommand(const std::string & commandKey,const std::string & commandValue)83 bool ArkIMFAdapterWrapper::SendPrivateCommand(const std::string& commandKey, const std::string& commandValue)
84 {
85     ArkWebString keyStr = ArkWebStringClassToStruct(commandKey);
86     ArkWebString valueStr = ArkWebStringClassToStruct(commandValue);
87     auto result = ctocpp_->SendPrivateCommand(keyStr, valueStr);
88 
89     ArkWebStringStructRelease(keyStr);
90     ArkWebStringStructRelease(valueStr);
91 
92     return result;
93 }
94 
95 } // namespace OHOS::ArkWeb
96