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_pan_function.h"
17 
18 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
19 
20 namespace OHOS::Ace::Framework {
21 
createPanInfo(const TouchLocationInfo & info)22 JSRef<JSObject> JsPanFunction::createPanInfo(const TouchLocationInfo& info)
23 {
24     JSRef<JSObject> panInfoObj = JSRef<JSObject>::New();
25     const OHOS::Ace::Offset& globalLocation = info.GetGlobalLocation();
26     const OHOS::Ace::Offset& localLocation = info.GetLocalLocation();
27     panInfoObj->SetProperty<double>("globalX", globalLocation.GetX());
28     panInfoObj->SetProperty<double>("globalY", globalLocation.GetY());
29     panInfoObj->SetProperty<double>("localX", localLocation.GetX());
30     panInfoObj->SetProperty<double>("localY", localLocation.GetY());
31     return panInfoObj;
32 }
33 
Execute(const DragStartInfo & info)34 void JsPanFunction::Execute(const DragStartInfo& info)
35 {
36     JSRef<JSVal> param = createPanInfo(static_cast<TouchLocationInfo>(info));
37     JsFunction::ExecuteJS(1, &param);
38 }
39 
Execute(const DragUpdateInfo & info)40 void JsPanFunction::Execute(const DragUpdateInfo& info)
41 {
42     JSRef<JSObject> paramObj = createPanInfo(static_cast<TouchLocationInfo>(info));
43     const OHOS::Ace::Offset& deltaLocation = info.GetDelta();
44     JSRef<JSObject> deltaInfoObj = JSRef<JSObject>::New();
45     deltaInfoObj->SetProperty<double>("x", deltaLocation.GetX());
46     deltaInfoObj->SetProperty<double>("y", deltaLocation.GetY());
47     paramObj->SetPropertyObject("delta", deltaInfoObj);
48     paramObj->SetProperty<double>("mainDelta", info.GetMainDelta());
49 
50     JSRef<JSVal> param = paramObj;
51     JsFunction::ExecuteJS(1, &param);
52 }
53 
Execute(const DragEndInfo & info)54 void JsPanFunction::Execute(const DragEndInfo& info)
55 {
56     JSRef<JSObject> paramObj = createPanInfo(static_cast<TouchLocationInfo>(info));
57     const OHOS::Ace::Velocity& velocityLocation = info.GetVelocity();
58     JSRef<JSObject> velocityInfoObj = JSRef<JSObject>::New();
59     velocityInfoObj->SetProperty<double>("x", velocityLocation.GetVelocityX());
60     velocityInfoObj->SetProperty<double>("y", velocityLocation.GetVelocityY());
61     paramObj->SetPropertyObject("velocity", velocityInfoObj);
62     paramObj->SetProperty<double>("mainVelocity", info.GetMainVelocity());
63 
64     JSRef<JSVal> param = paramObj;
65     JsFunction::ExecuteJS(1, &param);
66 }
67 
Execute()68 void JsPanFunction::Execute()
69 {
70     JsFunction::ExecuteJS();
71 }
72 
73 } // namespace OHOS::Ace::Framework
74