• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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         theme_ = theme->clone();
241     }
242 
GetTheme()243     const RefPtr<SelectTheme>& GetTheme() const
244     {
245         return theme_;
246     }
247 
248     void HideDialog(uint32_t index);
249     void ShowDialog(
250         const RefPtr<StackElement>& stackElement, const Offset& leftTop, const Offset& rightBottom, bool isMenu);
251     void ShowContextMenu(const Offset& offset);
252     void CloseContextMenu();
253 
254     bool Initialize(const RefPtr<AccessibilityManager>& manager);
255 
GetNode()256     const RefPtr<AccessibilityNode> GetNode() const
257     {
258         return node_;
259     }
260 
GetStackElement()261     RefPtr<StackElement> GetStackElement() const
262     {
263         return stackElement_;
264     }
265 
SetNode(const RefPtr<AccessibilityNode> & value)266     void SetNode(const RefPtr<AccessibilityNode>& value)
267     {
268         node_ = value;
269     }
270 
271     // used for inspector node in PC preview
SetSelectPopupId(const int32_t nodeId)272     void SetSelectPopupId(const int32_t nodeId)
273     {
274         nodeId_ = nodeId;
275     }
276 
277     // used for inspector node in PC preview
GetSelectPopupId()278     int32_t GetSelectPopupId() const
279     {
280         return nodeId_;
281     }
282 
283     void SetDefaultSelecting();
284 
GetPopupShowed()285     bool GetPopupShowed()
286     {
287         return dialogShowed_;
288     }
289 
290 private:
291     RefPtr<BoxComponent> InitializeInnerBox(const RefPtr<ScrollComponent>& scroll);
292     void HandleOptionClick(std::size_t index);
293     void HandleOptionModify(std::size_t index);
294     void InnerHideDialog(uint32_t index);
295 
296     bool isFullScreen_ = true;
297     std::size_t optionSize_ { SELECT_DEFAULT_OPTION_SIZE };
298     Offset selectLeftTop_;
299     Offset selectRightBottom_;
300     bool isMenu_ = false;
301     bool isContextMenu_ = false;
302     std::vector<RefPtr<OptionComponent>> options_;
303     std::string title_;
304     RefPtr<SelectTheme> theme_;
305     bool dialogShowed_ = false;
306     // used for inspector node in PC preview
307     int32_t nodeId_ = -1;
308     std::function<void(std::size_t)> optionClickedCallback_;
309     std::function<void(std::size_t)> optionModifiedCallback_;
310     std::function<void()> popupCanceledCallback_;
311     RefPtr<StackElement> stackElement_;
312 
313     TweenOption showOption_;
314     TweenOption hideOption_;
315     RefreshAnimationCallback refreshAnimationCallback_;
316     RefPtr<Animator> animationController_;
317 
318     RefPtr<AccessibilityNode> node_;
319     WeakPtr<AccessibilityManager> manager_;
320 };
321 
322 } // namespace OHOS::Ace
323 
324 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_POPUP_SELECT_POPUP_COMPONENT_H
325