• 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 #include "ui_observer_listener.h"
17 #include "js_native_api.h"
18 #include "core/event/ace_events.h"
19 
20 namespace OHOS::Ace::Napi {
21 namespace {
22 constexpr char NAV_BAR[] = "navBar";
23 constexpr char GET_TAG[] = "getTag";
24 constexpr char GET_TYPE[] = "getType";
25 constexpr char IS_BUILT_IN[] = "isBuiltIn";
26 constexpr char SET_ENABLED[] = "setEnabled";
27 constexpr char IS_ENABLED[] = "isEnabled";
28 constexpr char GET_STATE[] = "getState";
29 constexpr char GET_EVENT_TARGET_INFO[] = "getEventTargetInfo";
30 constexpr char IS_VALID[] = "isValid";
31 constexpr char GET_ID[] = "getId";
32 constexpr char GET_MODIFIER_KEY_STATE[] = "getModifierKeyState";
33 constexpr int32_t PARAM_SIZE_ZERO = 0;
34 constexpr int32_t PARAM_SIZE_ONE = 1;
35 constexpr int32_t PARAM_SIZE_TWO = 2;
36 constexpr int32_t PARAM_SIZE_THREE = 3;
37 constexpr int32_t PARAM_SIZE_SIX = 6;
38 
GetCurrentGestureRecognizer(napi_env env,napi_callback_info info,size_t * argc=nullptr,napi_value * argv=nullptr)39 static NG::PanRecognizer* GetCurrentGestureRecognizer(
40     napi_env env, napi_callback_info info, size_t* argc = nullptr, napi_value* argv = nullptr)
41 {
42     napi_value thisVar = nullptr;
43     NAPI_CALL(env, napi_get_cb_info(env, info, argc, argv, &thisVar, nullptr));
44 
45     NG::PanRecognizer* current = nullptr;
46     napi_unwrap(env, thisVar, (void**)&current);
47     CHECK_NULL_RETURN(current, nullptr);
48     return current;
49 }
50 
GetTag(napi_env env,napi_callback_info info)51 static napi_value GetTag(napi_env env, napi_callback_info info)
52 {
53     napi_handle_scope scope = nullptr;
54     napi_open_handle_scope(env, &scope);
55     CHECK_NULL_RETURN(scope, nullptr);
56     NG::PanRecognizer* current = GetCurrentGestureRecognizer(env, info);
57     CHECK_NULL_RETURN(current, nullptr);
58     napi_value result = nullptr;
59     auto gestureInfo = current->GetGestureInfo();
60     CHECK_NULL_RETURN(gestureInfo, nullptr);
61     if (gestureInfo->GetTag().has_value()) {
62         std::string tag = gestureInfo->GetTag().value();
63         napi_create_string_utf8(env, tag.c_str(), NAPI_AUTO_LENGTH, &result);
64     } else {
65         napi_get_undefined(env, &result);
66     }
67     napi_close_handle_scope(env, scope);
68     return result;
69 }
70 
GetType(napi_env env,napi_callback_info info)71 static napi_value GetType(napi_env env, napi_callback_info info)
72 {
73     napi_handle_scope scope = nullptr;
74     napi_open_handle_scope(env, &scope);
75     CHECK_NULL_RETURN(scope, nullptr);
76     NG::PanRecognizer* current = GetCurrentGestureRecognizer(env, info);
77     CHECK_NULL_RETURN(current, nullptr);
78     napi_value result = nullptr;
79     auto gestureInfo = current->GetGestureInfo();
80     CHECK_NULL_RETURN(gestureInfo, nullptr);
81     napi_create_int32(env, static_cast<int32_t>(gestureInfo->GetRecognizerType()), &result);
82     napi_close_handle_scope(env, scope);
83     return result;
84 }
85 
IsBuiltIn(napi_env env,napi_callback_info info)86 static napi_value IsBuiltIn(napi_env env, napi_callback_info info)
87 {
88     napi_handle_scope scope = nullptr;
89     napi_open_handle_scope(env, &scope);
90     CHECK_NULL_RETURN(scope, nullptr);
91     NG::PanRecognizer* current = GetCurrentGestureRecognizer(env, info);
92     CHECK_NULL_RETURN(current, nullptr);
93     napi_value result = nullptr;
94     auto gestureInfo = current->GetGestureInfo();
95     CHECK_NULL_RETURN(gestureInfo, nullptr);
96     napi_get_boolean(env, gestureInfo->IsSystemGesture(), &result);
97     napi_close_handle_scope(env, scope);
98     return result;
99 }
100 
SetEnabled(napi_env env,napi_callback_info info)101 static napi_value SetEnabled(napi_env env, napi_callback_info info)
102 {
103     napi_handle_scope scope = nullptr;
104     napi_open_handle_scope(env, &scope);
105     CHECK_NULL_RETURN(scope, nullptr);
106     size_t argc = PARAM_SIZE_ONE;
107     napi_value argv[PARAM_SIZE_ONE] = { nullptr };
108     NG::PanRecognizer* current = GetCurrentGestureRecognizer(env, info, &argc, argv);
109     CHECK_NULL_RETURN(current, nullptr);
110     napi_valuetype valueType = napi_undefined;
111     NAPI_CALL(env, napi_typeof(env, argv[PARAM_SIZE_ZERO], &valueType));
112     NAPI_ASSERT(env, (valueType == napi_boolean), "Invalid arguments");
113     bool isEnabled = false;
114     napi_get_value_bool(env, argv[PARAM_SIZE_ZERO], &isEnabled);
115     current->SetEnabled(isEnabled);
116     napi_close_handle_scope(env, scope);
117     return nullptr;
118 }
119 
IsEnabled(napi_env env,napi_callback_info info)120 static napi_value IsEnabled(napi_env env, napi_callback_info info)
121 {
122     napi_handle_scope scope = nullptr;
123     napi_open_handle_scope(env, &scope);
124     CHECK_NULL_RETURN(scope, nullptr);
125     NG::PanRecognizer* current = GetCurrentGestureRecognizer(env, info);
126     CHECK_NULL_RETURN(current, nullptr);
127     napi_value result = nullptr;
128     napi_get_boolean(env, current->IsEnabled(), &result);
129     napi_close_handle_scope(env, scope);
130     return result;
131 }
132 
GetState(napi_env env,napi_callback_info info)133 static napi_value GetState(napi_env env, napi_callback_info info)
134 {
135     napi_handle_scope scope = nullptr;
136     napi_open_handle_scope(env, &scope);
137     CHECK_NULL_RETURN(scope, nullptr);
138     NG::PanRecognizer* current = GetCurrentGestureRecognizer(env, info);
139     CHECK_NULL_RETURN(current, nullptr);
140     napi_value result = nullptr;
141     napi_create_int32(env, static_cast<int32_t>(current->GetRefereeState()), &result);
142     napi_close_handle_scope(env, scope);
143     return result;
144 }
145 
GetEventTargetInfo(napi_env env,napi_callback_info info)146 static napi_value GetEventTargetInfo(napi_env env, napi_callback_info info)
147 {
148     napi_handle_scope scope = nullptr;
149     napi_open_handle_scope(env, &scope);
150     CHECK_NULL_RETURN(scope, nullptr);
151     NG::PanRecognizer* current = GetCurrentGestureRecognizer(env, info);
152     CHECK_NULL_RETURN(current, nullptr);
153     napi_value result = nullptr;
154     napi_create_object(env, &result);
155     napi_value funcValue = nullptr;
156     auto attactNode = current->GetAttachedNode().Upgrade();
157     CHECK_NULL_RETURN(attactNode, nullptr);
158     auto inspectorId = std::make_shared<std::string>(attactNode->GetInspectorIdValue(""));
159     auto inspectorIdHolder = std::make_unique<std::shared_ptr<std::string>>(inspectorId);
160     auto* rawPtr = inspectorIdHolder.release();
161     auto status = napi_wrap(
162         env, result, rawPtr,
163         [](napi_env env, void* data, void* hint) {
164             auto origin = static_cast<std::string*>(data);
165             if (origin) {
166                 delete origin;
167             }
168         },
169         nullptr, nullptr);
170     if (status != napi_ok) {
171         LOGE("napi_wrap failed");
172         delete rawPtr;
173         return nullptr;
174     }
175     auto getId = [](napi_env env, napi_callback_info info) -> napi_value {
176         napi_value result = nullptr;
177         void* data = nullptr;
178         napi_get_cb_info(env, info, nullptr, nullptr, nullptr, &data);
179         auto inspectorId = static_cast<std::shared_ptr<std::string>*>(data);
180         CHECK_NULL_RETURN(inspectorId, nullptr);
181         napi_create_string_utf8(env, (*inspectorId)->c_str(), NAPI_AUTO_LENGTH, &result);
182         return result;
183     };
184     napi_create_function(env, GET_ID, 0, getId, rawPtr, &funcValue);
185     napi_set_named_property(env, result, GET_ID, funcValue);
186     napi_close_handle_scope(env, scope);
187     return result;
188 }
189 
IsValid(napi_env env,napi_callback_info info)190 static napi_value IsValid(napi_env env, napi_callback_info info)
191 {
192     napi_handle_scope scope = nullptr;
193     napi_open_handle_scope(env, &scope);
194     CHECK_NULL_RETURN(scope, nullptr);
195     NG::PanRecognizer* current = GetCurrentGestureRecognizer(env, info);
196     CHECK_NULL_RETURN(current, nullptr);
197     napi_value result = nullptr;
198     napi_get_boolean(env, current->IsInResponseLinkRecognizers(), &result);
199     napi_close_handle_scope(env, scope);
200     return result;
201 }
202 
CheckKeysPressed(const std::vector<KeyCode> & pressedKeyCodes,std::vector<std::string> & checkKeyCodes)203 bool CheckKeysPressed(const std::vector<KeyCode>& pressedKeyCodes, std::vector<std::string>& checkKeyCodes)
204 {
205     auto hasKeyCode = [pressedKeyCodes](const KeyCode& keyCode) -> bool {
206         auto it = std::find(pressedKeyCodes.begin(), pressedKeyCodes.end(), keyCode);
207         return it != pressedKeyCodes.end();
208     };
209     for (auto& checkKeyCode : checkKeyCodes) {
210         if (checkKeyCode == "ctrl") {
211             if (!hasKeyCode(KeyCode::KEY_CTRL_LEFT) && !hasKeyCode(KeyCode::KEY_CTRL_RIGHT)) {
212                 return false;
213             }
214         } else if (checkKeyCode == "shift") {
215             if (!hasKeyCode(KeyCode::KEY_SHIFT_LEFT) && !hasKeyCode(KeyCode::KEY_SHIFT_RIGHT)) {
216                 return false;
217             }
218         } else if (checkKeyCode == "alt") {
219             if (!hasKeyCode(KeyCode::KEY_ALT_LEFT) && !hasKeyCode(KeyCode::KEY_ALT_RIGHT)) {
220                 return false;
221             }
222         } else if (checkKeyCode == "fn") {
223             if (!hasKeyCode(KeyCode::KEY_FN)) {
224                 return false;
225             }
226         } else {
227             return false;
228         }
229     }
230     return true;
231 }
232 
GetParamLen(napi_env env,napi_value param)233 size_t GetParamLen(napi_env env, napi_value param)
234 {
235     size_t buffSize = 0;
236     napi_status status = napi_get_value_string_utf8(env, param, nullptr, 0, &buffSize);
237     if (status != napi_ok || buffSize == 0) {
238         return 0;
239     }
240     return buffSize;
241 }
242 
CheckAndParseStr(napi_env env,napi_value arg,std::string & recv)243 bool CheckAndParseStr(napi_env env, napi_value arg, std::string& recv)
244 {
245     if (arg == nullptr) {
246         return false;
247     }
248 
249     napi_valuetype valueType = napi_undefined;
250     napi_typeof(env, arg, &valueType);
251     if (valueType != napi_string) {
252         return false;
253     }
254 
255     size_t msgLen = GetParamLen(env, arg) + 1;
256     std::unique_ptr<char[]> message = std::make_unique<char[]>(msgLen);
257     size_t ret = 0;
258     if (napi_get_value_string_utf8(env, arg, message.get(), msgLen, &ret) != napi_ok) {
259         return false;
260     }
261 
262     recv = message.get();
263     return true;
264 }
265 
GetBaseEventInfo(napi_env env,napi_callback_info info,size_t * argc=nullptr,napi_value * argv=nullptr)266 static GestureEvent* GetBaseEventInfo(
267     napi_env env, napi_callback_info info, size_t* argc = nullptr, napi_value* argv = nullptr)
268 {
269     napi_value thisVar = nullptr;
270     NAPI_CALL(env, napi_get_cb_info(env, info, argc, argv, &thisVar, nullptr));
271 
272     GestureEvent* baseEventInfo = nullptr;
273     napi_status status = napi_unwrap(env, thisVar, (void**)&baseEventInfo);
274     if (status != napi_ok) {
275         LOGE("Failed to unwrap native object");
276         return nullptr;
277     }
278     CHECK_NULL_RETURN(baseEventInfo, nullptr);
279     return baseEventInfo;
280 }
281 
GetModifierKeyState(napi_env env,napi_callback_info info)282 static napi_value GetModifierKeyState(napi_env env, napi_callback_info info)
283 {
284     napi_handle_scope scope = nullptr;
285     napi_open_handle_scope(env, &scope);
286     CHECK_NULL_RETURN(scope, nullptr);
287     size_t argc = PARAM_SIZE_ONE;
288     napi_value argv = nullptr;
289     GestureEvent* gestureEventInfo = GetBaseEventInfo(env, info, &argc, &argv);
290     CHECK_NULL_RETURN(gestureEventInfo, nullptr);
291 
292     bool ret = false;
293     NAPI_CALL(env, napi_is_array(env, argv, &ret));
294     auto pressedKeyCodes = gestureEventInfo->GetPressedKeyCodes();
295     bool checkRet = false;
296     uint32_t length = 0;
297     std::string checkedKeyCode;
298     std::vector<std::string> checkedKeyCodes;
299     std::vector<std::string> validKeyCodes = { "ctrl", "shift", "alt", "fn" };
300     napi_get_array_length(env, argv, &length);
301 
302     for (uint32_t i = 0; i < length; ++i) {
303         napi_value element = nullptr;
304         napi_get_element(env, argv, i, &element);
305         std::string errMsg;
306         if (CheckAndParseStr(env, element, checkedKeyCode)) {
307             auto it = std::find(validKeyCodes.begin(), validKeyCodes.end(), checkedKeyCode);
308             if (it != validKeyCodes.end()) {
309                 checkedKeyCodes.emplace_back(checkedKeyCode);
310             }
311         }
312     }
313 
314     checkRet = CheckKeysPressed(pressedKeyCodes, checkedKeyCodes);
315     napi_value result = nullptr;
316     napi_get_boolean(env, checkRet, &result);
317     napi_close_handle_scope(env, scope);
318     return result;
319 }
320 }
321 
OnNavigationStateChange(const NG::NavDestinationInfo & info)322 void UIObserverListener::OnNavigationStateChange(const NG::NavDestinationInfo& info)
323 {
324     if (!env_ || !callback_) {
325         TAG_LOGW(AceLogTag::ACE_OBSERVER,
326             "Handle navDestination state change failed, runtime or callback function invalid!");
327         return;
328     }
329     napi_handle_scope scope = nullptr;
330     auto status = napi_open_handle_scope(env_, &scope);
331     if (status != napi_ok) {
332         return;
333     }
334     napi_value callback = nullptr;
335     napi_get_reference_value(env_, callback_, &callback);
336     napi_value argv[] = { CreateNavDestinationInfoObj(info) };
337     napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
338     napi_close_handle_scope(env_, scope);
339 }
340 
OnScrollEventStateChange(const std::string & id,int32_t uniqueId,NG::ScrollEventType eventType,float offset)341 void UIObserverListener::OnScrollEventStateChange(
342     const std::string& id, int32_t uniqueId, NG::ScrollEventType eventType, float offset)
343 {
344     if (!env_ || !callback_) {
345         TAG_LOGW(AceLogTag::ACE_OBSERVER,
346             "Handle scrollEvent state change failed, runtime or callback function invalid!");
347         return;
348     }
349     napi_handle_scope scope = nullptr;
350     auto status = napi_open_handle_scope(env_, &scope);
351     if (status != napi_ok) {
352         return;
353     }
354     napi_value callback = nullptr;
355     napi_get_reference_value(env_, callback_, &callback);
356     napi_value objValue = nullptr;
357     napi_create_object(env_, &objValue);
358     napi_value scrollId = nullptr;
359     napi_value frameNodeId = nullptr;
360     napi_value scrollEventType = nullptr;
361     napi_value scrollOffset = nullptr;
362     napi_create_string_utf8(env_, id.c_str(), id.length(), &scrollId);
363     napi_create_int32(env_, uniqueId, &frameNodeId);
364     napi_create_int32(env_, static_cast<int32_t>(eventType), &scrollEventType);
365     napi_create_double(env_, offset, &scrollOffset);
366     napi_set_named_property(env_, objValue, "id", scrollId);
367     napi_set_named_property(env_, objValue, "uniqueId", frameNodeId);
368     napi_set_named_property(env_, objValue, "scrollEvent", scrollEventType);
369     napi_set_named_property(env_, objValue, "offset", scrollOffset);
370     napi_value argv[] = { objValue };
371     napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
372     napi_close_handle_scope(env_, scope);
373 }
374 
OnRouterPageStateChange(const NG::RouterPageInfoNG & pageInfo)375 void UIObserverListener::OnRouterPageStateChange(const NG::RouterPageInfoNG& pageInfo)
376 {
377     if (!env_ || !callback_) {
378         TAG_LOGW(AceLogTag::ACE_OBSERVER,
379             "Handle router page state change failed, runtime or callback function invalid!");
380         return;
381     }
382     napi_handle_scope scope = nullptr;
383     auto status = napi_open_handle_scope(env_, &scope);
384     if (status != napi_ok) {
385         return;
386     }
387     napi_value callback = nullptr;
388     napi_get_reference_value(env_, callback_, &callback);
389     napi_value objValue = nullptr;
390     napi_create_object(env_, &objValue);
391     napi_value napiCtx = pageInfo.context;
392     napi_value napiIndex = nullptr;
393     napi_value napiName = nullptr;
394     napi_value napiPath = nullptr;
395     napi_value napiState = nullptr;
396     napi_value napiPageId = nullptr;
397     napi_create_int32(env_, pageInfo.index, &napiIndex);
398     napi_create_string_utf8(env_, pageInfo.name.c_str(), pageInfo.name.length(), &napiName);
399     napi_create_string_utf8(env_, pageInfo.path.c_str(), pageInfo.path.length(), &napiPath);
400     napi_create_int32(env_, static_cast<int32_t>(pageInfo.state), &napiState);
401     napi_create_string_utf8(env_, pageInfo.pageId.c_str(), pageInfo.pageId.length(), &napiPageId);
402     napi_set_named_property(env_, objValue, "context", napiCtx);
403     napi_set_named_property(env_, objValue, "index", napiIndex);
404     napi_set_named_property(env_, objValue, "name", napiName);
405     napi_set_named_property(env_, objValue, "path", napiPath);
406     napi_set_named_property(env_, objValue, "state", napiState);
407     napi_set_named_property(env_, objValue, "pageId", napiPageId);
408     napi_value argv[] = { objValue };
409     napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
410     napi_close_handle_scope(env_, scope);
411 }
412 
OnDensityChange(double density)413 void UIObserverListener::OnDensityChange(double density)
414 {
415     if (!env_ || !callback_) {
416         TAG_LOGW(AceLogTag::ACE_OBSERVER,
417             "Handle density change failed, runtime or callback function invalid!");
418         return;
419     }
420     napi_handle_scope scope = nullptr;
421     auto status = napi_open_handle_scope(env_, &scope);
422     if (status != napi_ok) {
423         return;
424     }
425     napi_value callback = nullptr;
426     napi_get_reference_value(env_, callback_, &callback);
427     napi_value objValue = nullptr;
428     napi_create_object(env_, &objValue);
429     napi_value napiDensity = nullptr;
430     napi_create_double(env_, density, &napiDensity);
431     napi_set_named_property(env_, objValue, "density", napiDensity);
432     napi_value argv[] = { objValue };
433     napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
434     napi_close_handle_scope(env_, scope);
435 }
436 
OnDrawOrLayout()437 void UIObserverListener::OnDrawOrLayout()
438 {
439     if (!env_ || !callback_) {
440         TAG_LOGW(AceLogTag::ACE_OBSERVER, "Handle draw or layout failed, runtime or callback function invalid!");
441         return;
442     }
443     napi_handle_scope scope = nullptr;
444     auto status = napi_open_handle_scope(env_, &scope);
445     if (status != napi_ok) {
446         return;
447     }
448     napi_value callback = nullptr;
449     napi_get_reference_value(env_, callback_, &callback);
450     napi_value objValue = nullptr;
451     napi_create_object(env_, &objValue);
452     napi_value argv[] = { objValue };
453     napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
454     napi_close_handle_scope(env_, scope);
455 }
456 
OnNavDestinationSwitch(const NG::NavDestinationSwitchInfo & switchInfo)457 void UIObserverListener::OnNavDestinationSwitch(const NG::NavDestinationSwitchInfo& switchInfo)
458 {
459     if (!env_ || !callback_) {
460         TAG_LOGW(AceLogTag::ACE_OBSERVER,
461             "Handle navDestination switch failed, runtime or callback function invalid!");
462         return;
463     }
464     napi_handle_scope scope = nullptr;
465     auto status = napi_open_handle_scope(env_, &scope);
466     if (status != napi_ok) {
467         return;
468     }
469     napi_value callback = nullptr;
470     napi_get_reference_value(env_, callback_, &callback);
471     napi_value argv[] = { CreateNavDestinationSwitchInfoObj(switchInfo) };
472     napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
473     napi_close_handle_scope(env_, scope);
474 }
475 
CreateNavDestinationSwitchInfoObj(const NG::NavDestinationSwitchInfo & switchInfo)476 napi_value UIObserverListener::CreateNavDestinationSwitchInfoObj(const NG::NavDestinationSwitchInfo& switchInfo)
477 {
478     napi_value objValue = nullptr;
479     napi_create_object(env_, &objValue);
480     napi_value napiOperation = nullptr;
481     napi_value napiFrom = nullptr;
482     if (switchInfo.from.has_value()) {
483         napiFrom = CreateNavDestinationInfoObj(switchInfo.from.value());
484     } else {
485         napi_create_string_utf8(env_, NAV_BAR, NAPI_AUTO_LENGTH, &napiFrom);
486     }
487     napi_value napiTo = nullptr;
488     if (switchInfo.to.has_value()) {
489         napiTo = CreateNavDestinationInfoObj(switchInfo.to.value());
490     } else {
491         napi_create_string_utf8(env_, NAV_BAR, NAPI_AUTO_LENGTH, &napiTo);
492     }
493     napi_create_int32(env_, static_cast<int32_t>(switchInfo.operation), &napiOperation);
494     napi_set_named_property(env_, objValue, "context", switchInfo.context);
495     napi_set_named_property(env_, objValue, "from", napiFrom);
496     napi_set_named_property(env_, objValue, "to", napiTo);
497     napi_set_named_property(env_, objValue, "operation", napiOperation);
498     return objValue;
499 }
500 
OnWillClick(const GestureEvent & gestureEventInfo,const ClickInfo & clickInfo,const RefPtr<NG::FrameNode> frameNode)501 void UIObserverListener::OnWillClick(
502     const GestureEvent& gestureEventInfo, const ClickInfo& clickInfo, const RefPtr<NG::FrameNode> frameNode)
503 {
504     if (!env_ || !callback_) {
505         TAG_LOGW(AceLogTag::ACE_OBSERVER,
506             "Handle density change failed, runtime or callback function invalid!");
507         return;
508     }
509     napi_handle_scope scope = nullptr;
510     auto status = napi_open_handle_scope(env_, &scope);
511     if (status != napi_ok) {
512         return;
513     }
514 
515     napi_value callback = nullptr;
516     napi_get_reference_value(env_, callback_, &callback);
517 
518     napi_value objValueClickEvent = nullptr;
519     napi_create_object(env_, &objValueClickEvent);
520 
521     AddBaseEventInfo(objValueClickEvent, clickInfo);
522     AddGestureEventInfoOne(objValueClickEvent, gestureEventInfo);
523     AddGestureEventInfoTwo(objValueClickEvent, gestureEventInfo);
524     AddGestureEventInfoThree(objValueClickEvent, gestureEventInfo);
525     AddClickEventInfoOne(objValueClickEvent, clickInfo);
526     AddClickEventInfoTwo(objValueClickEvent, clickInfo);
527 
528     napi_value objValueFrameNode = nullptr;
529     napi_create_object(env_, &objValueFrameNode);
530 
531     auto container = Container::Current();
532     CHECK_NULL_VOID(container);
533     auto frontEnd = container->GetFrontend();
534     CHECK_NULL_VOID(frontEnd);
535     auto nodeId = frameNode->GetId();
536     objValueFrameNode = frontEnd->GetFrameNodeValueByNodeId(nodeId);
537 
538     napi_value argv[] = { objValueClickEvent, objValueFrameNode };
539     napi_call_function(env_, nullptr, callback, PARAM_SIZE_TWO, argv, nullptr);
540     napi_close_handle_scope(env_, scope);
541 }
542 
OnDidClick(const GestureEvent & gestureEventInfo,const ClickInfo & clickInfo,const RefPtr<NG::FrameNode> frameNode)543 void UIObserverListener::OnDidClick(
544     const GestureEvent& gestureEventInfo, const ClickInfo& clickInfo, const RefPtr<NG::FrameNode> frameNode)
545 {
546     if (!env_ || !callback_) {
547         TAG_LOGW(AceLogTag::ACE_OBSERVER,
548             "Handle density change failed, runtime or callback function invalid!");
549         return;
550     }
551     napi_handle_scope scope = nullptr;
552     auto status = napi_open_handle_scope(env_, &scope);
553     if (status != napi_ok) {
554         return;
555     }
556 
557     napi_value callback = nullptr;
558     napi_get_reference_value(env_, callback_, &callback);
559 
560     napi_value objValueClickEvent = nullptr;
561     napi_create_object(env_, &objValueClickEvent);
562 
563     AddBaseEventInfo(objValueClickEvent, clickInfo);
564     AddGestureEventInfoOne(objValueClickEvent, gestureEventInfo);
565     AddGestureEventInfoTwo(objValueClickEvent, gestureEventInfo);
566     AddGestureEventInfoThree(objValueClickEvent, gestureEventInfo);
567     AddClickEventInfoOne(objValueClickEvent, clickInfo);
568     AddClickEventInfoTwo(objValueClickEvent, clickInfo);
569 
570     napi_value objValueFrameNode = nullptr;
571     napi_create_object(env_, &objValueFrameNode);
572 
573     auto container = Container::Current();
574     CHECK_NULL_VOID(container);
575     auto frontEnd = container->GetFrontend();
576     CHECK_NULL_VOID(frontEnd);
577     auto nodeId = frameNode->GetId();
578     objValueFrameNode = frontEnd->GetFrameNodeValueByNodeId(nodeId);
579 
580     napi_value argv[] = { objValueClickEvent, objValueFrameNode };
581     napi_call_function(env_, nullptr, callback, PARAM_SIZE_TWO, argv, nullptr);
582     napi_close_handle_scope(env_, scope);
583 }
584 
OnPanGestureStateChange(const GestureEvent & gestureEventInfo,const RefPtr<NG::PanRecognizer> & current,const RefPtr<NG::FrameNode> frameNode)585 void UIObserverListener::OnPanGestureStateChange(const GestureEvent& gestureEventInfo,
586     const RefPtr<NG::PanRecognizer>& current, const RefPtr<NG::FrameNode> frameNode)
587 {
588     if (!env_ || !callback_) {
589         TAG_LOGW(AceLogTag::ACE_OBSERVER, "Handle pan gesture change failed, runtime or callback function invalid!");
590         return;
591     }
592     napi_handle_scope scope = nullptr;
593     auto status = napi_open_handle_scope(env_, &scope);
594     if (status != napi_ok) {
595         return;
596     }
597 
598     napi_value callback = nullptr;
599     napi_get_reference_value(env_, callback_, &callback);
600 
601     napi_value objValueGestureEvent = nullptr;
602     napi_create_object(env_, &objValueGestureEvent);
603     napi_value objValueGestureRecognizer = nullptr;
604     napi_create_object(env_, &objValueGestureRecognizer);
605 
606     AddBaseEventInfo(objValueGestureEvent, gestureEventInfo);
607     AddGestureEventInfoOne(objValueGestureEvent, gestureEventInfo);
608     AddGestureEventInfoTwo(objValueGestureEvent, gestureEventInfo);
609     AddGestureEventInfoThree(objValueGestureEvent, gestureEventInfo);
610     AddGestureEventInfoFour(objValueGestureEvent, gestureEventInfo);
611     AddTargetObject(objValueGestureEvent, gestureEventInfo);
612     AddGestureRecognizerInfo(objValueGestureRecognizer, current);
613 
614     napi_value objValueFrameNode = nullptr;
615     napi_create_object(env_, &objValueFrameNode);
616 
617     auto container = Container::Current();
618     CHECK_NULL_VOID(container);
619     auto frontEnd = container->GetFrontend();
620     CHECK_NULL_VOID(frontEnd);
621     auto nodeId = frameNode->GetId();
622     objValueFrameNode = frontEnd->GetFrameNodeValueByNodeId(nodeId);
623 
624     napi_value argv[] = { objValueGestureEvent, objValueGestureRecognizer, objValueFrameNode };
625     napi_call_function(env_, nullptr, callback, PARAM_SIZE_THREE, argv, nullptr);
626     napi_close_handle_scope(env_, scope);
627 }
628 
OnTabContentStateChange(const NG::TabContentInfo & tabContentInfo)629 void UIObserverListener::OnTabContentStateChange(const NG::TabContentInfo& tabContentInfo)
630 {
631     if (!env_ || !callback_) {
632         TAG_LOGW(AceLogTag::ACE_OBSERVER,
633             "Handle tabContent state change failed, runtime or callback function invalid!");
634         return;
635     }
636     napi_handle_scope scope = nullptr;
637     auto status = napi_open_handle_scope(env_, &scope);
638     if (status != napi_ok) {
639         return;
640     }
641     napi_value callback = nullptr;
642     napi_get_reference_value(env_, callback_, &callback);
643     napi_value objValue = nullptr;
644     napi_value param1 = nullptr;
645     napi_value param2 = nullptr;
646     napi_value param3 = nullptr;
647     napi_value param4 = nullptr;
648     napi_value param5 = nullptr;
649     napi_value param6 = nullptr;
650     napi_create_string_utf8(env_, tabContentInfo.tabContentId.c_str(), tabContentInfo.tabContentId.length(), &param1);
651     napi_create_int32(env_, tabContentInfo.tabContentUniqueId, &param2);
652     napi_create_int32(env_, static_cast<int32_t>(tabContentInfo.state), &param3);
653     napi_create_int32(env_, tabContentInfo.index, &param4);
654     napi_create_string_utf8(env_, tabContentInfo.id.c_str(), tabContentInfo.id.length(), &param5);
655     napi_create_int32(env_, tabContentInfo.uniqueId, &param6);
656     const char *keys[] = {
657         "tabContentId",
658         "tabContentUniqueId",
659         "state",
660         "index",
661         "id",
662         "uniqueId",
663     };
664     const napi_value values[] = {
665         param1,
666         param2,
667         param3,
668         param4,
669         param5,
670         param6,
671     };
672     napi_create_object_with_named_properties(env_, &objValue, PARAM_SIZE_SIX, keys, values);
673     napi_value argv[] = { objValue };
674     napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
675     napi_close_handle_scope(env_, scope);
676 }
677 
GetValueType(napi_env env,napi_value value)678 napi_valuetype UIObserverListener::GetValueType(napi_env env, napi_value value)
679 {
680     if (value == nullptr) {
681         return napi_undefined;
682     }
683 
684     napi_valuetype valueType = napi_undefined;
685     NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), napi_undefined);
686     return valueType;
687 }
688 
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)689 napi_value UIObserverListener::GetNamedProperty(napi_env env, napi_value object, const std::string& propertyName)
690 {
691     if (GetValueType(env, object) != napi_object) {
692         napi_value undefined = nullptr;
693         NAPI_CALL(env, napi_get_undefined(env, &undefined));
694         return undefined;
695     }
696 
697     napi_value value = nullptr;
698     NAPI_CALL(env, napi_get_named_property(env, object, propertyName.c_str(), &value));
699     return value;
700 }
701 
AddBaseEventInfo(napi_value objValueEvent,const BaseEventInfo & baseEventInfo)702 void UIObserverListener::AddBaseEventInfo(napi_value objValueEvent, const BaseEventInfo& baseEventInfo)
703 {
704     napi_handle_scope scope = nullptr;
705     auto status = napi_open_handle_scope(env_, &scope);
706     if (status != napi_ok) {
707         return;
708     }
709 
710     napi_value napiTimeStamp = nullptr;
711     napi_value napiSource = nullptr;
712     napi_value napiPressure = nullptr;
713     napi_value napiTiltX = nullptr;
714     napi_value napiTiltY = nullptr;
715     napi_value napiSourceTool = nullptr;
716 
717     napi_create_double(env_,
718         static_cast<double>(baseEventInfo.GetTimeStamp().time_since_epoch().count()), &napiTimeStamp);
719     napi_create_double(env_, static_cast<int32_t>(baseEventInfo.GetSourceDevice()), &napiSource);
720     napi_create_double(env_, baseEventInfo.GetForce(), &napiPressure);
721     if (baseEventInfo.GetTiltX().has_value()) {
722         napi_create_double(env_, baseEventInfo.GetTiltX().value(), &napiTiltX);
723     }
724     if (baseEventInfo.GetTiltY().has_value()) {
725         napi_create_double(env_, baseEventInfo.GetTiltY().value(), &napiTiltY);
726     }
727     napi_create_double(env_, static_cast<int32_t>(baseEventInfo.GetSourceTool()), &napiSourceTool);
728 
729     napi_set_named_property(env_, objValueEvent, "timestamp", napiTimeStamp);
730     napi_set_named_property(env_, objValueEvent, "source", napiSource);
731     napi_set_named_property(env_, objValueEvent, "pressure", napiPressure);
732     napi_set_named_property(env_, objValueEvent, "tiltX", napiTiltX);
733     napi_set_named_property(env_, objValueEvent, "tiltY", napiTiltY);
734     napi_set_named_property(env_, objValueEvent, "sourceTool", napiSourceTool);
735 
736     napi_close_handle_scope(env_, scope);
737 }
738 
AddGestureEventInfoOne(napi_value objValueEvent,const GestureEvent & gestureEventInfo)739 void UIObserverListener::AddGestureEventInfoOne(napi_value objValueEvent, const GestureEvent& gestureEventInfo)
740 {
741     napi_handle_scope scope = nullptr;
742     auto status = napi_open_handle_scope(env_, &scope);
743     if (status != napi_ok) {
744         return;
745     }
746     double scale = Dimension(1.0, DimensionUnit::VP).ConvertToPx();
747     if (NearZero(scale)) {
748         scale = 1.0;
749     }
750     napi_value napiRepeat = GetNamedProperty(env_, objValueEvent, "repeat");
751     if (GetValueType(env_, napiRepeat) != napi_null) {
752         napi_get_boolean(env_, gestureEventInfo.GetRepeat(), &napiRepeat);
753         napi_set_named_property(env_, objValueEvent, "repeat", napiRepeat);
754     }
755     napi_value napiOffsetX = GetNamedProperty(env_, objValueEvent, "offsetX");
756     if (GetValueType(env_, napiOffsetX) != napi_null) {
757         napi_create_double(env_, gestureEventInfo.GetOffsetX() / scale, &napiOffsetX);
758         napi_set_named_property(env_, objValueEvent, "offsetX", napiOffsetX);
759     }
760     napi_value napiOffsetY = GetNamedProperty(env_, objValueEvent, "offsetY");
761     if (GetValueType(env_, napiOffsetY) != napi_null) {
762         napi_create_double(env_, gestureEventInfo.GetOffsetY() / scale, &napiOffsetY);
763         napi_set_named_property(env_, objValueEvent, "offsetY", napiOffsetY);
764     }
765     napi_value napiScale = GetNamedProperty(env_, objValueEvent, "scale");
766     if (GetValueType(env_, napiScale) != napi_null) {
767         napi_create_double(env_, gestureEventInfo.GetScale(), &napiScale);
768         napi_set_named_property(env_, objValueEvent, "scale", napiScale);
769     }
770     napi_value napiAngle = GetNamedProperty(env_, objValueEvent, "angle");
771     if (GetValueType(env_, napiAngle) != napi_null) {
772         napi_create_double(env_, gestureEventInfo.GetAngle() / scale, &napiAngle);
773         napi_set_named_property(env_, objValueEvent, "angle", napiAngle);
774     }
775     napi_value napiSpeed = GetNamedProperty(env_, objValueEvent, "speed");
776     if (GetValueType(env_, napiSpeed) != napi_null) {
777         napi_create_double(env_, gestureEventInfo.GetSpeed() / scale, &napiSpeed);
778         napi_set_named_property(env_, objValueEvent, "speed", napiSpeed);
779     }
780     napi_close_handle_scope(env_, scope);
781 }
782 
AddGestureEventInfoTwo(napi_value objValueEvent,const GestureEvent & gestureEventInfo)783 void UIObserverListener::AddGestureEventInfoTwo(napi_value objValueEvent, const GestureEvent& gestureEventInfo)
784 {
785     napi_handle_scope scope = nullptr;
786     auto status = napi_open_handle_scope(env_, &scope);
787     if (status != napi_ok) {
788         return;
789     }
790     double scale = Dimension(1.0, DimensionUnit::VP).ConvertToPx();
791     if (NearZero(scale)) {
792         scale = 1.0;
793     }
794     napi_value napiGlobalX = GetNamedProperty(env_, objValueEvent, "globalX");
795     if (GetValueType(env_, napiGlobalX) != napi_null) {
796         napi_create_double(env_, gestureEventInfo.GetGlobalLocation().GetX() / scale, &napiGlobalX);
797         napi_set_named_property(env_, objValueEvent, "globalX", napiGlobalX);
798     }
799     napi_value napiGlobalY = GetNamedProperty(env_, objValueEvent, "globalY");
800     if (GetValueType(env_, napiGlobalY) != napi_null) {
801         napi_create_double(env_, gestureEventInfo.GetGlobalLocation().GetY() / scale, &napiGlobalY);
802         napi_set_named_property(env_, objValueEvent, "globalY", napiGlobalY);
803     }
804     napi_value napiLocalX = GetNamedProperty(env_, objValueEvent, "localX");
805     if (GetValueType(env_, napiLocalX) != napi_null) {
806         napi_create_double(env_, gestureEventInfo.GetLocalLocation().GetX() / scale, &napiLocalX);
807         napi_set_named_property(env_, objValueEvent, "localX", napiLocalX);
808     }
809     napi_value napiLocalY = GetNamedProperty(env_, objValueEvent, "localY");
810     if (GetValueType(env_, napiLocalY) != napi_null) {
811         napi_create_double(env_, gestureEventInfo.GetLocalLocation().GetY() / scale, &napiLocalY);
812         napi_set_named_property(env_, objValueEvent, "localY", napiLocalY);
813     }
814     napi_value napiPinchCenterX = GetNamedProperty(env_, objValueEvent, "pinchCenterX");
815     if (GetValueType(env_, napiPinchCenterX) != napi_null) {
816         napi_create_double(env_, gestureEventInfo.GetPinchCenter().GetX() / scale, &napiPinchCenterX);
817         napi_set_named_property(env_, objValueEvent, "pinchCenterX", napiPinchCenterX);
818     }
819     napi_value napiPinchCenterY = GetNamedProperty(env_, objValueEvent, "pinchCenterY");
820     if (GetValueType(env_, napiPinchCenterY) != napi_null) {
821         napi_create_double(env_, gestureEventInfo.GetPinchCenter().GetY() / scale, &napiPinchCenterY);
822         napi_set_named_property(env_, objValueEvent, "pinchCenterY", napiPinchCenterY);
823     }
824     napi_close_handle_scope(env_, scope);
825 }
826 
AddGestureEventInfoThree(napi_value objValueEvent,const GestureEvent & gestureEventInfo)827 void UIObserverListener::AddGestureEventInfoThree(napi_value objValueEvent, const GestureEvent& gestureEventInfo)
828 {
829     napi_handle_scope scope = nullptr;
830     auto status = napi_open_handle_scope(env_, &scope);
831     if (status != napi_ok) {
832         return;
833     }
834     double scale = Dimension(1.0, DimensionUnit::VP).ConvertToPx();
835     if (NearZero(scale)) {
836         scale = 1.0;
837     }
838     napi_value napiVelocityX = GetNamedProperty(env_, objValueEvent, "velocityX");
839     if (GetValueType(env_, napiVelocityX) != napi_null) {
840         napi_create_double(env_, gestureEventInfo.GetVelocity().GetVelocityX() / scale, &napiVelocityX);
841         napi_set_named_property(env_, objValueEvent, "velocityX", napiVelocityX);
842     }
843     napi_value napiVelocityY = GetNamedProperty(env_, objValueEvent, "velocityY");
844     if (GetValueType(env_, napiVelocityY) != napi_null) {
845         napi_create_double(env_, gestureEventInfo.GetVelocity().GetVelocityY() / scale, &napiVelocityY);
846         napi_set_named_property(env_, objValueEvent, "velocityY", napiVelocityY);
847     }
848     napi_value napiVelocity = GetNamedProperty(env_, objValueEvent, "velocity");
849     if (GetValueType(env_, napiVelocity) != napi_null) {
850         napi_create_double(env_, gestureEventInfo.GetVelocity().GetVelocityValue() / scale, &napiVelocity);
851         napi_set_named_property(env_, objValueEvent, "velocity", napiVelocity);
852     }
853     napi_value napiAxisHorizontal = nullptr;
854     napi_value napiAxisVertical = nullptr;
855     napi_value napiDeviceId = nullptr;
856     napi_value napiTargetDisplayId = nullptr;
857     napi_create_double(env_, gestureEventInfo.GetHorizontalAxis(), &napiAxisHorizontal);
858     napi_set_named_property(env_, objValueEvent, "axisHorizontal", napiAxisHorizontal);
859     napi_create_double(env_, gestureEventInfo.GetVerticalAxis(), &napiAxisVertical);
860     napi_set_named_property(env_, objValueEvent, "axisVertical", napiAxisVertical);
861     napi_create_double(env_, gestureEventInfo.GetDeviceId(), &napiDeviceId);
862     napi_set_named_property(env_, objValueEvent, "deviceId", napiDeviceId);
863     napi_create_double(env_, gestureEventInfo.GetTargetDisplayId(), &napiTargetDisplayId);
864     napi_set_named_property(env_, objValueEvent, "targetDisplayId", napiTargetDisplayId);
865     AddFingerListInfo(objValueEvent, gestureEventInfo);
866     napi_close_handle_scope(env_, scope);
867 }
868 
AddFingerListInfo(napi_value objValueClickEvent,const GestureEvent & gestureEventInfo)869 void UIObserverListener::AddFingerListInfo(napi_value objValueClickEvent, const GestureEvent& gestureEventInfo)
870 {
871     napi_handle_scope scope = nullptr;
872     auto status = napi_open_handle_scope(env_, &scope);
873     if (status != napi_ok) {
874         return;
875     }
876 
877     const std::list<FingerInfo>& fingerList = gestureEventInfo.GetFingerList();
878     napi_value napiFingerList = nullptr;
879     napi_create_array(env_, &napiFingerList);
880     bool isArray = false;
881     if (napi_is_array(env_, napiFingerList, &isArray) != napi_ok || !isArray) {
882         return;
883     }
884     double scale = Dimension(1.0, DimensionUnit::VP).ConvertToPx();
885     if (NearZero(scale)) {
886         scale = 1.0;
887     }
888     int32_t index = 0;
889     if (fingerList.size() > 0) {
890         for (auto finger : fingerList) {
891             napi_value napiFinger = nullptr;
892             napi_create_object(env_, &napiFinger);
893 
894             napi_value napiId = nullptr;
895             napi_create_double(env_, finger.fingerId_, &napiId);
896             napi_set_named_property(env_, napiFinger, "id", napiId);
897             const OHOS::Ace::Offset& globalLocation = finger.globalLocation_;
898             const OHOS::Ace::Offset& localLocation = finger.localLocation_;
899             napi_value napiGlobalX = nullptr;
900             napi_create_double(env_, globalLocation.GetX() / scale, &napiGlobalX);
901             napi_set_named_property(env_, napiFinger, "globalX", napiGlobalX);
902             napi_value napiGlobalY = nullptr;
903             napi_create_double(env_, globalLocation.GetY() / scale, &napiGlobalY);
904             napi_set_named_property(env_, napiFinger, "globalY", napiGlobalY);
905             napi_value napiLocalX = nullptr;
906             napi_create_double(env_, localLocation.GetX() / scale, &napiLocalX);
907             napi_set_named_property(env_, napiFinger, "localX", napiLocalX);
908             napi_value napiLocalY = nullptr;
909             napi_create_double(env_, localLocation.GetY() / scale, &napiLocalY);
910             napi_set_named_property(env_, napiFinger, "localY", napiLocalY);
911 
912             napi_set_element(env_, napiFingerList, index++, napiFinger);
913         }
914     }
915     napi_set_named_property(env_, objValueClickEvent, "fingerList", napiFingerList);
916     napi_close_handle_scope(env_, scope);
917 }
918 
AddClickEventInfoOne(napi_value objValueClickEvent,const ClickInfo & clickInfo)919 void UIObserverListener::AddClickEventInfoOne(napi_value objValueClickEvent, const ClickInfo& clickInfo)
920 {
921     napi_handle_scope scope = nullptr;
922     auto status = napi_open_handle_scope(env_, &scope);
923     if (status != napi_ok) {
924         return;
925     }
926 
927     double scale = Dimension(1.0, DimensionUnit::VP).ConvertToPx();
928     if (NearZero(scale)) {
929         scale = 1.0;
930     }
931     Offset globalOffset = clickInfo.GetGlobalLocation();
932     Offset screenOffset = clickInfo.GetScreenLocation();
933     napi_value napiDisplayX = GetNamedProperty(env_, objValueClickEvent, "displayX");
934     if (GetValueType(env_, napiDisplayX) != napi_null) {
935         napi_create_double(env_, screenOffset.GetX() / scale, &napiDisplayX);
936         napi_set_named_property(env_, objValueClickEvent, "displayX", napiDisplayX);
937     }
938     napi_value napiDisplayY = GetNamedProperty(env_, objValueClickEvent, "displayY");
939     if (GetValueType(env_, napiDisplayY) != napi_null) {
940         napi_create_double(env_, screenOffset.GetY() / scale, &napiDisplayY);
941         napi_set_named_property(env_, objValueClickEvent, "displayY", napiDisplayY);
942     }
943     napi_value napiWindowX = GetNamedProperty(env_, objValueClickEvent, "windowX");
944     if (GetValueType(env_, napiWindowX) != napi_null) {
945         napi_create_double(env_, globalOffset.GetX() / scale, &napiWindowX);
946         napi_set_named_property(env_, objValueClickEvent, "windowX", napiWindowX);
947     }
948     napi_value napiWindowY = GetNamedProperty(env_, objValueClickEvent, "windowY");
949     if (GetValueType(env_, napiWindowY) != napi_null) {
950         napi_create_double(env_, globalOffset.GetY() / scale, &napiWindowY);
951         napi_set_named_property(env_, objValueClickEvent, "windowY", napiWindowY);
952     }
953     napi_close_handle_scope(env_, scope);
954 }
955 
AddClickEventInfoTwo(napi_value objValueClickEvent,const ClickInfo & clickInfo)956 void UIObserverListener::AddClickEventInfoTwo(napi_value objValueClickEvent, const ClickInfo& clickInfo)
957 {
958     napi_handle_scope scope = nullptr;
959     auto status = napi_open_handle_scope(env_, &scope);
960     if (status != napi_ok) {
961         return;
962     }
963 
964     double scale = Dimension(1.0, DimensionUnit::VP).ConvertToPx();
965     if (NearZero(scale)) {
966         scale = 1.0;
967     }
968     Offset globalOffset = clickInfo.GetGlobalLocation();
969     Offset localOffset = clickInfo.GetLocalLocation();
970     napi_value napiScreenX = GetNamedProperty(env_, objValueClickEvent, "screenX");
971     if (GetValueType(env_, napiScreenX) != napi_null) {
972         napi_create_double(env_, globalOffset.GetX() / scale, &napiScreenX);
973         napi_set_named_property(env_, objValueClickEvent, "screenX", napiScreenX);
974     }
975     napi_value napiScreenY = GetNamedProperty(env_, objValueClickEvent, "screenY");
976     if (GetValueType(env_, napiScreenY) != napi_null) {
977         napi_create_double(env_, globalOffset.GetY() / scale, &napiScreenY);
978         napi_set_named_property(env_, objValueClickEvent, "screenY", napiScreenY);
979     }
980     napi_value napiX = GetNamedProperty(env_, objValueClickEvent, "x");
981     if (GetValueType(env_, napiX) != napi_null) {
982         napi_create_double(env_, localOffset.GetX() / scale, &napiX);
983         napi_set_named_property(env_, objValueClickEvent, "x", napiX);
984     }
985     napi_value napiY = GetNamedProperty(env_, objValueClickEvent, "y");
986     if (GetValueType(env_, napiY) != napi_null) {
987         napi_create_double(env_, localOffset.GetY() / scale, &napiY);
988         napi_set_named_property(env_, objValueClickEvent, "y", napiY);
989     }
990     AddTargetObject(objValueClickEvent, clickInfo);
991     napi_close_handle_scope(env_, scope);
992 }
993 
AddGestureEventInfoFour(napi_value objValueEvent,const GestureEvent & gestureEventInfo)994 void UIObserverListener::AddGestureEventInfoFour(napi_value objValueEvent, const GestureEvent& gestureEventInfo)
995 {
996     napi_handle_scope scope = nullptr;
997     auto status = napi_open_handle_scope(env_, &scope);
998     if (status != napi_ok) {
999         return;
1000     }
1001 
1002     std::unique_ptr<GestureEvent> infoHolder = std::make_unique<GestureEvent>(gestureEventInfo);
1003     auto* info = infoHolder.release();
1004     status = napi_wrap(
1005         env_, objValueEvent, info,
1006         [](napi_env env, void* data, void* hint) {
1007             GestureEvent* info = reinterpret_cast<GestureEvent*>(data);
1008             if (info != nullptr) {
1009                 delete info;
1010             }
1011         },
1012         nullptr, nullptr);
1013     if (status != napi_ok) {
1014         LOGE("napi_wrap failed");
1015         return;
1016     }
1017     napi_value funcValue = nullptr;
1018     napi_create_function(env_, GET_MODIFIER_KEY_STATE, 0, GetModifierKeyState, nullptr, &funcValue);
1019     napi_set_named_property(env_, objValueEvent, GET_MODIFIER_KEY_STATE, funcValue);
1020     napi_close_handle_scope(env_, scope);
1021 }
1022 
AddGestureRecognizerInfo(napi_value objValueGestureRecognizer,const RefPtr<NG::PanRecognizer> & current)1023 void UIObserverListener::AddGestureRecognizerInfo(
1024     napi_value objValueGestureRecognizer, const RefPtr<NG::PanRecognizer>& current)
1025 {
1026     napi_handle_scope scope = nullptr;
1027     auto status = napi_open_handle_scope(env_, &scope);
1028     if (status != napi_ok) {
1029         return;
1030     }
1031     current->IncRefCount();
1032     status = napi_wrap(
1033         env_, objValueGestureRecognizer, AceType::RawPtr(current),
1034         [](napi_env env, void* data, void* hint) {
1035             NG::PanRecognizer* current = reinterpret_cast<NG::PanRecognizer*>(data);
1036             if (current != nullptr) {
1037                 current->DecRefCount();
1038             }
1039         },
1040         nullptr, nullptr);
1041     if (status != napi_ok) {
1042         LOGE("napi_wrap failed");
1043         return;
1044     }
1045 
1046     napi_value funcValue = nullptr;
1047     napi_create_function(env_, GET_TAG, 0, GetTag, nullptr, &funcValue);
1048     napi_set_named_property(env_, objValueGestureRecognizer, GET_TAG, funcValue);
1049     napi_create_function(env_, GET_TYPE, 0, GetType, nullptr, &funcValue);
1050     napi_set_named_property(env_, objValueGestureRecognizer, GET_TYPE, funcValue);
1051     napi_create_function(env_, IS_BUILT_IN, 0, IsBuiltIn, nullptr, &funcValue);
1052     napi_set_named_property(env_, objValueGestureRecognizer, IS_BUILT_IN, funcValue);
1053     napi_create_function(env_, SET_ENABLED, 0, SetEnabled, nullptr, &funcValue);
1054     napi_set_named_property(env_, objValueGestureRecognizer, SET_ENABLED, funcValue);
1055     napi_create_function(env_, IS_ENABLED, 0, IsEnabled, nullptr, &funcValue);
1056     napi_set_named_property(env_, objValueGestureRecognizer, IS_ENABLED, funcValue);
1057     napi_create_function(env_, GET_STATE, 0, GetState, nullptr, &funcValue);
1058     napi_set_named_property(env_, objValueGestureRecognizer, GET_STATE, funcValue);
1059     napi_create_function(env_, GET_EVENT_TARGET_INFO, 0, GetEventTargetInfo, nullptr, &funcValue);
1060     napi_set_named_property(env_, objValueGestureRecognizer, GET_EVENT_TARGET_INFO, funcValue);
1061     napi_create_function(env_, IS_VALID, 0, IsValid, nullptr, &funcValue);
1062     napi_set_named_property(env_, objValueGestureRecognizer, IS_VALID, funcValue);
1063     napi_close_handle_scope(env_, scope);
1064 }
1065 
AddTargetObject(napi_value objValueEvent,const BaseEventInfo & baseEventInfo)1066 void UIObserverListener::AddTargetObject(napi_value objValueEvent, const BaseEventInfo& baseEventInfo)
1067 {
1068     napi_handle_scope scope = nullptr;
1069     auto status = napi_open_handle_scope(env_, &scope);
1070     if (status != napi_ok) {
1071         return;
1072     }
1073 
1074     napi_value napiTargetObject = nullptr;
1075     napi_create_object(env_, &napiTargetObject);
1076     const auto& localOffset = baseEventInfo.GetTarget().area.GetOffset();
1077     const auto& origin = baseEventInfo.GetTarget().origin;
1078 
1079     napi_value napiOffset = nullptr;
1080     napi_create_object(env_, &napiOffset);
1081     napi_value napiX = nullptr;
1082     napi_create_double(env_, localOffset.GetX().ConvertToVp(), &napiX);
1083     napi_set_named_property(env_, napiOffset, "x", napiX);
1084     napi_value napiY = nullptr;
1085     napi_create_double(env_, localOffset.GetY().ConvertToVp(), &napiY);
1086     napi_set_named_property(env_, napiOffset, "y", napiY);
1087     napi_set_named_property(env_, napiTargetObject, "position", napiOffset);
1088 
1089     napi_value napiGlobalOffset = nullptr;
1090     napi_create_object(env_, &napiGlobalOffset);
1091     napi_value napiGlobalX = nullptr;
1092     napi_create_double(env_, localOffset.GetX().ConvertToVp() + origin.GetX().ConvertToVp(),
1093         &napiGlobalX);
1094     napi_set_named_property(env_, napiGlobalOffset, "x", napiGlobalX);
1095     napi_value napiGlobalY = nullptr;
1096     napi_create_double(env_, localOffset.GetY().ConvertToVp() + origin.GetY().ConvertToVp(),
1097         &napiGlobalY);
1098     napi_set_named_property(env_, napiGlobalOffset, "y", napiGlobalY);
1099     napi_set_named_property(env_, napiTargetObject, "globalPosition", napiGlobalOffset);
1100 
1101     napi_value napiArea = nullptr;
1102     napi_create_object(env_, &napiArea);
1103     napi_value napiWidth = nullptr;
1104     napi_create_double(env_, baseEventInfo.GetTarget().area.GetWidth().ConvertToVp(), &napiWidth);
1105     napi_set_named_property(env_, napiArea, "width", napiWidth);
1106     napi_value napiHeight = nullptr;
1107     napi_create_double(env_, baseEventInfo.GetTarget().area.GetHeight().ConvertToVp(), &napiHeight);
1108     napi_set_named_property(env_, napiArea, "height", napiHeight);
1109     napi_set_named_property(env_, napiTargetObject, "area", napiArea);
1110     napi_value id = nullptr;
1111     napi_create_string_utf8(env_, baseEventInfo.GetTarget().id.c_str(), NAPI_AUTO_LENGTH, &id);
1112     napi_set_named_property(env_, napiTargetObject, "id", id);
1113 
1114     napi_set_named_property(env_, objValueEvent, "target", napiTargetObject);
1115     napi_close_handle_scope(env_, scope);
1116 }
1117 
CreateNavDestinationInfoObj(const NG::NavDestinationInfo & info)1118 napi_value UIObserverListener::CreateNavDestinationInfoObj(const NG::NavDestinationInfo& info)
1119 {
1120     napi_value objValue = nullptr;
1121     napi_create_object(env_, &objValue);
1122     napi_value napiNavId = nullptr;
1123     napi_value napiName = nullptr;
1124     napi_value napiState = nullptr;
1125     napi_value napiIdx = nullptr;
1126     napi_value napiNavDesId = nullptr;
1127     napi_create_string_utf8(env_, info.navigationId.c_str(), info.navigationId.length(), &napiNavId);
1128     napi_create_string_utf8(env_, info.name.c_str(), info.name.length(), &napiName);
1129     napi_create_int32(env_, static_cast<int32_t>(info.state), &napiState);
1130     napi_create_int32(env_, info.index, &napiIdx);
1131     napi_create_string_utf8(env_, info.navDestinationId.c_str(), info.navDestinationId.length(), &napiNavDesId);
1132     napi_set_named_property(env_, objValue, "navigationId", napiNavId);
1133     napi_set_named_property(env_, objValue, "name", napiName);
1134     napi_set_named_property(env_, objValue, "state", napiState);
1135     napi_set_named_property(env_, objValue, "index", napiIdx);
1136     napi_set_named_property(env_, objValue, "param", info.param);
1137     napi_set_named_property(env_, objValue, "navDestinationId", napiNavDesId);
1138     if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_FIFTEEN)) {
1139         napi_value napiMode = nullptr;
1140         napi_value napiUniqueId = nullptr;
1141         napi_create_int32(env_, static_cast<int32_t>(info.mode), &napiMode);
1142         napi_create_int32(env_, static_cast<int32_t>(info.uniqueId), &napiUniqueId);
1143         napi_set_named_property(env_, objValue, "mode", napiMode);
1144         napi_set_named_property(env_, objValue, "uniqueId", napiUniqueId);
1145     }
1146     return objValue;
1147 }
1148 
GetNapiCallback()1149 napi_value UIObserverListener::GetNapiCallback()
1150 {
1151     napi_value callback = nullptr;
1152     napi_get_reference_value(env_, callback_, &callback);
1153     return callback;
1154 }
1155 
NapiEqual(napi_value cb)1156 bool UIObserverListener::NapiEqual(napi_value cb)
1157 {
1158     bool isEquals = false;
1159     napi_strict_equals(env_, cb, GetNapiCallback(), &isEquals);
1160     return isEquals;
1161 }
1162 } // namespace OHOS::Ace::Napi
1163