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_imf_adapter_impl.h"
17 
18 #include "ohos_adapter/bridge/ark_imfcursor_info_adapter_wrapper.h"
19 #include "ohos_adapter/bridge/ark_imftext_config_adapter_wrapper.h"
20 #include "ohos_adapter/bridge/ark_imftext_listener_adapter_wrapper.h"
21 
22 #include "base/bridge/ark_web_bridge_macros.h"
23 
24 namespace OHOS::ArkWeb {
25 
ArkIMFAdapterImpl(std::shared_ptr<OHOS::NWeb::IMFAdapter> ref)26 ArkIMFAdapterImpl::ArkIMFAdapterImpl(std::shared_ptr<OHOS::NWeb::IMFAdapter> ref) : real_(ref) {}
27 
Attach(ArkWebRefPtr<ArkIMFTextListenerAdapter> listener,bool isShowKeyboard)28 bool ArkIMFAdapterImpl::Attach(ArkWebRefPtr<ArkIMFTextListenerAdapter> listener, bool isShowKeyboard)
29 {
30     if (CHECK_REF_PTR_IS_NULL(listener)) {
31         return real_->Attach(nullptr, isShowKeyboard);
32     }
33 
34     return real_->Attach(std::make_shared<ArkIMFTextListenerAdapterWrapper>(listener), isShowKeyboard);
35 }
36 
Attach(ArkWebRefPtr<ArkIMFTextListenerAdapter> listener,bool isShowKeyboard,ArkWebRefPtr<ArkIMFTextConfigAdapter> config,bool isResetListener)37 bool ArkIMFAdapterImpl::Attach(ArkWebRefPtr<ArkIMFTextListenerAdapter> listener, bool isShowKeyboard,
38     ArkWebRefPtr<ArkIMFTextConfigAdapter> config, bool isResetListener)
39 {
40     if (CHECK_REF_PTR_IS_NULL(listener) && CHECK_REF_PTR_IS_NULL(config)) {
41         return real_->Attach(nullptr, isShowKeyboard, nullptr, isResetListener);
42     } else if (!CHECK_REF_PTR_IS_NULL(listener) && !CHECK_REF_PTR_IS_NULL(config)) {
43         return real_->Attach(std::make_shared<ArkIMFTextListenerAdapterWrapper>(listener), isShowKeyboard,
44             std::make_shared<ArkIMFTextConfigAdapterWrapper>(config), isResetListener);
45     } else if (CHECK_REF_PTR_IS_NULL(listener)) {
46         return real_->Attach(
47             nullptr, isShowKeyboard, std::make_shared<ArkIMFTextConfigAdapterWrapper>(config), isResetListener);
48     } else {
49         return real_->Attach(
50             std::make_shared<ArkIMFTextListenerAdapterWrapper>(listener), isShowKeyboard, nullptr, isResetListener);
51     }
52 }
53 
ShowCurrentInput(const int32_t & inputType)54 void ArkIMFAdapterImpl::ShowCurrentInput(const int32_t& inputType)
55 {
56     real_->ShowCurrentInput((OHOS::NWeb::IMFAdapterTextInputType)inputType);
57 }
58 
HideTextInput()59 void ArkIMFAdapterImpl::HideTextInput()
60 {
61     real_->HideTextInput();
62 }
63 
Close()64 void ArkIMFAdapterImpl::Close()
65 {
66     real_->Close();
67 }
68 
OnCursorUpdate(ArkWebRefPtr<ArkIMFCursorInfoAdapter> cursorInfo)69 void ArkIMFAdapterImpl::OnCursorUpdate(ArkWebRefPtr<ArkIMFCursorInfoAdapter> cursorInfo)
70 {
71     if (CHECK_REF_PTR_IS_NULL(cursorInfo)) {
72         return real_->OnCursorUpdate(nullptr);
73     }
74     real_->OnCursorUpdate(std::make_shared<ArkIMFCursorInfoAdapterWrapper>(cursorInfo));
75 }
76 
OnSelectionChange(ArkWebU16String & text,int start,int end)77 void ArkIMFAdapterImpl::OnSelectionChange(ArkWebU16String& text, int start, int end)
78 {
79     real_->OnSelectionChange(ArkWebU16StringStructToClass(text), start, end);
80 }
81 
SendPrivateCommand(const ArkWebString & commandKey,const ArkWebString & commandValue)82 bool ArkIMFAdapterImpl::SendPrivateCommand(const ArkWebString& commandKey, const ArkWebString& commandValue)
83 {
84     std::string key = ArkWebStringStructToClass(commandKey);
85     std::string value = ArkWebStringStructToClass(commandValue);
86 
87     return real_->SendPrivateCommand(key, value);
88 }
89 
90 } // namespace OHOS::ArkWeb
91