1 /*
2  * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_SELECT_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_SELECT_COMPONENT_H
18 
19 #include <vector>
20 
21 #include "base/geometry/dimension.h"
22 #include "core/components/box/box_component.h"
23 #include "core/components/common/properties/color.h"
24 #include "core/components/select/select_theme.h"
25 #include "core/components/select_popup/select_popup_component.h"
26 #include "core/components/text/text_component.h"
27 #include "core/components_v2/common/common_def.h"
28 #include "core/pipeline/base/sole_child_component.h"
29 
30 namespace OHOS::Ace {
31 
32 class ACE_EXPORT SelectComponent : public SoleChildComponent {
33     DECLARE_ACE_TYPE(SelectComponent, SoleChildComponent);
34 
35 public:
36     SelectComponent();
37     ~SelectComponent() override = default;
38 
39     RefPtr<RenderNode> CreateRenderNode() override;
40     RefPtr<Element> CreateElement() override;
41 
42     void InitTheme(const RefPtr<ThemeManager>& themeManager);
43 
SetTextDirection(TextDirection direction)44     void SetTextDirection(TextDirection direction) override
45     {
46         SoleChildComponent::SetTextDirection(direction);
47         if (popup_) {
48             popup_->SetTextDirection(direction);
49         } else {
50             LOGE("popup component of select is null.");
51         }
52     }
53 
HasSetTipText()54     bool HasSetTipText() const
55     {
56         return hasSetTipText_;
57     }
58 
SetTipText(bool hasSetTipText)59     void SetTipText(bool hasSetTipText)
60     {
61         hasSetTipText_ = hasSetTipText;
62     }
63 
GetDisabled()64     bool GetDisabled() const
65     {
66         return disabled_;
67     }
SetDisabled(bool disabled)68     void SetDisabled(bool disabled)
69     {
70         disabled_ = disabled;
71     }
72 
GetClicked()73     bool GetClicked() const
74     {
75         return clicked_;
76     }
77     void SetClicked(bool clicked, const Color& pressColor);
78 
GetClickedColor()79     const Color& GetClickedColor() const
80     {
81         return theme_->GetClickedColor();
82     }
SetClickedColor(const Color & clickedColor)83     void SetClickedColor(const Color& clickedColor)
84     {
85         theme_->SetClickedColor(clickedColor);
86     }
87 
GetSelectedColor()88     const Color& GetSelectedColor() const
89     {
90         return theme_->GetSelectedColor();
91     }
SetSelectedColor(const Color & selectedColor)92     void SetSelectedColor(const Color& selectedColor)
93     {
94         theme_->SetSelectedColor(selectedColor);
95     }
96 
GetDisabledColor()97     const Color& GetDisabledColor() const
98     {
99         return theme_->GetDisabledColor();
100     }
SetDisabledColor(const Color & disabledColor)101     void SetDisabledColor(const Color& disabledColor)
102     {
103         theme_->SetDisabledColor(disabledColor);
104     }
105 
IsAllowScale()106     bool IsAllowScale() const
107     {
108         return theme_->IsAllowScale();
109     }
SetAllowScale(bool value)110     void SetAllowScale(bool value)
111     {
112         theme_->SetAllowScale(value);
113     }
114 
GetTextColor()115     const Color& GetTextColor() const
116     {
117         return theme_->GetFontColor();
118     }
SetTextColor(const Color & value)119     void SetTextColor(const Color& value)
120     {
121         theme_->SetFontColor(value);
122     }
123 
GetFontWeight()124     FontWeight GetFontWeight() const
125     {
126         return theme_->GetFontWeight();
127     }
SetFontWeight(FontWeight value)128     void SetFontWeight(FontWeight value)
129     {
130         theme_->SetFontWeight(value);
131     }
132 
GetTextDecoration()133     TextDecoration GetTextDecoration() const
134     {
135         return theme_->GetTextDecoration();
136     }
SetTextDecoration(TextDecoration value)137     void SetTextDecoration(TextDecoration value)
138     {
139         theme_->SetTextDecoration(value);
140     }
141 
GetOnChanged()142     const EventMarker& GetOnChanged() const
143     {
144         return onChanged_;
145     }
SetOnChanged(const EventMarker & onChanged)146     void SetOnChanged(const EventMarker& onChanged)
147     {
148         onChanged_ = onChanged;
149     }
150 
GetTipText()151     const RefPtr<TextComponent>& GetTipText() const
152     {
153         return tipText_;
154     }
SetTipText(const RefPtr<TextComponent> & tipText)155     void SetTipText(const RefPtr<TextComponent>& tipText)
156     {
157         tipText_ = tipText;
158     }
159 
IsFontSet()160     bool IsFontSet() const
161     {
162         return isSelectFontSize_;
163     }
164 
SetIsFontSetFlag(bool flag)165     void SetIsFontSetFlag(bool flag)
166     {
167         isSelectFontSize_ = flag;
168     }
169 
GetFontSize()170     const Dimension& GetFontSize() const
171     {
172         return theme_->GetFontSize();
173     }
SetFontSize(const Dimension & fontSize)174     void SetFontSize(const Dimension& fontSize)
175     {
176         theme_->SetFontSize(fontSize);
177     }
178 
GetFontFamily()179     const std::string& GetFontFamily() const
180     {
181         return theme_->GetFontFamily();
182     }
SetFontFamily(const std::string & fontFamily)183     void SetFontFamily(const std::string& fontFamily)
184     {
185         theme_->SetFontFamily(fontFamily);
186     }
187 
GetOptionSize()188     std::size_t GetOptionSize() const
189     {
190         return theme_->GetOptionSize();
191     }
SetOptionSize(std::size_t optionSize)192     void SetOptionSize(std::size_t optionSize)
193     {
194         theme_->SetOptionSize(optionSize);
195     }
196 
GetRRectSize()197     const Dimension& GetRRectSize() const
198     {
199         return theme_->GetRRectSize();
200     }
SetRRectSize(const Dimension & rrectSize)201     void SetRRectSize(const Dimension& rrectSize)
202     {
203         theme_->SetRRectSize(rrectSize);
204     }
205 
GetPopupShadowWidth()206     const Dimension& GetPopupShadowWidth() const
207     {
208         return theme_->GetPopupShadowWidth();
209     }
SetPopupShadowWidth(const Dimension & popupShadowWidth)210     void SetPopupShadowWidth(const Dimension& popupShadowWidth)
211     {
212         theme_->SetPopupShadowWidth(popupShadowWidth);
213     }
214 
GetPopupBorderWidth()215     const Dimension& GetPopupBorderWidth() const
216     {
217         return theme_->GetPopupBorderWidth();
218     }
SetPopupBorderWidth(const Dimension & popupBorderWidth)219     void SetPopupBorderWidth(const Dimension& popupBorderWidth)
220     {
221         theme_->SetPopupBorderWidth(popupBorderWidth);
222     }
223 
GetNormalPadding()224     const Dimension& GetNormalPadding() const
225     {
226         return theme_->GetNormalPadding();
227     }
228 
SetOptionClickedCallback(const std::function<void (std::size_t)> & clickedCallback)229     void SetOptionClickedCallback(const std::function<void(std::size_t)>& clickedCallback)
230     {
231         if (popup_) {
232             popup_->SetOptionClickedCallback(clickedCallback);
233         }
234     }
235 
SetOptionModifiedCallback(const std::function<void (std::size_t)> & value)236     void SetOptionModifiedCallback(const std::function<void(std::size_t)>& value)
237     {
238         if (popup_) {
239             popup_->SetOptionModifiedCallback(value);
240         }
241     }
242 
GetSelectOptionCount()243     std::size_t GetSelectOptionCount() const
244     {
245         if (!popup_) {
246             return 0;
247         }
248 
249         return popup_->GetSelectOptionCount();
250     }
251 
GetSelectOption(std::size_t index)252     RefPtr<OptionComponent> GetSelectOption(std::size_t index)
253     {
254         if (!popup_) {
255             return nullptr;
256         }
257         selected_ = index;
258         return popup_->GetSelectOption(index);
259     }
260 
GetSelected()261     std::size_t GetSelected() const
262     {
263         return selected_;
264     }
265 
ClearAllOptions()266     void ClearAllOptions()
267     {
268         if (popup_) {
269             popup_->ClearAllOptions();
270         }
271     }
272 
AppendSelectOption(const RefPtr<OptionComponent> & selectOption)273     void AppendSelectOption(const RefPtr<OptionComponent>& selectOption)
274     {
275         if (popup_) {
276             popup_->AppendSelectOption(selectOption);
277         }
278     }
279 
RemoveSelectOption(const RefPtr<OptionComponent> & selectOption)280     void RemoveSelectOption(const RefPtr<OptionComponent>& selectOption)
281     {
282         if (popup_) {
283             popup_->RemoveSelectOption(selectOption);
284         }
285     }
286 
InsertSelectOption(const RefPtr<OptionComponent> & selectOption,uint32_t index)287     void InsertSelectOption(const RefPtr<OptionComponent>& selectOption, uint32_t index)
288     {
289         if (popup_) {
290             popup_->InsertSelectOption(selectOption, index);
291         }
292     }
293 
294     RefPtr<SelectPopupComponent> GetPopup() const;
295 
296     RefPtr<BoxComponent> GetBoxComponent() const;
297 
298     bool Initialize();
299 
FlushRefresh()300     void FlushRefresh()
301     {
302         if (flushRefreshCallback_) {
303             flushRefreshCallback_();
304         }
305     }
306 
SetBackgroundColor(const Color & value)307     void SetBackgroundColor(const Color& value)
308     {
309         backgroundColor_ = value;
310     }
311 
SetInnerPadding(const Edge & innerPadding)312     void SetInnerPadding(const Edge& innerPadding)
313     {
314         innerPadding_ = innerPadding;
315     }
316 
SetInnerSize(const Dimension & width,const Dimension & height)317     void SetInnerSize(const Dimension& width, const Dimension& height)
318     {
319         width_ = width;
320         height_ = height;
321     }
322 
SetInnerBorder(const Border & border)323     void SetInnerBorder(const Border& border)
324     {
325         border_ = border;
326     }
327 
GetInnerBorder()328     const Border& GetInnerBorder() const
329     {
330         return border_;
331     }
332 
333     // used for inspector node in PC preview
SetNodeId(const int32_t nodeId)334     void SetNodeId(const int32_t nodeId)
335     {
336         nodeId_ = nodeId;
337     }
338 
339     // used for inspector node in PC preview
GetNodeId()340     int32_t GetNodeId() const
341     {
342         return nodeId_;
343     }
344 
GetSelectStyle()345     const TextStyle& GetSelectStyle() const
346     {
347         return theme_->GetTitleStyle();
348     }
349 
SetSelectStyle(const TextStyle & style)350     void SetSelectStyle(const TextStyle& style)
351     {
352         theme_->SetTitleStyle(style);
353     }
354 
SetTheme(const RefPtr<SelectTheme> & theme)355     void SetTheme(const RefPtr<SelectTheme>& theme)
356     {
357         if (popup_) {
358             popup_->SetTheme(theme);
359         }
360         theme_ = theme;
361     }
362     ACE_DEFINE_COMPONENT_EVENT(OnSelected, void(int32_t, std::string));
363 
GetSelectPopupComponent()364     RefPtr<SelectPopupComponent> GetSelectPopupComponent() const
365     {
366         return popup_;
367     }
368 
SetWidth(const Dimension & width)369     void SetWidth(const Dimension& width)
370     {
371         width_ = width;
372     }
373 
SetHeight(const Dimension & height)374     void SetHeight(const Dimension& height)
375     {
376         height_ = height;
377     }
378 
SetLeftPadding(const Dimension & padding)379     void SetLeftPadding(const Dimension& padding)
380     {
381         innerPadding_.SetLeft(padding);;
382     }
383 
SetRightPadding(const Dimension & padding)384     void SetRightPadding(const Dimension& padding)
385     {
386         innerPadding_.SetRight(padding);
387     }
388 
GetLeftPadding()389     const Dimension& GetLeftPadding() const
390     {
391         return innerPadding_.Left();
392     }
393 
GetRightPadding()394     const Dimension& GetRightPadding() const
395     {
396         return innerPadding_.Right();
397     }
398 
SetTopPadding(const Dimension & padding)399     void SetTopPadding(const Dimension& padding)
400     {
401         innerPadding_.SetTop(padding);
402     }
403 
SetBottomPadding(const Dimension & padding)404     void SetBottomPadding(const Dimension& padding)
405     {
406         innerPadding_.SetBottom(padding);
407     }
408 
GetTopPadding()409     const Dimension& GetTopPadding() const
410     {
411         return innerPadding_.Top();
412     }
413 
GetBottomPadding()414     const Dimension& GetBottomPadding() const
415     {
416         return innerPadding_.Bottom();
417     }
418 
GetHeight()419     const Dimension& GetHeight() const
420     {
421         return height_;
422     }
423 
GetWidth()424     const Dimension& GetWidth() const
425     {
426         return width_;
427     }
428 
429 private:
430     bool disabled_ { false };
431     bool clicked_ { false };
432     bool hasSetTipText_ { false };
433     EventMarker onChanged_;
434     RefPtr<TextComponent> tipText_;
435     RefPtr<SelectPopupComponent> popup_;
436     RefPtr<BoxComponent> boxComponent_;
437     RefPtr<SelectTheme> theme_;
438     std::function<void()> flushRefreshCallback_;
439     Color backgroundColor_ = Color::TRANSPARENT;
440     std::size_t selected_ = 0;
441     // used for inspector node in PC preview
442     int32_t nodeId_ = -1;
443 
444     // used in navigation
445     bool isSelectFontSize_ = false;
446     Edge innerPadding_ = Edge(8.0, 9.25, 8.0, 9.25, DimensionUnit::VP);
447 
448     Dimension width_ = -1.0_px;
449     Dimension height_ = -1.0_px;
450     Border border_;
451 };
452 
453 } // namespace OHOS::Ace
454 
455 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_SELECT_COMPONENT_H
456