• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetBackgroundColor(const Color & color)69 void TextPickerModelImpl::SetBackgroundColor(const Color& color)
70 {
71     auto pickerBase = AceType::DynamicCast<PickerBaseComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
72     if (!pickerBase) {
73         LOGE("PickerBaseComponent is null");
74         return;
75     }
76 
77     pickerBase->SetHasBackgroundColor(true);
78 }
79 
CreateObject()80 RefPtr<AceType> TextPickerDialogModelImpl::CreateObject()
81 {
82     return AceType::MakeRefPtr<PickerTextComponent>();
83 }
84 
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,TextPickerDialogEvent & textPickerDialogEvent,const std::vector<ButtonInfo> & buttonInfos)85 void TextPickerDialogModelImpl::SetTextPickerDialogShow(RefPtr<AceType>& PickerText,
86     NG::TextPickerSettingData& settingData, std::function<void()>&& onCancel,
87     std::function<void(const std::string&)>&& onAccept, std::function<void(const std::string&)>&& onChange,
88     TextPickerDialog& textPickerDialog, TextPickerDialogEvent& textPickerDialogEvent,
89     const std::vector<ButtonInfo>& buttonInfos)
90 {
91     auto pickerText = AceType::DynamicCast<PickerTextComponent>(PickerText);
92     pickerText->SetIsDialog(true);
93     pickerText->SetIsCreateDialogComponent(true);
94     if (textPickerDialog.isDefaultHeight == true) {
95         pickerText->SetColumnHeight(textPickerDialog.height);
96         pickerText->SetDefaultHeight(true);
97     }
98     pickerText->SetSelected(textPickerDialog.selectedValue);
99     pickerText->SetRange(textPickerDialog.getRangeVector);
100 
101     DialogProperties properties {};
102     properties.alignment = Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN) ? DialogAlignment::CENTER
103                                                                                           : DialogAlignment::DEFAULT;
104     properties.customComponent = pickerText;
105     properties.customStyle = true;
106     auto acceptId = EventMarker(std::move(onAccept));
107     pickerText->SetDialogAcceptEvent(acceptId);
108     auto cancelId = EventMarker(std::move(onCancel));
109     pickerText->SetDialogCancelEvent(cancelId);
110     auto changeId = EventMarker(std::move(onChange));
111     pickerText->SetDialogChangeEvent(changeId);
112     pickerText->SetDialogName("pickerTextDialog");
113     pickerText->OpenDialog(properties);
114 }
115 } // namespace OHOS::Ace::Framework
116