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 = 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 return size;
34 }
35
36 SizeF componentSize(0.0f, 0.0f);
37 do {
38 // 2.1 Width and height are not set. Determine size from parent.
39 const auto& modelLayoutProperty = layoutWrapper->GetLayoutProperty();
40 SizeF layoutConstraintMaxSize = modelLayoutProperty->GetLayoutConstraint()->maxSize;
41 if (contentConstraint.selfIdealSize.IsNull()) {
42 componentSize.SetSizeT(layoutConstraintMaxSize);
43 break; // exit to return statement
44 }
45
46 // 2.2 Either width or height are set. Set size based on aspect ratio.
47 auto sizeSet = contentConstraint.selfIdealSize.ConvertToSizeT();
48 componentSize.SetSizeT(sizeSet);
49 uint8_t sizeSetStatus = (Negative(sizeSet.Width()) << 1) | Negative(sizeSet.Height());
50 double aspectRatio = Size::CalcRatio(Size(4, 3)); // default aspect ratio os 4:3
51 switch (sizeSetStatus) {
52 case 0b01: // width is positive and height is negative
53 componentSize.SetHeight(static_cast<float>(sizeSet.Width() / aspectRatio));
54 break;
55 case 0b10: // width is negative and height is positive
56 componentSize.SetWidth(static_cast<float>(sizeSet.Height() * aspectRatio));
57 break;
58 case 0b11: // both width and height are negative
59 default:
60 break;
61 }
62 } while (false);
63
64 auto size = contentConstraint.Constrain(componentSize);
65 return size;
66 }
67 } // namespace OHOS::Ace::NG
68