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_POPUP_SELECT_POPUP_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_POPUP_SELECT_POPUP_COMPONENT_H
18 
19 #include "core/components/box/box_component.h"
20 #include "core/components/common/properties/tween_option.h"
21 #include "core/components/option/option_component.h"
22 #include "core/components/scroll/scroll_component.h"
23 #include "core/components/select/select_theme.h"
24 #include "core/components/stack/stack_element.h"
25 #include "core/pipeline/base/constants.h"
26 #include "core/pipeline/base/sole_child_component.h"
27 
28 namespace OHOS::Ace {
29 
30 using RefreshAnimationCallback = std::function<void(const TweenOption&, bool)>;
31 
32 class ACE_EXPORT SelectPopupComponent : public SoleChildComponent {
33     DECLARE_ACE_TYPE(SelectPopupComponent, SoleChildComponent);
34 
35 public:
36     SelectPopupComponent();
37     ~SelectPopupComponent() override = default;
38 
39     RefPtr<RenderNode> CreateRenderNode() override;
40     RefPtr<Element> CreateElement() override;
41 
42     void InitTheme(const RefPtr<ThemeManager>& themeManager);
43 
GetOptionSize()44     std::size_t GetOptionSize() const
45     {
46         return optionSize_;
47     }
48 
SetOptionSize(std::size_t optionSize)49     void SetOptionSize(std::size_t optionSize)
50     {
51         optionSize_ = optionSize;
52     }
53 
GetPopupLayout(Offset & leftTop,Offset & rightBottom)54     void GetPopupLayout(Offset& leftTop, Offset& rightBottom) const
55     {
56         leftTop = selectLeftTop_;
57         rightBottom = selectRightBottom_;
58     }
59 
GetPopupRRectSize()60     const Dimension& GetPopupRRectSize() const
61     {
62         return theme_->GetPopupRRectSize();
63     }
64 
GetNormalPadding()65     const Dimension& GetNormalPadding() const
66     {
67         return theme_->GetNormalPadding();
68     }
69 
GetPopupMinWidth()70     const Dimension& GetPopupMinWidth() const
71     {
72         return theme_->GetPopupMinWidth();
73     }
74 
GetBorderWidth()75     const Dimension& GetBorderWidth() const
76     {
77         return theme_->GetPopupBorderWidth();
78     }
79 
GetShadowWidth()80     const Dimension& GetShadowWidth() const
81     {
82         return theme_->GetPopupShadowWidth();
83     }
84 
GetSelectOptionCount()85     std::size_t GetSelectOptionCount() const
86     {
87         return options_.size();
88     }
89 
90     RefPtr<OptionComponent> GetSelectOption(std::size_t index) const;
91 
GetSelectOptions()92     std::vector<RefPtr<OptionComponent>> GetSelectOptions() const
93     {
94         return options_;
95     }
96 
ClearAllOptions()97     void ClearAllOptions()
98     {
99         options_.clear();
100     }
101 
102     void AppendSelectOption(const RefPtr<OptionComponent>& selectOption);
103 
104     void RemoveSelectOption(const RefPtr<OptionComponent>& selectOption);
105 
106     void InsertSelectOption(const RefPtr<OptionComponent>& selectOption, uint32_t index);
107 
SetOptionClickedCallback(const std::function<void (std::size_t)> & clickedCallback)108     void SetOptionClickedCallback(const std::function<void(std::size_t)>& clickedCallback)
109     {
110         optionClickedCallback_ = clickedCallback;
111     }
112 
SetPopupCanceledCallback(const std::function<void ()> & canceledCallback)113     void SetPopupCanceledCallback(const std::function<void()>& canceledCallback)
114     {
115         popupCanceledCallback_ = canceledCallback;
116     }
117 
SetOptionModifiedCallback(const std::function<void (std::size_t)> & value)118     void SetOptionModifiedCallback(const std::function<void(std::size_t)>& value)
119     {
120         optionModifiedCallback_ = value;
121     }
122 
GetPopupCanceledCallback()123     const std::function<void()>& GetPopupCanceledCallback() const
124     {
125         return popupCanceledCallback_;
126     }
127 
GetDialogShowed()128     bool GetDialogShowed() const
129     {
130         return dialogShowed_;
131     }
132 
GetTitle()133     const std::string& GetTitle() const
134     {
135         return title_;
136     }
137 
SetTitle(const std::string & title)138     void SetTitle(const std::string& title)
139     {
140         title_ = title;
141     }
142 
GetTitleStyle()143     const TextStyle& GetTitleStyle() const
144     {
145         return theme_->GetTitleStyle();
146     }
147 
SetTitleStyle(const TextStyle & style)148     void SetTitleStyle(const TextStyle& style)
149     {
150         theme_->SetTitleStyle(style);
151     }
152 
IsTV()153     bool IsTV() const
154     {
155         return theme_->IsTV();
156     }
157 
IsMenu()158     bool IsMenu() const
159     {
160         return isMenu_;
161     }
162 
IsContextMenu()163     bool IsContextMenu() const
164     {
165         return isContextMenu_;
166     }
167 
SetIsContextMenu(bool isContextMenu)168     void SetIsContextMenu(bool isContextMenu)
169     {
170         isContextMenu_ = isContextMenu;
171     }
172 
GetHorizontalSpacing()173     const Dimension& GetHorizontalSpacing() const
174     {
175         return theme_->GetHorizontalSpacing();
176     }
177 
GetVerticalSpacing()178     const Dimension& GetVerticalSpacing() const
179     {
180         return theme_->GetVerticalSpacing();
181     }
182 
GetContentSpacing()183     const Dimension& GetContentSpacing() const
184     {
185         return theme_->GetContentSpacing();
186     }
187 
GetShowOption()188     const TweenOption& GetShowOption() const
189     {
190         return showOption_;
191     }
SetShowOption(const TweenOption & value)192     void SetShowOption(const TweenOption& value)
193     {
194         showOption_ = value;
195         if (refreshAnimationCallback_) {
196             refreshAnimationCallback_(showOption_, true);
197         } else {
198             LOGE("refresh animation callback is null.");
199         }
200     }
201 
SetRefreshAnimationCallback(const RefreshAnimationCallback & value)202     void SetRefreshAnimationCallback(const RefreshAnimationCallback& value)
203     {
204         refreshAnimationCallback_ = value;
205     }
GetRefreshAnimationCallback()206     const RefreshAnimationCallback& GetRefreshAnimationCallback() const
207     {
208         return refreshAnimationCallback_;
209     }
210 
GetHideOption()211     const TweenOption& GetHideOption() const
212     {
213         return hideOption_;
214     }
SetHideOption(const TweenOption & value)215     void SetHideOption(const TweenOption& value)
216     {
217         hideOption_ = value;
218     }
219 
GetAnimationController()220     const RefPtr<Animator>& GetAnimationController() const
221     {
222         return animationController_;
223     }
SetAnimationController(const RefPtr<Animator> & value)224     void SetAnimationController(const RefPtr<Animator>& value)
225     {
226         animationController_ = value;
227     }
228 
IsFullScreen()229     bool IsFullScreen() const
230     {
231         return isFullScreen_;
232     }
SetIsFullScreen(bool isFullScreen)233     void SetIsFullScreen(bool isFullScreen)
234     {
235         isFullScreen_ = isFullScreen;
236     }
237 
SetTheme(const RefPtr<SelectTheme> & theme)238     void SetTheme(const RefPtr<SelectTheme>& theme)
239     {
240         if (theme) {
241             theme_ = theme->clone();
242         }
243     }
244 
GetTheme()245     const RefPtr<SelectTheme>& GetTheme() const
246     {
247         return theme_;
248     }
249 
250     void HideDialog(uint32_t index);
251     void ShowDialog(
252         const RefPtr<StackElement>& stackElement, const Offset& leftTop, const Offset& rightBottom, bool isMenu);
253     void ShowContextMenu(const Offset& offset);
254     void CloseContextMenu();
255 
256     bool Initialize(const RefPtr<AccessibilityManager>& manager);
257 
GetNode()258     const RefPtr<AccessibilityNode> GetNode() const
259     {
260         return node_;
261     }
262 
GetStackElement()263     RefPtr<StackElement> GetStackElement() const
264     {
265         return stackElement_;
266     }
267 
SetNode(const RefPtr<AccessibilityNode> & value)268     void SetNode(const RefPtr<AccessibilityNode>& value)
269     {
270         node_ = value;
271     }
272 
273     // used for inspector node in PC preview
SetSelectPopupId(const int32_t nodeId)274     void SetSelectPopupId(const int32_t nodeId)
275     {
276         nodeId_ = nodeId;
277     }
278 
279     // used for inspector node in PC preview
GetSelectPopupId()280     int32_t GetSelectPopupId() const
281     {
282         return nodeId_;
283     }
284 
285     void SetDefaultSelecting();
286 
GetPopupShowed()287     bool GetPopupShowed()
288     {
289         return dialogShowed_;
290     }
291 
SetIsCustomMenu(bool isCustomMenu)292     void SetIsCustomMenu(bool isCustomMenu)
293     {
294         isCustomMenu_ = isCustomMenu;
295     }
296 
297 private:
298     RefPtr<BoxComponent> InitializeInnerBox(const RefPtr<ScrollComponent>& scroll);
299     void HandleOptionClick(std::size_t index);
300     void HandleOptionModify(std::size_t index);
301     void InnerHideDialog(uint32_t index);
302 
303     bool isFullScreen_ = true;
304     std::size_t optionSize_ { SELECT_DEFAULT_OPTION_SIZE };
305     Offset selectLeftTop_;
306     Offset selectRightBottom_;
307     bool isMenu_ = false;
308     bool isContextMenu_ = false;
309     bool isCustomMenu_ = false;
310     std::vector<RefPtr<OptionComponent>> options_;
311     std::string title_;
312     RefPtr<SelectTheme> theme_;
313     bool dialogShowed_ = false;
314     // used for inspector node in PC preview
315     int32_t nodeId_ = -1;
316     std::function<void(std::size_t)> optionClickedCallback_;
317     std::function<void(std::size_t)> optionModifiedCallback_;
318     std::function<void()> popupCanceledCallback_;
319     RefPtr<StackElement> stackElement_;
320 
321     TweenOption showOption_;
322     TweenOption hideOption_;
323     RefreshAnimationCallback refreshAnimationCallback_;
324     RefPtr<Animator> animationController_;
325 
326     RefPtr<AccessibilityNode> node_;
327     WeakPtr<AccessibilityManager> manager_;
328 };
329 
330 } // namespace OHOS::Ace
331 
332 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_POPUP_SELECT_POPUP_COMPONENT_H
333