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 class PanelListenerImpl : public PanelStatusListener {
39 public:
40     struct UvEntry {
41         WindowSize size;
42         std::shared_ptr<JSCallbackObject> cbCopy;
UvEntryUvEntry43         explicit UvEntry(const std::shared_ptr<JSCallbackObject> &cb) : cbCopy(cb)
44         {
45         }
46     };
47     using EntrySetter = std::function<void(UvEntry &)>;
48     static std::shared_ptr<PanelListenerImpl> GetInstance();
49     ~PanelListenerImpl();
50     void OnPanelStatus(uint32_t windowId, bool isShow) override;
51     void OnSizeChange(uint32_t windowId, const WindowSize &size) override;
52     void Subscribe(uint32_t windowId, const std::string &type, std::shared_ptr<JSCallbackObject> cbObject);
53     void RemoveInfo(const std::string &type, uint32_t windowId);
54     void SetEventHandler(std::shared_ptr<AppExecFwk::EventHandler> handler);
55     std::shared_ptr<JSCallbackObject> GetCallback(uint32_t windowId, const std::string &type);
56     std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler();
57 
58     ConcurrentMap<uint32_t, std::map<std::string, std::shared_ptr<JSCallbackObject>>> callbacks_;
59     static std::mutex listenerMutex_;
60     static std::shared_ptr<PanelListenerImpl> instance_;
61     mutable std::shared_mutex eventHandlerMutex_;
62     std::shared_ptr<AppExecFwk::EventHandler> handler_;
63 };
64 } // namespace MiscServices
65 } // namespace OHOS
66 
67 #endif //INPUTMETHOD_IMF_PANEL_LISTENER_IMPL_H
68