1 /*
2  * Copyright (c) 2024 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 "movingphoto_layout_algorithm.h"
17 #include "movingphoto_pattern.h"
18 
19 namespace OHOS::Ace::NG {
20 
Layout(LayoutWrapper* layoutWrapper)21 void MovingPhotoLayoutAlgorithm::Layout(LayoutWrapper* layoutWrapper)
22 {
23     BoxLayoutAlgorithm::PerformLayout(layoutWrapper);
24     auto contentOffset = layoutWrapper->GetGeometryNode()->GetContentOffset();
25     auto host = layoutWrapper->GetHostNode();
26     CHECK_NULL_VOID(host);
27     auto pattern = DynamicCast<MovingPhotoPattern>(host->GetPattern());
28     CHECK_NULL_VOID(pattern);
29     for (auto&& child : layoutWrapper->GetAllChildrenWithBuild()) {
30         if (child->GetHostTag() == V2::IMAGE_ETS_TAG || child->GetHostTag() == V2::COLUMN_ETS_TAG) {
31             child->GetGeometryNode()->SetMarginFrameOffset({ contentOffset.GetX(), contentOffset.GetY() });
32         }
33         child->Layout();
34     }
35 }
36 
Measure(LayoutWrapper* layoutWrapper)37 void MovingPhotoLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper)
38 {
39     auto layoutConstraint = layoutWrapper->GetLayoutProperty()->CreateChildConstraint();
40     auto contentSize = layoutWrapper->GetGeometryNode()->GetContentSize();
41     auto layoutProperty = DynamicCast<MovingPhotoLayoutProperty>(layoutWrapper->GetLayoutProperty());
42     auto host = layoutWrapper->GetHostNode();
43     CHECK_NULL_VOID(host);
44     auto pattern = DynamicCast<MovingPhotoPattern>(host->GetPattern());
45     CHECK_NULL_VOID(pattern);
46     for (auto&& child : layoutWrapper->GetAllChildrenWithBuild()) {
47         if (child->GetHostTag() == V2::IMAGE_ETS_TAG || child->GetHostTag() == V2::COLUMN_ETS_TAG) {
48             auto layoutConstraintForImage = layoutConstraint;
49             layoutConstraintForImage.UpdateSelfMarginSizeWithCheck(OptionalSizeF(contentSize));
50             layoutConstraintForImage.UpdateMaxSizeWithCheck(contentSize);
51             layoutConstraintForImage.UpdateMinSizeWithCheck(contentSize);
52             child->Measure(layoutConstraintForImage);
53         }
54     }
55     PerformMeasureSelf(layoutWrapper);
56 }
57 
MeasureContent( const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper)58 std::optional<SizeF> MovingPhotoLayoutAlgorithm::MeasureContent(
59     const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper)
60 {
61     auto layoutSize = contentConstraint.selfIdealSize.IsValid() ? contentConstraint.selfIdealSize.ConvertToSizeT()
62                                                                 : contentConstraint.maxSize;
63     return layoutSize;
64 }
65 
66 } // namespace OHOS::Ace::NG