1 /*
2 * Copyright (c) 2023 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/select/select_layout_algorithm.h"
17
18 #include "base/geometry/dimension.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/select/select_theme.h"
21 #include "core/components_ng/base/frame_node.h"
22 #include "core/components_ng/pattern/flex/flex_layout_property.h"
23 #include "core/components_ng/pattern/select/select_pattern.h"
24 #include "core/components_ng/pattern/text/text_layout_property.h"
25 #include "core/components_ng/pattern/menu/menu_theme.h"
26 #include "core/components_ng/pattern/option/option_pattern.h"
27 #include "core/pipeline/pipeline_base.h"
28
29 namespace OHOS::Ace::NG {
30 namespace {
31 constexpr float MIN_SPACE = 8.0f;
32 constexpr float MIN_CHAR_VAL = 2.0f;
33 constexpr float SMALL_MIN_CHAR_VAL = 1.0f;
34 } // namespace
Measure(LayoutWrapper * layoutWrapper)35 void SelectLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper)
36 {
37 NeedAgingUpdateParams(layoutWrapper);
38 auto layoutProps = layoutWrapper->GetLayoutProperty();
39 CHECK_NULL_VOID(layoutProps);
40 auto childConstraint = layoutProps->CreateChildConstraint();
41 // Measure child row to get row height and width.
42 auto rowWrapper = layoutWrapper->GetOrCreateChildByIndex(0);
43 CHECK_NULL_VOID(rowWrapper);
44 auto rowProps = DynamicCast<FlexLayoutProperty>(rowWrapper->GetLayoutProperty());
45 CHECK_NULL_VOID(rowProps);
46 auto space = static_cast<float>(rowProps->GetSpaceValue(Dimension()).ConvertToPx());
47 auto minSpace = Dimension(MIN_SPACE, DimensionUnit::VP);
48 if (space < minSpace.ConvertToPx()) {
49 space = minSpace.ConvertToPx();
50 rowProps->UpdateSpace(minSpace);
51 }
52 auto pipeline = PipelineBase::GetCurrentContext();
53 CHECK_NULL_VOID(pipeline);
54 auto theme = pipeline->GetTheme<SelectTheme>();
55 CHECK_NULL_VOID(theme);
56 UpdateMargin(layoutWrapper, theme);
57 auto spinnerSize = MeasureAndGetSize(rowWrapper->GetOrCreateChildByIndex(1), childConstraint);
58 childConstraint.maxSize.MinusWidth(spinnerSize.Width() + space);
59 auto textWrapper = rowWrapper->GetOrCreateChildByIndex(0);
60 CHECK_NULL_VOID(textWrapper);
61 auto textLayoutProperty = AceType::DynamicCast<TextLayoutProperty>(textWrapper->GetLayoutProperty());
62 CHECK_NULL_VOID(textLayoutProperty);
63 auto textLayoutConstraint = textLayoutProperty->CreateContentConstraint();
64 auto textSize = MeasureAndGetSize(textWrapper, childConstraint);
65 if (childConstraint.parentIdealSize.Width().has_value()) {
66 // Make the spinner icon layout at the right end
67 textSize.SetWidth(childConstraint.parentIdealSize.Width().value() - spinnerSize.Width() - space);
68 }
69
70 auto fontSize = textLayoutProperty->GetFontSize().value().ConvertToPx();
71 bool isTextMin = false;
72 MeasureAndGetTextSize(fontSize, textSize, isTextMin);
73
74 if (isTextMin || childConstraint.parentIdealSize.Width().has_value()) {
75 textLayoutProperty->UpdateMarginSelfIdealSize(textSize);
76 textLayoutConstraint.selfIdealSize = OptionalSize<float>(textSize.Width(), textSize.Height());
77 textLayoutConstraint.maxSize.SetSizeT(textSize);
78 textWrapper->Measure(textLayoutConstraint);
79 }
80
81 auto rowGeometry = rowWrapper->GetGeometryNode();
82 CHECK_NULL_VOID(rowGeometry);
83
84 auto rowWidth = textSize.Width() + space + spinnerSize.Width();
85 auto rowHeight = std::max(textSize.Height(), spinnerSize.Height());
86 rowGeometry->SetFrameSize(SizeF(rowWidth, rowHeight));
87 rowWrapper->GetLayoutProperty()->UpdatePropertyChangeFlag(PROPERTY_UPDATE_LAYOUT);
88
89 float defaultHeight = MeasureAndGetDefaultHeight(layoutProps, theme);
90 layoutWrapper->GetGeometryNode()->SetContentSize(
91 SizeF(rowWidth, rowHeight > defaultHeight ? rowHeight : defaultHeight));
92
93 // Measure same as box, base on the child row.
94 BoxLayoutAlgorithm::PerformMeasureSelf(layoutWrapper);
95 }
96
MeasureAndGetTextSize(double fontSize,SizeF & textSize,bool & isTextMin)97 void SelectLayoutAlgorithm::MeasureAndGetTextSize(double fontSize, SizeF& textSize, bool& isTextMin)
98 {
99 float minCharVal =
100 Container::LessThanAPITargetVersion(PlatformVersion::VERSION_TWELVE) ? MIN_CHAR_VAL : SMALL_MIN_CHAR_VAL;
101 if (textSize.Width() < fontSize * minCharVal) {
102 textSize.SetWidth(fontSize * minCharVal);
103 isTextMin = true;
104 }
105 }
106
MeasureAndGetDefaultHeight(RefPtr<LayoutProperty> layoutProps,RefPtr<SelectTheme> theme)107 float SelectLayoutAlgorithm::MeasureAndGetDefaultHeight(RefPtr<LayoutProperty> layoutProps, RefPtr<SelectTheme> theme)
108 {
109 float defaultHeight = 0.0f;
110 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
111 auto host = layoutProps->GetHost();
112 auto selectPattern = host->GetPattern<SelectPattern>();
113 defaultHeight =
114 static_cast<float>(theme->GetSelectDefaultHeight(selectPattern->GetControlSize()).ConvertToPx());
115 } else {
116 defaultHeight = static_cast<float>(theme->GetSelectDefaultHeight().ConvertToPx());
117 }
118 return defaultHeight;
119 }
120
MeasureAndGetSize(const RefPtr<LayoutWrapper> & childLayoutWrapper,const LayoutConstraintF & constraint)121 SizeF SelectLayoutAlgorithm::MeasureAndGetSize(
122 const RefPtr<LayoutWrapper>& childLayoutWrapper, const LayoutConstraintF& constraint)
123 {
124 CHECK_NULL_RETURN(childLayoutWrapper, SizeF());
125 childLayoutWrapper->Measure(constraint);
126 auto geometry = childLayoutWrapper->GetGeometryNode();
127 CHECK_NULL_RETURN(geometry, SizeF());
128 return geometry->GetMarginFrameSize();
129 }
130
NeedAgingUpdateParams(LayoutWrapper * layoutWrapper)131 void SelectLayoutAlgorithm::NeedAgingUpdateParams(LayoutWrapper* layoutWrapper)
132 {
133 auto host = layoutWrapper->GetHostNode();
134 CHECK_NULL_VOID(host);
135 auto context = host->GetContext();
136 CHECK_NULL_VOID(context);
137 if (fontScale_ == context->GetFontScale()) {
138 return;
139 }
140 fontScale_ = context->GetFontScale();
141 auto menuTheme = context->GetTheme<NG::MenuTheme>();
142 CHECK_NULL_VOID(menuTheme);
143 auto pattern = host->GetPattern<SelectPattern>();
144 CHECK_NULL_VOID(pattern);
145 auto options = pattern->GetOptions();
146 if (NearEqual(fontScale_, menuTheme->GetBigFontSizeScale()) ||
147 NearEqual(fontScale_, menuTheme->GetLargeFontSizeScale()) ||
148 NearEqual(fontScale_, menuTheme->GetMaxFontSizeScale())) {
149 UpdateOptionsMaxLines(options, menuTheme->GetTextMaxLines());
150 } else {
151 UpdateOptionsMaxLines(options, 1);
152 }
153 }
154
UpdateOptionsMaxLines(const std::vector<RefPtr<FrameNode>> & options,int32_t maxLines)155 void SelectLayoutAlgorithm::UpdateOptionsMaxLines(const std::vector<RefPtr<FrameNode>>& options, int32_t maxLines)
156 {
157 for (auto child :options) {
158 auto optionPattern = child->GetPattern<OptionPattern>();
159 CHECK_NULL_VOID(optionPattern);
160 auto textNode = AceType::DynamicCast<FrameNode>(optionPattern->GetTextNode());
161 CHECK_NULL_VOID(textNode);
162 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
163 CHECK_NULL_VOID(textLayoutProperty);
164 textLayoutProperty->UpdateMaxLines(maxLines);
165 }
166 }
167
UpdateMargin(LayoutWrapper * layoutWrapper,RefPtr<SelectTheme> theme)168 void SelectLayoutAlgorithm::UpdateMargin(LayoutWrapper* layoutWrapper, RefPtr<SelectTheme> theme)
169 {
170 auto rowWrapper = layoutWrapper->GetOrCreateChildByIndex(0);
171 CHECK_NULL_VOID(rowWrapper);
172 auto spinner = rowWrapper->GetOrCreateChildByIndex(1);
173 auto spinnerLayoutProperty = spinner->GetLayoutProperty();
174 auto layoutProps = layoutWrapper->GetLayoutProperty();
175 auto rowProps = DynamicCast<FlexLayoutProperty>(rowWrapper->GetLayoutProperty());
176 CHECK_NULL_VOID(rowProps);
177 auto arrowStart = rowProps->GetFlexDirection() == FlexDirection::ROW_REVERSE;
178 CHECK_NULL_VOID(layoutProps);
179 auto isRtl = layoutProps->GetNonAutoLayoutDirection() == TextDirection::RTL;
180 MarginProperty spinnerMargin;
181 MarginProperty TextMargin;
182 if (arrowStart ^ isRtl) {
183 spinnerMargin.left = CalcLength(theme->GetContentMargin());
184 spinnerMargin.right = CalcLength();
185 TextMargin.left = CalcLength();
186 TextMargin.right = CalcLength(theme->GetContentMargin());
187 } else {
188 spinnerMargin.left = CalcLength();
189 spinnerMargin.right = CalcLength(theme->GetContentMargin());
190 TextMargin.left = CalcLength(theme->GetContentMargin());
191 TextMargin.right = CalcLength();
192 }
193 spinnerLayoutProperty->UpdateMargin(spinnerMargin);
194 auto textWrapper = rowWrapper->GetOrCreateChildByIndex(0);
195 CHECK_NULL_VOID(textWrapper);
196 auto textLayoutProperty = AceType::DynamicCast<TextLayoutProperty>(textWrapper->GetLayoutProperty());
197 CHECK_NULL_VOID(textLayoutProperty);
198 textLayoutProperty->UpdateMargin(TextMargin);
199 }
200 } // namespace OHOS::Ace::NG
201