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 info->SetPropertyObject("measureResult", size);
375 info->Wrap<NG::MeasureLayoutChild>(&Get(index));
376 info->SetPropertyObject("measure", measureFunc);
377 info->SetPropertyObject("layout", layoutFunc);
378 info->SetPropertyObject("getMargin", getMarginFunc);
379 info->SetPropertyObject("getPadding", getPaddingFunc);
380 info->SetPropertyObject("getBorderWidth", getBorderWidthFunc);
381 childArray_->SetValueAt(index, info);
382 }
383 }
384
GetConstraint()385 JSRef<JSObject> JSMeasureLayoutParamNG::GetConstraint()
386 {
387 auto layoutWrapper = GetLayoutWrapper();
388 if (layoutWrapper && layoutWrapper->GetLayoutProperty() &&
389 layoutWrapper->GetLayoutProperty()->GetLayoutConstraint()) {
390 auto layoutConstraint = layoutWrapper->GetLayoutProperty()->GetLayoutConstraint().value();
391 return GenConstraintNG(layoutConstraint);
392 }
393 return GenConstraintNG(LayoutConstraintF());
394 }
395
GetPlaceChildrenConstraint()396 JSRef<JSObject> JSMeasureLayoutParamNG::GetPlaceChildrenConstraint()
397 {
398 auto layoutWrapper = GetLayoutWrapper();
399 if (layoutWrapper && layoutWrapper->GetLayoutProperty() && layoutWrapper->GetGeometryNode()) {
400 auto layoutFrameSize = layoutWrapper->GetGeometryNode()->GetFrameSize();
401 return GenPlaceChildrenConstraintNG(layoutFrameSize, layoutWrapper->GetLayoutProperty());
402 }
403 return GenPlaceChildrenConstraintNG(SizeF(), MakeRefPtr<LayoutProperty>());
404 }
405
GetSelfLayoutInfo()406 JSRef<JSObject> JSMeasureLayoutParamNG::GetSelfLayoutInfo()
407 {
408 auto layoutWrapper = GetLayoutWrapper();
409 return GenSelfLayoutInfo(layoutWrapper && layoutWrapper->GetLayoutProperty() ? layoutWrapper->GetLayoutProperty()
410 : MakeRefPtr<LayoutProperty>());
411 }
412
UpdateSize(int32_t index,const NG::SizeF & size)413 void JSMeasureLayoutParamNG::UpdateSize(int32_t index, const NG::SizeF& size)
414 {
415 auto info = JSRef<JSObjTemplate>::Cast(childArray_->GetValueAt(index));
416 auto layoutWrapper = GetChildByIndex(index);
417 FillPlaceSizeProperty(info, size);
418 }
419
Update(NG::LayoutWrapper * layoutWrapper)420 void JSMeasureLayoutParamNG::Update(NG::LayoutWrapper* layoutWrapper)
421 {
422 NG::MeasureLayoutChild* addr = nullptr;
423 int32_t count = GetTotalChildCount();
424 if (count > 0) {
425 addr = &Get(0);
426 }
427 MeasureLayoutParam::Update(layoutWrapper);
428 int32_t newCount = GetTotalChildCount();
429 if (count == newCount) {
430 return;
431 }
432 childArray_->SetLength(newCount);
433 if (count < newCount) {
434 GenChildArray(count, newCount);
435 }
436 if (addr != &Get(0)) {
437 for (int32_t index = 0; index < count; index++) {
438 auto info = JSRef<JSObjTemplate>::Cast(childArray_->GetValueAt(index));
439 info->Wrap<NG::MeasureLayoutChild>(&Get(index));
440 }
441 }
442 }
443
GetInstance(NG::LayoutWrapper * layoutWrapper)444 RefPtr<JSMeasureLayoutParamNG> JSMeasureLayoutParamNG::GetInstance(NG::LayoutWrapper* layoutWrapper)
445 {
446 auto host = AceType::DynamicCast<NG::CustomMeasureLayoutNode>(layoutWrapper->GetHostNode());
447 CHECK_NULL_RETURN(host, nullptr);
448 auto jsParam = AceType::DynamicCast<JSMeasureLayoutParamNG>(host->GetMeasureLayoutParam());
449 if (!jsParam) {
450 jsParam = AceType::MakeRefPtr<JSMeasureLayoutParamNG>(layoutWrapper);
451 host->SetMeasureLayoutParam(jsParam);
452 } else {
453 jsParam->Update(layoutWrapper);
454 }
455 return jsParam;
456 }
457
JSMeasure(panda::JsiRuntimeCallInfo * runtimeCallInfo)458 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSMeasure(panda::JsiRuntimeCallInfo* runtimeCallInfo)
459 {
460 ACE_SCOPED_TRACE("ViewMeasureLayout::JSMeasure");
461 EcmaVM* vm = runtimeCallInfo->GetVM();
462 Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
463 auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
464 vm, 0));
465 CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
466 auto child = ptr->GetOrCreateChild();
467 if (!child) {
468 return panda::JSValueRef::Undefined(vm);
469 }
470
471 auto childLayoutConstraint = ptr->CreateChildConstraint();
472 auto layoutProperty = child->GetLayoutProperty();
473 auto info = runtimeCallInfo;
474 if (info->GetArgsNumber() >= 1 && info->GetCallArgRef(0)->IsObject(vm)) {
475 auto jsObject = JsiObject(info->GetCallArgRef(0)->ToObject(vm));
476 JSRef<JSObject> sizeObj = JSRef<JSObject>::Make(jsObject);
477 CalcDimension minWidth;
478 if (JSViewAbstract::ParseJsDimensionVp(sizeObj->GetProperty("minWidth"), minWidth)) {
479 if (layoutProperty) {
480 layoutProperty->UpdateCalcMinSize(NG::CalcSize(NG::CalcLength(minWidth), std::nullopt));
481 } else {
482 auto length = ConvertToPx(NG::CalcLength(minWidth), childLayoutConstraint.scaleProperty,
483 childLayoutConstraint.percentReference.Width());
484 if (length) {
485 childLayoutConstraint.minSize.SetWidth(length.value());
486 }
487 }
488 }
489
490 CalcDimension maxWidth;
491 if (JSViewAbstract::ParseJsDimensionVp(sizeObj->GetProperty("maxWidth"), maxWidth)) {
492 if (layoutProperty) {
493 layoutProperty->UpdateCalcMaxSize(NG::CalcSize(NG::CalcLength(maxWidth), std::nullopt));
494 } else {
495 auto length = ConvertToPx(NG::CalcLength(maxWidth), childLayoutConstraint.scaleProperty,
496 childLayoutConstraint.percentReference.Width());
497 if (length) {
498 childLayoutConstraint.maxSize.SetWidth(length.value());
499 }
500 }
501 }
502
503 CalcDimension minHeight;
504 if (JSViewAbstract::ParseJsDimensionVp(sizeObj->GetProperty("minHeight"), minHeight)) {
505 if (layoutProperty) {
506 layoutProperty->UpdateCalcMinSize(NG::CalcSize(std::nullopt, NG::CalcLength(minHeight)));
507 } else {
508 auto length = ConvertToPx(NG::CalcLength(minHeight), childLayoutConstraint.scaleProperty,
509 childLayoutConstraint.percentReference.Height());
510 if (length) {
511 childLayoutConstraint.minSize.SetHeight(length.value());
512 }
513 }
514 }
515
516 CalcDimension maxHeight;
517 if (JSViewAbstract::ParseJsDimensionVp(sizeObj->GetProperty("maxHeight"), maxHeight)) {
518 if (layoutProperty) {
519 layoutProperty->UpdateCalcMaxSize(NG::CalcSize(std::nullopt, NG::CalcLength(maxHeight)));
520 } else {
521 auto length = ConvertToPx(NG::CalcLength(maxHeight), childLayoutConstraint.scaleProperty,
522 childLayoutConstraint.percentReference.Height());
523 if (length) {
524 childLayoutConstraint.maxSize.SetHeight(length.value());
525 }
526 }
527 }
528 }
529 child->Measure(childLayoutConstraint);
530
531 auto size = child->GetGeometryNode()->GetFrameSize();
532 ptr->UpdateSize(size);
533 Dimension measureWidth(size.Width(), DimensionUnit::PX);
534 Dimension measureHeight(size.Height(), DimensionUnit::PX);
535 Local<ObjectRef> measureResultObject = ObjectRef::New(vm);
536 measureResultObject->Set(vm, ToJSValue("width"), ToJSValue(measureWidth.ConvertToVp()));
537 measureResultObject->Set(vm, ToJSValue("height"), ToJSValue(measureHeight.ConvertToVp()));
538 return measureResultObject;
539 }
540
JSLayout(panda::JsiRuntimeCallInfo * runtimeCallInfo)541 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSLayout(panda::JsiRuntimeCallInfo* runtimeCallInfo)
542 {
543 ACE_SCOPED_TRACE("ViewMeasureLayout::JSLayout");
544 EcmaVM* vm = runtimeCallInfo->GetVM();
545 Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
546 auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
547 vm, 0));
548 CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
549 auto child = ptr->GetChild();
550 if (!child) {
551 return panda::JSValueRef::Undefined(vm);
552 }
553
554 auto info = runtimeCallInfo;
555 if (info->GetArgsNumber() != 1 || !info->GetCallArgRef(0)->IsObject(vm)) {
556 LOGE("JSLayout arg is wrong");
557 child->Layout();
558 return panda::JSValueRef::Undefined(vm);
559 }
560
561 auto jsObject = JsiObject(info->GetCallArgRef(0)->ToObject(vm));
562 JSRef<JSObject> layoutInfo = JSRef<JSObject>::Make(jsObject);
563 JSRef<JSObject> sizeObj = layoutInfo->GetProperty("position");
564 JSRef<JSVal> xVal = sizeObj->GetProperty("x");
565 JSRef<JSVal> yVal = sizeObj->GetProperty("y");
566 CalcDimension dimenX;
567 CalcDimension dimenY;
568 auto xResult = JSViewAbstract::ParseJsDimensionVp(xVal, dimenX);
569 auto yResult = JSViewAbstract::ParseJsDimensionVp(yVal, dimenY);
570 if (!(xResult || yResult)) {
571 LOGE("the position prop is illegal");
572 } else {
573 child->GetGeometryNode()->SetMarginFrameOffset({ dimenX.ConvertToPx(), dimenY.ConvertToPx() });
574 }
575 child->Layout();
576
577 return panda::JSValueRef::Undefined(vm);
578 }
579
JSPlaceChildren(panda::JsiRuntimeCallInfo * runtimeCallInfo)580 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSPlaceChildren(panda::JsiRuntimeCallInfo* runtimeCallInfo)
581 {
582 ACE_SCOPED_TRACE("ViewMeasureLayout::JSPlaceChildren");
583 EcmaVM* vm = runtimeCallInfo->GetVM();
584 Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
585 auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
586 vm, 0));
587 CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
588 auto child = ptr->GetChild();
589 if (!child) {
590 return panda::JSValueRef::Undefined(vm);
591 }
592
593 auto info = runtimeCallInfo;
594 if (info->GetArgsNumber() != 1 || !info->GetCallArgRef(0)->IsObject(vm)) {
595 LOGE("JSPlaceChildren arg is wrong");
596 child->Layout();
597 return panda::JSValueRef::Undefined(vm);
598 }
599
600 auto jsObject = JsiObject(info->GetCallArgRef(0)->ToObject(vm));
601 JSRef<JSObject> layoutInfo = JSRef<JSObject>::Make(jsObject);
602 JSRef<JSVal> xVal = layoutInfo->GetProperty("x");
603 JSRef<JSVal> yVal = layoutInfo->GetProperty("y");
604 CalcDimension dimenX;
605 CalcDimension dimenY;
606 auto xResult = JSViewAbstract::ParseJsDimensionVp(xVal, dimenX);
607 auto yResult = JSViewAbstract::ParseJsDimensionVp(yVal, dimenY);
608 if (!(xResult || yResult)) {
609 LOGE("the position prop is illegal");
610 } else {
611 child->GetGeometryNode()->SetMarginFrameOffset({ dimenX.ConvertToPx(), dimenY.ConvertToPx() });
612 }
613 child->Layout();
614 return panda::JSValueRef::Undefined(vm);
615 }
616
JSGetMargin(panda::JsiRuntimeCallInfo * runtimeCallInfo)617 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSGetMargin(panda::JsiRuntimeCallInfo* runtimeCallInfo)
618 {
619 EcmaVM* vm = runtimeCallInfo->GetVM();
620 Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
621 auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
622 vm, 0));
623 CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
624 auto child = ptr->GetOrCreateChild();
625 if (!(child && child->GetLayoutProperty())) {
626 return GenEdgesGlobalized({}, TextDirection::LTR).Get().GetLocalHandle();
627 }
628 auto layoutProperty = child->GetLayoutProperty();
629 auto direction = layoutProperty->GetNonAutoLayoutDirection();
630 return GenEdgesGlobalized(layoutProperty->CreateMarginWithoutCache(), direction).Get().GetLocalHandle();
631 }
632
JSGetPadding(panda::JsiRuntimeCallInfo * runtimeCallInfo)633 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSGetPadding(panda::JsiRuntimeCallInfo* runtimeCallInfo)
634 {
635 EcmaVM* vm = runtimeCallInfo->GetVM();
636 Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
637 auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
638 vm, 0));
639 CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
640 auto child = ptr->GetOrCreateChild();
641 if (!(child && child->GetLayoutProperty())) {
642 return GenEdgesGlobalized({}, TextDirection::LTR).Get().GetLocalHandle();
643 }
644 auto layoutProperty = child->GetLayoutProperty();
645 auto direction = layoutProperty->GetNonAutoLayoutDirection();
646 return GenEdgesGlobalized(layoutProperty->CreatePaddingWithoutBorder(false, false), direction)
647 .Get()
648 .GetLocalHandle();
649 }
650
JSGetBorderWidth(panda::JsiRuntimeCallInfo * runtimeCallInfo)651 panda::Local<panda::JSValueRef> ViewMeasureLayout::JSGetBorderWidth(panda::JsiRuntimeCallInfo* runtimeCallInfo)
652 {
653 EcmaVM* vm = runtimeCallInfo->GetVM();
654 Local<JSValueRef> thisObj = runtimeCallInfo->GetThisRef();
655 auto ptr = static_cast<NG::MeasureLayoutChild*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
656 vm, 0));
657 CHECK_NULL_RETURN(ptr, panda::JSValueRef::Undefined(vm));
658 auto child = ptr->GetOrCreateChild();
659 if (!(child && child->GetLayoutProperty())) {
660 return GenBorderWidthGlobalized({}, TextDirection::LTR).Get().GetLocalHandle();
661 }
662 auto layoutProperty = child->GetLayoutProperty();
663 auto direction = layoutProperty->GetNonAutoLayoutDirection();
664 return GenBorderWidthGlobalized(layoutProperty->CreateBorder(), direction).Get().GetLocalHandle();
665 }
666 #endif
667
668 } // namespace OHOS::Ace::Framework