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 #ifndef IMF_ADAPTER_IMPL_H
17 #define IMF_ADAPTER_IMPL_H
18 
19 #include "imf_adapter.h"
20 #include "input_method_controller.h"
21 
22 namespace OHOS::NWeb {
23 
24 class IMFAdapterFunctionKeyAdapterImpl : public IMFAdapterFunctionKeyAdapter {
25 public:
26     IMFAdapterFunctionKeyAdapterImpl() = default;
27 
28     ~IMFAdapterFunctionKeyAdapterImpl() = default;
29 
GetEnterKeyType()30     IMFAdapterEnterKeyType GetEnterKeyType() override
31     {
32         return enterKeyType;
33     }
34 
SetEnterKeyType(IMFAdapterEnterKeyType keyType)35     void SetEnterKeyType(IMFAdapterEnterKeyType keyType)
36     {
37         enterKeyType = keyType;
38     }
39 
40 private:
41     IMFAdapterEnterKeyType enterKeyType = IMFAdapterEnterKeyType::UNSPECIFIED;
42 };
43 
44 class IMFTextListenerAdapterImpl : public MiscServices::OnTextChangedListener {
45 public:
46     explicit IMFTextListenerAdapterImpl(const std::shared_ptr<IMFTextListenerAdapter>& listener);
47 
48     ~IMFTextListenerAdapterImpl();
49 
50     void InsertText(const std::u16string& text) override;
51 
52     void DeleteForward(int32_t length) override;
53 
54     void DeleteBackward(int32_t length) override;
55 
56     void SendKeyEventFromInputMethod(const MiscServices::KeyEvent& event) override;
57 
58     void SendKeyboardStatus(const MiscServices::KeyboardStatus& keyboardStatus) override;
59 
60     void SendFunctionKey(const MiscServices::FunctionKey& functionKey) override;
61 
62     void SetKeyboardStatus(bool status) override;
63 
64     void MoveCursor(const MiscServices::Direction direction) override;
65 
66     void HandleSetSelection(int32_t start, int32_t end) override;
67 
68     void HandleExtendAction(int32_t action) override;
69 
70     void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override;
71 
72     int32_t GetTextIndexAtCursor() override;
73 
74     std::u16string GetLeftTextOfCursor(int32_t number) override;
75 
76     std::u16string GetRightTextOfCursor(int32_t number) override;
77 
78     int32_t SetPreviewText(const std::u16string& text, const MiscServices::Range& range) override;
79 
80     void FinishTextPreview() override;
81 
82     int32_t ReceivePrivateCommand(
83         const std::unordered_map<std::string, MiscServices::PrivateDataValue>& privateCommand) override;
84 
85     void NotifyPanelStatusInfo(const MiscServices::PanelStatusInfo& info) override;
86 
87 private:
88     std::shared_ptr<IMFTextListenerAdapter> listener_ = nullptr;
89     const std::string PREVIEW_TEXT_STYLE_KEY = "previewTextStyle";
90     const std::string PREVIEW_TEXT_STYLE_UNDERLINE = "underline";
91     const std::string AUTO_FILL_PARAMS_USERNAME = "com.autofill.params.userName";
92     const std::string AUTO_FILL_PARAMS_OTHERACCOUNT = "com.autofill.params.otherAccount";
93 };
94 
95 class IMFAdapterImpl : public IMFAdapter {
96 public:
97     IMFAdapterImpl() = default;
98 
99     ~IMFAdapterImpl() override = default;
100 
101     bool Attach(std::shared_ptr<IMFTextListenerAdapter> listener, bool isShowKeyboard) override;
102 
103     bool Attach(std::shared_ptr<IMFTextListenerAdapter> listener, bool isShowKeyboard,
104         const std::shared_ptr<IMFTextConfigAdapter> config, bool isResetListener) override;
105 
106     void ShowCurrentInput(const IMFAdapterTextInputType& inputType) override;
107 
108     void HideTextInput() override;
109 
110     void Close() override;
111 
112     void OnCursorUpdate(const std::shared_ptr<IMFCursorInfoAdapter> cursorInfo) override;
113 
114     void OnSelectionChange(std::u16string text, int start, int end) override;
115 
116     bool SendPrivateCommand(const std::string& commandKey, const std::string& commandValue) override;
117 
118 private:
119     sptr<MiscServices::OnTextChangedListener> textListener_ = nullptr;
120 
121     bool ParseFillContentJsonValue(const std::string& jsonStr,
122         std::unordered_map<std::string, std::variant<std::string, bool, int32_t>>& map);
123 };
124 } // namespace OHOS::NWeb
125 
126 #endif // IMF_ADAPTER_IMPL_H
127