• 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_JSPANEL_H
17 #define INPUTMETHOD_IMF_JSPANEL_H
18 
19 #include <mutex>
20 #include <uv.h>
21 
22 #include "async_call.h"
23 #include "input_method_panel.h"
24 #include "js_callback_object.h"
25 #include "napi/native_api.h"
26 #include "napi/native_common.h"
27 #include "napi/native_node_api.h"
28 #include "native_engine/native_engine.h"
29 #include "native_engine/native_value.h"
30 
31 namespace OHOS {
32 namespace MiscServices {
33 class JsPanel {
34 public:
35     JsPanel() = default;
36     ~JsPanel();
37     static napi_value Init(napi_env env);
38     static napi_value SetUiContent(napi_env env, napi_callback_info info);
39     static napi_value Resize(napi_env env, napi_callback_info info);
40     static napi_value MoveTo(napi_env env, napi_callback_info info);
41     static napi_value Show(napi_env env, napi_callback_info info);
42     static napi_value Hide(napi_env env, napi_callback_info info);
43     static napi_value ChangeFlag(napi_env env, napi_callback_info info);
44     static napi_value SetPrivacyMode(napi_env env, napi_callback_info info);
45     static napi_value Subscribe(napi_env env, napi_callback_info info);
46     static napi_value UnSubscribe(napi_env env, napi_callback_info info);
47     void SetNative(const std::shared_ptr<InputMethodPanel> &panel);
48     std::shared_ptr<InputMethodPanel> GetNative();
49 
50 private:
51     struct PanelContentContext : public AsyncCall::Context {
52         std::string path = "";
53         uint32_t width = 0;
54         uint32_t height = 0;
55         int32_t x = 0;
56         int32_t y = 0;
57         std::shared_ptr<InputMethodPanel> inputMethodPanel = nullptr;
58         std::shared_ptr<NativeReference> contentStorage = nullptr;
PanelContentContextPanelContentContext59         PanelContentContext(napi_env env, napi_callback_info info) : Context(nullptr, nullptr)
60         {
61             napi_value self = nullptr;
62             napi_status status = napi_get_cb_info(env, info, 0, nullptr, &self, nullptr);
63             CHECK_RETURN_VOID((status == napi_ok) && (self != nullptr), "get callback info failed.");
64             void *native = nullptr;
65             status = napi_unwrap(env, self, &native);
66             CHECK_RETURN_VOID((status == napi_ok) && (native != nullptr), "get jsPanel failed.");
67             inputMethodPanel = reinterpret_cast<JsPanel *>(native)->GetNative();
68         };
PanelContentContextPanelContentContext69         PanelContentContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){};
operatorPanelContentContext70         napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
71         {
72             CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg);
73             return Context::operator()(env, argc, argv, self);
74         }
operatorPanelContentContext75         napi_status operator()(napi_env env, napi_value *result) override
76         {
77             if (status_ != napi_ok) {
78                 output_ = nullptr;
79                 return status_;
80             }
81             return Context::operator()(env, result);
82         }
83     };
84     static napi_value JsNew(napi_env env, napi_callback_info info);
85     static std::shared_ptr<InputMethodPanel> UnwrapPanel(napi_env env, napi_value thisVar);
86     static const std::string CLASS_NAME;
87     static constexpr size_t ARGC_MAX = 6;
88     std::shared_ptr<InputMethodPanel> inputMethodPanel_ = nullptr;
89 
90     static std::mutex panelConstructorMutex_;
91     static thread_local napi_ref panelConstructorRef_;
92 };
93 } // namespace MiscServices
94 } // namespace OHOS
95 
96 #endif //INPUTMETHOD_IMF_JSPANEL_H
97