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 "core/components_ng/pattern/model/model_layout_algorithm.h"
17
18 #include "core/components_ng/base/frame_node.h"
19
20 namespace OHOS::Ace::NG {
21
ModelLayoutAlgorithm(const WeakPtr<ModelAdapterWrapper> & adapter)22 ModelLayoutAlgorithm::ModelLayoutAlgorithm(const WeakPtr<ModelAdapterWrapper>& adapter) : modelAdapter_(adapter) {}
23
MeasureContent(const LayoutConstraintF & contentConstraint,LayoutWrapper * layoutWrapper)24 std::optional<SizeF> ModelLayoutAlgorithm::MeasureContent(
25 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper)
26 {
27 auto modelLayoutProperty = DynamicCast<ModelLayoutProperty>(layoutWrapper->GetLayoutProperty());
28 CHECK_NULL_RETURN(modelLayoutProperty, std::nullopt);
29
30 // 1. Width and height are set properly - return content constraint as component size
31 if (contentConstraint.selfIdealSize.IsValid()) {
32 auto size = contentConstraint.selfIdealSize.ConvertToSizeT();
33 LOGD("MODEL_NG: Size => width: %f, height: %f ", size.Width(), size.Height());
34 auto adapter = modelAdapter_.Upgrade();
35 CHECK_NULL_RETURN(adapter, size);
36 adapter->OnMeasureContent(modelLayoutProperty, size);
37 return size;
38 }
39
40 SizeF componentSize(0.0f, 0.0f);
41 do {
42 // 2.1 Width and height are not set. Determine size from parent.
43 const auto& modelLayoutProperty = DynamicCast<ModelLayoutProperty>(layoutWrapper->GetLayoutProperty());
44 SizeF layoutConstraintMaxSize = modelLayoutProperty->GetLayoutConstraint()->maxSize;
45 if (contentConstraint.selfIdealSize.IsNull()) {
46 componentSize.SetSizeT(layoutConstraintMaxSize);
47 break; // exit to return statement
48 }
49
50 // 2.2 Either width or height are set. Set size based on aspect ratio.
51 auto sizeSet = contentConstraint.selfIdealSize.ConvertToSizeT();
52 componentSize.SetSizeT(sizeSet);
53 uint8_t sizeSetStatus = (Negative(sizeSet.Width()) << 1) | Negative(sizeSet.Height());
54 double aspectRatio = Size::CalcRatio(Size(4, 3)); // default aspect ratio os 4:3
55 switch (sizeSetStatus) {
56 case 0b01: // width is positive and height is negative
57 componentSize.SetHeight(static_cast<float>(sizeSet.Width() / aspectRatio));
58 break;
59 case 0b10: // width is negative and height is positive
60 componentSize.SetWidth(static_cast<float>(sizeSet.Height() * aspectRatio));
61 break;
62 case 0b11: // both width and height are negative
63 default:
64 break;
65 }
66
67 } while (false);
68
69 auto size = contentConstraint.Constrain(componentSize);
70 LOGD("MODEL_NG: Size => width: %f, height: %f ", size.Width(), size.Height());
71 auto adapter = modelAdapter_.Upgrade();
72 CHECK_NULL_RETURN(adapter, size);
73 adapter->OnMeasureContent(modelLayoutProperty, size);
74 return size;
75 }
76 } // namespace OHOS::Ace::NG
77