• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "bridge/declarative_frontend/jsview/js_view_measure_layout.h"
17 
18 #include "jsnapi.h"
19 
20 #include "base/geometry/dimension.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
22 #include "frameworks/core/components_ng/base/frame_node.h"
23 #include "frameworks/core/components_ng/pattern/custom/custom_measure_layout_node.h"
24 
25 namespace OHOS::Ace::Framework {
26 
27 #ifdef USE_ARK_ENGINE
28 
29 namespace {
30 using OHOS::Ace::NG::LayoutConstraintF;
31 using OHOS::Ace::NG::LayoutProperty;
32 using OHOS::Ace::NG::SizeF;
GenConstraint(const std::optional<NG::LayoutConstraintF> & parentConstraint)33 JSRef<JSObject> GenConstraint(const std::optional<NG::LayoutConstraintF>& parentConstraint)
34 {
35     auto minSize = parentConstraint->minSize;
36     auto maxSize = parentConstraint->maxSize;
37     JSRef<JSObject> constraint = JSRef<JSObject>::New();
38     constraint->SetProperty<double>("minWidth", minSize.Width());
39     constraint->SetProperty<double>("minHeight", minSize.Height());
40     constraint->SetProperty<double>("maxWidth", maxSize.Width());
41     constraint->SetProperty<double>("maxHeight", maxSize.Height());
42     return constraint;
43 }
44 
GenConstraintNG(const NG::LayoutConstraintF & parentConstraint)45 JSRef<JSObject> GenConstraintNG(const NG::LayoutConstraintF& parentConstraint)
46 {
47     auto minSize = parentConstraint.minSize;
48     auto maxSize = parentConstraint.maxSize;
49     JSRef<JSObject> constraint = JSRef<JSObject>::New();
50     constraint->SetProperty<double>("minWidth", 0.0f);
51     constraint->SetProperty<double>("minHeight", 0.0f);
52     constraint->SetProperty<double>("maxWidth", 0.0f);
53     constraint->SetProperty<double>("maxHeight", 0.0f);
54     auto pipeline = PipelineBase::GetCurrentContext();
55     if (!pipeline) {
56         return constraint;
57     }
58     constraint->SetProperty<double>("minWidth", minSize.Width() / pipeline->GetDipScale());
59     constraint->SetProperty<double>("minHeight", minSize.Height() / pipeline->GetDipScale());
60     constraint->SetProperty<double>("maxWidth", maxSize.Width() / pipeline->GetDipScale());
61     constraint->SetProperty<double>("maxHeight", maxSize.Height() / pipeline->GetDipScale());
62     return constraint;
63 }
64 
GenPlaceChildrenConstraintNG(const NG::SizeF & size,RefPtr<NG::LayoutProperty> layoutProperty)65 JSRef<JSObject> GenPlaceChildrenConstraintNG(const NG::SizeF& size, RefPtr<NG::LayoutProperty> layoutProperty)
66 {
67     JSRef<JSObject> constraint = JSRef<JSObject>::New();
68     auto pipeline = PipelineBase::GetCurrentContext();
69     if (!layoutProperty || !pipeline) {
70         constraint->SetProperty<double>("minWidth", 0.0f);
71         constraint->SetProperty<double>("minHeight", 0.0f);
72         constraint->SetProperty<double>("maxWidth", 0.0f);
73         constraint->SetProperty<double>("maxHeight", 0.0f);
74         return constraint;
75     }
76     auto minSize = layoutProperty->GetLayoutConstraint().value().minSize;
77     constraint->SetProperty<double>("minWidth", minSize.Width() / pipeline->GetDipScale());
78     constraint->SetProperty<double>("minHeight", minSize.Height() / pipeline->GetDipScale());
79     auto parentNode = AceType::DynamicCast<NG::FrameNode>(layoutProperty->GetHost()->GetParent());
80     if (parentNode && parentNode->GetTag() == V2::COMMON_VIEW_ETS_TAG) {
81         layoutProperty = parentNode->GetLayoutProperty();
82     }
83     const std::unique_ptr<NG::PaddingProperty>& padding =  layoutProperty->GetPaddingProperty();
84     const std::unique_ptr<NG::BorderWidthProperty>& borderWidth =  layoutProperty->GetBorderWidthProperty();
85     auto topPadding = padding ? padding->top->GetDimension().ConvertToVp() : 0.0f;
86     auto bottomPadding = padding ? padding->bottom->GetDimension().ConvertToVp() : 0.0f;
87     auto leftPadding = padding ? padding->left->GetDimension().ConvertToVp() : 0.0f;
88     auto rightPadding = padding ? padding->right->GetDimension().ConvertToVp() : 0.0f;
89     auto topBorder = borderWidth ? borderWidth->topDimen->ConvertToVp() : 0.0f;
90     auto bottomBorder = borderWidth ? borderWidth->bottomDimen->ConvertToVp() : 0.0f;
91     auto leftBorder = borderWidth ? borderWidth->leftDimen->ConvertToVp() : 0.0f;
92     auto rightBorder = borderWidth ? borderWidth->rightDimen->ConvertToVp() : 0.0f;
93     constraint->SetProperty<double>("maxWidth", size.Width() / pipeline->GetDipScale() - leftPadding - rightPadding -
94         leftBorder - rightBorder);
95     constraint->SetProperty<double>("maxHeight", size.Height() / pipeline->GetDipScale() - topPadding - bottomPadding -
96         topBorder - bottomBorder);
97     return constraint;
98 }
99 
GenPadding(const std::unique_ptr<NG::PaddingProperty> & paddingNative)100 JSRef<JSObject> GenPadding(const std::unique_ptr<NG::PaddingProperty>& paddingNative)
101 {
102     JSRef<JSObject> padding = JSRef<JSObject>::New();
103     padding->SetProperty("top", paddingNative->top->GetDimension().ConvertToVp());
104     padding->SetProperty("right", paddingNative->right->GetDimension().ConvertToVp());
105     padding->SetProperty("bottom", paddingNative->bottom->GetDimension().ConvertToVp());
106     padding->SetProperty("left", paddingNative->left->GetDimension().ConvertToVp());
107     return padding;
108 }
109 
GenMargin(const std::unique_ptr<NG::MarginProperty> & marginNative)110 JSRef<JSObject> GenMargin(const std::unique_ptr<NG::MarginProperty>& marginNative)
111 {
112     JSRef<JSObject> margin = JSRef<JSObject>::New();
113     margin->SetProperty("top", marginNative->top->GetDimension().ConvertToVp());
114     margin->SetProperty("right", marginNative->right->GetDimension().ConvertToVp());
115     margin->SetProperty("bottom", marginNative->bottom->GetDimension().ConvertToVp());
116     margin->SetProperty("left", marginNative->left->GetDimension().ConvertToVp());
117     return margin;
118 }
119 
GenEdgeWidths(const std::unique_ptr<NG::BorderWidthProperty> & edgeWidthsNative)120 JSRef<JSObject> GenEdgeWidths(const std::unique_ptr<NG::BorderWidthProperty>& edgeWidthsNative)
121 {
122     JSRef<JSObject> edgeWidths = JSRef<JSObject>::New();
123     edgeWidths->SetProperty("top", edgeWidthsNative->topDimen->ConvertToVp());
124     edgeWidths->SetProperty("right", edgeWidthsNative->rightDimen->ConvertToVp());
125     edgeWidths->SetProperty("bottom", edgeWidthsNative->bottomDimen->ConvertToVp());
126     edgeWidths->SetProperty("left", edgeWidthsNative->leftDimen->ConvertToVp());
127     return edgeWidths;
128 }
129 
GenEdgesGlobalized(const NG::PaddingPropertyT<float> & edgeNative,TextDirection direction)130 JSRef<JSObject> GenEdgesGlobalized(const NG::PaddingPropertyT<float>& edgeNative, TextDirection direction)
131 {
132     JSRef<JSObject> edges = JSRef<JSObject>::New();
133     auto pipeline = PipelineBase::GetCurrentContext();
134     double px2vpScale = pipeline ? 1.0 / pipeline->GetDipScale() : 1.0;
135     edges->SetProperty("top", edgeNative.top.value_or(0) * px2vpScale);
136     edges->SetProperty("bottom", edgeNative.bottom.value_or(0) * px2vpScale);
137     if (direction != TextDirection::RTL) {
138         edges->SetProperty("start", edgeNative.left.value_or(0) * px2vpScale);
139         edges->SetProperty("end", edgeNative.right.value_or(0) * px2vpScale);
140     } else {
141         edges->SetProperty("start", edgeNative.right.value_or(0) * px2vpScale);
142         edges->SetProperty("end", edgeNative.left.value_or(0) * px2vpScale);
143     }
144     return edges;
145 }
146 
GenBorderWidthGlobalized(const NG::BorderWidthPropertyT<float> & edgeNative,TextDirection direction)147 JSRef<JSObject> GenBorderWidthGlobalized(const NG::BorderWidthPropertyT<float>& edgeNative, TextDirection direction)
148 {
149     JSRef<JSObject> edges = JSRef<JSObject>::New();
150     auto pipeline = PipelineBase::GetCurrentContext();
151     double px2vpScale = pipeline ? 1.0 / pipeline->GetDipScale() : 1.0;
152     edges->SetProperty("top", edgeNative.topDimen.value_or(0) * px2vpScale);
153     edges->SetProperty("bottom", edgeNative.bottomDimen.value_or(0) * px2vpScale);
154     if (direction != TextDirection::RTL) {
155         edges->SetProperty("start", edgeNative.leftDimen.value_or(0) * px2vpScale);
156         edges->SetProperty("end", edgeNative.rightDimen.value_or(0) * px2vpScale);
157     } else {
158         edges->SetProperty("start", edgeNative.rightDimen.value_or(0) * px2vpScale);
159         edges->SetProperty("end", edgeNative.leftDimen.value_or(0) * px2vpScale);
160     }
161     return edges;
162 }
163 
GenBorderInfo(const RefPtr<NG::LayoutWrapper> & layoutWrapper)164 JSRef<JSObject> GenBorderInfo(const RefPtr<NG::LayoutWrapper>& layoutWrapper)
165 {
166     JSRef<JSObject> borderInfo = JSRef<JSObject>::New();
167     auto layoutProperty = layoutWrapper->GetLayoutProperty();
168     const std::unique_ptr<NG::PaddingProperty> defaultPadding = std::make_unique<NG::PaddingProperty>();
169     const std::unique_ptr<NG::PaddingProperty> defaultMargin = std::make_unique<NG::MarginProperty>();
170     const std::unique_ptr<NG::BorderWidthProperty>& defaultEdgeWidth = std::make_unique<NG::BorderWidthProperty>();
171     if (!layoutProperty) {
172         borderInfo->SetPropertyObject("borderWidth", GenEdgeWidths(defaultEdgeWidth));
173         borderInfo->SetPropertyObject("margin", GenMargin(defaultPadding));
174         borderInfo->SetPropertyObject("padding", GenPadding(defaultPadding));
175         return borderInfo;
176     }
177 
178     borderInfo->SetPropertyObject("borderWidth",
179         GenEdgeWidths(
180             layoutProperty->GetBorderWidthProperty() ? layoutProperty->GetBorderWidthProperty() : defaultEdgeWidth));
181 
182     borderInfo->SetPropertyObject("margin",
183         GenMargin(layoutProperty->GetMarginProperty() ? layoutProperty->GetMarginProperty() : defaultMargin));
184     borderInfo->SetPropertyObject("padding",
185         GenPadding(layoutProperty->GetPaddingProperty() ? layoutProperty->GetPaddingProperty() : defaultPadding));
186 
187     return borderInfo;
188 }
189 
GenPositionInfo(const RefPtr<NG::LayoutWrapper> & layoutWrapper)190 JSRef<JSObject> GenPositionInfo(const RefPtr<NG::LayoutWrapper>& layoutWrapper)
191 {
192     auto offset = layoutWrapper->GetGeometryNode()->GetFrameOffset();
193     JSRef<JSObject> position = JSRef<JSObject>::New();
194     position->SetProperty("x", offset.GetX());
195     position->SetProperty("y", offset.GetY());
196     return position;
197 }
198 
GenSelfLayoutInfo(RefPtr<NG::LayoutProperty> layoutProperty)199 JSRef<JSObject> GenSelfLayoutInfo(RefPtr<NG::LayoutProperty> layoutProperty)
200 {
201     JSRef<JSObject> selfLayoutInfo = JSRef<JSObject>::New();
202     const std::unique_ptr<NG::PaddingProperty> defaultPadding = std::make_unique<NG::PaddingProperty>();
203     const std::unique_ptr<NG::PaddingProperty> defaultMargin = std::make_unique<NG::MarginProperty>();
204     const std::unique_ptr<NG::BorderWidthProperty>& defaultEdgeWidth = std::make_unique<NG::BorderWidthProperty>();
205     auto pipeline = PipelineBase::GetCurrentContext();
206     if (!layoutProperty || !pipeline) {
207         selfLayoutInfo->SetPropertyObject("borderWidth", GenEdgeWidths(defaultEdgeWidth));
208         selfLayoutInfo->SetPropertyObject("margin", GenMargin(defaultPadding));
209         selfLayoutInfo->SetPropertyObject("padding", GenPadding(defaultPadding));
210         selfLayoutInfo->SetProperty("width", 0.0f);
211         selfLayoutInfo->SetProperty("height", 0.0f);
212         return selfLayoutInfo;
213     }
214     auto parentNode = AceType::DynamicCast<NG::FrameNode>(layoutProperty->GetHost()->GetParent());
215     if (parentNode && parentNode->GetTag() == V2::COMMON_VIEW_ETS_TAG) {
216         layoutProperty = parentNode->GetLayoutProperty();
217     }
218     auto host = layoutProperty->GetHost();
219     NG::RectF originGeoRect;
220     if (host) {
221         originGeoRect = host->GetGeometryNode()->GetFrameRect();
222     }
223     auto width =
224         GreatNotEqual(originGeoRect.Width(), 0.0f) ? originGeoRect.Width() / pipeline->GetDipScale()
225         : layoutProperty->GetLayoutConstraint()
226             ? layoutProperty->GetLayoutConstraint()->selfIdealSize.Width().value_or(0.0) / pipeline->GetDipScale()
227             : 0.0f;
228     auto height =
229         GreatNotEqual(originGeoRect.Height(), 0.0f) ? originGeoRect.Height() / pipeline->GetDipScale()
230         : layoutProperty->GetLayoutConstraint()
231             ? layoutProperty->GetLayoutConstraint()->selfIdealSize.Height().value_or(0.0) / pipeline->GetDipScale()
232             : 0.0f;
233 
234     selfLayoutInfo->SetPropertyObject("borderWidth",
235         GenEdgeWidths(
236             layoutProperty->GetBorderWidthProperty() ? layoutProperty->GetBorderWidthProperty() : defaultEdgeWidth));
237     selfLayoutInfo->SetPropertyObject("margin",
238         GenMargin(layoutProperty->GetMarginProperty() ? layoutProperty->GetMarginProperty() : defaultPadding));
239     selfLayoutInfo->SetPropertyObject("padding",
240         GenPadding(layoutProperty->GetPaddingProperty() ? layoutProperty->GetPaddingProperty() : defaultPadding));
241     selfLayoutInfo->SetProperty(
242         "width", NearEqual(width, 0.0f)
243                      ? layoutProperty->GetLayoutConstraint()->percentReference.Width() / pipeline->GetDipScale()
244                      : width);
245     selfLayoutInfo->SetProperty(
246         "height", NearEqual(height, 0.0f)
247                       ? layoutProperty->GetLayoutConstraint()->percentReference.Height() / pipeline->GetDipScale()
248                       : height);
249     return selfLayoutInfo;
250 }
251 
FillSubComponentProperty(JSRef<JSObjTemplate> & info,const RefPtr<NG::LayoutWrapper> & layoutWrapper,const size_t & index)252 void FillSubComponentProperty(
253     JSRef<JSObjTemplate>& info, const RefPtr<NG::LayoutWrapper>& layoutWrapper, const size_t& index)
254 {
255     info->SetProperty<std::string>("name", layoutWrapper->GetHostNode()->GetTag());
256     info->SetProperty<std::string>("id", std::to_string(layoutWrapper->GetHostNode()->GetId()));
257     info->SetPropertyObject("constraint", GenConstraint(layoutWrapper->GetLayoutProperty()->GetLayoutConstraint()));
258     info->SetPropertyObject("borderInfo", GenBorderInfo(layoutWrapper));
259     info->SetPropertyObject("position", GenPositionInfo(layoutWrapper));
260 }
261 
FillPlaceSizeProperty(JSRef<JSObjTemplate> & info,const NG::SizeF & size)262 void FillPlaceSizeProperty(JSRef<JSObjTemplate>& info, const NG::SizeF& size)
263 {
264     JSRef<JSObject> measureResult = JSRef<JSObject>::New();
265     Dimension measureWidth(size.Width(), DimensionUnit::PX);
266     Dimension measureHeight(size.Height(), DimensionUnit::PX);
267     measureResult->SetProperty("width", measureWidth.ConvertToVp());
268     measureResult->SetProperty("height", measureHeight.ConvertToVp());
269     info->SetPropertyObject("measureResult", measureResult);
270 }
271 } // namespace
272 
JSMeasureLayoutParam(NG::LayoutWrapper * layoutWrapper)273 JSMeasureLayoutParam::JSMeasureLayoutParam(NG::LayoutWrapper* layoutWrapper) : MeasureLayoutParam(layoutWrapper)
274 {
275     Init();
276 }
277 
Init()278 void JSMeasureLayoutParam::Init()
279 {
280     int32_t count = GetTotalChildCount();
281     childArray_ = JSRef<JSArray>::New(count);
282     GenChildArray(0, count);
283 }
284 
GenChildArray(int32_t start,int32_t end)285 void JSMeasureLayoutParam::GenChildArray(int32_t start, int32_t end)
286 {
287     JSRef<JSFunc> measureFunc = JSRef<JSFunc>::New<FunctionCallback>(ViewMeasureLayout::JSMeasure);
288     JSRef<JSFunc> layoutFunc = JSRef<JSFunc>::New<FunctionCallback>(ViewMeasureLayout::JSLayout);
289     for (int32_t index = start; index < end; index++) {
290         JSRef<JSObjTemplate> info = JSRef<JSObjTemplate>::New();
291         info->SetInternalFieldCount(1);
292         FillSubComponentProperty(info, GetOrCreateChildByIndex(index), index);
293         info->Wrap<NG::MeasureLayoutChild>(&Get(index));
294         info->SetPropertyObject("measure", measureFunc);
295         info->SetPropertyObject("layout", layoutFunc);
296         childArray_->SetValueAt(index, info);
297     }
298 }
299 
GetConstraint()300 JSRef<JSObject> JSMeasureLayoutParam::GetConstraint()
301 {
302     auto layoutWrapper = GetLayoutWrapper();
303     if (layoutWrapper && layoutWrapper->GetGeometryNode() &&
304         layoutWrapper->GetGeometryNode()->GetParentLayoutConstraint()) {
305         auto parentConstraint = layoutWrapper->GetGeometryNode()->GetParentLayoutConstraint();
306         return GenConstraint(parentConstraint);
307     }
308     return GenConstraint(LayoutConstraintF());
309 }
310 
Update(NG::LayoutWrapper * layoutWrapper)311 void JSMeasureLayoutParam::Update(NG::LayoutWrapper* layoutWrapper)
312 {
313     NG::MeasureLayoutChild* addr = nullptr;
314     int32_t count = GetTotalChildCount();
315     if (count > 0) {
316         addr = &Get(0);
317     }
318     MeasureLayoutParam::Update(layoutWrapper);
319     int32_t newCount = GetTotalChildCount();
320     if (count == newCount) {
321         return;
322     }
323     childArray_->SetLength(newCount);
324     if (count < newCount) {
325         GenChildArray(count, newCount);
326     }
327     if (addr != &Get(0)) {
328         for (int32_t index = 0; index < count; index++) {
329             auto info = JSRef<JSObjTemplate>::Cast(childArray_->GetValueAt(index));
330             info->Wrap<NG::MeasureLayoutChild>(&Get(index));
331         }
332     }
333 }
334 
GetInstance(NG::LayoutWrapper * layoutWrapper)335 RefPtr<JSMeasureLayoutParam> JSMeasureLayoutParam::GetInstance(NG::LayoutWrapper* layoutWrapper)
336 {
337     auto host = AceType::DynamicCast<NG::CustomMeasureLayoutNode>(layoutWrapper->GetHostNode());
338     CHECK_NULL_RETURN(host, nullptr);
339     auto jsParam = AceType::DynamicCast<JSMeasureLayoutParam>(host->GetMeasureLayoutParam());
340     if (!jsParam) {
341         jsParam = AceType::MakeRefPtr<JSMeasureLayoutParam>(layoutWrapper);
342         host->SetMeasureLayoutParam(jsParam);
343     } else {
344         jsParam->Update(layoutWrapper);
345     }
346     return jsParam;
347 }
348 
JSMeasureLayoutParamNG(NG::LayoutWrapper * layoutWrapper)349 JSMeasureLayoutParamNG::JSMeasureLayoutParamNG(NG::LayoutWrapper* layoutWrapper) : MeasureLayoutParam(layoutWrapper)
350 {
351     Init();
352 }
353 
Init()354 void JSMeasureLayoutParamNG::Init()
355 {
356     int32_t count = GetTotalChildCount();
357     childArray_ = JSRef<JSArray>::New(count);
358     GenChildArray(0, count);
359 }
360 
GenChildArray(int32_t start,int32_t end)361 void JSMeasureLayoutParamNG::GenChildArray(int32_t start, int32_t end)
362 {
363     JSRef<JSObject> size = JSRef<JSObject>::New();
364     size->SetProperty("width", 0);
365     size->SetProperty("height", 0);
366     JSRef<JSFunc> measureFunc = JSRef<JSFunc>::New<FunctionCallback>(ViewMeasureLayout::JSMeasure);
367     JSRef<JSFunc> layoutFunc = JSRef<JSFunc>::New<FunctionCallback>(ViewMeasureLayout::JSPlaceChildren);
368     JSRef<JSFunc> getMarginFunc = JSRef<JSFunc>::New<FunctionCallback>(ViewMeasureLayout::JSGetMargin);
369     JSRef<JSFunc> getPaddingFunc = JSRef<JSFunc>::New<FunctionCallback>(ViewMeasureLayout::JSGetPadding);
370     JSRef<JSFunc> getBorderWidthFunc = JSRef<JSFunc>::New<FunctionCallback>(ViewMeasureLayout::JSGetBorderWidth);
371     for (int32_t index = start; index < end; index++) {
372         JSRef<JSObjTemplate> info = JSRef<JSObjTemplate>::New();
373         info->SetInternalFieldCount(1);
374         auto child = GetChildByIndex(index);
375         if (child && child->GetHostNode()) {
376             auto uniqueId = child->GetHostNode()->GetId();
377             info->SetProperty("uniqueId", uniqueId);
378         }
379         info->SetPropertyObject("measureResult", size);
380         info->Wrap<NG::MeasureLayoutChild>(&Get(index));
381         info->SetPropertyObject("measure", measureFunc);
382         info->SetPropertyObject("layout", layoutFunc);
383         info->SetPropertyObject("getMargin", getMarginFunc);
384         info->SetPropertyObject("getPadding", getPaddingFunc);
385         info->SetPropertyObject("getBorderWidth", getBorderWidthFunc);
386         childArray_->SetValueAt(index, info);
387     }
388 }
389 
GetConstraint()390 JSRef<JSObject> JSMeasureLayoutParamNG::GetConstraint()
391 {
392     auto layoutWrapper = GetLayoutWrapper();
393     if (layoutWrapper && layoutWrapper->GetLayoutProperty() &&
394         layoutWrapper->GetLayoutProperty()->GetLayoutConstraint()) {
395         auto layoutConstraint = layoutWrapper->GetLayoutProperty()->GetLayoutConstraint().value();
396         return GenConstraintNG(layoutConstraint);
397     }
398     return GenConstraintNG(LayoutConstraintF());
399 }
400 
GetPlaceChildrenConstraint()401 JSRef<JSObject> JSMeasureLayoutParamNG::GetPlaceChildrenConstraint()
402 {
403     auto layoutWrapper = GetLayoutWrapper();
404     if (layoutWrapper && layoutWrapper->GetLayoutProperty() && layoutWrapper->GetGeometryNode()) {
405         auto layoutFrameSize = layoutWrapper->GetGeometryNode()->GetFrameSize();
406         return GenPlaceChildrenConstraintNG(layoutFrameSize, layoutWrapper->GetLayoutProperty());
407     }
408     return GenPlaceChildrenConstraintNG(SizeF(), MakeRefPtr<LayoutProperty>());
409 }
410 
GetSelfLayoutInfo()411 JSRef<JSObject> JSMeasureLayoutParamNG::GetSelfLayoutInfo()
412 {
413     auto layoutWrapper = GetLayoutWrapper();
414     return GenSelfLayoutInfo(layoutWrapper && layoutWrapper->GetLayoutProperty() ? layoutWrapper->GetLayoutProperty()
415                                                                                  : MakeRefPtr<LayoutProperty>());
416 }
417 
UpdateSize(int32_t index,const NG::SizeF & size)418 void JSMeasureLayoutParamNG::UpdateSize(int32_t index, const NG::SizeF& size)
419 {
420     auto info = JSRef<JSObjTemplate>::Cast(childArray_->GetValueAt(index));
421     auto layoutWrapper = GetChildByIndex(index);
422     FillPlaceSizeProperty(info, size);
423 }
424 
Update(NG::LayoutWrapper * layoutWrapper)425 void JSMeasureLayoutParamNG::Update(NG::LayoutWrapper* layoutWrapper)
426 {
427     NG::MeasureLayoutChild* addr = nullptr;
428     int32_t count = GetTotalChildCount();
429     if (count > 0) {
430         addr = &Get(0);
431     }
432     MeasureLayoutParam::Update(layoutWrapper);
433     int32_t newCount = GetTotalChildCount();
434     if (count == newCount) {
435         return;
436     }
437     childArray_->SetLength(newCount);
438     if (count < newCount) {
439         GenChildArray(count, newCount);
440     }
441     if (addr != &Get(0)) {
442         for (int32_t index = 0; index < count; index++) {
443             auto info = JSRef<JSObjTemplate>::Cast(childArray_->GetValueAt(index));
444             info->Wrap<NG::MeasureLayoutChild>(&Get(index));
445         }
446     }
447 }
448 
GetInstance(NG::LayoutWrapper * layoutWrapper)449 RefPtr<JSMeasureLayoutParamNG> JSMeasureLayoutParamNG::GetInstance(NG::LayoutWrapper* layoutWrapper)
450 {
451     auto host = AceType::DynamicCast<NG::CustomMeasureLayoutNode>(layoutWrapper->GetHostNode());
452     CHECK_NULL_RETURN(host, nullptr);
453     auto jsParam = AceType::DynamicCast<JSMeasureLayoutParamNG>(host->GetMeasureLayoutParam());
454     if (!jsParam) {
455         jsParam = AceType::MakeRefPtr<JSMeasureLayoutParamNG>(layoutWrapper);
456         host->SetMeasureLayoutParam(jsParam);
457     } else {
458         jsParam->Update(layoutWrapper);
459     }
460     return jsParam;
461 }
462 
JSMeasure(panda::JsiRuntimeCallInfo * runtimeCallInfo)463 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSMeasure(panda::JsiRuntimeCallInfo* runtimeCallInfo)
464 {
465     ACE_SCOPED_TRACE("ViewMeasureLayout::JSMeasure");
466     EcmaVM* vm = runtimeCallInfo->GetVM();
467     Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
468     auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
469         vm, 0));
470     CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
471     auto child = ptr->GetOrCreateChild();
472     if (!child) {
473         return panda::JSValueRef::Undefined(vm);
474     }
475 
476     auto childLayoutConstraint = ptr->CreateChildConstraint();
477     auto layoutProperty = child->GetLayoutProperty();
478     auto info = runtimeCallInfo;
479     if (info->GetArgsNumber() >= 1 && info->GetCallArgRef(0)->IsObject(vm)) {
480         auto jsObject = JsiObject(info->GetCallArgRef(0)->ToObject(vm));
481         JSRef<JSObject> sizeObj = JSRef<JSObject>::Make(jsObject);
482         CalcDimension minWidth;
483         if (JSViewAbstract::ParseJsDimensionVp(sizeObj->GetProperty("minWidth"), minWidth)) {
484             if (layoutProperty) {
485                 layoutProperty->UpdateCalcMinSize(NG::CalcSize(NG::CalcLength(minWidth), std::nullopt));
486             } else {
487                 auto length = ConvertToPx(NG::CalcLength(minWidth), childLayoutConstraint.scaleProperty,
488                     childLayoutConstraint.percentReference.Width());
489                 if (length) {
490                     childLayoutConstraint.minSize.SetWidth(length.value());
491                 }
492             }
493         }
494 
495         CalcDimension maxWidth;
496         if (JSViewAbstract::ParseJsDimensionVp(sizeObj->GetProperty("maxWidth"), maxWidth)) {
497             if (layoutProperty) {
498                 layoutProperty->UpdateCalcMaxSize(NG::CalcSize(NG::CalcLength(maxWidth), std::nullopt));
499             } else {
500                 auto length = ConvertToPx(NG::CalcLength(maxWidth), childLayoutConstraint.scaleProperty,
501                     childLayoutConstraint.percentReference.Width());
502                 if (length) {
503                     childLayoutConstraint.maxSize.SetWidth(length.value());
504                 }
505             }
506         }
507 
508         CalcDimension minHeight;
509         if (JSViewAbstract::ParseJsDimensionVp(sizeObj->GetProperty("minHeight"), minHeight)) {
510             if (layoutProperty) {
511                 layoutProperty->UpdateCalcMinSize(NG::CalcSize(std::nullopt, NG::CalcLength(minHeight)));
512             } else {
513                 auto length = ConvertToPx(NG::CalcLength(minHeight), childLayoutConstraint.scaleProperty,
514                     childLayoutConstraint.percentReference.Height());
515                 if (length) {
516                     childLayoutConstraint.minSize.SetHeight(length.value());
517                 }
518             }
519         }
520 
521         CalcDimension maxHeight;
522         if (JSViewAbstract::ParseJsDimensionVp(sizeObj->GetProperty("maxHeight"), maxHeight)) {
523             if (layoutProperty) {
524                 layoutProperty->UpdateCalcMaxSize(NG::CalcSize(std::nullopt, NG::CalcLength(maxHeight)));
525             } else {
526                 auto length = ConvertToPx(NG::CalcLength(maxHeight), childLayoutConstraint.scaleProperty,
527                     childLayoutConstraint.percentReference.Height());
528                 if (length) {
529                     childLayoutConstraint.maxSize.SetHeight(length.value());
530                 }
531             }
532         }
533     }
534     child->Measure(childLayoutConstraint);
535 
536     auto size = child->GetGeometryNode()->GetFrameSize();
537     ptr->UpdateSize(size);
538     Dimension measureWidth(size.Width(), DimensionUnit::PX);
539     Dimension measureHeight(size.Height(), DimensionUnit::PX);
540     Local<ObjectRef> measureResultObject = ObjectRef::New(vm);
541     measureResultObject->Set(vm, ToJSValue("width"), ToJSValue(measureWidth.ConvertToVp()));
542     measureResultObject->Set(vm, ToJSValue("height"), ToJSValue(measureHeight.ConvertToVp()));
543     return measureResultObject;
544 }
545 
JSLayout(panda::JsiRuntimeCallInfo * runtimeCallInfo)546 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSLayout(panda::JsiRuntimeCallInfo* runtimeCallInfo)
547 {
548     ACE_SCOPED_TRACE("ViewMeasureLayout::JSLayout");
549     EcmaVM* vm = runtimeCallInfo->GetVM();
550     Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
551     auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
552         vm, 0));
553     CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
554     auto child = ptr->GetChild();
555     if (!child) {
556         return panda::JSValueRef::Undefined(vm);
557     }
558 
559     auto info = runtimeCallInfo;
560     if (info->GetArgsNumber() != 1 || !info->GetCallArgRef(0)->IsObject(vm)) {
561         LOGE("JSLayout arg is wrong");
562         child->Layout();
563         return panda::JSValueRef::Undefined(vm);
564     }
565 
566     auto jsObject = JsiObject(info->GetCallArgRef(0)->ToObject(vm));
567     JSRef<JSObject> layoutInfo = JSRef<JSObject>::Make(jsObject);
568     JSRef<JSObject> sizeObj = layoutInfo->GetProperty("position");
569     JSRef<JSVal> xVal = sizeObj->GetProperty("x");
570     JSRef<JSVal> yVal = sizeObj->GetProperty("y");
571     CalcDimension dimenX;
572     CalcDimension dimenY;
573     auto xResult = JSViewAbstract::ParseJsDimensionVp(xVal, dimenX);
574     auto yResult = JSViewAbstract::ParseJsDimensionVp(yVal, dimenY);
575     if (!(xResult || yResult)) {
576         LOGE("the position prop is illegal");
577     } else {
578         child->GetGeometryNode()->SetMarginFrameOffset({ dimenX.ConvertToPx(), dimenY.ConvertToPx() });
579     }
580     child->Layout();
581 
582     return panda::JSValueRef::Undefined(vm);
583 }
584 
JSPlaceChildren(panda::JsiRuntimeCallInfo * runtimeCallInfo)585 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSPlaceChildren(panda::JsiRuntimeCallInfo* runtimeCallInfo)
586 {
587     ACE_SCOPED_TRACE("ViewMeasureLayout::JSPlaceChildren");
588     EcmaVM* vm = runtimeCallInfo->GetVM();
589     Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
590     auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
591         vm, 0));
592     CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
593     auto child = ptr->GetChild();
594     if (!child) {
595         return panda::JSValueRef::Undefined(vm);
596     }
597 
598     auto info = runtimeCallInfo;
599     if (info->GetArgsNumber() != 1 || !info->GetCallArgRef(0)->IsObject(vm)) {
600         LOGE("JSPlaceChildren arg is wrong");
601         child->Layout();
602         return panda::JSValueRef::Undefined(vm);
603     }
604 
605     auto jsObject = JsiObject(info->GetCallArgRef(0)->ToObject(vm));
606     JSRef<JSObject> layoutInfo = JSRef<JSObject>::Make(jsObject);
607     JSRef<JSVal> xVal = layoutInfo->GetProperty("x");
608     JSRef<JSVal> yVal = layoutInfo->GetProperty("y");
609     CalcDimension dimenX;
610     CalcDimension dimenY;
611     auto xResult = JSViewAbstract::ParseJsDimensionVp(xVal, dimenX);
612     auto yResult = JSViewAbstract::ParseJsDimensionVp(yVal, dimenY);
613     if (!(xResult || yResult)) {
614         LOGE("the position prop is illegal");
615     } else {
616         child->GetGeometryNode()->SetMarginFrameOffset({ dimenX.ConvertToPx(), dimenY.ConvertToPx() });
617     }
618     child->Layout();
619     return panda::JSValueRef::Undefined(vm);
620 }
621 
JSGetMargin(panda::JsiRuntimeCallInfo * runtimeCallInfo)622 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSGetMargin(panda::JsiRuntimeCallInfo* runtimeCallInfo)
623 {
624     EcmaVM* vm = runtimeCallInfo->GetVM();
625     Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
626     auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
627         vm, 0));
628     CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
629     auto child = ptr->GetOrCreateChild();
630     if (!(child && child->GetLayoutProperty())) {
631         return GenEdgesGlobalized({}, TextDirection::LTR).Get().GetLocalHandle();
632     }
633     auto layoutProperty = child->GetLayoutProperty();
634     auto direction = layoutProperty->GetNonAutoLayoutDirection();
635     return GenEdgesGlobalized(layoutProperty->CreateMarginWithoutCache(), direction).Get().GetLocalHandle();
636 }
637 
JSGetPadding(panda::JsiRuntimeCallInfo * runtimeCallInfo)638 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSGetPadding(panda::JsiRuntimeCallInfo* runtimeCallInfo)
639 {
640     EcmaVM* vm = runtimeCallInfo->GetVM();
641     Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
642     auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
643         vm, 0));
644     CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
645     auto child = ptr->GetOrCreateChild();
646     if (!(child && child->GetLayoutProperty())) {
647         return GenEdgesGlobalized({}, TextDirection::LTR).Get().GetLocalHandle();
648     }
649     auto layoutProperty = child->GetLayoutProperty();
650     auto direction = layoutProperty->GetNonAutoLayoutDirection();
651     return GenEdgesGlobalized(layoutProperty->CreatePaddingWithoutBorder(false, false), direction)
652         .Get()
653         .GetLocalHandle();
654 }
655 
JSGetBorderWidth(panda::JsiRuntimeCallInfo * runtimeCallInfo)656 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSGetBorderWidth(panda::JsiRuntimeCallInfo* runtimeCallInfo)
657 {
658     EcmaVM* vm = runtimeCallInfo->GetVM();
659     Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
660     auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
661         vm, 0));
662     CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
663     auto child = ptr->GetOrCreateChild();
664     if (!(child && child->GetLayoutProperty())) {
665         return GenBorderWidthGlobalized({}, TextDirection::LTR).Get().GetLocalHandle();
666     }
667     auto layoutProperty = child->GetLayoutProperty();
668     auto direction = layoutProperty->GetNonAutoLayoutDirection();
669     return GenBorderWidthGlobalized(layoutProperty->CreateBorder(), direction).Get().GetLocalHandle();
670 }
671 #endif
672 
673 } // namespace OHOS::Ace::Framework