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>("deviceId", hoverInfo.GetDeviceId());
42     hoverObj->Wrap<HoverInfo>(&hoverInfo);
43     JSRef<JSVal> hoverVal = JSRef<JSObject>::Cast(hoverObj);
44     JSRef<JSVal> params[] = { isHoverParam, hoverVal };
45     JsFunction::ExecuteJS((sizeof(params) / sizeof(params[0])), params);
46 }
47 
AccessibilityHoverExecute(bool isHover,AccessibilityHoverInfo & hoverInfo)48 void JsHoverFunction::AccessibilityHoverExecute(bool isHover, AccessibilityHoverInfo& hoverInfo)
49 {
50     JSRef<JSVal> isHoverParam = JSRef<JSVal>::Make(ToJSValue(isHover));
51 
52     JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
53     objectTemplate->SetInternalFieldCount(1);
54     JSRef<JSObject> hoverObj = objectTemplate->NewInstance();
55     hoverObj->SetProperty<double>(
56         "timestamp", static_cast<double>(hoverInfo.GetTimeStamp().time_since_epoch().count()));
57     hoverObj->SetProperty<double>("source", static_cast<int32_t>(hoverInfo.GetSourceDevice()));
58     auto target = CreateEventTargetObject(hoverInfo);
59     hoverObj->SetPropertyObject("target", target);
60     hoverObj->SetProperty<double>("sourceTool", static_cast<int32_t>(hoverInfo.GetSourceTool()));
61     hoverObj->SetProperty<double>("axisVertical", 0.0f);
62     hoverObj->SetProperty<double>("axisHorizontal", 0.0f);
63     hoverObj->SetProperty<double>("tiltX", 0.0f);
64     hoverObj->SetProperty<double>("tiltY", 0.0f);
65 
66     const OHOS::Ace::Offset& globalLocation = hoverInfo.GetGlobalLocation();
67     const OHOS::Ace::Offset& localLocation = hoverInfo.GetLocalLocation();
68     const OHOS::Ace::Offset& screenLocation = hoverInfo.GetScreenLocation();
69     hoverObj->SetProperty<int32_t>("type", static_cast<int32_t>(hoverInfo.GetActionType()));
70     hoverObj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX()));
71     hoverObj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY()));
72     hoverObj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
73     hoverObj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
74     hoverObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
75     hoverObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
76 
77     hoverObj->Wrap<AccessibilityHoverInfo>(&hoverInfo);
78     JSRef<JSVal> hoverVal = JSRef<JSObject>::Cast(hoverObj);
79     JSRef<JSVal> params[] = { isHoverParam, hoverVal };
80     JsFunction::ExecuteJS((sizeof(params) / sizeof(params[0])), params);
81 }
82 
83 } // namespace OHOS::Ace::Framework
84