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/indexer_model_impl.h"
17
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19
20 namespace OHOS::Ace::Framework {
Create(std::vector<std::string> & indexerArray,int32_t selectedVal,bool isArc)21 void IndexerModelImpl::Create(std::vector<std::string>& indexerArray, int32_t selectedVal, bool isArc)
22 {
23 if (static_cast<int32_t>(indexerArray.size()) <= 0) {
24 return;
25 }
26 auto indexerComponent = AceType::MakeRefPtr<V2::IndexerComponent>(indexerArray, selectedVal);
27 ViewStackProcessor::GetInstance()->ClaimElementId(indexerComponent);
28 ViewStackProcessor::GetInstance()->Push(indexerComponent);
29 auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent();
30 CHECK_NULL_VOID(focusableComponent);
31 focusableComponent->SetFocusable(true);
32 focusableComponent->SetFocusNode(true);
33 }
34
SetSelectedColor(const std::optional<Color> & color)35 void IndexerModelImpl::SetSelectedColor(const std::optional<Color>& color)
36 {
37 auto indexerComponent =
38 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
39 if (indexerComponent && color.has_value()) {
40 auto textStyle = indexerComponent->GetActiveTextStyle();
41 textStyle.SetTextColor(color.value());
42 indexerComponent->SetActiveTextStyle(std::move(textStyle));
43 }
44 }
45
SetColor(const std::optional<Color> & color)46 void IndexerModelImpl::SetColor(const std::optional<Color>& color)
47 {
48 auto indexerComponent =
49 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
50 if (indexerComponent && color.has_value()) {
51 auto textStyle = indexerComponent->GetNormalTextStyle();
52 textStyle.SetTextColor(color.value());
53 indexerComponent->SetNormalTextStyle(std::move(textStyle));
54 }
55 }
56
SetPopupColor(const std::optional<Color> & color)57 void IndexerModelImpl::SetPopupColor(const std::optional<Color>& color)
58 {
59 auto indexerComponent =
60 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
61 if (indexerComponent && color.has_value()) {
62 auto textStyle = indexerComponent->GetBubbleTextStyle();
63 textStyle.SetTextColor(color.value());
64 indexerComponent->SetBubbleTextStyle(std::move(textStyle));
65 }
66 }
67
SetSelectedBackgroundColor(const std::optional<Color> & color)68 void IndexerModelImpl::SetSelectedBackgroundColor(const std::optional<Color>& color)
69 {
70 auto indexerComponent =
71 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
72 if (indexerComponent && color.has_value()) {
73 indexerComponent->SetSelectedBackgroundColor(color.value());
74 }
75 }
76
SetPopupBackground(const std::optional<Color> & color)77 void IndexerModelImpl::SetPopupBackground(const std::optional<Color>& color)
78 {
79 auto indexerComponent =
80 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
81 if (indexerComponent && color.has_value()) {
82 indexerComponent->SetBubbleBackgroundColor(color.value());
83 }
84 }
85
SetUsingPopup(bool state)86 void IndexerModelImpl::SetUsingPopup(bool state)
87 {
88 auto indexerComponent =
89 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
90 if (indexerComponent) {
91 indexerComponent->SetBubbleEnabled(state);
92 }
93 }
94
SetSelectedFont(std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)95 void IndexerModelImpl::SetSelectedFont(std::optional<Dimension>& fontSize, std::optional<FontWeight>& fontWeight,
96 std::optional<std::vector<std::string>>& fontFamily, std::optional<FontStyle>& fontStyle)
97 {
98 auto indexerComponent =
99 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
100 if (indexerComponent) {
101 TextStyle textStyle;
102 SetTextStyle(textStyle, fontSize, fontWeight, fontFamily, fontStyle);
103 indexerComponent->SetActiveTextStyle(textStyle);
104 }
105 }
106
SetPopupFont(std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)107 void IndexerModelImpl::SetPopupFont(std::optional<Dimension>& fontSize, std::optional<FontWeight>& fontWeight,
108 std::optional<std::vector<std::string>>& fontFamily, std::optional<FontStyle>& fontStyle)
109 {
110 auto indexerComponent =
111 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
112 if (indexerComponent) {
113 TextStyle textStyle;
114 SetTextStyle(textStyle, fontSize, fontWeight, fontFamily, fontStyle);
115 indexerComponent->SetBubbleTextStyle(textStyle);
116 }
117 }
118
SetFont(std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)119 void IndexerModelImpl::SetFont(std::optional<Dimension>& fontSize, std::optional<FontWeight>& fontWeight,
120 std::optional<std::vector<std::string>>& fontFamily, std::optional<FontStyle>& fontStyle)
121 {
122 auto indexerComponent =
123 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
124 if (indexerComponent) {
125 TextStyle textStyle;
126 SetTextStyle(textStyle, fontSize, fontWeight, fontFamily, fontStyle);
127 indexerComponent->SetNormalTextStyle(textStyle);
128 }
129 }
130
SetItemSize(const Dimension & value)131 void IndexerModelImpl::SetItemSize(const Dimension& value)
132 {
133 auto indexerComponent =
134 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
135 if (indexerComponent) {
136 indexerComponent->SetItemSize(value);
137 }
138 }
139
SetAlignStyle(int32_t value)140 void IndexerModelImpl::SetAlignStyle(int32_t value)
141 {
142 auto indexerComponent =
143 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
144 if (indexerComponent) {
145 indexerComponent->SetAlignStyle(ALIGN_STYLE[value]);
146 }
147 }
148
SetOnSelected(std::function<void (const int32_t selected)> && onSelect)149 void IndexerModelImpl::SetOnSelected(std::function<void(const int32_t selected)>&& onSelect)
150 {
151 auto fun = onSelect;
152 auto onSelectEvent = [fun](const BaseEventInfo* info) {
153 auto eventInfo = TypeInfoHelper::DynamicCast<V2::IndexerEventInfo>(info);
154 if (!eventInfo) {
155 return;
156 }
157 fun(eventInfo->GetSelectedIndex());
158 };
159
160 auto indexerComponent =
161 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
162 auto eventMarker = EventMarker(std::move(onSelectEvent));
163 if (indexerComponent) {
164 indexerComponent->SetSelectedEvent(eventMarker);
165 }
166 }
167
SetOnRequestPopupData(std::function<std::vector<std::string> (const int32_t selected)> && RequestPopupData)168 void IndexerModelImpl::SetOnRequestPopupData(
169 std::function<std::vector<std::string>(const int32_t selected)>&& RequestPopupData)
170 {
171 auto fun = RequestPopupData;
172 auto onSelectEvent = [fun](std::shared_ptr<V2::IndexerEventInfo> info) -> std::vector<std::string> {
173 return fun(info->GetSelectedIndex());
174 };
175
176 auto indexerComponent =
177 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
178 if (indexerComponent) {
179 indexerComponent->SetRequestPopupDataFunc(std::move(onSelectEvent));
180 }
181 }
182
SetOnPopupSelected(std::function<void (const int32_t selected)> && onPopupSelected)183 void IndexerModelImpl::SetOnPopupSelected(std::function<void(const int32_t selected)>&& onPopupSelected)
184 {
185 auto fun = onPopupSelected;
186 auto onPopupSelectedEvent = [fun](const BaseEventInfo* info) {
187 auto eventInfo = TypeInfoHelper::DynamicCast<V2::IndexerEventInfo>(info);
188 if (!eventInfo) {
189 return;
190 }
191 fun(eventInfo->GetSelectedIndex());
192 };
193
194 auto indexerComponent =
195 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
196 auto eventMarker = EventMarker(std::move(onPopupSelectedEvent));
197 if (indexerComponent) {
198 indexerComponent->SetPopupSelectedEvent(eventMarker);
199 }
200 }
201
SetTextStyle(TextStyle & textStyle,std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)202 void IndexerModelImpl::SetTextStyle(TextStyle& textStyle, std::optional<Dimension>& fontSize,
203 std::optional<FontWeight>& fontWeight, std::optional<std::vector<std::string>>& fontFamily,
204 std::optional<FontStyle>& fontStyle)
205 {
206 if (fontSize.has_value()) {
207 textStyle.SetFontSize(fontSize.value());
208 }
209 if (fontWeight.has_value()) {
210 textStyle.SetFontWeight(fontWeight.value());
211 }
212 if (fontFamily.has_value()) {
213 textStyle.SetFontFamilies(fontFamily.value());
214 }
215 if (fontStyle.has_value()) {
216 textStyle.SetFontStyle(fontStyle.value());
217 }
218 }
219 } // namespace OHOS::Ace::Framework
220