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 "frameworks/bridge/declarative_frontend/engine/functions/js_on_child_touch_test_function.h"
17 
18 namespace OHOS::Ace::Framework {
Execute()19 void JsOnChildTouchTestFunction::Execute()
20 {
21     JsFunction::Execute();
22 }
23 
Execute(const std::vector<NG::TouchTestInfo> & touchInfo)24 JSRef<JSVal> JsOnChildTouchTestFunction::Execute(const std::vector<NG::TouchTestInfo>& touchInfo)
25 {
26     JSRef<JSArray> infoArray = JSRef<JSArray>::New();
27     for (size_t i = 0; i < touchInfo.size(); i++) {
28         JSRef<JSVal> info = JSRef<JSObject>::Cast(CreateTouchTestInfo(touchInfo[i]));
29         infoArray->SetValueAt(i, info);
30     }
31     JSRef<JSVal> param = infoArray;
32     return JsFunction::ExecuteJS(1, &param);
33 }
34 
CreateTouchTestInfo(const NG::TouchTestInfo & info)35 JSRef<JSObject> JsOnChildTouchTestFunction::CreateTouchTestInfo(const NG::TouchTestInfo& info)
36 {
37     JSRef<JSObject> infoObj = JSRef<JSObject>::New();
38     infoObj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(info.windowPoint.GetX()));
39     infoObj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(info.windowPoint.GetY()));
40     infoObj->SetProperty<double>("parentX", PipelineBase::Px2VpWithCurrentDensity(info.currentCmpPoint.GetX()));
41     infoObj->SetProperty<double>("parentY", PipelineBase::Px2VpWithCurrentDensity(info.currentCmpPoint.GetY()));
42     infoObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(info.subCmpPoint.GetX()));
43     infoObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(info.subCmpPoint.GetY()));
44     infoObj->SetPropertyObject("rect", CreateRectangle(info.subRect));
45     infoObj->SetProperty<std::string>("id", info.id);
46     return infoObj;
47 }
48 
CreateRectangle(const NG::RectF & info)49 JSRef<JSObject> JsOnChildTouchTestFunction::CreateRectangle(const NG::RectF& info)
50 {
51     JSRef<JSObject> rectangleObj = JSRef<JSObject>::New();
52     rectangleObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(info.GetX()));
53     rectangleObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(info.GetY()));
54     rectangleObj->SetProperty<double>("width", PipelineBase::Px2VpWithCurrentDensity(info.Width()));
55     rectangleObj->SetProperty<double>("height", PipelineBase::Px2VpWithCurrentDensity(info.Height()));
56     return rectangleObj;
57 }
58 } // namespace OHOS::Ace::Framework
59