• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "core/components_ng/pattern/text_picker/textpicker_model_ng.h"
17 
18 #include <securec.h>
19 
20 #include "base/geometry/dimension.h"
21 #include "base/geometry/ng/size_t.h"
22 #include "base/utils/utils.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/base/view_stack_processor.h"
25 #include "core/components_ng/pattern/button/button_pattern.h"
26 #include "core/components_ng/pattern/image/image_pattern.h"
27 #include "core/components_ng/pattern/picker/picker_type_define.h"
28 #include "core/components_ng/pattern/stack/stack_pattern.h"
29 #include "core/components_ng/pattern/text/text_pattern.h"
30 #include "core/components_ng/pattern/text_picker/textpicker_column_pattern.h"
31 #include "core/components_ng/pattern/text_picker/textpicker_event_hub.h"
32 #include "core/components_ng/pattern/text_picker/textpicker_layout_property.h"
33 #include "core/components_ng/pattern/text_picker/textpicker_pattern.h"
34 #include "core/components_v2/inspector/inspector_constants.h"
35 
36 namespace OHOS::Ace::NG {
37 namespace {
38 constexpr float PICKER_MAXFONTSCALE = 1.0f;
39 constexpr bool DEFAULT_ENABLE_HAPTIC_FEEDBACK = true;
40 const int32_t BUFFER_NODE_NUMBER = 2;
41 
SetDialogProperties(DialogProperties & properties,TextPickerDialog & textPickerDialog,const RefPtr<DialogTheme> & theme)42 void SetDialogProperties(DialogProperties& properties, TextPickerDialog& textPickerDialog,
43                          const RefPtr<DialogTheme>& theme)
44 {
45     if (Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN)) {
46         properties.alignment = theme->GetAlignment();
47     }
48     if (textPickerDialog.alignment.has_value()) {
49         properties.alignment = textPickerDialog.alignment.value();
50     }
51 
52     if (textPickerDialog.backgroundColor.has_value()) {
53         properties.backgroundColor = textPickerDialog.backgroundColor.value();
54     }
55     if (textPickerDialog.backgroundBlurStyle.has_value()) {
56         properties.backgroundBlurStyle = textPickerDialog.backgroundBlurStyle.value();
57     }
58     if (textPickerDialog.blurStyleOption.has_value()) {
59         properties.blurStyleOption = textPickerDialog.blurStyleOption.value();
60     }
61     if (textPickerDialog.effectOption.has_value()) {
62         properties.effectOption = textPickerDialog.effectOption.value();
63     }
64     if (textPickerDialog.shadow.has_value()) {
65         properties.shadow = textPickerDialog.shadow.value();
66     }
67     properties.customStyle = false;
68     if (Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN)) {
69         properties.offset = DimensionOffset(Offset(0, -theme->GetMarginBottom().ConvertToPx()));
70     }
71     if (textPickerDialog.offset.has_value()) {
72         properties.offset = textPickerDialog.offset.value();
73     }
74 
75     properties.maskRect = textPickerDialog.maskRect;
76     properties.enableHoverMode = textPickerDialog.enableHoverMode;
77     if (textPickerDialog.hoverModeArea.has_value()) {
78         properties.hoverModeArea = textPickerDialog.hoverModeArea.value();
79     }
80 }
81 
82 using TextPickerGetTextStyleFunc = const std::unique_ptr<FontStyle>& (TextPickerLayoutProperty::*)() const;
ResetTextPickerTextStyleColor(FrameNode * frameNode,TextPickerGetTextStyleFunc getTextStyleFunc)83 void ResetTextPickerTextStyleColor(FrameNode* frameNode, TextPickerGetTextStyleFunc getTextStyleFunc)
84 {
85     CHECK_NULL_VOID(frameNode);
86     auto property = frameNode->GetLayoutPropertyPtr<TextPickerLayoutProperty>();
87     CHECK_NULL_VOID(property);
88     auto& textStyle = (property->*getTextStyleFunc)();
89     CHECK_NULL_VOID(textStyle);
90     if (textStyle->HasTextColor()) {
91         textStyle->ResetTextColor();
92         property->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE_SELF);
93     }
94 }
95 }
96 
Create(RefPtr<PickerTheme> pickerTheme,uint32_t columnKind)97 void TextPickerModelNG::Create(RefPtr<PickerTheme> pickerTheme, uint32_t columnKind)
98 {
99     auto* stack = ViewStackProcessor::GetInstance();
100     auto nodeId = stack->ClaimNodeId();
101     ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::TEXT_PICKER_ETS_TAG, nodeId);
102     auto textPickerNode = FrameNode::GetOrCreateFrameNode(
103         V2::TEXT_PICKER_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<TextPickerPattern>(); });
104     auto textPickerPattern = textPickerNode->GetPattern<TextPickerPattern>();
105     CHECK_NULL_VOID(textPickerPattern);
106     textPickerPattern->SetColumnsKind(columnKind);
107     auto pipeline = PipelineBase::GetCurrentContext();
108     CHECK_NULL_VOID(pipeline);
109     auto dialogTheme = pipeline->GetTheme<DialogTheme>();
110     CHECK_NULL_VOID(dialogTheme);
111     textPickerPattern->SetBackgroundColor(dialogTheme->GetBackgroundColor());
112     CHECK_NULL_VOID(pickerTheme);
113     uint32_t showCount = pickerTheme->GetShowOptionCount() + BUFFER_NODE_NUMBER;
114 
115     if (textPickerNode->GetChildren().empty()) {
116         auto columnNode = CreateColumnNode(columnKind, showCount);
117         auto stackNode = CreateStackNode();
118         auto buttonNode = CreateButtonNode();
119         auto columnBlendNode = CreateColumnNode();
120         buttonNode->MountToParent(stackNode);
121         columnNode->MountToParent(columnBlendNode);
122         columnBlendNode->MountToParent(stackNode);
123         columnNode->MarkModifyDone();
124         columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
125         auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
126         layoutProperty->UpdateAlignment(Alignment::CENTER);
127         stackNode->MountToParent(textPickerNode);
128     }
129     stack->Push(textPickerNode);
130     if (pickerTheme->IsCircleDial()) {
131         auto renderContext = textPickerNode->GetRenderContext();
132         renderContext->UpdateBackgroundColor(pickerTheme->GetBackgroundColor());
133     }
134 }
135 
SetDefaultAttributes(const RefPtr<PickerTheme> & pickerTheme)136 void TextPickerModelNG::SetDefaultAttributes(const RefPtr<PickerTheme>& pickerTheme)
137 {
138     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
139     CHECK_NULL_VOID(frameNode);
140     CHECK_NULL_VOID(pickerTheme);
141     auto selectedStyle = pickerTheme->GetOptionStyle(true, false);
142     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontSize,
143         ConvertFontScaleValue(selectedStyle.GetFontSize()));
144     ResetTextPickerTextStyleColor(frameNode, &TextPickerLayoutProperty::GetSelectedTextStyle);
145     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedWeight, selectedStyle.GetFontWeight());
146     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontFamily, selectedStyle.GetFontFamilies());
147     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontStyle, selectedStyle.GetFontStyle());
148 
149     auto disappearStyle = pickerTheme->GetDisappearOptionStyle();
150     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontSize,
151         ConvertFontScaleValue(disappearStyle.GetFontSize()));
152     ResetTextPickerTextStyleColor(frameNode, &TextPickerLayoutProperty::GetDisappearTextStyle);
153     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearWeight, disappearStyle.GetFontWeight());
154     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontFamily, disappearStyle.GetFontFamilies());
155     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontStyle, disappearStyle.GetFontStyle());
156 
157     auto normalStyle = pickerTheme->GetOptionStyle(false, false);
158     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize,
159         ConvertFontScaleValue(normalStyle.GetFontSize()));
160     ResetTextPickerTextStyleColor(frameNode, &TextPickerLayoutProperty::GetTextStyle);
161     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Weight, normalStyle.GetFontWeight());
162     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontFamily, normalStyle.GetFontFamilies());
163     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontStyle, normalStyle.GetFontStyle());
164     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, CanLoop, true);
165     ACE_UPDATE_LAYOUT_PROPERTY(
166         TextPickerLayoutProperty, DigitalCrownSensitivity, pickerTheme->GetDigitalCrownSensitivity());
167 }
168 
CreateColumnNode(uint32_t columnKind,uint32_t showCount)169 RefPtr<FrameNode> TextPickerModelNG::CreateColumnNode(uint32_t columnKind, uint32_t showCount)
170 {
171     auto columnNode =
172         FrameNode::GetOrCreateFrameNode(V2::COLUMN_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
173         []() { return AceType::MakeRefPtr<TextPickerColumnPattern>(); });
174     if (columnKind == ICON) {
175         for (uint32_t index = 0; index < showCount; index++) {
176             auto row = FrameNode::CreateFrameNode(
177                 V2::ROW_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
178                 AceType::MakeRefPtr<LinearLayoutPattern>(false));
179             CHECK_NULL_RETURN(row, nullptr);
180             auto layoutProps = row->GetLayoutProperty<LinearLayoutProperty>();
181             CHECK_NULL_RETURN(layoutProps, nullptr);
182             layoutProps->UpdateMainAxisAlign(FlexAlign::CENTER);
183             layoutProps->UpdateCrossAxisAlign(FlexAlign::CENTER);
184 
185             auto imageNode = FrameNode::CreateFrameNode(
186                 V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
187                 AceType::MakeRefPtr<ImagePattern>());
188             CHECK_NULL_RETURN(imageNode, nullptr);
189             imageNode->MountToParent(row);
190             row->MountToParent(columnNode);
191         }
192     } else if (columnKind == TEXT) {
193         for (uint32_t index = 0; index < showCount; index++) {
194             auto textNode = FrameNode::CreateFrameNode(
195                 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
196                 AceType::MakeRefPtr<TextPattern>());
197             CHECK_NULL_RETURN(textNode, nullptr);
198             textNode->MountToParent(columnNode);
199         }
200     } else if (columnKind == MIXTURE) {
201         for (uint32_t index = 0; index < showCount; index++) {
202             auto row = FrameNode::CreateFrameNode(
203                 V2::ROW_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
204                 AceType::MakeRefPtr<LinearLayoutPattern>(false));
205             CHECK_NULL_RETURN(row, nullptr);
206 
207             auto imageNode = FrameNode::CreateFrameNode(
208                 V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
209                 AceType::MakeRefPtr<ImagePattern>());
210             CHECK_NULL_RETURN(imageNode, nullptr);
211             imageNode->MountToParent(row);
212 
213             auto textNode = FrameNode::CreateFrameNode(
214                 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
215                 AceType::MakeRefPtr<TextPattern>());
216             CHECK_NULL_RETURN(textNode, nullptr);
217             textNode->MountToParent(row);
218             row->MountToParent(columnNode);
219         }
220     }
221     return columnNode;
222 }
223 
CreateStackNode()224 RefPtr<FrameNode> TextPickerModelNG::CreateStackNode()
225 {
226     auto stackId = ElementRegister::GetInstance()->MakeUniqueId();
227     return FrameNode::GetOrCreateFrameNode(
228         V2::STACK_ETS_TAG, stackId, []() { return AceType::MakeRefPtr<StackPattern>(); });
229 }
230 
CreateColumnNode()231 RefPtr<FrameNode> TextPickerModelNG::CreateColumnNode()
232 {
233     auto columnId = ElementRegister::GetInstance()->MakeUniqueId();
234     return FrameNode::GetOrCreateFrameNode(
235         V2::COLUMN_ETS_TAG, columnId, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
236 }
237 
CreateButtonNode()238 RefPtr<FrameNode> TextPickerModelNG::CreateButtonNode()
239 {
240     auto buttonId = ElementRegister::GetInstance()->MakeUniqueId();
241     return FrameNode::GetOrCreateFrameNode(
242         V2::BUTTON_ETS_TAG, buttonId, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
243 }
244 
CreateFrameNode(int32_t nodeId)245 RefPtr<FrameNode> TextPickerModelNG::CreateFrameNode(int32_t nodeId)
246 {
247     auto textPickerNode = FrameNode::GetOrCreateFrameNode(
248         V2::TEXT_PICKER_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<TextPickerPattern>(); });
249     auto pipeline = PipelineBase::GetCurrentContextSafely();
250     CHECK_NULL_RETURN(pipeline, textPickerNode);
251     auto pickerTheme = pipeline->GetTheme<PickerTheme>(textPickerNode->GetThemeScopeId());
252     CHECK_NULL_RETURN(pickerTheme, textPickerNode);
253     std::lock_guard<std::shared_mutex> lock(showCountMutex_);
254     showCount_ = BUFFER_NODE_NUMBER + pickerTheme->GetShowOptionCount();
255     SetDefaultAttributes(textPickerNode, pickerTheme);
256     return textPickerNode;
257 }
258 
SetSelected(uint32_t value)259 void TextPickerModelNG::SetSelected(uint32_t value)
260 {
261     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
262     CHECK_NULL_VOID(frameNode);
263     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
264     textPickerPattern->SetSelected(value);
265     std::vector<uint32_t> values;
266     values.emplace_back(value);
267     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Selected, value);
268     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedIndex, values);
269 }
270 
SetColumnWidths(const std::vector<Dimension> & widths)271 void TextPickerModelNG::SetColumnWidths(const std::vector<Dimension>& widths)
272 {
273     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
274     CHECK_NULL_VOID(frameNode);
275     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
276     textPickerPattern->SetColumnWidths(widths);
277 }
278 
SetColumnWidths(FrameNode * frameNode,const std::vector<Dimension> & widths)279 void TextPickerModelNG::SetColumnWidths(FrameNode* frameNode, const std::vector<Dimension>& widths)
280 {
281     CHECK_NULL_VOID(frameNode);
282     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
283     textPickerPattern->SetColumnWidths(widths);
284 }
285 
GetColumnWidths(FrameNode * frameNode)286 std::vector<Dimension> TextPickerModelNG::GetColumnWidths(FrameNode* frameNode)
287 {
288     std::vector<Dimension> columnWidths;
289     CHECK_NULL_RETURN(frameNode, columnWidths);
290     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
291     columnWidths = textPickerPattern->GetColumnWidths();
292     return columnWidths;
293 }
294 
SetRange(const std::vector<NG::RangeContent> & value)295 void TextPickerModelNG::SetRange(const std::vector<NG::RangeContent>& value)
296 {
297     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
298     CHECK_NULL_VOID(frameNode);
299     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
300     textPickerPattern->SetRange(value);
301 }
302 
SetDefaultPickerItemHeight(const Dimension & value)303 void TextPickerModelNG::SetDefaultPickerItemHeight(const Dimension& value)
304 {
305     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultPickerItemHeight, value);
306 }
307 
SetGradientHeight(const Dimension & value)308 void TextPickerModelNG::SetGradientHeight(const Dimension& value)
309 {
310     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
311     CHECK_NULL_VOID(frameNode);
312     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
313     CHECK_NULL_VOID(textPickerPattern);
314     textPickerPattern->SetGradientHeight(value);
315     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, GradientHeight, value);
316 }
317 
SetCanLoop(const bool value)318 void TextPickerModelNG::SetCanLoop(const bool value)
319 {
320     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
321     CHECK_NULL_VOID(frameNode);
322     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
323     CHECK_NULL_VOID(textPickerPattern);
324     textPickerPattern->SetCanLoop(value);
325     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, CanLoop, value);
326 }
327 
SetBackgroundColor(const Color & color)328 void TextPickerModelNG::SetBackgroundColor(const Color& color)
329 {
330     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
331     CHECK_NULL_VOID(frameNode);
332     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
333     CHECK_NULL_VOID(textPickerPattern);
334     textPickerPattern->SetBackgroundColor(color);
335 }
336 
SetDisableTextStyleAnimation(const bool value)337 void TextPickerModelNG::SetDisableTextStyleAnimation(const bool value)
338 {
339     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
340     CHECK_NULL_VOID(frameNode);
341     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
342     CHECK_NULL_VOID(textPickerPattern);
343     textPickerPattern->SetDisableTextStyleAnimation(value);
344     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisableTextStyleAnimation, value);
345 }
346 
SetDisappearTextStyle(const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)347 void TextPickerModelNG::SetDisappearTextStyle(const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
348 {
349     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
350     CHECK_NULL_VOID(frameNode);
351     CHECK_NULL_VOID(pickerTheme);
352     auto disappearStyle = pickerTheme->GetDisappearOptionStyle();
353     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
354         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontSize,
355             ConvertFontScaleValue(value.fontSize.value()));
356     } else {
357         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontSize,
358             ConvertFontScaleValue(disappearStyle.GetFontSize()));
359     }
360     if (value.textColor.has_value()) {
361         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearColor, value.textColor.value());
362     } else {
363         ResetTextPickerTextStyleColor(frameNode, &TextPickerLayoutProperty::GetDisappearTextStyle);
364     }
365     ACE_UPDATE_LAYOUT_PROPERTY(
366         TextPickerLayoutProperty, DisappearWeight, value.fontWeight.value_or(disappearStyle.GetFontWeight()));
367     ACE_UPDATE_LAYOUT_PROPERTY(
368         TextPickerLayoutProperty, DisappearFontFamily, value.fontFamily.value_or(disappearStyle.GetFontFamilies()));
369     ACE_UPDATE_LAYOUT_PROPERTY(
370         TextPickerLayoutProperty, DisappearFontStyle, value.fontStyle.value_or(disappearStyle.GetFontStyle()));
371 }
372 
SetNormalTextStyle(const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)373 void TextPickerModelNG::SetNormalTextStyle(const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
374 {
375     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
376     CHECK_NULL_VOID(frameNode);
377     CHECK_NULL_VOID(pickerTheme);
378     auto normalStyle = pickerTheme->GetOptionStyle(false, false);
379     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
380         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize,
381             ConvertFontScaleValue(value.fontSize.value()));
382     } else {
383         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize,
384             ConvertFontScaleValue(normalStyle.GetFontSize()));
385     }
386     if (value.textColor.has_value()) {
387         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Color, value.textColor.value());
388     } else {
389         ResetTextPickerTextStyleColor(frameNode, &TextPickerLayoutProperty::GetTextStyle);
390     }
391     ACE_UPDATE_LAYOUT_PROPERTY(
392         TextPickerLayoutProperty, Weight, value.fontWeight.value_or(normalStyle.GetFontWeight()));
393     ACE_UPDATE_LAYOUT_PROPERTY(
394         TextPickerLayoutProperty, FontFamily, value.fontFamily.value_or(normalStyle.GetFontFamilies()));
395     ACE_UPDATE_LAYOUT_PROPERTY(
396         TextPickerLayoutProperty, FontStyle, value.fontStyle.value_or(normalStyle.GetFontStyle()));
397 }
398 
SetSelectedTextStyle(const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)399 void TextPickerModelNG::SetSelectedTextStyle(const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
400 {
401     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
402     CHECK_NULL_VOID(frameNode);
403     CHECK_NULL_VOID(pickerTheme);
404     auto selectedStyle = pickerTheme->GetOptionStyle(true, false);
405     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
406         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontSize,
407             ConvertFontScaleValue(value.fontSize.value()));
408     } else {
409         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontSize,
410             ConvertFontScaleValue(selectedStyle.GetFontSize()));
411     }
412     if (value.textColor.has_value()) {
413         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedColor, value.textColor.value());
414     } else {
415         ResetTextPickerTextStyleColor(frameNode, &TextPickerLayoutProperty::GetSelectedTextStyle);
416     }
417     ACE_UPDATE_LAYOUT_PROPERTY(
418         TextPickerLayoutProperty, SelectedWeight, value.fontWeight.value_or(selectedStyle.GetFontWeight()));
419     ACE_UPDATE_LAYOUT_PROPERTY(
420         TextPickerLayoutProperty, SelectedFontFamily, value.fontFamily.value_or(selectedStyle.GetFontFamilies()));
421     ACE_UPDATE_LAYOUT_PROPERTY(
422         TextPickerLayoutProperty, SelectedFontStyle, value.fontStyle.value_or(selectedStyle.GetFontStyle()));
423 }
424 
SetDefaultTextStyle(const RefPtr<TextTheme> & textTheme,const NG::PickerTextStyle & value)425 void TextPickerModelNG::SetDefaultTextStyle(const RefPtr<TextTheme>& textTheme, const NG::PickerTextStyle& value)
426 {
427     CHECK_NULL_VOID(textTheme);
428     auto textStyle = textTheme->GetTextStyle();
429 
430     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
431         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultFontSize,
432             ConvertFontScaleValue(value.fontSize.value()));
433     } else {
434         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultFontSize,
435             ConvertFontScaleValue(textStyle.GetFontSize()));
436     }
437     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultColor,
438         value.textColor.value_or(textStyle.GetTextColor()));
439     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultWeight,
440         value.fontWeight.value_or(textStyle.GetFontWeight()));
441     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultFontFamily,
442         value.fontFamily.value_or(textStyle.GetFontFamilies()));
443     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultFontStyle,
444         value.fontStyle.value_or(textStyle.GetFontStyle()));
445     if (value.minFontSize.has_value() && value.minFontSize->IsValid()) {
446         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultMinFontSize,
447             ConvertFontScaleValue(value.minFontSize.value()));
448     } else {
449         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultMinFontSize, Dimension());
450     }
451     if (value.maxFontSize.has_value() && value.maxFontSize->IsValid()) {
452         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultMaxFontSize,
453             ConvertFontScaleValue(value.maxFontSize.value()));
454     } else {
455         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultMaxFontSize, Dimension());
456     }
457     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultTextOverflow,
458         value.textOverflow.value_or(textStyle.GetTextOverflow()));
459 }
460 
HasUserDefinedDisappearFontFamily(bool isUserDefined)461 void TextPickerModelNG::HasUserDefinedDisappearFontFamily(bool isUserDefined)
462 {
463     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
464     CHECK_NULL_VOID(frameNode);
465     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
466     CHECK_NULL_VOID(textPickerPattern);
467     textPickerPattern->HasUserDefinedDisappearFontFamily(isUserDefined);
468 }
469 
HasUserDefinedNormalFontFamily(bool isUserDefined)470 void TextPickerModelNG::HasUserDefinedNormalFontFamily(bool isUserDefined)
471 {
472     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
473     CHECK_NULL_VOID(frameNode);
474     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
475     CHECK_NULL_VOID(textPickerPattern);
476     textPickerPattern->HasUserDefinedNormalFontFamily(isUserDefined);
477 }
478 
HasUserDefinedSelectedFontFamily(bool isUserDefined)479 void TextPickerModelNG::HasUserDefinedSelectedFontFamily(bool isUserDefined)
480 {
481     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
482     CHECK_NULL_VOID(frameNode);
483     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
484     CHECK_NULL_VOID(textPickerPattern);
485     textPickerPattern->HasUserDefinedSelectedFontFamily(isUserDefined);
486 }
487 
SetOnCascadeChange(TextCascadeChangeEvent && onChange)488 void TextPickerModelNG::SetOnCascadeChange(TextCascadeChangeEvent&& onChange)
489 {
490     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
491     CHECK_NULL_VOID(frameNode);
492     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
493     CHECK_NULL_VOID(eventHub);
494     eventHub->SetOnChange(std::move(onChange));
495 }
496 
SetOnScrollStop(TextCascadeChangeEvent && onScrollStop)497 void TextPickerModelNG::SetOnScrollStop(TextCascadeChangeEvent&& onScrollStop)
498 {
499     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
500     CHECK_NULL_VOID(frameNode);
501     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
502     CHECK_NULL_VOID(eventHub);
503     eventHub->SetOnScrollStop(std::move(onScrollStop));
504 }
505 
SetOnEnterSelectedArea(TextCascadeChangeEvent && onEnterSelectedArea)506 void TextPickerModelNG::SetOnEnterSelectedArea(TextCascadeChangeEvent&& onEnterSelectedArea)
507 {
508     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
509     CHECK_NULL_VOID(frameNode);
510     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
511     CHECK_NULL_VOID(eventHub);
512     eventHub->SetOnEnterSelectedArea(std::move(onEnterSelectedArea));
513 }
514 
SetValue(const std::string & value)515 void TextPickerModelNG::SetValue(const std::string& value)
516 {
517     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Value, value);
518 }
519 
getDisappearTextStyle(FrameNode * frameNode)520 PickerTextStyle TextPickerModelNG::getDisappearTextStyle(FrameNode* frameNode)
521 {
522     PickerTextStyle pickerTextStyle;
523     CHECK_NULL_RETURN(frameNode, pickerTextStyle);
524     auto context = frameNode->GetContext();
525     CHECK_NULL_RETURN(context, pickerTextStyle);
526     auto theme = context->GetTheme<PickerTheme>();
527     CHECK_NULL_RETURN(theme, pickerTextStyle);
528     auto style = theme->GetDisappearOptionStyle();
529     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
530         TextPickerLayoutProperty, DisappearFontSize, pickerTextStyle.fontSize, frameNode, style.GetFontSize());
531     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
532         TextPickerLayoutProperty, DisappearColor, pickerTextStyle.textColor, frameNode, style.GetTextColor());
533     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
534         TextPickerLayoutProperty, DisappearWeight, pickerTextStyle.fontWeight, frameNode, style.GetFontWeight());
535     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(TextPickerLayoutProperty, DisappearFontFamily,
536         pickerTextStyle.fontFamily, frameNode, style.GetFontFamilies());
537     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(TextPickerLayoutProperty, DisappearFontStyle,
538         pickerTextStyle.fontStyle, frameNode, style.GetFontStyle());
539     return pickerTextStyle;
540 }
541 
getNormalTextStyle(FrameNode * frameNode)542 PickerTextStyle TextPickerModelNG::getNormalTextStyle(FrameNode* frameNode)
543 {
544     PickerTextStyle pickerTextStyle;
545     CHECK_NULL_RETURN(frameNode, pickerTextStyle);
546     auto context = frameNode->GetContext();
547     CHECK_NULL_RETURN(context, pickerTextStyle);
548     auto theme = context->GetTheme<PickerTheme>();
549     CHECK_NULL_RETURN(theme, pickerTextStyle);
550     auto style = theme->GetOptionStyle(false, false);
551     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
552         TextPickerLayoutProperty, FontSize, pickerTextStyle.fontSize, frameNode, style.GetFontSize());
553     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
554         TextPickerLayoutProperty, Color, pickerTextStyle.textColor, frameNode, style.GetTextColor());
555     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
556         TextPickerLayoutProperty, Weight, pickerTextStyle.fontWeight, frameNode, style.GetFontWeight());
557     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(TextPickerLayoutProperty, FontFamily,
558         pickerTextStyle.fontFamily, frameNode, style.GetFontFamilies());
559     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(TextPickerLayoutProperty, FontStyle,
560         pickerTextStyle.fontStyle, frameNode, style.GetFontStyle());
561     return pickerTextStyle;
562 }
563 
getSelectedTextStyle(FrameNode * frameNode)564 PickerTextStyle TextPickerModelNG::getSelectedTextStyle(FrameNode* frameNode)
565 {
566     PickerTextStyle pickerTextStyle;
567     CHECK_NULL_RETURN(frameNode, pickerTextStyle);
568     auto context = frameNode->GetContext();
569     CHECK_NULL_RETURN(context, pickerTextStyle);
570     auto theme = context->GetTheme<PickerTheme>();
571     CHECK_NULL_RETURN(theme, pickerTextStyle);
572     auto style = theme->GetOptionStyle(true, false);
573     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
574         TextPickerLayoutProperty, SelectedFontSize, pickerTextStyle.fontSize, frameNode, style.GetFontSize());
575     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
576         TextPickerLayoutProperty, SelectedColor, pickerTextStyle.textColor, frameNode, style.GetTextColor());
577     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(
578         TextPickerLayoutProperty, SelectedWeight, pickerTextStyle.fontWeight, frameNode, style.GetFontWeight());
579     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(TextPickerLayoutProperty, SelectedFontFamily,
580         pickerTextStyle.fontFamily, frameNode, style.GetFontFamilies());
581     ACE_GET_NODE_LAYOUT_PROPERTY_WITH_DEFAULT_VALUE(TextPickerLayoutProperty, SelectedFontStyle,
582         pickerTextStyle.fontStyle, frameNode, style.GetFontStyle());
583     return pickerTextStyle;
584 }
585 
getTextPickerSelectedIndex(FrameNode * frameNode)586 int32_t TextPickerModelNG::getTextPickerSelectedIndex(FrameNode* frameNode)
587 {
588     CHECK_NULL_RETURN(frameNode, 0);
589     return frameNode->GetLayoutProperty<TextPickerLayoutProperty>()->GetSelectedValue(0);
590 }
591 
MultiInit(const RefPtr<PickerTheme> pickerTheme)592 void TextPickerModelNG::MultiInit(const RefPtr<PickerTheme> pickerTheme)
593 {
594     auto* stack = ViewStackProcessor::GetInstance();
595     auto nodeId = stack->ClaimNodeId();
596     auto textPickerNode = FrameNode::GetOrCreateFrameNode(
597         V2::TEXT_PICKER_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<TextPickerPattern>(); });
598     auto textPickerPattern = textPickerNode->GetPattern<TextPickerPattern>();
599 
600     CHECK_NULL_VOID(pickerTheme);
601     std::lock_guard<std::shared_mutex> lock(showCountMutex_);
602     showCount_ = pickerTheme->GetShowOptionCount() + BUFFER_NODE_NUMBER;
603     stack->Push(textPickerNode);
604 
605     if (pickerTheme->IsCircleDial()) {
606         auto renderContext = textPickerNode->GetRenderContext();
607         renderContext->UpdateBackgroundColor(pickerTheme->GetBackgroundColor());
608     }
609 }
610 
SetIsCascade(bool isCascade)611 void TextPickerModelNG::SetIsCascade(bool isCascade)
612 {
613     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
614     CHECK_NULL_VOID(frameNode);
615     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
616     CHECK_NULL_VOID(textPickerPattern);
617     textPickerPattern->SetIsCascade(isCascade);
618 }
619 
SetHasSelectAttr(bool value)620 void TextPickerModelNG::SetHasSelectAttr(bool value)
621 {
622     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
623     CHECK_NULL_VOID(frameNode);
624     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
625     CHECK_NULL_VOID(textPickerPattern);
626     textPickerPattern->SetHasSelectAttr(value);
627 }
628 
SetUnCascadeColumns(const std::vector<NG::TextCascadePickerOptions> & options)629 void TextPickerModelNG::SetUnCascadeColumns(const std::vector<NG::TextCascadePickerOptions>& options)
630 {
631     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
632     CHECK_NULL_VOID(frameNode);
633     if (frameNode->GetChildren().empty()) {
634         std::lock_guard<std::shared_mutex> lock(showCountMutex_);
635         for (uint32_t i = 0; i < options.size(); i++) {
636             auto columnNode = CreateColumnNode(NG::TEXT, showCount_);
637             auto stackNode = CreateStackNode();
638             auto buttonNode = CreateButtonNode();
639             auto columnBlendNode = CreateColumnNode();
640             buttonNode->MountToParent(stackNode);
641             columnNode->MountToParent(columnBlendNode);
642             columnBlendNode->MountToParent(stackNode);
643             columnNode->MarkModifyDone();
644             columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
645             auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
646             layoutProperty->UpdateAlignment(Alignment::CENTER);
647             stackNode->MountToParent(AceType::Claim(frameNode));
648         }
649     }
650 
651     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
652     CHECK_NULL_VOID(textPickerPattern);
653     textPickerPattern->SetCascadeOptions(options, options);
654 }
655 
SetCascadeColumns(const std::vector<NG::TextCascadePickerOptions> & options)656 void TextPickerModelNG::SetCascadeColumns(const std::vector<NG::TextCascadePickerOptions>& options)
657 {
658     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
659     CHECK_NULL_VOID(frameNode);
660     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
661     CHECK_NULL_VOID(textPickerPattern);
662     std::vector<NG::TextCascadePickerOptions> reOptions;
663     // Caculate max depth
664     size_t columnCount = options.empty()? 0 : 1;
665     for (size_t i = 0; i < options.size(); i++) {
666         size_t tmp  = textPickerPattern->ProcessCascadeOptionDepth(options[i]);
667         if (tmp > columnCount) {
668             columnCount = tmp;
669         }
670     }
671 
672     // Create Node
673     if (frameNode->GetChildren().empty()) {
674         std::lock_guard<std::shared_mutex> lock(showCountMutex_);
675         for (size_t i = 0; i < columnCount; i++) {
676             auto columnNode = CreateColumnNode(NG::TEXT, showCount_);
677             auto stackNode = CreateStackNode();
678             auto buttonNode = CreateButtonNode();
679             auto columnBlendNode = CreateColumnNode();
680             buttonNode->MountToParent(stackNode);
681             columnNode->MountToParent(columnBlendNode);
682             columnBlendNode->MountToParent(stackNode);
683             columnNode->MarkModifyDone();
684             columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
685             auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
686             layoutProperty->UpdateAlignment(Alignment::CENTER);
687             stackNode->MountToParent(AceType::Claim<NG::FrameNode>(frameNode));
688         }
689     }
690 
691     textPickerPattern->ProcessCascadeOptions(options, reOptions, 0);
692     if (reOptions.size() < columnCount) {
693         auto differ = columnCount - reOptions.size();
694         for (uint32_t i = 0; i < differ; i++) {
695             NG::TextCascadePickerOptions differOption;
696             memset_s(&differOption, sizeof(differOption), 0, sizeof(differOption));
697             reOptions.emplace_back(differOption);
698         }
699     }
700     textPickerPattern->SetCascadeOptions(options, reOptions);
701 }
702 
SetColumns(const std::vector<NG::TextCascadePickerOptions> & options)703 void TextPickerModelNG::SetColumns(const std::vector<NG::TextCascadePickerOptions>& options)
704 {
705     if (!IsCascade()) {
706         SetUnCascadeColumns(options);
707     } else {
708         SetCascadeColumns(options);
709     }
710 }
711 
IsSingle()712 bool TextPickerModelNG::IsSingle()
713 {
714     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
715     CHECK_NULL_RETURN(frameNode, false);
716     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
717     CHECK_NULL_RETURN(textPickerPattern, false);
718     return textPickerPattern->GetRange().size() > 0;
719 }
720 
IsSingle(FrameNode * frameNode)721 bool TextPickerModelNG::IsSingle(FrameNode* frameNode)
722 {
723     CHECK_NULL_RETURN(frameNode, false);
724     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
725     CHECK_NULL_RETURN(textPickerPattern, false);
726     return textPickerPattern->GetRange().size() > 0;
727 }
728 
GetSingleRange(std::vector<NG::RangeContent> & rangeValue)729 bool TextPickerModelNG::GetSingleRange(std::vector<NG::RangeContent>& rangeValue)
730 {
731     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
732     CHECK_NULL_RETURN(frameNode, false);
733     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
734     CHECK_NULL_RETURN(textPickerPattern, false);
735     rangeValue = textPickerPattern->GetRange();
736     return true;
737 }
738 
GetSingleRange(FrameNode * frameNode,std::vector<NG::RangeContent> & rangeValue)739 bool TextPickerModelNG::GetSingleRange(FrameNode* frameNode, std::vector<NG::RangeContent>& rangeValue)
740 {
741     rangeValue.clear();
742     CHECK_NULL_RETURN(frameNode, false);
743     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
744     CHECK_NULL_RETURN(textPickerPattern, false);
745     for (auto& item : textPickerPattern->GetRange()) {
746         rangeValue.emplace_back(std::move(item));
747     }
748     return true;
749 }
750 
IsCascade(FrameNode * frameNode)751 bool TextPickerModelNG::IsCascade(FrameNode* frameNode)
752 {
753     CHECK_NULL_RETURN(frameNode, false);
754     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
755     CHECK_NULL_RETURN(textPickerPattern, false);
756     return textPickerPattern->GetIsCascade();
757 }
758 
GetMultiOptions(std::vector<NG::TextCascadePickerOptions> & options)759 bool TextPickerModelNG::GetMultiOptions(std::vector<NG::TextCascadePickerOptions>& options)
760 {
761     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
762     CHECK_NULL_RETURN(frameNode, false);
763     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
764     CHECK_NULL_RETURN(textPickerPattern, false);
765     options = textPickerPattern->GetMultiOptions();
766     return true;
767 }
768 
GetMultiOptions(FrameNode * frameNode,std::vector<NG::TextCascadePickerOptions> & options)769 bool TextPickerModelNG::GetMultiOptions(FrameNode* frameNode, std::vector<NG::TextCascadePickerOptions>& options)
770 {
771     options.clear();
772     CHECK_NULL_RETURN(frameNode, false);
773     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
774     CHECK_NULL_RETURN(textPickerPattern, false);
775     for (auto& item : textPickerPattern->GetMultiOptions()) {
776         options.emplace_back(std::move(item));
777     }
778     return true;
779 }
780 
GetMaxCount(FrameNode * frameNode)781 uint32_t TextPickerModelNG::GetMaxCount(FrameNode* frameNode)
782 {
783     return 1;
784 }
785 
SetValues(const std::vector<std::string> & values)786 void TextPickerModelNG::SetValues(const std::vector<std::string>& values)
787 {
788     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
789     CHECK_NULL_VOID(frameNode);
790     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
791     CHECK_NULL_VOID(textPickerPattern);
792     textPickerPattern->SetValues(values);
793     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Values, values);
794 }
795 
SetSelecteds(const std::vector<uint32_t> & values)796 void TextPickerModelNG::SetSelecteds(const std::vector<uint32_t>& values)
797 {
798     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
799     CHECK_NULL_VOID(frameNode);
800     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
801     CHECK_NULL_VOID(textPickerPattern);
802     textPickerPattern->SetSelecteds(values);
803     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Selecteds, values);
804     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedIndex, values);
805 }
806 
SetOnValueChangeEvent(TextCascadeValueChangeEvent && onValueChangeEvent)807 void TextPickerModelNG::SetOnValueChangeEvent(TextCascadeValueChangeEvent&& onValueChangeEvent)
808 {
809     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
810     CHECK_NULL_VOID(frameNode);
811     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
812     CHECK_NULL_VOID(eventHub);
813     eventHub->SetOnValueChangeEvent(std::move(onValueChangeEvent));
814 }
815 
SetOnSelectedChangeEvent(TextCascadeSelectedChangeEvent && onSelectedChangeEvent)816 void TextPickerModelNG::SetOnSelectedChangeEvent(TextCascadeSelectedChangeEvent&& onSelectedChangeEvent)
817 {
818     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
819     CHECK_NULL_VOID(frameNode);
820     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
821     CHECK_NULL_VOID(eventHub);
822     eventHub->SetOnSelectedChangeEvent(std::move(onSelectedChangeEvent));
823 }
824 
CreateObject()825 RefPtr<AceType> TextPickerDialogModelNG::CreateObject()
826 {
827     return nullptr;
828 }
829 
SetTextPickerDialogShow(RefPtr<AceType> & PickerText,NG::TextPickerSettingData & settingData,std::function<void ()> && onCancel,std::function<void (const std::string &)> && onAccept,std::function<void (const std::string &)> && onChange,std::function<void (const std::string &)> && onScrollStop,std::function<void (const std::string &)> && onEnterSelectedArea,TextPickerDialog & textPickerDialog,TextPickerDialogEvent & textPickerDialogEvent,const std::vector<ButtonInfo> & buttonInfos)830 void TextPickerDialogModelNG::SetTextPickerDialogShow(RefPtr<AceType>& PickerText,
831     NG::TextPickerSettingData& settingData, std::function<void()>&& onCancel,
832     std::function<void(const std::string&)>&& onAccept, std::function<void(const std::string&)>&& onChange,
833     std::function<void(const std::string&)>&& onScrollStop,
834     std::function<void(const std::string&)>&& onEnterSelectedArea, TextPickerDialog& textPickerDialog,
835     TextPickerDialogEvent& textPickerDialogEvent, const std::vector<ButtonInfo>& buttonInfos)
836 {
837     auto container = Container::Current();
838     if (!container) {
839         return;
840     }
841     auto pipelineContext = AccessibilityManager::DynamicCast<NG::PipelineContext>(container->GetPipelineContext());
842     if (!pipelineContext) {
843         return;
844     }
845     auto executor = pipelineContext->GetTaskExecutor();
846     if (!executor) {
847         return;
848     }
849     auto pipeline = PipelineBase::GetCurrentContext();
850     CHECK_NULL_VOID(pipeline);
851     auto theme = pipeline->GetTheme<DialogTheme>();
852     CHECK_NULL_VOID(theme);
853 
854     std::map<std::string, NG::DialogTextEvent> dialogEvent;
855     std::map<std::string, NG::DialogCancelEvent> dialogLifeCycleEvent;
856     std::map<std::string, NG::DialogGestureEvent> dialogCancelEvent;
857     dialogEvent["acceptId"] = onAccept;
858     dialogEvent["changeId"] = onChange;
859     dialogEvent["scrollStopId"] = onScrollStop;
860     dialogEvent["enterSelectedAreaId"] = onEnterSelectedArea;
861     auto func = [onCancel](const GestureEvent& /* info */) {
862         if (onCancel) {
863             onCancel();
864         }
865     };
866     dialogCancelEvent["cancelId"] = func;
867     dialogLifeCycleEvent["didAppearId"] = textPickerDialogEvent.onDidAppear;
868     dialogLifeCycleEvent["didDisappearId"] = textPickerDialogEvent.onDidDisappear;
869     dialogLifeCycleEvent["willAppearId"] = textPickerDialogEvent.onWillAppear;
870     dialogLifeCycleEvent["willDisappearId"] = textPickerDialogEvent.onWillDisappear;
871     DialogProperties properties;
872     SetDialogProperties(properties, textPickerDialog, theme);
873 
874     auto context = AccessibilityManager::DynamicCast<NG::PipelineContext>(pipelineContext);
875     auto overlayManager = context ? context->GetOverlayManager() : nullptr;
876     executor->PostTask(
877         [properties, settingData, dialogEvent, dialogCancelEvent, dialogLifeCycleEvent, buttonInfos,
878             weak = WeakPtr<NG::OverlayManager>(overlayManager)] {
879             auto overlayManager = weak.Upgrade();
880             CHECK_NULL_VOID(overlayManager);
881             overlayManager->ShowTextDialog(
882                 properties, settingData, dialogEvent, dialogCancelEvent, dialogLifeCycleEvent, buttonInfos);
883         },
884         TaskExecutor::TaskType::UI, "ArkUITextPickerShowTextDialog",
885         TaskExecutor::GetPriorityTypeWithCheck(PriorityType::VIP));
886 }
887 
SetCanLoop(FrameNode * frameNode,const bool value)888 void TextPickerModelNG::SetCanLoop(FrameNode* frameNode, const bool value)
889 {
890     CHECK_NULL_VOID(frameNode);
891     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
892     CHECK_NULL_VOID(textPickerPattern);
893     textPickerPattern->SetCanLoop(value);
894     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, CanLoop, value, frameNode);
895 }
896 
GetCanLoop(FrameNode * frameNode)897 int32_t TextPickerModelNG::GetCanLoop(FrameNode* frameNode)
898 {
899     CHECK_NULL_RETURN(frameNode, 1);
900     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
901     CHECK_NULL_RETURN(textPickerPattern, 1);
902     return textPickerPattern->GetCanLoop();
903 }
904 
SetDigitalCrownSensitivity(int32_t crownSensitivity)905 void TextPickerModelNG::SetDigitalCrownSensitivity(int32_t crownSensitivity)
906 {
907     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
908     CHECK_NULL_VOID(frameNode);
909     SetDigitalCrownSensitivity(frameNode, crownSensitivity);
910 }
911 
SetDigitalCrownSensitivity(FrameNode * frameNode,int32_t crownSensitivity)912 void TextPickerModelNG::SetDigitalCrownSensitivity(FrameNode* frameNode, int32_t crownSensitivity)
913 {
914     if (crownSensitivity < CROWN_SENSITIVITY_MIN || crownSensitivity > CROWN_SENSITIVITY_MAX) {
915         return;
916     }
917     CHECK_NULL_VOID(frameNode);
918     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
919     CHECK_NULL_VOID(textPickerPattern);
920     textPickerPattern->SetDigitalCrownSensitivity(crownSensitivity);
921     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DigitalCrownSensitivity, crownSensitivity, frameNode);
922 }
923 
SetSelecteds(FrameNode * frameNode,const std::vector<uint32_t> & values)924 void TextPickerModelNG::SetSelecteds(FrameNode* frameNode, const std::vector<uint32_t>& values)
925 {
926     CHECK_NULL_VOID(frameNode);
927     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
928     CHECK_NULL_VOID(textPickerPattern);
929     textPickerPattern->SetSelecteds(values);
930     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Selecteds, values, frameNode);
931     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedIndex, values, frameNode);
932 }
SetSelected(FrameNode * frameNode,uint32_t value)933 void TextPickerModelNG::SetSelected(FrameNode* frameNode, uint32_t value)
934 {
935     CHECK_NULL_VOID(frameNode);
936     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
937     textPickerPattern->SetSelected(value);
938     std::vector<uint32_t> values;
939     values.emplace_back(value);
940     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Selected, value, frameNode);
941     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedIndex, values, frameNode);
942 }
SetHasSelectAttr(FrameNode * frameNode,bool value)943 void TextPickerModelNG::SetHasSelectAttr(FrameNode* frameNode, bool value)
944 {
945     CHECK_NULL_VOID(frameNode);
946     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
947     CHECK_NULL_VOID(textPickerPattern);
948     textPickerPattern->SetHasSelectAttr(value);
949 }
950 
SetIsCascade(FrameNode * frameNode,bool isCascade)951 void TextPickerModelNG::SetIsCascade(FrameNode* frameNode, bool isCascade)
952 {
953     CHECK_NULL_VOID(frameNode);
954     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
955     CHECK_NULL_VOID(textPickerPattern);
956     textPickerPattern->SetIsCascade(isCascade);
957 }
958 
SetColumnKind(FrameNode * frameNode,uint32_t columnKind)959 void TextPickerModelNG::SetColumnKind(FrameNode* frameNode, uint32_t columnKind)
960 {
961     CHECK_NULL_VOID(frameNode);
962     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
963     CHECK_NULL_VOID(textPickerPattern);
964     columnKind_ = columnKind;
965     textPickerPattern->SetColumnsKind(columnKind);
966 }
967 
SetNormalTextStyle(FrameNode * frameNode,const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)968 void TextPickerModelNG::SetNormalTextStyle(
969     FrameNode* frameNode, const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
970 {
971     CHECK_NULL_VOID(frameNode);
972     CHECK_NULL_VOID(pickerTheme);
973     auto normalStyle = pickerTheme->GetOptionStyle(false, false);
974     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
975         ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize,
976             ConvertFontScaleValue(value.fontSize.value()), frameNode);
977     } else {
978         ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize,
979             ConvertFontScaleValue(normalStyle.GetFontSize()), frameNode);
980     }
981     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
982         TextPickerLayoutProperty, Color, value.textColor.value_or(normalStyle.GetTextColor()), frameNode);
983     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
984         TextPickerLayoutProperty, Weight, value.fontWeight.value_or(normalStyle.GetFontWeight()), frameNode);
985     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
986         TextPickerLayoutProperty, FontFamily, value.fontFamily.value_or(normalStyle.GetFontFamilies()), frameNode);
987     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
988         TextPickerLayoutProperty, FontStyle, value.fontStyle.value_or(normalStyle.GetFontStyle()), frameNode);
989 }
990 
SetSelectedTextStyle(FrameNode * frameNode,const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)991 void TextPickerModelNG::SetSelectedTextStyle(
992     FrameNode* frameNode, const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
993 {
994     CHECK_NULL_VOID(frameNode);
995     CHECK_NULL_VOID(pickerTheme);
996     auto selectedStyle = pickerTheme->GetOptionStyle(true, false);
997     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
998         ACE_UPDATE_NODE_LAYOUT_PROPERTY(
999             TextPickerLayoutProperty, SelectedFontSize,
1000             ConvertFontScaleValue(value.fontSize.value()), frameNode);
1001     } else {
1002         ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1003             TextPickerLayoutProperty, SelectedFontSize,
1004             ConvertFontScaleValue(selectedStyle.GetFontSize()), frameNode);
1005     }
1006     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1007         TextPickerLayoutProperty, SelectedColor,
1008         value.textColor.value_or(selectedStyle.GetTextColor()), frameNode);
1009 
1010     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1011         TextPickerLayoutProperty, SelectedWeight,
1012         value.fontWeight.value_or(selectedStyle.GetFontWeight()), frameNode);
1013     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1014         TextPickerLayoutProperty, SelectedFontFamily,
1015         value.fontFamily.value_or(selectedStyle.GetFontFamilies()), frameNode);
1016     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1017         TextPickerLayoutProperty, SelectedFontStyle, value.fontStyle.value_or(selectedStyle.GetFontStyle()), frameNode);
1018 }
1019 
SetDisappearTextStyle(FrameNode * frameNode,const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)1020 void TextPickerModelNG::SetDisappearTextStyle(
1021     FrameNode* frameNode, const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
1022 {
1023     CHECK_NULL_VOID(frameNode);
1024     CHECK_NULL_VOID(pickerTheme);
1025     auto disappearStyle = pickerTheme->GetDisappearOptionStyle();
1026     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
1027         ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1028             TextPickerLayoutProperty, DisappearFontSize,
1029             ConvertFontScaleValue(value.fontSize.value()), frameNode);
1030     } else {
1031         ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1032             TextPickerLayoutProperty, DisappearFontSize,
1033             ConvertFontScaleValue(disappearStyle.GetFontSize()), frameNode);
1034     }
1035     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1036         TextPickerLayoutProperty, DisappearColor, value.textColor.value_or(disappearStyle.GetTextColor()), frameNode);
1037     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1038         TextPickerLayoutProperty, DisappearWeight,
1039         value.fontWeight.value_or(disappearStyle.GetFontWeight()), frameNode);
1040     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1041         TextPickerLayoutProperty, DisappearFontFamily,
1042         value.fontFamily.value_or(disappearStyle.GetFontFamilies()), frameNode);
1043     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1044         TextPickerLayoutProperty, DisappearFontStyle,
1045         value.fontStyle.value_or(disappearStyle.GetFontStyle()), frameNode);
1046 }
1047 
SetDefaultPickerItemHeight(FrameNode * frameNode,const Dimension & value)1048 void TextPickerModelNG::SetDefaultPickerItemHeight(FrameNode* frameNode, const Dimension& value)
1049 {
1050     CHECK_NULL_VOID(frameNode);
1051     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultPickerItemHeight, value, frameNode);
1052 }
1053 
GetDefaultPickerItemHeight(FrameNode * frameNode)1054 Dimension TextPickerModelNG::GetDefaultPickerItemHeight(FrameNode* frameNode)
1055 {
1056     Dimension value = Dimension(0.0f);
1057     CHECK_NULL_RETURN(frameNode, value);
1058     auto layoutProperty = frameNode->GetLayoutProperty<TextPickerLayoutProperty>();
1059     CHECK_NULL_RETURN(layoutProperty, value);
1060     return layoutProperty->HasDefaultPickerItemHeight() ? layoutProperty->GetDefaultPickerItemHeightValue() : value;
1061 }
1062 
SetBackgroundColor(FrameNode * frameNode,const Color & color)1063 void TextPickerModelNG::SetBackgroundColor(FrameNode* frameNode, const Color& color)
1064 {
1065     CHECK_NULL_VOID(frameNode);
1066     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1067     CHECK_NULL_VOID(textPickerPattern);
1068     textPickerPattern->SetBackgroundColor(color);
1069 }
1070 
SetRange(FrameNode * frameNode,const std::vector<NG::RangeContent> & value)1071 void TextPickerModelNG::SetRange(FrameNode* frameNode, const std::vector<NG::RangeContent>& value)
1072 {
1073     CHECK_NULL_VOID(frameNode);
1074     if (frameNode->GetChildren().empty()) {
1075         std::lock_guard<std::shared_mutex> lock(showCountMutex_);
1076         RefPtr<FrameNode> columnNode = nullptr;
1077         if (columnKind_ == TEXT) {
1078             columnNode = CreateColumnNode(TEXT, showCount_);
1079         } else if (columnKind_ == MIXTURE) {
1080             columnNode = CreateColumnNode(MIXTURE, showCount_);
1081         }
1082         auto stackNode = CreateStackNode();
1083         auto buttonNode = CreateButtonNode();
1084         auto columnBlendNode = CreateColumnNode();
1085         buttonNode->MountToParent(stackNode);
1086         columnNode->MountToParent(columnBlendNode);
1087         columnBlendNode->MountToParent(stackNode);
1088         columnNode->MarkModifyDone();
1089         columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
1090         auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
1091         layoutProperty->UpdateAlignment(Alignment::CENTER);
1092         stackNode->MountToParent(AceType::Claim(frameNode));
1093     }
1094     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1095     textPickerPattern->SetRange(value);
1096 }
1097 
SetColumns(FrameNode * frameNode,const std::vector<NG::TextCascadePickerOptions> & options)1098 void TextPickerModelNG::SetColumns(FrameNode* frameNode, const std::vector<NG::TextCascadePickerOptions>& options)
1099 {
1100     if (!IsCascade(frameNode)) {
1101         SetUnCascadeColumnsNode(frameNode, options);
1102     } else {
1103         SetCascadeColumnsNode(frameNode, options);
1104     }
1105 }
1106 
SetUnCascadeColumnsNode(FrameNode * frameNode,const std::vector<NG::TextCascadePickerOptions> & options)1107 void TextPickerModelNG::SetUnCascadeColumnsNode(FrameNode* frameNode,
1108     const std::vector<NG::TextCascadePickerOptions>& options)
1109 {
1110     CHECK_NULL_VOID(frameNode);
1111     if (frameNode->GetChildren().empty()) {
1112         std::lock_guard<std::shared_mutex> lock(showCountMutex_);
1113         for (uint32_t i = 0; i < options.size(); i++) {
1114             auto columnNode = CreateColumnNode(TEXT, showCount_);
1115             auto stackNode = CreateStackNode();
1116             auto buttonNode = CreateButtonNode();
1117             auto columnBlendNode = CreateColumnNode();
1118             buttonNode->MountToParent(stackNode);
1119             columnNode->MountToParent(columnBlendNode);
1120             columnBlendNode->MountToParent(stackNode);
1121             columnNode->MarkModifyDone();
1122             columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
1123             auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
1124             layoutProperty->UpdateAlignment(Alignment::CENTER);
1125             stackNode->MountToParent(AceType::Claim(frameNode));
1126         }
1127     }
1128 
1129     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1130     CHECK_NULL_VOID(textPickerPattern);
1131     textPickerPattern->SetCascadeOptions(options, options);
1132 }
1133 
SetCascadeColumnsNode(FrameNode * frameNode,const std::vector<NG::TextCascadePickerOptions> & options)1134 void TextPickerModelNG::SetCascadeColumnsNode(FrameNode* frameNode,
1135     const std::vector<NG::TextCascadePickerOptions>& options)
1136 {
1137     CHECK_NULL_VOID(frameNode);
1138     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1139     CHECK_NULL_VOID(textPickerPattern);
1140     std::vector<NG::TextCascadePickerOptions> reOptions;
1141     // Caculate max depth
1142     size_t columnCount = options.empty()? 0 : 1;
1143     for (size_t i = 0; i < options.size(); i++) {
1144         size_t tmp  = textPickerPattern->ProcessCascadeOptionDepth(options[i]);
1145         if (tmp > columnCount) {
1146             columnCount = tmp;
1147         }
1148     }
1149 
1150     // Create Node
1151     if (frameNode->GetChildren().empty()) {
1152         std::lock_guard<std::shared_mutex> lock(showCountMutex_);
1153         for (size_t i = 0; i < columnCount; i++) {
1154             auto columnNode = CreateColumnNode(NG::TEXT, showCount_);
1155             auto stackNode = CreateStackNode();
1156             auto buttonNode = CreateButtonNode();
1157             auto columnBlendNode = CreateColumnNode();
1158             buttonNode->MountToParent(stackNode);
1159             columnNode->MountToParent(columnBlendNode);
1160             columnBlendNode->MountToParent(stackNode);
1161             columnNode->MarkModifyDone();
1162             columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
1163             auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
1164             layoutProperty->UpdateAlignment(Alignment::CENTER);
1165             stackNode->MountToParent(AceType::Claim(frameNode));
1166         }
1167     }
1168 
1169     textPickerPattern->ProcessCascadeOptions(options, reOptions, 0);
1170     if (reOptions.size() < columnCount) {
1171         auto differ = columnCount - reOptions.size();
1172         for (uint32_t i = 0; i < differ; i++) {
1173             NG::TextCascadePickerOptions differOption;
1174             memset_s(&differOption, sizeof(differOption), 0, sizeof(differOption));
1175             reOptions.emplace_back(differOption);
1176         }
1177     }
1178     textPickerPattern->SetCascadeOptions(options, reOptions);
1179 }
1180 
SetValue(FrameNode * frameNode,const std::string & value)1181 void TextPickerModelNG::SetValue(FrameNode* frameNode, const std::string& value)
1182 {
1183     CHECK_NULL_VOID(frameNode);
1184     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Value, value, frameNode);
1185     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1186     CHECK_NULL_VOID(textPickerPattern);
1187     auto rangeValue = textPickerPattern->GetRange();
1188     auto valueIterator = std::find_if(rangeValue.begin(), rangeValue.end(),
1189         [&value](const NG::RangeContent& range) { return range.text_ == value; });
1190     if (valueIterator != rangeValue.end()) {
1191         TextPickerModelNG::SetSelected(frameNode, std::distance(rangeValue.begin(), valueIterator));
1192     }
1193 }
1194 
SetValues(FrameNode * frameNode,const std::vector<std::string> & values)1195 void TextPickerModelNG::SetValues(FrameNode* frameNode, const std::vector<std::string>& values)
1196 {
1197     CHECK_NULL_VOID(frameNode);
1198     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1199     CHECK_NULL_VOID(textPickerPattern);
1200     std::vector<std::string> selectedValues;
1201     std::vector<uint32_t> valuesIndex;
1202     auto options = textPickerPattern->GetMultiOptions();
1203     for (uint32_t i = 0; i < options.size(); i++) {
1204         if (values.size() > 0 && values.size() < i + 1) {
1205             if (options[i].rangeResult.size() > 0) {
1206                 selectedValues.emplace_back(options[i].rangeResult[0]);
1207             } else {
1208                 selectedValues.emplace_back("");
1209             }
1210             valuesIndex.emplace_back(0);
1211         } else {
1212             auto valueIterator = std::find(options[i].rangeResult.begin(), options[i].rangeResult.end(), values[i]);
1213             if (valueIterator == options[i].rangeResult.end()) {
1214                 selectedValues[i] = options[i].rangeResult.front();
1215                 valuesIndex.emplace_back(0);
1216             } else {
1217                 selectedValues.emplace_back(values[i]);
1218                 valuesIndex.emplace_back(std::distance(options[i].rangeResult.begin(), valueIterator));
1219             }
1220         }
1221     }
1222     TextPickerModelNG::SetSelecteds(frameNode, valuesIndex);
1223     textPickerPattern->SetValues(selectedValues);
1224     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Values, selectedValues, frameNode);
1225 }
1226 
SetDefaultAttributes(RefPtr<FrameNode> & frameNode,const RefPtr<PickerTheme> & pickerTheme)1227 void TextPickerModelNG::SetDefaultAttributes(RefPtr<FrameNode>& frameNode, const RefPtr<PickerTheme>& pickerTheme)
1228 {
1229     auto selectedStyle = pickerTheme->GetOptionStyle(true, false);
1230     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontSize,
1231         ConvertFontScaleValue(selectedStyle.GetFontSize()), frameNode);
1232     ResetTextPickerTextStyleColor(Referenced::RawPtr(frameNode), &TextPickerLayoutProperty::GetSelectedTextStyle);
1233     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedWeight, selectedStyle.GetFontWeight(), frameNode);
1234     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1235         TextPickerLayoutProperty, SelectedFontFamily, selectedStyle.GetFontFamilies(), frameNode);
1236     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1237         TextPickerLayoutProperty, SelectedFontStyle, selectedStyle.GetFontStyle(), frameNode);
1238 
1239     auto disappearStyle = pickerTheme->GetDisappearOptionStyle();
1240     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1241         TextPickerLayoutProperty, DisappearFontSize,
1242         ConvertFontScaleValue(disappearStyle.GetFontSize()), frameNode);
1243     ResetTextPickerTextStyleColor(Referenced::RawPtr(frameNode), &TextPickerLayoutProperty::GetDisappearTextStyle);
1244     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1245         TextPickerLayoutProperty, DisappearWeight, disappearStyle.GetFontWeight(), frameNode);
1246     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1247         TextPickerLayoutProperty, DisappearFontFamily, disappearStyle.GetFontFamilies(), frameNode);
1248     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1249         TextPickerLayoutProperty, DisappearFontStyle, disappearStyle.GetFontStyle(), frameNode);
1250 
1251     auto normalStyle = pickerTheme->GetOptionStyle(false, false);
1252     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize,
1253         ConvertFontScaleValue(normalStyle.GetFontSize()), frameNode);
1254     ResetTextPickerTextStyleColor(Referenced::RawPtr(frameNode), &TextPickerLayoutProperty::GetTextStyle);
1255     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Weight, normalStyle.GetFontWeight(), frameNode);
1256     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontFamily, normalStyle.GetFontFamilies(), frameNode);
1257     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontStyle, normalStyle.GetFontStyle(), frameNode);
1258 
1259     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, CanLoop, true, frameNode);
1260     ACE_UPDATE_NODE_LAYOUT_PROPERTY(
1261         TextPickerLayoutProperty, DigitalCrownSensitivity, pickerTheme->GetDigitalCrownSensitivity(), frameNode);
1262 }
1263 
SetDefaultTextStyle(FrameNode * frameNode,const RefPtr<TextTheme> & textTheme,const NG::PickerTextStyle & value)1264 void TextPickerModelNG::SetDefaultTextStyle(
1265     FrameNode* frameNode, const RefPtr<TextTheme>& textTheme, const NG::PickerTextStyle& value)
1266 {
1267     CHECK_NULL_VOID(frameNode);
1268     CHECK_NULL_VOID(textTheme);
1269     auto textStyle = textTheme->GetTextStyle();
1270     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
1271         ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultFontSize,
1272             ConvertFontScaleValue(value.fontSize.value()), frameNode);
1273     } else {
1274         ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultFontSize,
1275             ConvertFontScaleValue(textStyle.GetFontSize()), frameNode);
1276     }
1277     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultColor,
1278         value.textColor.value_or(textStyle.GetTextColor()), frameNode);
1279     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultWeight,
1280         value.fontWeight.value_or(textStyle.GetFontWeight()), frameNode);
1281     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultFontFamily,
1282         value.fontFamily.value_or(textStyle.GetFontFamilies()), frameNode);
1283     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultFontStyle,
1284         value.fontStyle.value_or(textStyle.GetFontStyle()), frameNode);
1285     if (value.minFontSize.has_value() && value.minFontSize->IsValid()) {
1286         ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultMinFontSize,
1287             ConvertFontScaleValue(value.minFontSize.value()), frameNode);
1288     } else {
1289         ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultMinFontSize, Dimension(), frameNode);
1290     }
1291     if (value.maxFontSize.has_value() && value.maxFontSize->IsValid()) {
1292         ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultMaxFontSize,
1293             ConvertFontScaleValue(value.maxFontSize.value()), frameNode);
1294     } else {
1295         ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultMaxFontSize, Dimension(), frameNode);
1296     }
1297     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultTextOverflow,
1298         value.textOverflow.value_or(textStyle.GetTextOverflow()), frameNode);
1299 }
1300 
getTextPickerValue(FrameNode * frameNode)1301 std::string TextPickerModelNG::getTextPickerValue(FrameNode* frameNode)
1302 {
1303     CHECK_NULL_RETURN(frameNode, "");
1304     return frameNode->GetLayoutProperty<TextPickerLayoutProperty>()->GetValueValue("");
1305 }
1306 
getTextPickerRange(FrameNode * frameNode)1307 std::string TextPickerModelNG::getTextPickerRange(FrameNode* frameNode)
1308 {
1309     CHECK_NULL_RETURN(frameNode, "");
1310     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1311     CHECK_NULL_RETURN(textPickerPattern, "");
1312     return textPickerPattern->GetTextPickerRange();
1313 }
1314 
SetDivider(const ItemDivider & divider)1315 void TextPickerModelNG::SetDivider(const ItemDivider& divider)
1316 {
1317     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
1318     CHECK_NULL_VOID(frameNode);
1319     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1320     CHECK_NULL_VOID(textPickerPattern);
1321     textPickerPattern->SetDivider(divider);
1322     textPickerPattern->SetCustomDividerFlag(true);
1323     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Divider, divider);
1324 }
1325 
SetDivider(FrameNode * frameNode,const ItemDivider & divider)1326 void TextPickerModelNG::SetDivider(FrameNode* frameNode, const ItemDivider& divider)
1327 {
1328     CHECK_NULL_VOID(frameNode);
1329     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1330     CHECK_NULL_VOID(textPickerPattern);
1331     textPickerPattern->SetDivider(divider);
1332     textPickerPattern->SetCustomDividerFlag(true);
1333     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Divider, divider, frameNode);
1334 }
1335 
SetGradientHeight(FrameNode * frameNode,const Dimension & value)1336 void TextPickerModelNG::SetGradientHeight(FrameNode* frameNode, const Dimension& value)
1337 {
1338     CHECK_NULL_VOID(frameNode);
1339     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1340     CHECK_NULL_VOID(textPickerPattern);
1341     textPickerPattern->SetGradientHeight(value);
1342     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, GradientHeight, value, frameNode);
1343 }
1344 
SetDisableTextStyleAnimation(FrameNode * frameNode,const bool value)1345 void TextPickerModelNG::SetDisableTextStyleAnimation(FrameNode* frameNode, const bool value)
1346 {
1347     CHECK_NULL_VOID(frameNode);
1348     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1349     CHECK_NULL_VOID(textPickerPattern);
1350     textPickerPattern->SetDisableTextStyleAnimation(value);
1351     ACE_UPDATE_NODE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisableTextStyleAnimation, value, frameNode);
1352 }
1353 
SetOnCascadeChange(FrameNode * frameNode,TextCascadeChangeEvent && onChange)1354 void TextPickerModelNG::SetOnCascadeChange(FrameNode* frameNode, TextCascadeChangeEvent&& onChange)
1355 {
1356     CHECK_NULL_VOID(frameNode);
1357     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
1358     CHECK_NULL_VOID(eventHub);
1359     eventHub->SetOnChange(std::move(onChange));
1360 }
1361 
SetOnScrollStop(FrameNode * frameNode,TextCascadeChangeEvent && onScrollStop)1362 void TextPickerModelNG::SetOnScrollStop(FrameNode* frameNode, TextCascadeChangeEvent&& onScrollStop)
1363 {
1364     CHECK_NULL_VOID(frameNode);
1365     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
1366     CHECK_NULL_VOID(eventHub);
1367     eventHub->SetOnScrollStop(std::move(onScrollStop));
1368 }
1369 
GetSelectedSize(FrameNode * frameNode)1370 int32_t TextPickerModelNG::GetSelectedSize(FrameNode* frameNode)
1371 {
1372     CHECK_NULL_RETURN(frameNode, 0);
1373     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1374     CHECK_NULL_RETURN(textPickerPattern, 0);
1375     return textPickerPattern->GetSelecteds().size();
1376 }
1377 
GetColumnWidthsSize(FrameNode * frameNode)1378 int32_t TextPickerModelNG::GetColumnWidthsSize(FrameNode* frameNode)
1379 {
1380     CHECK_NULL_RETURN(frameNode, 0);
1381     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1382     CHECK_NULL_RETURN(textPickerPattern, 0);
1383     return textPickerPattern->GetColumnWidths().size();
1384 }
1385 
getTextPickerValues(FrameNode * frameNode)1386 std::string TextPickerModelNG::getTextPickerValues(FrameNode* frameNode)
1387 {
1388     CHECK_NULL_RETURN(frameNode, "");
1389     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1390     CHECK_NULL_RETURN(textPickerPattern, "");
1391     auto values = textPickerPattern->GetValues();
1392     std::string result;
1393     for (auto& valueRet : values) {
1394         result.append(valueRet + ';');
1395     }
1396     const size_t length = result.length();
1397     result = result.substr(0, length > 0 ? length - 1 : 0);
1398     return result;
1399 }
1400 
getTextPickerSelecteds(FrameNode * frameNode)1401 std::vector<uint32_t> TextPickerModelNG::getTextPickerSelecteds(FrameNode* frameNode)
1402 {
1403     std::vector<uint32_t> defaultValue = { 0 };
1404     CHECK_NULL_RETURN(frameNode, defaultValue);
1405     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1406     CHECK_NULL_RETURN(textPickerPattern, defaultValue);
1407     return textPickerPattern->GetSelecteds();
1408 }
1409 
SetTextPickerRangeType(FrameNode * frameNode,int32_t rangeType)1410 void TextPickerModelNG::SetTextPickerRangeType(FrameNode* frameNode, int32_t rangeType)
1411 {
1412     CHECK_NULL_VOID(frameNode);
1413     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1414     CHECK_NULL_VOID(textPickerPattern);
1415     textPickerPattern->SetRangeType(rangeType);
1416 }
1417 
GetTextPickerRangeType(FrameNode * frameNode)1418 int32_t TextPickerModelNG::GetTextPickerRangeType(FrameNode* frameNode)
1419 {
1420     CHECK_NULL_RETURN(frameNode, 0);
1421     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1422     CHECK_NULL_RETURN(textPickerPattern, 0);
1423     return textPickerPattern->GetRangeType();
1424 }
1425 
ConvertFontScaleValue(const Dimension & fontSizeValue)1426 const Dimension TextPickerModelNG::ConvertFontScaleValue(const Dimension& fontSizeValue)
1427 {
1428     auto pipeline = PipelineContext::GetCurrentContext();
1429     CHECK_NULL_RETURN(pipeline, fontSizeValue);
1430     auto maxAppFontScale = pipeline->GetMaxAppFontScale();
1431     auto follow = pipeline->IsFollowSystem();
1432     float fontScale = pipeline->GetFontScale();
1433     if (NearZero(fontScale) || (fontSizeValue.Unit() == DimensionUnit::VP)) {
1434         return fontSizeValue;
1435     }
1436     if (GreatOrEqualCustomPrecision(fontScale, PICKER_MAXFONTSCALE) && follow) {
1437         fontScale = std::clamp(fontScale, 0.0f, maxAppFontScale);
1438         if (fontScale != 0.0f) {
1439             return Dimension(fontSizeValue / fontScale);
1440         }
1441     }
1442     return fontSizeValue;
1443 }
1444 
HasUserDefinedOpacity()1445 void TextPickerModelNG::HasUserDefinedOpacity()
1446 {
1447     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
1448     CHECK_NULL_VOID(frameNode);
1449     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1450     CHECK_NULL_VOID(textPickerPattern);
1451     auto renderContext = frameNode->GetRenderContext();
1452     CHECK_NULL_VOID(renderContext);
1453     textPickerPattern->SetUserDefinedOpacity(renderContext->GetOpacityValue(1.0));
1454 }
1455 
SetEnableHapticFeedback(bool isEnableHapticFeedback)1456 void TextPickerModelNG::SetEnableHapticFeedback(bool isEnableHapticFeedback)
1457 {
1458     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
1459     SetEnableHapticFeedback(frameNode, isEnableHapticFeedback);
1460 }
1461 
SetEnableHapticFeedback(FrameNode * frameNode,bool isEnableHapticFeedback)1462 void TextPickerModelNG::SetEnableHapticFeedback(FrameNode* frameNode, bool isEnableHapticFeedback)
1463 {
1464     CHECK_NULL_VOID(frameNode);
1465     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1466     CHECK_NULL_VOID(textPickerPattern);
1467     textPickerPattern->SetIsEnableHaptic(isEnableHapticFeedback);
1468 }
1469 
GetEnableHapticFeedback(FrameNode * frameNode)1470 bool TextPickerModelNG::GetEnableHapticFeedback(FrameNode* frameNode)
1471 {
1472     CHECK_NULL_RETURN(frameNode, DEFAULT_ENABLE_HAPTIC_FEEDBACK);
1473     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1474     CHECK_NULL_RETURN(textPickerPattern, DEFAULT_ENABLE_HAPTIC_FEEDBACK);
1475     return textPickerPattern->GetIsEnableHaptic();
1476 }
1477 
IsCascade()1478 bool TextPickerModelNG::IsCascade()
1479 {
1480     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
1481     CHECK_NULL_RETURN(frameNode, false);
1482     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1483     CHECK_NULL_RETURN(textPickerPattern, false);
1484     return textPickerPattern->GetIsCascade();
1485 }
1486 
SetSingleRange(bool isSingleRange)1487 void TextPickerModelNG::SetSingleRange(bool isSingleRange)
1488 {
1489     std::lock_guard<std::shared_mutex> lock(isSingleMutex_);
1490     isSingleRange_ = isSingleRange;
1491     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
1492     CHECK_NULL_VOID(frameNode);
1493     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1494     CHECK_NULL_VOID(textPickerPattern);
1495     textPickerPattern->SetSingleRange(isSingleRange);
1496 }
1497 
UpdateUserSetSelectColor()1498 void TextPickerModelNG::UpdateUserSetSelectColor()
1499 {
1500     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
1501     CHECK_NULL_VOID(frameNode);
1502     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
1503     CHECK_NULL_VOID(textPickerPattern);
1504     textPickerPattern->UpdateUserSetSelectColor();
1505 }
1506 } // namespace OHOS::Ace::NG
1507