• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H
17 #define INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H
18 
19 #include <uv.h>
20 
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 
25 #include "async_call.h"
26 #include "block_data.h"
27 #include "global.h"
28 #include "input_attribute.h"
29 #include "js_callback_object.h"
30 #include "keyboard_listener.h"
31 #include "napi/native_api.h"
32 
33 namespace OHOS {
34 namespace MiscServices {
35 class JsKeyboardDelegateSetting : public KeyboardListener {
36 public:
37     JsKeyboardDelegateSetting() = default;
38     ~JsKeyboardDelegateSetting() override = default;
39     static napi_value Init(napi_env env, napi_value exports);
40     static napi_value CreateKeyboardDelegate(napi_env env, napi_callback_info info);
41     static napi_value GetKeyboardDelegate(napi_env env, napi_callback_info info);
42     static napi_value Subscribe(napi_env env, napi_callback_info info);
43     static napi_value UnSubscribe(napi_env env, napi_callback_info info);
44     bool OnKeyEvent(int32_t keyCode, int32_t keyStatus) override;
45     bool OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent) override;
46     void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override;
47     void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override;
48     void OnTextChange(const std::string &text) override;
49     void OnEditorAttributeChange(const InputAttribute &inputAttribute) override;
50 
51 private:
52     static napi_value GetResultOnKeyEvent(napi_env env, int32_t keyCode, int32_t keyStatus);
53     static napi_value GetJsConstProperty(napi_env env, uint32_t num);
54     static napi_value GetKDInstance(napi_env env, napi_callback_info info);
55     static std::shared_ptr<JsKeyboardDelegateSetting> GetKeyboardDelegateSetting();
56     static bool InitKeyboardDelegate();
57     static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo);
58     void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj);
59     void UnRegisterListener(napi_value callback, std::string type);
60     static constexpr int32_t MAX_TIMEOUT = 2000;
61     static const std::string KDS_CLASS_NAME;
62     static thread_local napi_ref KDSRef_;
63     struct CursorPara {
64         int32_t positionX = 0;
65         int32_t positionY = 0;
66         int height = 0;
67     };
68     struct SelectionPara {
69         int32_t oldBegin = 0;
70         int32_t oldEnd = 0;
71         int32_t newBegin = 0;
72         int32_t newEnd = 0;
73     };
74     struct KeyEventPara {
75         int32_t keyCode = 0;
76         int32_t keyStatus = 0;
77         bool isOnKeyEvent = false;
78     };
79     struct UvEntry {
80         std::vector<std::shared_ptr<JSCallbackObject>> vecCopy;
81         std::string type;
82         CursorPara curPara;
83         SelectionPara selPara;
84         KeyEventPara keyEventPara;
85         std::shared_ptr<MMI::KeyEvent> pullKeyEventPara;
86         std::shared_ptr<BlockData<bool>> isDone;
87         std::string text;
88         InputAttribute inputAttribute;
UvEntryUvEntry89         UvEntry(const std::vector<std::shared_ptr<JSCallbackObject>> &cbVec, const std::string &type)
90             : vecCopy(cbVec), type(type)
91         {
92         }
93     };
94     using EntrySetter = std::function<void(UvEntry &)>;
95     uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr);
96     uv_loop_s *loop_ = nullptr;
97     std::recursive_mutex mutex_;
98     std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_;
99     static std::mutex keyboardMutex_;
100     static std::shared_ptr<JsKeyboardDelegateSetting> keyboardDelegate_;
101 };
102 } // namespace MiscServices
103 } // namespace OHOS
104 #endif // INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H
105