• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_click_function.h"
17 
18 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
19 
20 namespace OHOS::Ace::Framework {
21 
Execute()22 void JsClickFunction::Execute()
23 {
24     JsFunction::ExecuteJS();
25 
26     // This is required to request for new frame, which eventually will call
27     // FlushBuild, FlushLayout and FlushRender on the dirty elements
28 #ifdef USE_ARK_ENGINE
29     JsiDeclarativeEngineInstance::TriggerPageUpdate(JsiDeclarativeEngineInstance::GetCurrentRuntime());
30 #endif
31 }
32 
Execute(const ClickInfo & info)33 void JsClickFunction::Execute(const ClickInfo& info)
34 {
35     JSRef<JSObject> obj = JSRef<JSObject>::New();
36     Offset globalOffset = info.GetGlobalLocation();
37     Offset localOffset = info.GetLocalLocation();
38     Offset screenOffset = info.GetScreenLocation();
39     obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
40     obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
41     obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
42     obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
43     obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
44     obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
45     obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
46     obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
47     obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
48     obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
49     auto target = CreateEventTargetObject(info);
50     obj->SetPropertyObject("target", target);
51     obj->SetProperty<double>("pressure", info.GetForce());
52     if (info.GetTiltX().has_value()) {
53         obj->SetProperty<double>("tiltX", info.GetTiltX().value());
54     }
55     if (info.GetTiltY().has_value()) {
56         obj->SetProperty<double>("tiltY", info.GetTiltY().value());
57     }
58     obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
59 
60     JSRef<JSVal> param = obj;
61     JsFunction::ExecuteJS(1, &param);
62 }
63 
Execute(const GestureEvent & info)64 void JsClickFunction::Execute(const GestureEvent& info)
65 {
66     JSRef<JSObject> obj = JSRef<JSObject>::New();
67     Offset globalOffset = info.GetGlobalLocation();
68     Offset localOffset = info.GetLocalLocation();
69     Offset screenOffset = info.GetScreenLocation();
70     obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
71     obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
72     obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
73     obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
74     obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
75     obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
76     obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
77     obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
78     obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
79     obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
80     obj->SetProperty<double>("pressure", info.GetForce());
81     if (info.GetTiltX().has_value()) {
82         obj->SetProperty<double>("tiltX", info.GetTiltX().value());
83     }
84     if (info.GetTiltY().has_value()) {
85         obj->SetProperty<double>("tiltY", info.GetTiltY().value());
86     }
87     obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
88     auto target = CreateEventTargetObject(info);
89     obj->SetPropertyObject("target", target);
90 
91     JSRef<JSVal> param = obj;
92     JsFunction::ExecuteJS(1, &param);
93 }
94 
Execute(MouseInfo & info)95 void JsClickFunction::Execute(MouseInfo& info)
96 {
97     JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
98     objectTemplate->SetInternalFieldCount(1);
99     JSRef<JSObject> obj = objectTemplate->NewInstance();
100     obj->SetProperty<int32_t>("button", static_cast<int32_t>(info.GetButton()));
101     obj->SetProperty<int32_t>("action", static_cast<int32_t>(info.GetAction()));
102     Offset globalOffset = info.GetGlobalLocation();
103     Offset localOffset = info.GetLocalLocation();
104     Offset screenOffset = info.GetScreenLocation();
105     obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
106     obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
107     obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
108     obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
109     obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
110     obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
111     obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
112     obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
113     obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
114     obj->SetPropertyObject(
115         "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
116     obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
117     obj->SetProperty<double>("pressure", info.GetForce());
118     if (info.GetTiltX().has_value()) {
119         obj->SetProperty<double>("tiltX", info.GetTiltX().value());
120     }
121     if (info.GetTiltY().has_value()) {
122         obj->SetProperty<double>("tiltY", info.GetTiltY().value());
123     }
124     obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
125     auto target = CreateEventTargetObject(info);
126     obj->SetPropertyObject("target", target);
127     obj->Wrap<MouseInfo>(&info);
128 
129     JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
130     JsFunction::ExecuteJS(1, &param);
131 }
132 
133 } // namespace OHOS::Ace::Framework
134