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/select_model_impl.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
19 #include "core/components/select/select_component.h"
20 #include "core/components/select/select_theme.h"
21 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
22 
23 namespace OHOS::Ace::Framework {
Create(const std::vector<SelectParam> & params)24 void SelectModelImpl::Create(const std::vector<SelectParam>& params)
25 {
26     auto selectTheme = JSViewAbstract::GetTheme<SelectTheme>();
27     auto selectComponent = AceType::MakeRefPtr<SelectComponent>();
28     CHECK_NULL_VOID(selectComponent);
29     selectComponent->SetTheme(selectTheme);
30 
31     auto tipText = AceType::MakeRefPtr<TextComponent>("");
32     selectComponent->SetTipText(tipText);
33 
34     for (size_t i = 0; i < params.size(); i++) {
35         auto optionTheme = JSViewAbstract::GetTheme<SelectTheme>();
36             if (!optionTheme) {
37                 LOGE("JSSelect: Get option theme is null.");
38                 continue;
39             }
40             auto optionComponent = AceType::MakeRefPtr<OHOS::Ace::OptionComponent>(optionTheme);
41             auto textComponent = AceType::MakeRefPtr<OHOS::Ace::TextComponent>(params[i].text);
42             if (!params[i].icon.empty()) {
43                 optionComponent->SetIcon(AceType::MakeRefPtr<OHOS::Ace::ImageComponent>(params[i].icon));
44             }
45             optionComponent->SetTheme(optionTheme);
46             optionComponent->SetText(textComponent);
47             optionComponent->SetTextStyle(optionTheme->GetTitleStyle());
48             optionComponent->SetSelectedTextStyle(optionTheme->GetTitleStyle());
49             optionComponent->SetSelectedBackgroundColor(optionTheme->GetSelectedColor());
50             optionComponent->SetValue(params[i].text);
51             selectComponent->AppendSelectOption(optionComponent);
52     }
53     ViewStackProcessor::GetInstance()->ClaimElementId(selectComponent);
54     ViewStackProcessor::GetInstance()->Push(selectComponent);
55 }
56 
SetSelected(int32_t idx)57 void SelectModelImpl::SetSelected(int32_t idx)
58 {
59     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
60     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
61     CHECK_NULL_VOID(selectComponent);
62     auto popup = selectComponent->GetPopup();
63     CHECK_NULL_VOID(popup);
64     auto option = popup->GetSelectOptions();
65     if (idx >= static_cast<int32_t>(option.size())) {
66         LOGE("Input selected index error, use the default value");
67         idx = 0;
68     }
69 
70     auto tipText = selectComponent->GetTipText();
71     auto optionComponent = selectComponent->GetSelectOption(idx);
72     CHECK_NULL_VOID(optionComponent);
73     optionComponent->SetSelected(true);
74 
75     auto optionText = optionComponent->GetText();
76     CHECK_NULL_VOID(optionText);
77     if (!selectComponent->HasSetTipText()) {
78         tipText->SetData(optionText->GetData());
79     }
80 }
81 
SetValue(const std::string & value)82 void SelectModelImpl::SetValue(const std::string& value)
83 {
84     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
85     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
86     CHECK_NULL_VOID(selectComponent);
87     auto tipText = selectComponent->GetTipText();
88     if (!value.empty()) {
89         selectComponent->SetTipText(true);
90     }
91     tipText->SetData(value);
92 }
93 
SetFontSize(const Dimension & value)94 void SelectModelImpl::SetFontSize(const Dimension& value)
95 {
96     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
97     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
98     CHECK_NULL_VOID(selectComponent);
99 
100     TextStyle textStyle = selectComponent->GetSelectStyle();
101     textStyle.SetFontSize(value);
102     selectComponent->SetSelectStyle(std::move(textStyle));
103 }
104 
SetFontWeight(const FontWeight & value)105 void SelectModelImpl::SetFontWeight(const FontWeight& value)
106 {
107     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
108     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
109     CHECK_NULL_VOID(selectComponent);
110 
111     TextStyle textStyle = selectComponent->GetSelectStyle();
112     textStyle.SetFontWeight(value);
113     selectComponent->SetSelectStyle(std::move(textStyle));
114 }
115 
SetFontFamily(const std::vector<std::string> & value)116 void SelectModelImpl::SetFontFamily(const std::vector<std::string>& value)
117 {
118     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
119     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
120     CHECK_NULL_VOID(selectComponent);
121 
122     TextStyle textStyle = selectComponent->GetSelectStyle();
123     textStyle.SetFontFamilies(value);
124     selectComponent->SetSelectStyle(std::move(textStyle));
125 }
126 
SetItalicFontStyle(const Ace::FontStyle & value)127 void SelectModelImpl::SetItalicFontStyle(const Ace::FontStyle& value)
128 {
129     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
130     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
131     CHECK_NULL_VOID(selectComponent);
132 
133     TextStyle textStyle = selectComponent->GetSelectStyle();
134     textStyle.SetFontStyle(value);
135     selectComponent->SetSelectStyle(std::move(textStyle));
136 }
137 
SetFontColor(const Color & color)138 void SelectModelImpl::SetFontColor(const Color& color)
139 {
140     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
141     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
142     CHECK_NULL_VOID(selectComponent);
143 
144     auto textStyle = selectComponent->GetSelectStyle();
145     textStyle.SetTextColor(color);
146     selectComponent->SetSelectStyle(std::move(textStyle));
147 }
148 
SetSelectedOptionBgColor(const Color & color)149 void SelectModelImpl::SetSelectedOptionBgColor(const Color& color)
150 {
151     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
152     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
153     CHECK_NULL_VOID(selectComponent);
154     auto popup = selectComponent->GetPopup();
155     CHECK_NULL_VOID(popup);
156     auto option = popup->GetSelectOptions();
157     for (auto& optionItem : option) {
158         if (optionItem) {
159             optionItem->SetSelectedBackgroundColor(color);
160         }
161     }
162 }
163 
SetSelectedOptionFontSize(const Dimension & value)164 void SelectModelImpl::SetSelectedOptionFontSize(const Dimension& value)
165 {
166     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
167     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
168     CHECK_NULL_VOID(selectComponent);
169     auto popup = selectComponent->GetPopup();
170     CHECK_NULL_VOID(popup);
171     auto option = popup->GetSelectOptions();
172     for (auto& optionItem : option) {
173         TextStyle textStyle = optionItem->GetSelectedTextStyle();
174         textStyle.SetFontSize(value);
175         optionItem->SetSelectedTextStyle(std::move(textStyle));
176     }
177 }
178 
SetSelectedOptionFontWeight(const FontWeight & value)179 void SelectModelImpl::SetSelectedOptionFontWeight(const FontWeight& value)
180 {
181     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
182     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
183     CHECK_NULL_VOID(selectComponent);
184     auto popup = selectComponent->GetPopup();
185     CHECK_NULL_VOID(popup);
186     auto option = popup->GetSelectOptions();
187     for (auto& optionItem : option) {
188         TextStyle textStyle = optionItem->GetSelectedTextStyle();
189         textStyle.SetFontWeight(value);
190         optionItem->SetSelectedTextStyle(std::move(textStyle));
191     }
192 }
193 
SetSelectedOptionFontFamily(const std::vector<std::string> & value)194 void SelectModelImpl::SetSelectedOptionFontFamily(const std::vector<std::string>& value)
195 {
196     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
197     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
198     CHECK_NULL_VOID(selectComponent);
199     auto popup = selectComponent->GetPopup();
200     CHECK_NULL_VOID(popup);
201     auto option = popup->GetSelectOptions();
202     for (auto& optionItem : option) {
203         TextStyle textStyle = optionItem->GetSelectedTextStyle();
204         textStyle.SetFontFamilies(value);
205         optionItem->SetSelectedTextStyle(std::move(textStyle));
206     }
207 }
208 
SetSelectedOptionItalicFontStyle(const Ace::FontStyle & value)209 void SelectModelImpl::SetSelectedOptionItalicFontStyle(const Ace::FontStyle& value)
210 {
211     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
212     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
213     CHECK_NULL_VOID(selectComponent);
214     auto popup = selectComponent->GetPopup();
215     CHECK_NULL_VOID(popup);
216     auto option = popup->GetSelectOptions();
217     for (auto& optionItem : option) {
218         TextStyle textStyle = optionItem->GetSelectedTextStyle();
219         textStyle.SetFontStyle(value);
220         optionItem->SetSelectedTextStyle(std::move(textStyle));
221     }
222 }
223 
SetSelectedOptionFontColor(const Color & color)224 void SelectModelImpl::SetSelectedOptionFontColor(const Color& color)
225 {
226     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
227     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
228     CHECK_NULL_VOID(selectComponent);
229     auto popup = selectComponent->GetPopup();
230     CHECK_NULL_VOID(popup);
231     auto option = popup->GetSelectOptions();
232     for (auto& optionItem : option) {
233         if (optionItem) {
234             TextStyle textStyle = optionItem->GetSelectedTextStyle();
235             textStyle.SetTextColor(color);
236             optionItem->SetSelectedTextStyle(textStyle);
237         }
238     }
239 }
240 
SetOptionBgColor(const Color & color)241 void SelectModelImpl::SetOptionBgColor(const Color& color)
242 {
243     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
244     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
245     CHECK_NULL_VOID(selectComponent);
246     auto popup = selectComponent->GetPopup();
247     CHECK_NULL_VOID(popup);
248     auto option = popup->GetSelectOptions();
249     for (auto& optionItem : option) {
250         if (optionItem) {
251             optionItem->SetBackgroundColor(color);
252         }
253     }
254 }
255 
SetOptionFontSize(const Dimension & value)256 void SelectModelImpl::SetOptionFontSize(const Dimension& value)
257 {
258     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
259     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
260     CHECK_NULL_VOID(selectComponent);
261     auto popup = selectComponent->GetPopup();
262     CHECK_NULL_VOID(popup);
263     auto option = popup->GetSelectOptions();
264     for (auto& optionItem : option) {
265         TextStyle textStyle = optionItem->GetTextStyle();
266         textStyle.SetFontSize(value);
267         optionItem->SetTextStyle(std::move(textStyle));
268     }
269 }
270 
SetOptionFontWeight(const FontWeight & value)271 void SelectModelImpl::SetOptionFontWeight(const FontWeight& value)
272 {
273     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
274     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
275     CHECK_NULL_VOID(selectComponent);
276     auto popup = selectComponent->GetPopup();
277     CHECK_NULL_VOID(popup);
278     auto option = popup->GetSelectOptions();
279     for (auto& optionItem : option) {
280         TextStyle textStyle = optionItem->GetTextStyle();
281         textStyle.SetFontWeight(value);
282         optionItem->SetTextStyle(std::move(textStyle));
283     }
284 }
285 
SetOptionFontFamily(const std::vector<std::string> & value)286 void SelectModelImpl::SetOptionFontFamily(const std::vector<std::string>& value)
287 {
288     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
289     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
290     CHECK_NULL_VOID(selectComponent);
291     auto popup = selectComponent->GetPopup();
292     CHECK_NULL_VOID(popup);
293     auto option = popup->GetSelectOptions();
294     for (auto& optionItem : option) {
295         TextStyle textStyle = optionItem->GetTextStyle();
296         textStyle.SetFontFamilies(value);
297         optionItem->SetTextStyle(std::move(textStyle));
298     }
299 }
300 
SetOptionItalicFontStyle(const Ace::FontStyle & value)301 void SelectModelImpl::SetOptionItalicFontStyle(const Ace::FontStyle& value)
302 {
303     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
304     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
305     CHECK_NULL_VOID(selectComponent);
306     auto popup = selectComponent->GetPopup();
307     CHECK_NULL_VOID(popup);
308     auto option = popup->GetSelectOptions();
309     for (auto& optionItem : option) {
310         TextStyle textStyle = optionItem->GetTextStyle();
311         textStyle.SetFontStyle(value);
312         optionItem->SetTextStyle(std::move(textStyle));
313     }
314 }
315 
SetOptionFontColor(const Color & color)316 void SelectModelImpl::SetOptionFontColor(const Color& color)
317 {
318     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
319     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
320     CHECK_NULL_VOID(selectComponent);
321     auto popup = selectComponent->GetPopup();
322     CHECK_NULL_VOID(popup);
323     auto option = popup->GetSelectOptions();
324     for (auto& optionItem : option) {
325         if (optionItem) {
326             TextStyle textStyle = optionItem->GetTextStyle();
327             textStyle.SetTextColor(color);
328             optionItem->SetTextStyle(textStyle);
329         }
330     }
331 }
332 
SetOnSelect(NG::SelectEvent && onSelect)333 void SelectModelImpl::SetOnSelect(NG::SelectEvent&& onSelect)
334 {
335     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
336     auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
337     CHECK_NULL_VOID(selectComponent);
338     selectComponent->SetOnSelected(std::move(onSelect));
339 }
340 
SetWidth(Dimension & value)341 void SelectModelImpl::SetWidth(Dimension& value)
342 {
343     auto stack = ViewStackProcessor::GetInstance();
344     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
345     CHECK_NULL_VOID(selectComponent);
346     selectComponent->SetWidth(value);
347 }
348 
SetHeight(Dimension & value)349 void SelectModelImpl::SetHeight(Dimension& value)
350 {
351     auto stack = ViewStackProcessor::GetInstance();
352     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
353     CHECK_NULL_VOID(selectComponent);
354     selectComponent->SetHeight(value);
355 }
356 
SetSize(Dimension & width,Dimension & height)357 void SelectModelImpl::SetSize(Dimension& width, Dimension& height)
358 {
359     auto stack = ViewStackProcessor::GetInstance();
360     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
361     CHECK_NULL_VOID(selectComponent);
362     selectComponent->SetWidth(width);
363     selectComponent->SetHeight(height);
364 }
365 
SetPaddings(const std::optional<CalcDimension> & top,const std::optional<CalcDimension> & bottom,const std::optional<CalcDimension> & left,const std::optional<CalcDimension> & right)366 void SelectModelImpl::SetPaddings(const std::optional<CalcDimension>& top, const std::optional<CalcDimension>& bottom,
367     const std::optional<CalcDimension>& left, const std::optional<CalcDimension>& right)
368 {
369     auto stack = ViewStackProcessor::GetInstance();
370     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
371     CHECK_NULL_VOID(selectComponent);
372     CalcDimension topDimen;
373     topDimen.SetValue(top.value().Value());
374     selectComponent->SetTopPadding(topDimen);
375     CalcDimension leftDimen;
376     leftDimen.SetValue(left.value().Value());
377     selectComponent->SetLeftPadding(leftDimen);
378     CalcDimension rightDimen;
379     rightDimen.SetValue(right.value().Value());
380     selectComponent->SetRightPadding(rightDimen);
381     CalcDimension bottomDimen;
382     bottomDimen.SetValue(bottom.value().Value());
383     selectComponent->SetBottomPadding(bottomDimen);
384 }
385 
SetPadding(const CalcDimension & value)386 void SelectModelImpl::SetPadding(const CalcDimension& value)
387 {
388     auto stack = ViewStackProcessor::GetInstance();
389     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
390     CHECK_NULL_VOID(selectComponent);
391     selectComponent->SetLeftPadding(value);
392     selectComponent->SetTopPadding(value);
393     selectComponent->SetRightPadding(value);
394     selectComponent->SetBottomPadding(value);
395 }
396 
SetPaddingLeft(const CalcDimension & value)397 void SelectModelImpl::SetPaddingLeft(const CalcDimension& value)
398 {
399     auto stack = ViewStackProcessor::GetInstance();
400     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
401     CHECK_NULL_VOID(selectComponent);
402     selectComponent->SetLeftPadding(value);
403 }
404 
SetPaddingTop(const CalcDimension & value)405 void SelectModelImpl::SetPaddingTop(const CalcDimension& value)
406 {
407     auto stack = ViewStackProcessor::GetInstance();
408     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
409     CHECK_NULL_VOID(selectComponent);
410     selectComponent->SetTopPadding(value);
411 }
412 
SetPaddingRight(const CalcDimension & value)413 void SelectModelImpl::SetPaddingRight(const CalcDimension& value)
414 {
415     auto stack = ViewStackProcessor::GetInstance();
416     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
417     CHECK_NULL_VOID(selectComponent);
418     selectComponent->SetRightPadding(value);
419 }
420 
SetPaddingBottom(const CalcDimension & value)421 void SelectModelImpl::SetPaddingBottom(const CalcDimension& value)
422 {
423     auto stack = ViewStackProcessor::GetInstance();
424     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
425     CHECK_NULL_VOID(selectComponent);
426     selectComponent->SetBottomPadding(value);
427 }
428 
SetSpace(const Dimension & value)429 void SelectModelImpl::SetSpace(const Dimension& value) {}
430 
SetArrowPosition(const ArrowPosition value)431 void SelectModelImpl::SetArrowPosition(const ArrowPosition value) {}
432 
SetMenuAlign(const MenuAlign & menuAlign)433 void SelectModelImpl::SetMenuAlign(const MenuAlign& menuAlign) {}
434 
SetSelectChangeEvent(NG::SelectChangeEvent && selectChangeEvent)435 void SelectModelImpl::SetSelectChangeEvent(NG::SelectChangeEvent&& selectChangeEvent) {}
436 
SetValueChangeEvent(NG::ValueChangeEvent && valueChangeEvent)437 void SelectModelImpl::SetValueChangeEvent(NG::ValueChangeEvent&& valueChangeEvent) {}
438 
SetOptionWidth(const Dimension & value)439 void SelectModelImpl::SetOptionWidth(const Dimension& value) {}
440 
SetOptionHeight(const Dimension & value)441 void SelectModelImpl::SetOptionHeight(const Dimension& value) {}
442 
SetOptionWidthFitTrigger(bool isFitTrigger)443 void SelectModelImpl::SetOptionWidthFitTrigger(bool isFitTrigger) {}
444 
SetHasOptionWidth(bool haveOptionWidth)445 void SelectModelImpl::SetHasOptionWidth(bool haveOptionWidth) {}
446 
SetMenuBackgroundColor(const Color & color)447 void SelectModelImpl::SetMenuBackgroundColor(const Color& color) {}
448 
SetMenuBackgroundBlurStyle(const BlurStyleOption & blurStyle)449 void SelectModelImpl::SetMenuBackgroundBlurStyle(const BlurStyleOption& blurStyle) {}
450 
SetLayoutDirection(TextDirection value)451 void SelectModelImpl::SetLayoutDirection(TextDirection value)
452 {
453     auto stack = ViewStackProcessor::GetInstance();
454     auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
455     CHECK_NULL_VOID(selectComponent);
456     selectComponent->SetTextDirection(value);
457 }
458 } // namespace OHOS::Ace::Framework
459