1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bridge/declarative_frontend/jsview/models/textpicker_model_impl.h"
17
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 #include "core/components/picker/picker_text_component.h"
20
21 namespace OHOS::Ace::Framework {
Create(RefPtr<PickerTheme> pickerTheme,uint32_t columnKind)22 void TextPickerModelImpl::Create(RefPtr<PickerTheme> pickerTheme, uint32_t columnKind)
23 {
24 (void)columnKind;
25 auto textPicker = AceType::MakeRefPtr<PickerTextComponent>();
26 ViewStackProcessor::GetInstance()->ClaimElementId(textPicker);
27
28 textPicker->SetIsDialog(false);
29 textPicker->SetHasButtons(false);
30 textPicker->SetTheme(pickerTheme);
31 ViewStackProcessor::GetInstance()->Push(textPicker);
32 }
33
SetDefaultPickerItemHeight(const Dimension & value)34 void TextPickerModelImpl::SetDefaultPickerItemHeight(const Dimension& value)
35 {
36 auto textPicker = AceType::DynamicCast<PickerTextComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
37 textPicker->SetDefaultHeight(true);
38 JSViewSetProperty(&PickerTextComponent::SetColumnHeight, value);
39 }
40
SetSelected(uint32_t value)41 void TextPickerModelImpl::SetSelected(uint32_t value)
42 {
43 JSViewSetProperty(&PickerTextComponent::SetSelected, value);
44 }
45
SetRange(const std::vector<NG::RangeContent> & value)46 void TextPickerModelImpl::SetRange(const std::vector<NG::RangeContent>& value)
47 {
48 if (!value.size()) {
49 return;
50 }
51 std::vector<std::string> textRange;
52 for (auto& range : value) {
53 textRange.emplace_back(range.text_);
54 }
55 JSViewSetProperty(&PickerTextComponent::SetRange, textRange);
56 }
57
SetOnCascadeChange(TextCascadeChangeEvent && onChange)58 void TextPickerModelImpl::SetOnCascadeChange(TextCascadeChangeEvent&& onChange)
59 {
60 auto func = onChange;
61 auto onChangeEvent = [func](const std::string& value, const double& index) {
62 std::vector<std::string> changeValue { value };
63 std::vector<double> changeIndex { index };
64 func(changeValue, changeIndex);
65 };
66 JSViewSetProperty(&PickerBaseComponent::SetOnTextChange, std::move(onChangeEvent));
67 }
68
SetOnScrollStop(TextCascadeChangeEvent && onScrollStop)69 void TextPickerModelImpl::SetOnScrollStop(TextCascadeChangeEvent&& onScrollStop) {}
70
SetOnEnterSelectedArea(TextCascadeChangeEvent && onEnterSelectedArea)71 void TextPickerModelImpl::SetOnEnterSelectedArea(TextCascadeChangeEvent&& onEnterSelectedArea) {}
72
SetBackgroundColor(const Color & color)73 void TextPickerModelImpl::SetBackgroundColor(const Color& color)
74 {
75 auto pickerBase = AceType::DynamicCast<PickerBaseComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
76 if (!pickerBase) {
77 LOGE("PickerBaseComponent is null");
78 return;
79 }
80
81 pickerBase->SetHasBackgroundColor(true);
82 }
83
CreateObject()84 RefPtr<AceType> TextPickerDialogModelImpl::CreateObject()
85 {
86 return AceType::MakeRefPtr<PickerTextComponent>();
87 }
88
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)89 void TextPickerDialogModelImpl::SetTextPickerDialogShow(RefPtr<AceType>& PickerText,
90 NG::TextPickerSettingData& settingData, std::function<void()>&& onCancel,
91 std::function<void(const std::string&)>&& onAccept, std::function<void(const std::string&)>&& onChange,
92 std::function<void(const std::string&)>&& onScrollStop,
93 std::function<void(const std::string&)>&& onEnterSelectedArea, TextPickerDialog& textPickerDialog,
94 TextPickerDialogEvent& textPickerDialogEvent, const std::vector<ButtonInfo>& buttonInfos)
95 {
96 auto pickerText = AceType::DynamicCast<PickerTextComponent>(PickerText);
97 pickerText->SetIsDialog(true);
98 pickerText->SetIsCreateDialogComponent(true);
99 if (textPickerDialog.isDefaultHeight == true) {
100 pickerText->SetColumnHeight(textPickerDialog.height);
101 pickerText->SetDefaultHeight(true);
102 }
103 pickerText->SetSelected(textPickerDialog.selectedValue);
104 pickerText->SetRange(textPickerDialog.getRangeVector);
105
106 DialogProperties properties {};
107 properties.alignment = Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN) ? DialogAlignment::CENTER
108 : DialogAlignment::DEFAULT;
109 properties.customComponent = pickerText;
110 properties.customStyle = true;
111 auto acceptId = EventMarker(std::move(onAccept));
112 pickerText->SetDialogAcceptEvent(acceptId);
113 auto cancelId = EventMarker(std::move(onCancel));
114 pickerText->SetDialogCancelEvent(cancelId);
115 auto changeId = EventMarker(std::move(onChange));
116 pickerText->SetDialogChangeEvent(changeId);
117 pickerText->SetDialogName("pickerTextDialog");
118 pickerText->OpenDialog(properties);
119 }
120 } // namespace OHOS::Ace::Framework
121