• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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/text_picker/textpicker_model_ng.h"
17 
18 #include <securec.h>
19 
20 #include "base/geometry/ng/size_t.h"
21 #include "base/utils/utils.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/base/view_stack_processor.h"
24 #include "core/components_ng/pattern/button/button_pattern.h"
25 #include "core/components_ng/pattern/image/image_pattern.h"
26 #include "core/components_ng/pattern/stack/stack_pattern.h"
27 #include "core/components_ng/pattern/text/text_pattern.h"
28 #include "core/components_ng/pattern/text_picker/textpicker_column_pattern.h"
29 #include "core/components_ng/pattern/text_picker/textpicker_event_hub.h"
30 #include "core/components_ng/pattern/text_picker/textpicker_layout_property.h"
31 #include "core/components_ng/pattern/text_picker/textpicker_pattern.h"
32 #include "core/components_v2/inspector/inspector_constants.h"
33 #include "core/pipeline_ng/pipeline_context.h"
34 
35 namespace OHOS::Ace::NG {
36 namespace {
SetDialogProperties(DialogProperties & properties,TextPickerDialog & textPickerDialog,const RefPtr<DialogTheme> & theme)37 void SetDialogProperties(DialogProperties& properties, TextPickerDialog& textPickerDialog,
38                          const RefPtr<DialogTheme>& theme)
39 {
40     if (SystemProperties::GetDeviceType() == DeviceType::PHONE) {
41         properties.alignment = DialogAlignment::BOTTOM;
42     } else {
43         properties.alignment = DialogAlignment::CENTER;
44     }
45     if (textPickerDialog.alignment.has_value()) {
46         properties.alignment = textPickerDialog.alignment.value();
47     }
48     properties.customStyle = false;
49     properties.offset = DimensionOffset(Offset(0, -theme->GetMarginBottom().ConvertToPx()));
50     if (textPickerDialog.offset.has_value()) {
51         properties.offset = textPickerDialog.offset.value();
52     }
53 
54     properties.maskRect = textPickerDialog.maskRect;
55 }
56 }
57 
Create(RefPtr<PickerTheme> pickerTheme,uint32_t columnKind)58 void TextPickerModelNG::Create(RefPtr<PickerTheme> pickerTheme, uint32_t columnKind)
59 {
60     auto* stack = ViewStackProcessor::GetInstance();
61     auto nodeId = stack->ClaimNodeId();
62     auto textPickerNode = FrameNode::GetOrCreateFrameNode(
63         V2::TEXT_PICKER_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<TextPickerPattern>(); });
64     auto textPickerPattern = textPickerNode->GetPattern<TextPickerPattern>();
65     CHECK_NULL_VOID(textPickerPattern);
66     textPickerPattern->SetColumnsKind(columnKind);
67     CHECK_NULL_VOID(pickerTheme);
68     uint32_t showCount = pickerTheme->GetShowOptionCount();
69 
70     if (textPickerNode->GetChildren().empty()) {
71         auto columnNode = CreateColumnNode(columnKind, showCount);
72         auto stackNode = CreateStackNode();
73         auto buttonNode = CreateButtonNode();
74         buttonNode->MountToParent(stackNode);
75         columnNode->MountToParent(stackNode);
76         columnNode->MarkModifyDone();
77         columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
78         auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
79         layoutProperty->UpdateAlignment(Alignment::CENTER);
80         stackNode->MountToParent(textPickerNode);
81     }
82     stack->Push(textPickerNode);
83     options_.clear();
84 }
85 
SetDefaultAttributes(const RefPtr<PickerTheme> & pickerTheme)86 void TextPickerModelNG::SetDefaultAttributes(const RefPtr<PickerTheme>& pickerTheme)
87 {
88     CHECK_NULL_VOID(pickerTheme);
89     auto selectedStyle = pickerTheme->GetOptionStyle(true, false);
90     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontSize, selectedStyle.GetFontSize());
91     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedColor, selectedStyle.GetTextColor());
92     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedWeight, selectedStyle.GetFontWeight());
93     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontFamily, selectedStyle.GetFontFamilies());
94     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontStyle, selectedStyle.GetFontStyle());
95 
96     auto disappearStyle = pickerTheme->GetDisappearOptionStyle();
97     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontSize, disappearStyle.GetFontSize());
98     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearColor, disappearStyle.GetTextColor());
99     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearWeight, disappearStyle.GetFontWeight());
100     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontFamily, disappearStyle.GetFontFamilies());
101     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontStyle, disappearStyle.GetFontStyle());
102 
103     auto normalStyle = pickerTheme->GetOptionStyle(false, false);
104     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize, normalStyle.GetFontSize());
105     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Color, normalStyle.GetTextColor());
106     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Weight, normalStyle.GetFontWeight());
107     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontFamily, normalStyle.GetFontFamilies());
108     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontStyle, normalStyle.GetFontStyle());
109 
110     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, CanLoop, true);
111 }
112 
CreateColumnNode(uint32_t columnKind,uint32_t showCount)113 RefPtr<FrameNode> TextPickerModelNG::CreateColumnNode(uint32_t columnKind, uint32_t showCount)
114 {
115     auto columnNode =
116         FrameNode::GetOrCreateFrameNode(V2::COLUMN_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
117         []() { return AceType::MakeRefPtr<TextPickerColumnPattern>(); });
118     if (columnKind == ICON) {
119         for (uint32_t index = 0; index < showCount; index++) {
120             auto row = FrameNode::CreateFrameNode(
121                 V2::ROW_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
122                 AceType::MakeRefPtr<LinearLayoutPattern>(false));
123             CHECK_NULL_RETURN(row, nullptr);
124             auto layoutProps = row->GetLayoutProperty<LinearLayoutProperty>();
125             CHECK_NULL_RETURN(layoutProps, nullptr);
126             layoutProps->UpdateMainAxisAlign(FlexAlign::CENTER);
127             layoutProps->UpdateCrossAxisAlign(FlexAlign::CENTER);
128 
129             auto imageNode = FrameNode::CreateFrameNode(
130                 V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
131                 AceType::MakeRefPtr<ImagePattern>());
132             CHECK_NULL_RETURN(imageNode, nullptr);
133             imageNode->MountToParent(row);
134             row->MountToParent(columnNode);
135         }
136     } else if (columnKind == TEXT) {
137         for (uint32_t index = 0; index < showCount; index++) {
138             auto textNode = FrameNode::CreateFrameNode(
139                 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
140                 AceType::MakeRefPtr<TextPattern>());
141             CHECK_NULL_RETURN(textNode, nullptr);
142             textNode->MountToParent(columnNode);
143         }
144     } else if (columnKind == MIXTURE) {
145         for (uint32_t index = 0; index < showCount; index++) {
146             auto row = FrameNode::CreateFrameNode(
147                 V2::ROW_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
148                 AceType::MakeRefPtr<LinearLayoutPattern>(false));
149             CHECK_NULL_RETURN(row, nullptr);
150 
151             auto imageNode = FrameNode::CreateFrameNode(
152                 V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
153                 AceType::MakeRefPtr<ImagePattern>());
154             CHECK_NULL_RETURN(imageNode, nullptr);
155             imageNode->MountToParent(row);
156 
157             auto textNode = FrameNode::CreateFrameNode(
158                 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
159                 AceType::MakeRefPtr<TextPattern>());
160             CHECK_NULL_RETURN(textNode, nullptr);
161             textNode->MountToParent(row);
162             row->MountToParent(columnNode);
163         }
164     }
165     return columnNode;
166 }
167 
CreateStackNode()168 RefPtr<FrameNode> TextPickerModelNG::CreateStackNode()
169 {
170     auto stackId = ElementRegister::GetInstance()->MakeUniqueId();
171     return FrameNode::GetOrCreateFrameNode(
172         V2::STACK_ETS_TAG, stackId, []() { return AceType::MakeRefPtr<StackPattern>(); });
173 }
174 
CreateButtonNode()175 RefPtr<FrameNode> TextPickerModelNG::CreateButtonNode()
176 {
177     auto buttonId = ElementRegister::GetInstance()->MakeUniqueId();
178     return FrameNode::GetOrCreateFrameNode(
179         V2::BUTTON_ETS_TAG, buttonId, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
180 }
181 
SetSelected(uint32_t value)182 void TextPickerModelNG::SetSelected(uint32_t value)
183 {
184     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
185     CHECK_NULL_VOID(frameNode);
186     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
187     textPickerPattern->SetSelected(value);
188     std::vector<uint32_t> values;
189     values.emplace_back(value);
190     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Selected, value);
191     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedIndex, values);
192 }
193 
SetRange(const std::vector<NG::RangeContent> & value)194 void TextPickerModelNG::SetRange(const std::vector<NG::RangeContent>& value)
195 {
196     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
197     CHECK_NULL_VOID(frameNode);
198     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
199     textPickerPattern->SetRange(value);
200     for (auto& range : value) {
201         rangeValue_.emplace_back(std::move(range));
202     }
203 }
204 
SetDefaultPickerItemHeight(const Dimension & value)205 void TextPickerModelNG::SetDefaultPickerItemHeight(const Dimension& value)
206 {
207     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DefaultPickerItemHeight, value);
208 }
209 
SetCanLoop(const bool value)210 void TextPickerModelNG::SetCanLoop(const bool value)
211 {
212     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, CanLoop, value);
213 }
214 
SetBackgroundColor(const Color & color)215 void TextPickerModelNG::SetBackgroundColor(const Color& color)
216 {
217     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
218     CHECK_NULL_VOID(frameNode);
219     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
220     CHECK_NULL_VOID(textPickerPattern);
221     textPickerPattern->SetBackgroundColor(color);
222 }
223 
SetDisappearTextStyle(const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)224 void TextPickerModelNG::SetDisappearTextStyle(const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
225 {
226     CHECK_NULL_VOID(pickerTheme);
227     auto disappearStyle = pickerTheme->GetDisappearOptionStyle();
228     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
229         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontSize, value.fontSize.value());
230     } else {
231         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, DisappearFontSize, disappearStyle.GetFontSize());
232     }
233     ACE_UPDATE_LAYOUT_PROPERTY(
234         TextPickerLayoutProperty, DisappearColor, value.textColor.value_or(disappearStyle.GetTextColor()));
235     ACE_UPDATE_LAYOUT_PROPERTY(
236         TextPickerLayoutProperty, DisappearWeight, value.fontWeight.value_or(disappearStyle.GetFontWeight()));
237     ACE_UPDATE_LAYOUT_PROPERTY(
238         TextPickerLayoutProperty, DisappearFontFamily, value.fontFamily.value_or(disappearStyle.GetFontFamilies()));
239     ACE_UPDATE_LAYOUT_PROPERTY(
240         TextPickerLayoutProperty, DisappearFontStyle, value.fontStyle.value_or(disappearStyle.GetFontStyle()));
241 }
242 
SetNormalTextStyle(const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)243 void TextPickerModelNG::SetNormalTextStyle(const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
244 {
245     CHECK_NULL_VOID(pickerTheme);
246     auto normalStyle = pickerTheme->GetOptionStyle(false, false);
247     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
248         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize, value.fontSize.value());
249     } else {
250         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, FontSize, normalStyle.GetFontSize());
251     }
252     ACE_UPDATE_LAYOUT_PROPERTY(
253         TextPickerLayoutProperty, Color, value.textColor.value_or(normalStyle.GetTextColor()));
254     ACE_UPDATE_LAYOUT_PROPERTY(
255         TextPickerLayoutProperty, Weight, value.fontWeight.value_or(normalStyle.GetFontWeight()));
256     ACE_UPDATE_LAYOUT_PROPERTY(
257         TextPickerLayoutProperty, FontFamily, value.fontFamily.value_or(normalStyle.GetFontFamilies()));
258     ACE_UPDATE_LAYOUT_PROPERTY(
259         TextPickerLayoutProperty, FontStyle, value.fontStyle.value_or(normalStyle.GetFontStyle()));
260 }
261 
SetSelectedTextStyle(const RefPtr<PickerTheme> & pickerTheme,const NG::PickerTextStyle & value)262 void TextPickerModelNG::SetSelectedTextStyle(const RefPtr<PickerTheme>& pickerTheme, const NG::PickerTextStyle& value)
263 {
264     CHECK_NULL_VOID(pickerTheme);
265     auto selectedStyle = pickerTheme->GetOptionStyle(true, false);
266     if (value.fontSize.has_value() && value.fontSize->IsValid()) {
267         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontSize, value.fontSize.value());
268     } else {
269         ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedFontSize, selectedStyle.GetFontSize());
270     }
271     ACE_UPDATE_LAYOUT_PROPERTY(
272         TextPickerLayoutProperty, SelectedColor, value.textColor.value_or(selectedStyle.GetTextColor()));
273     ACE_UPDATE_LAYOUT_PROPERTY(
274         TextPickerLayoutProperty, SelectedWeight, value.fontWeight.value_or(selectedStyle.GetFontWeight()));
275     ACE_UPDATE_LAYOUT_PROPERTY(
276         TextPickerLayoutProperty, SelectedFontFamily, value.fontFamily.value_or(selectedStyle.GetFontFamilies()));
277     ACE_UPDATE_LAYOUT_PROPERTY(
278         TextPickerLayoutProperty, SelectedFontStyle, value.fontStyle.value_or(selectedStyle.GetFontStyle()));
279 }
280 
SetOnCascadeChange(TextCascadeChangeEvent && onChange)281 void TextPickerModelNG::SetOnCascadeChange(TextCascadeChangeEvent&& onChange)
282 {
283     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
284     CHECK_NULL_VOID(frameNode);
285     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
286     CHECK_NULL_VOID(eventHub);
287     eventHub->SetOnChange(std::move(onChange));
288 }
289 
SetValue(const std::string & value)290 void TextPickerModelNG::SetValue(const std::string& value)
291 {
292     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Value, value);
293 }
294 
MultiInit(const RefPtr<PickerTheme> pickerTheme)295 void TextPickerModelNG::MultiInit(const RefPtr<PickerTheme> pickerTheme)
296 {
297     auto* stack = ViewStackProcessor::GetInstance();
298     auto nodeId = stack->ClaimNodeId();
299     auto textPickerNode = FrameNode::GetOrCreateFrameNode(
300         V2::TEXT_PICKER_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<TextPickerPattern>(); });
301     auto textPickerPattern = textPickerNode->GetPattern<TextPickerPattern>();
302 
303     CHECK_NULL_VOID(pickerTheme);
304     showCount_ = pickerTheme->GetShowOptionCount();
305     stack->Push(textPickerNode);
306     rangeValue_.clear();
307 }
308 
SetIsCascade(bool isCascade)309 void TextPickerModelNG::SetIsCascade(bool isCascade)
310 {
311     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
312     CHECK_NULL_VOID(frameNode);
313     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
314     CHECK_NULL_VOID(textPickerPattern);
315     isCascade_ = isCascade;
316     textPickerPattern->SetIsCascade(isCascade_);
317 }
318 
SetHasSelectAttr(bool value)319 void TextPickerModelNG::SetHasSelectAttr(bool value)
320 {
321     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
322     CHECK_NULL_VOID(frameNode);
323     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
324     CHECK_NULL_VOID(textPickerPattern);
325     textPickerPattern->SetHasSelectAttr(value);
326 }
327 
SetUnCascadeColumns(const std::vector<NG::TextCascadePickerOptions> & options)328 void TextPickerModelNG::SetUnCascadeColumns(const std::vector<NG::TextCascadePickerOptions>& options)
329 {
330     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
331     CHECK_NULL_VOID(frameNode);
332     if (frameNode->GetChildren().empty()) {
333         for (uint32_t i = 0; i < options.size(); i++) {
334             auto columnNode = CreateColumnNode(NG::TEXT, showCount_);
335             auto stackNode = CreateStackNode();
336             auto buttonNode = CreateButtonNode();
337             buttonNode->MountToParent(stackNode);
338             columnNode->MountToParent(stackNode);
339             columnNode->MarkModifyDone();
340             columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
341             auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
342             layoutProperty->UpdateAlignment(Alignment::CENTER);
343             stackNode->MountToParent(frameNode);
344         }
345     }
346 
347     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
348     CHECK_NULL_VOID(textPickerPattern);
349     textPickerPattern->SetCascadeOptions(options, options);
350 }
351 
SetCascadeColumns(const std::vector<NG::TextCascadePickerOptions> & options)352 void TextPickerModelNG::SetCascadeColumns(const std::vector<NG::TextCascadePickerOptions>& options)
353 {
354     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
355     CHECK_NULL_VOID(frameNode);
356     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
357     CHECK_NULL_VOID(textPickerPattern);
358     std::vector<NG::TextCascadePickerOptions> reOptions;
359     // Caculate max depth
360     size_t columnCount = options.empty()? 0 : 1;
361     for (size_t i = 0; i < options.size(); i++) {
362         size_t tmp  = textPickerPattern->ProcessCascadeOptionDepth(options[i]);
363         if (tmp > columnCount) {
364             columnCount = tmp;
365         }
366     }
367 
368     // Create Node
369     if (frameNode->GetChildren().empty()) {
370         for (size_t i = 0; i < columnCount; i++) {
371             auto columnNode = CreateColumnNode(NG::TEXT, showCount_);
372             auto stackNode = CreateStackNode();
373             auto buttonNode = CreateButtonNode();
374             buttonNode->MountToParent(stackNode);
375             columnNode->MountToParent(stackNode);
376             columnNode->MarkModifyDone();
377             columnNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
378             auto layoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
379             layoutProperty->UpdateAlignment(Alignment::CENTER);
380             stackNode->MountToParent(frameNode);
381         }
382     }
383 
384     textPickerPattern->ProcessCascadeOptions(options, reOptions, 0);
385     if (reOptions.size() < columnCount) {
386         auto differ = columnCount - reOptions.size();
387         for (uint32_t i = 0; i < differ; i++) {
388             NG::TextCascadePickerOptions differOption;
389             memset_s(&differOption, sizeof(differOption), 0, sizeof(differOption));
390             reOptions.emplace_back(differOption);
391         }
392     }
393     textPickerPattern->SetCascadeOptions(options, reOptions);
394 }
395 
SetColumns(const std::vector<NG::TextCascadePickerOptions> & options)396 void TextPickerModelNG::SetColumns(const std::vector<NG::TextCascadePickerOptions>& options)
397 {
398     options_.clear();
399     for (auto& option : options) {
400         options_.emplace_back(std::move(option));
401     }
402     if (!isCascade_) {
403         SetUnCascadeColumns(options);
404     } else {
405         SetCascadeColumns(options);
406     }
407 }
408 
IsSingle()409 bool TextPickerModelNG::IsSingle()
410 {
411     return rangeValue_.size() > 0;
412 }
413 
GetSingleRange(std::vector<NG::RangeContent> & rangeValue)414 bool TextPickerModelNG::GetSingleRange(std::vector<NG::RangeContent>& rangeValue)
415 {
416     rangeValue.clear();
417     for (auto& item : rangeValue_) {
418         rangeValue.emplace_back(std::move(item));
419     }
420     return true;
421 }
422 
GetMultiOptions(std::vector<NG::TextCascadePickerOptions> & options)423 bool TextPickerModelNG::GetMultiOptions(std::vector<NG::TextCascadePickerOptions>& options)
424 {
425     options.clear();
426     for (auto& item : options_) {
427         options.emplace_back(std::move(item));
428     }
429     return true;
430 }
431 
SetValues(const std::vector<std::string> & values)432 void TextPickerModelNG::SetValues(const std::vector<std::string>& values)
433 {
434     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
435     CHECK_NULL_VOID(frameNode);
436     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
437     CHECK_NULL_VOID(textPickerPattern);
438     textPickerPattern->SetValues(values);
439     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Values, values);
440 }
441 
SetSelecteds(const std::vector<uint32_t> & values)442 void TextPickerModelNG::SetSelecteds(const std::vector<uint32_t>& values)
443 {
444     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
445     CHECK_NULL_VOID(frameNode);
446     auto textPickerPattern = frameNode->GetPattern<TextPickerPattern>();
447     CHECK_NULL_VOID(textPickerPattern);
448     textPickerPattern->SetSelecteds(values);
449     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, Selecteds, values);
450     ACE_UPDATE_LAYOUT_PROPERTY(TextPickerLayoutProperty, SelectedIndex, values);
451 }
452 
SetOnValueChangeEvent(TextCascadeValueChangeEvent && onValueChangeEvent)453 void TextPickerModelNG::SetOnValueChangeEvent(TextCascadeValueChangeEvent&& onValueChangeEvent)
454 {
455     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
456     CHECK_NULL_VOID(frameNode);
457     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
458     CHECK_NULL_VOID(eventHub);
459     eventHub->SetOnValueChangeEvent(std::move(onValueChangeEvent));
460 }
461 
SetOnSelectedChangeEvent(TextCascadeSelectedChangeEvent && onSelectedChangeEvent)462 void TextPickerModelNG::SetOnSelectedChangeEvent(TextCascadeSelectedChangeEvent&& onSelectedChangeEvent)
463 {
464     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
465     CHECK_NULL_VOID(frameNode);
466     auto eventHub = frameNode->GetEventHub<TextPickerEventHub>();
467     CHECK_NULL_VOID(eventHub);
468     eventHub->SetOnSelectedChangeEvent(std::move(onSelectedChangeEvent));
469 }
470 
CreateObject()471 RefPtr<AceType> TextPickerDialogModelNG::CreateObject()
472 {
473     return nullptr;
474 }
475 
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,TextPickerDialog & textPickerDialog)476 void TextPickerDialogModelNG::SetTextPickerDialogShow(RefPtr<AceType>& PickerText,
477     NG::TextPickerSettingData& settingData, std::function<void()>&& onCancel,
478     std::function<void(const std::string&)>&& onAccept, std::function<void(const std::string&)>&& onChange,
479     TextPickerDialog& textPickerDialog)
480 {
481     auto container = Container::Current();
482     if (!container) {
483         return;
484     }
485     auto pipelineContext = AccessibilityManager::DynamicCast<NG::PipelineContext>(container->GetPipelineContext());
486     if (!pipelineContext) {
487         return;
488     }
489     auto executor = pipelineContext->GetTaskExecutor();
490     if (!executor) {
491         return;
492     }
493     auto pipeline = PipelineBase::GetCurrentContext();
494     CHECK_NULL_VOID(pipeline);
495     auto theme = pipeline->GetTheme<DialogTheme>();
496     if (!theme) {
497         LOGE("DialogTheme is null");
498         return;
499     }
500 
501     std::map<std::string, NG::DialogTextEvent> dialogEvent;
502     std::map<std::string, NG::DialogGestureEvent> dialogCancelEvent;
503     dialogEvent["acceptId"] = onAccept;
504     dialogEvent["changeId"] = onChange;
505     auto func = [onCancel](const GestureEvent& /* info */) {
506         if (onCancel) {
507             onCancel();
508         }
509     };
510     dialogCancelEvent["cancelId"] = func;
511     DialogProperties properties;
512     ButtonInfo buttonInfo;
513     SetDialogProperties(properties, textPickerDialog, theme);
514 
515     auto context = AccessibilityManager::DynamicCast<NG::PipelineContext>(pipelineContext);
516     auto overlayManager = context ? context->GetOverlayManager() : nullptr;
517     executor->PostTask(
518         [properties, settingData, dialogEvent, dialogCancelEvent, weak = WeakPtr<NG::OverlayManager>(overlayManager)] {
519             auto overlayManager = weak.Upgrade();
520             CHECK_NULL_VOID(overlayManager);
521             overlayManager->ShowTextDialog(properties, settingData, dialogEvent, dialogCancelEvent);
522         },
523         TaskExecutor::TaskType::UI);
524 }
525 } // namespace OHOS::Ace::NG
526