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_on_area_change_function.h"
17
18 #include "frameworks/bridge/declarative_frontend/engine/js_types.h"
19
20 #include "base/geometry/ng/offset_t.h"
21 #include "base/geometry/offset.h"
22 #include "base/log/log.h"
23 #include "base/utils/system_properties.h"
24
25 namespace OHOS::Ace::Framework {
26 namespace {
27
28 template<typename Rect, typename Offset>
CreateAreaObject(const Rect & rect,const Offset & origin)29 JSRef<JSObject> CreateAreaObject(const Rect& rect, const Offset& origin)
30 {
31 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
32 JSRef<JSObject> area = objectTemplate->NewInstance();
33 JSRef<JSObject> offset = objectTemplate->NewInstance();
34 JSRef<JSObject> globalOffset = objectTemplate->NewInstance();
35 auto localOffset = rect.GetOffset();
36 offset->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
37 offset->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
38 globalOffset->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX() + origin.GetX()));
39 globalOffset->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY() + origin.GetY()));
40 // keep compatible, need remove after
41 area->SetPropertyObject("pos", offset);
42 area->SetPropertyObject("position", offset);
43 // keep compatible, need remove after
44 area->SetPropertyObject("globalPos", globalOffset);
45 area->SetPropertyObject("globalPosition", globalOffset);
46 area->SetProperty<double>("width", PipelineBase::Px2VpWithCurrentDensity(rect.Width()));
47 area->SetProperty<double>("height", PipelineBase::Px2VpWithCurrentDensity(rect.Height()));
48 return area;
49 }
50
51 } // namespace
52
Execute(const Rect & oldRect,const Offset & oldOrigin,const Rect & rect,const Offset & origin)53 void JsOnAreaChangeFunction::Execute(
54 const Rect& oldRect, const Offset& oldOrigin, const Rect& rect, const Offset& origin)
55 {
56 panda::JsiFastNativeScope fastNativeScope(jsFunction_->GetEcmaVM());
57 auto oldArea = CreateAreaObject<Rect, Offset>(oldRect, oldOrigin);
58 auto area = CreateAreaObject<Rect, Offset>(rect, origin);
59 JSRef<JSVal> params[2];
60 params[0] = oldArea;
61 params[1] = area;
62 JsFunction::ExecuteJS(2, params);
63 }
64
Execute(const NG::RectF & oldRect,const NG::OffsetF & oldOrigin,const NG::RectF & rect,const NG::OffsetF & origin)65 void JsOnAreaChangeFunction::Execute(
66 const NG::RectF& oldRect, const NG::OffsetF& oldOrigin, const NG::RectF& rect, const NG::OffsetF& origin)
67 {
68 panda::JsiFastNativeScope fastNativeScope(jsFunction_->GetEcmaVM());
69 auto oldArea = CreateAreaObject<NG::RectF, NG::OffsetF>(oldRect, oldOrigin);
70 auto area = CreateAreaObject<NG::RectF, NG::OffsetF>(rect, origin);
71 JSRef<JSVal> params[2];
72 params[0] = oldArea;
73 params[1] = area;
74 JsFunction::ExecuteJS(2, params);
75 }
76
77 } // namespace OHOS::Ace::Framework
78