1 /*
2 * Copyright (c) 2021 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 "frameworks/bridge/declarative_frontend/engine/functions/js_hover_function.h"
17
18 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19
20 namespace OHOS::Ace::Framework {
HoverExecute(bool isHover,HoverInfo & hoverInfo)21 void JsHoverFunction::HoverExecute(bool isHover, HoverInfo& hoverInfo)
22 {
23 JSRef<JSVal> isHoverParam = JSRef<JSVal>::Make(ToJSValue(isHover));
24
25 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
26 objectTemplate->SetInternalFieldCount(1);
27 JSRef<JSObject> hoverObj = objectTemplate->NewInstance();
28 hoverObj->SetPropertyObject("stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
29 hoverObj->SetPropertyObject(
30 "getModifierKeyState", JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
31 hoverObj->SetProperty<double>(
32 "timestamp", static_cast<double>(hoverInfo.GetTimeStamp().time_since_epoch().count()));
33 hoverObj->SetProperty<double>("source", static_cast<int32_t>(hoverInfo.GetSourceDevice()));
34 auto target = CreateEventTargetObject(hoverInfo);
35 hoverObj->SetPropertyObject("target", target);
36 hoverObj->SetProperty<double>("sourceTool", static_cast<int32_t>(hoverInfo.GetSourceTool()));
37 hoverObj->SetProperty<double>("axisVertical", 0.0f);
38 hoverObj->SetProperty<double>("axisHorizontal", 0.0f);
39 hoverObj->SetProperty<double>("tiltX", hoverInfo.GetTiltX().value_or(0.0f));
40 hoverObj->SetProperty<double>("tiltY", hoverInfo.GetTiltY().value_or(0.0f));
41 hoverObj->SetProperty<double>("rollAngle", hoverInfo.GetRollAngle().value_or(0.0f));
42 hoverObj->SetProperty<double>("deviceId", hoverInfo.GetDeviceId());
43 hoverObj->SetProperty<int32_t>("targetDisplayId", hoverInfo.GetTargetDisplayId());
44 hoverObj->SetProperty<int32_t>("pressure", 0.0f);
45 const OHOS::Ace::Offset& globalLocation = hoverInfo.GetGlobalLocation();
46 const OHOS::Ace::Offset& localLocation = hoverInfo.GetLocalLocation();
47 const OHOS::Ace::Offset& screenLocation = hoverInfo.GetScreenLocation();
48 hoverObj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX()));
49 hoverObj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY()));
50 hoverObj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
51 hoverObj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
52 hoverObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
53 hoverObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
54 hoverObj->Wrap<HoverInfo>(&hoverInfo);
55 JSRef<JSVal> hoverVal = JSRef<JSObject>::Cast(hoverObj);
56 JSRef<JSVal> params[] = { isHoverParam, hoverVal };
57 JsFunction::ExecuteJS((sizeof(params) / sizeof(params[0])), params);
58 }
59
HoverMoveExecute(HoverInfo & hoverInfo)60 void JsHoverFunction::HoverMoveExecute(HoverInfo& hoverInfo)
61 {
62 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
63 objectTemplate->SetInternalFieldCount(1);
64 JSRef<JSObject> hoverObj = objectTemplate->NewInstance();
65 hoverObj->SetPropertyObject("stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
66 hoverObj->SetPropertyObject(
67 "getModifierKeyState", JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
68 hoverObj->SetProperty<double>(
69 "timestamp", static_cast<double>(hoverInfo.GetTimeStamp().time_since_epoch().count()));
70 hoverObj->SetProperty<double>("source", static_cast<int32_t>(hoverInfo.GetSourceDevice()));
71 auto target = CreateEventTargetObject(hoverInfo);
72 hoverObj->SetPropertyObject("target", target);
73 hoverObj->SetProperty<double>("sourceTool", static_cast<int32_t>(hoverInfo.GetSourceTool()));
74 hoverObj->SetProperty<double>("axisVertical", 0.0f);
75 hoverObj->SetProperty<double>("axisHorizontal", 0.0f);
76 hoverObj->SetProperty<double>("tiltX", hoverInfo.GetTiltX().value_or(0.0f));
77 hoverObj->SetProperty<double>("tiltY", hoverInfo.GetTiltY().value_or(0.0f));
78 hoverObj->SetProperty<double>("rollAngle", hoverInfo.GetRollAngle().value_or(0.0f));
79 hoverObj->SetProperty<double>("deviceId", hoverInfo.GetDeviceId());
80 const OHOS::Ace::Offset& globalLocation = hoverInfo.GetGlobalLocation();
81 const OHOS::Ace::Offset& localLocation = hoverInfo.GetLocalLocation();
82 const OHOS::Ace::Offset& screenLocation = hoverInfo.GetScreenLocation();
83 hoverObj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX()));
84 hoverObj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY()));
85 hoverObj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
86 hoverObj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
87 hoverObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
88 hoverObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
89 hoverObj->Wrap<HoverInfo>(&hoverInfo);
90 JSRef<JSVal> hoverVal = JSRef<JSObject>::Cast(hoverObj);
91 JSRef<JSVal> params[] = { hoverVal };
92 JsFunction::ExecuteJS((sizeof(params) / sizeof(params[0])), params);
93 }
94
AccessibilityHoverExecute(bool isHover,AccessibilityHoverInfo & hoverInfo)95 void JsHoverFunction::AccessibilityHoverExecute(bool isHover, AccessibilityHoverInfo& hoverInfo)
96 {
97 JSRef<JSVal> isHoverParam = JSRef<JSVal>::Make(ToJSValue(isHover));
98
99 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
100 objectTemplate->SetInternalFieldCount(1);
101 JSRef<JSObject> hoverObj = objectTemplate->NewInstance();
102 hoverObj->SetPropertyObject(
103 "getModifierKeyState", JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
104 hoverObj->SetProperty<double>(
105 "timestamp", static_cast<double>(hoverInfo.GetTimeStamp().time_since_epoch().count()));
106 hoverObj->SetProperty<double>("source", static_cast<int32_t>(hoverInfo.GetSourceDevice()));
107 auto target = CreateEventTargetObject(hoverInfo);
108 hoverObj->SetPropertyObject("target", target);
109 hoverObj->SetProperty<double>("sourceTool", static_cast<int32_t>(hoverInfo.GetSourceTool()));
110 hoverObj->SetProperty<double>("axisVertical", 0.0f);
111 hoverObj->SetProperty<double>("axisHorizontal", 0.0f);
112 hoverObj->SetProperty<double>("tiltX", 0.0f);
113 hoverObj->SetProperty<double>("tiltY", 0.0f);
114 hoverObj->SetProperty<double>("rollAngle", 0.0f);
115
116 const OHOS::Ace::Offset& globalLocation = hoverInfo.GetGlobalLocation();
117 const OHOS::Ace::Offset& localLocation = hoverInfo.GetLocalLocation();
118 const OHOS::Ace::Offset& screenLocation = hoverInfo.GetScreenLocation();
119 hoverObj->SetProperty<int32_t>("type", static_cast<int32_t>(hoverInfo.GetActionType()));
120 hoverObj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX()));
121 hoverObj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY()));
122 hoverObj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
123 hoverObj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
124 hoverObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
125 hoverObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
126 hoverObj->SetProperty<int32_t>("targetDisplayId", hoverInfo.GetTargetDisplayId());
127
128 hoverObj->Wrap<AccessibilityHoverInfo>(&hoverInfo);
129 JSRef<JSVal> hoverVal = JSRef<JSObject>::Cast(hoverObj);
130 JSRef<JSVal> params[] = { isHoverParam, hoverVal };
131 JsFunction::ExecuteJS((sizeof(params) / sizeof(params[0])), params);
132 }
133
134 } // namespace OHOS::Ace::Framework
135