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/divider/divider_layout_algorithm.h"
17 
18 #include "base/utils/utils.h"
19 #include "core/components/divider/divider_theme.h"
20 #include "core/components_ng/base/frame_node.h"
21 #include "core/components_ng/pattern/divider/divider_layout_property.h"
22 
23 namespace OHOS::Ace::NG {
24 namespace {}
25 DividerLayoutAlgorithm::DividerLayoutAlgorithm() = default;
26 
MeasureContent(const LayoutConstraintF & contentConstraint,LayoutWrapper * layoutWrapper)27 std::optional<SizeF> DividerLayoutAlgorithm::MeasureContent(
28     const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper)
29 {
30     auto dividerLayoutProperty = DynamicCast<DividerLayoutProperty>(layoutWrapper->GetLayoutProperty());
31     CHECK_NULL_RETURN(dividerLayoutProperty, std::nullopt);
32 
33     auto pipeline = PipelineBase::GetCurrentContext();
34     CHECK_NULL_RETURN(pipeline, std::nullopt);
35     auto theme = pipeline->GetTheme<DividerTheme>();
36     CHECK_NULL_RETURN(theme, std::nullopt);
37     auto defaultStrokeWidth =
38         Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN) ? 1.0_px : theme->GetStokeWidth();
39     Dimension strokeWidth = dividerLayoutProperty->GetStrokeWidth().value_or(defaultStrokeWidth);
40     constrainStrokeWidth_ = Positive(strokeWidth.ConvertToPx()) ? static_cast<float>(strokeWidth.ConvertToPx())
41                                                                 : static_cast<float>(defaultStrokeWidth.ConvertToPx());
42     vertical_ = dividerLayoutProperty->GetVertical().value_or(false);
43     SizeF constrainSize;
44     strokeWidthLimitation_ = dividerLayoutProperty->GetStrokeWidthLimitation().value_or(true);
45     if (!vertical_) {
46         dividerLength_ = (contentConstraint.selfIdealSize.Width()) ? contentConstraint.selfIdealSize.Width().value()
47                                                                    : contentConstraint.percentReference.Width();
48         if (strokeWidthLimitation_) {
49             constrainStrokeWidth_ = std::min(constrainStrokeWidth_, dividerLength_);
50         }
51         constrainStrokeWidth_ = (contentConstraint.selfIdealSize.Height())
52                                     ? std::min(contentConstraint.selfIdealSize.Height().value(), constrainStrokeWidth_)
53                                     : constrainStrokeWidth_;
54         constrainSize = SizeF(dividerLength_, constrainStrokeWidth_);
55         constrainSize.Constrain(contentConstraint.minSize, contentConstraint.maxSize);
56         dividerLength_ = constrainSize.Width();
57     } else {
58         dividerLength_ = (contentConstraint.selfIdealSize.Height()) ? contentConstraint.selfIdealSize.Height().value()
59                                                                     : contentConstraint.percentReference.Height();
60         if (strokeWidthLimitation_) {
61             constrainStrokeWidth_ = std::min(constrainStrokeWidth_, dividerLength_);
62         }
63         constrainStrokeWidth_ = (contentConstraint.selfIdealSize.Width())
64                                     ? std::min(contentConstraint.selfIdealSize.Width().value(), constrainStrokeWidth_)
65                                     : constrainStrokeWidth_;
66         constrainSize = SizeF(constrainStrokeWidth_, dividerLength_);
67         constrainSize.Constrain(contentConstraint.minSize, contentConstraint.maxSize);
68         dividerLength_ = constrainSize.Height();
69     }
70     return constrainSize;
71 }
GetConstrainStrokeWidth() const72 float DividerLayoutAlgorithm::GetConstrainStrokeWidth() const
73 {
74     return constrainStrokeWidth_;
75 }
GetDividerLength() const76 float DividerLayoutAlgorithm::GetDividerLength() const
77 {
78     return dividerLength_;
79 }
GetVertical() const80 bool DividerLayoutAlgorithm::GetVertical() const
81 {
82     return vertical_;
83 }
GetStrokeWidthLimitation() const84 bool DividerLayoutAlgorithm::GetStrokeWidthLimitation() const
85 {
86     return strokeWidthLimitation_;
87 }
88 } // namespace OHOS::Ace::NG
89