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/marquee/marquee_layout_algorithm.h"
17 #include "base/utils/utils.h"
18 #include "core/components_ng/pattern/marquee/marquee_layout_property.h"
19 #include "core/components_ng/pattern/marquee/marquee_pattern.h"
20
21 #include "base/geometry/ng/offset_t.h"
22 #include "base/utils/utils.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/layout/layout_wrapper.h"
25 #include "core/components_ng/pattern/marquee/marquee_layout_property.h"
26 #include "core/components_ng/pattern/marquee/marquee_pattern.h"
27 #include "core/components_ng/property/calc_length.h"
28 #include "core/components_ng/property/measure_property.h"
29 #include "core/components_ng/property/measure_utils.h"
30 #include "core/pipeline_ng/pipeline_context.h"
31
32 namespace OHOS::Ace::NG {
33 namespace {
34 constexpr double MULTIPLE = 2.0;
35 } // namespace
36
Measure(LayoutWrapper * layoutWrapper)37 void MarqueeLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper)
38 {
39 const auto& layoutConstraint = layoutWrapper->GetLayoutProperty()->GetLayoutConstraint();
40 const auto& maxSize = layoutConstraint->maxSize;
41 const auto& padding = layoutWrapper->GetLayoutProperty()->CreatePaddingAndBorder();
42 auto child = layoutWrapper->GetAllChildrenWithBuild().front();
43 LayoutConstraintF textLayoutConstraint;
44 textLayoutConstraint.UpdateMaxSizeWithCheck(SizeF(Infinity<float>(), maxSize.Height()));
45 // use marquee constrain size as text child max constrain size
46 if (layoutConstraint->selfIdealSize.Height().has_value()) {
47 textLayoutConstraint.selfIdealSize.SetHeight(layoutConstraint->selfIdealSize.Height().value());
48 }
49 // measure text, and add marquee padding to text child
50 PaddingProperty textPadding;
51 textPadding.left = CalcLength(padding.left.value());
52 textPadding.right = CalcLength(padding.right.value());
53 textPadding.top = CalcLength(padding.top.value());
54 textPadding.bottom = CalcLength(padding.bottom.value());
55 child->GetLayoutProperty()->UpdatePadding(textPadding);
56 child->Measure(textLayoutConstraint);
57 // measure marquee self, and update marquee padding to zero
58 layoutWrapper->GetGeometryNode()->UpdatePaddingWithBorder({ 0.0, 0.0, 0.0, 0.0 });
59 OptionalSizeF frameSize;
60 do {
61 // use idea size first if it is valid.
62 frameSize.UpdateSizeWithCheck(layoutConstraint->selfIdealSize);
63 if (frameSize.IsValid()) {
64 break;
65 }
66 // don't set marquee's height, and use text child's height
67 frameSize.UpdateIllegalSizeWithCheck(layoutConstraint->parentIdealSize);
68 frameSize.UpdateIllegalSizeWithCheck(layoutConstraint->percentReference);
69 auto childFrame = child->GetGeometryNode()->GetMarginFrameSize();
70 frameSize.Constrain(SizeF { 0.0f, 0.0f }, SizeF { maxSize.Width(), childFrame.Height() });
71 break;
72 } while (false);
73 layoutWrapper->GetGeometryNode()->SetFrameSize(frameSize.ConvertToSizeT());
74 }
75
Layout(LayoutWrapper * layoutWrapper)76 void MarqueeLayoutAlgorithm::Layout(LayoutWrapper* layoutWrapper)
77 {
78 auto frameSize = layoutWrapper->GetGeometryNode()->GetFrameSize();
79 CHECK_NULL_VOID(!layoutWrapper->GetAllChildrenWithBuild().empty());
80 auto child = layoutWrapper->GetAllChildrenWithBuild().front();
81 // init align, and get user defined alignment
82 auto layoutProperty = layoutWrapper->GetLayoutProperty();
83 CHECK_NULL_VOID(layoutProperty);
84
85 auto host = layoutWrapper->GetHostNode();
86 CHECK_NULL_VOID(host);
87 auto pattern = host->GetPattern<MarqueePattern>();
88 CHECK_NULL_VOID(pattern);
89 auto marqueeLayoutProperty = AceType::DynamicCast<MarqueeLayoutProperty>(layoutProperty);
90 CHECK_NULL_VOID(marqueeLayoutProperty);
91 auto content = marqueeLayoutProperty->GetSrc().value_or("");
92 auto direction = layoutProperty->GetLayoutDirection();
93 auto textDirection = pattern->GetTextDirection(content, direction);
94 auto align = (textDirection == TextDirection::RTL ? Alignment::CENTER_RIGHT : Alignment::CENTER_LEFT);
95
96 auto textGeoNode = child->GetGeometryNode();
97 CHECK_NULL_VOID(textGeoNode);
98 auto minusLen = frameSize.Width() - textGeoNode->GetMarginFrameSize().Width();
99 if (layoutProperty->GetPositionProperty() && GreatOrEqual(minusLen, 0.0f)) {
100 align = layoutWrapper->GetLayoutProperty()->GetPositionProperty()->GetAlignment().value_or(align);
101 }
102 OffsetF translate;
103 translate.SetX((1.0 + align.GetHorizontal()) * minusLen / MULTIPLE);
104 translate.SetY(0.0f);
105 textGeoNode->SetMarginFrameOffset(translate);
106 child->Layout();
107 }
108 } // namespace OHOS::Ace::NG
109