• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 INPUTMETHOD_IMF_PANEL_LISTENER_IMPL_H
17 #define INPUTMETHOD_IMF_PANEL_LISTENER_IMPL_H
18 
19 #include <mutex>
20 #include <thread>
21 #include <tuple>
22 #include <uv.h>
23 
24 #include "concurrent_map.h"
25 #include "event_handler.h"
26 #include "input_method_panel.h"
27 #include "js_callback_object.h"
28 #include "napi/native_api.h"
29 #include "napi/native_node_api.h"
30 #include "panel_status_listener.h"
31 
32 namespace OHOS {
33 namespace MiscServices {
34 struct JsWindowSize {
35     static napi_value Write(napi_env env, const WindowSize &nativeObject);
36     static bool Read(napi_env env, napi_value jsObject, WindowSize &nativeObject);
37 };
38 struct JsKeyboardArea {
39     static napi_value Write(napi_env env, const PanelAdjustInfo &nativeObject);
40     static bool Read(napi_env env, napi_value jsObject, PanelAdjustInfo &nativeObject);
41 };
42 class PanelListenerImpl : public PanelStatusListener {
43 public:
44     struct UvEntry {
45         WindowSize size;
46         PanelAdjustInfo keyboardArea;
47         std::shared_ptr<JSCallbackObject> cbCopy;
UvEntryUvEntry48         explicit UvEntry(const std::shared_ptr<JSCallbackObject> &cb) : cbCopy(cb)
49         {
50         }
51     };
52     using EntrySetter = std::function<void(UvEntry &)>;
53     static std::shared_ptr<PanelListenerImpl> GetInstance();
54     ~PanelListenerImpl();
55     void OnPanelStatus(uint32_t windowId, bool isShow) override;
56     void OnSizeChange(uint32_t windowId, const WindowSize &size) override;
57     void OnSizeChange(uint32_t windowId, const WindowSize &size, const PanelAdjustInfo &keyboardArea,
58         const std::string &event) override;
59     void Subscribe(uint32_t windowId, const std::string &type, std::shared_ptr<JSCallbackObject> cbObject);
60     void RemoveInfo(const std::string &type, uint32_t windowId);
61     void SetEventHandler(std::shared_ptr<AppExecFwk::EventHandler> handler);
62     std::shared_ptr<JSCallbackObject> GetCallback(uint32_t windowId, const std::string &type);
63     std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler();
64 
65     ConcurrentMap<uint32_t, std::map<std::string, std::shared_ptr<JSCallbackObject>>> callbacks_;
66     static std::mutex listenerMutex_;
67     static std::shared_ptr<PanelListenerImpl> instance_;
68     mutable std::shared_mutex eventHandlerMutex_;
69     std::shared_ptr<AppExecFwk::EventHandler> handler_;
70 };
71 } // namespace MiscServices
72 } // namespace OHOS
73 
74 #endif //INPUTMETHOD_IMF_PANEL_LISTENER_IMPL_H
75