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_MENU_MENU_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_MENU_MENU_COMPONENT_H
18 
19 #include "base/utils/string_utils.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/option/option_component.h"
22 #include "core/components/select_popup/select_popup_component.h"
23 #include "core/components/text/text_component.h"
24 #include "core/pipeline/base/component.h"
25 #include "core/pipeline/base/composed_component.h"
26 #include "core/pipeline/base/sole_child_component.h"
27 
28 namespace OHOS::Ace {
29 
30 enum MenuStatus {
31     OFF = 0,
32     ON,
33     INIT
34 };
35 
36 class ACE_EXPORT MenuComponent : public ComposedComponent {
37     DECLARE_ACE_TYPE(MenuComponent, ComposedComponent);
38 
39 public:
MenuComponent(const ComposeId & id,const std::string & name,const RefPtr<Component> & child)40     MenuComponent(const ComposeId& id, const std::string& name, const RefPtr<Component>& child)
41         : ComposedComponent(id, name, child)
42     {}
43 
MenuComponent(const ComposeId & id,const std::string & name)44     MenuComponent(const ComposeId& id, const std::string& name) : ComposedComponent(id, name)
45     {
46 #if defined(PREVIEW)
47         popup_->SetSelectPopupId(StringUtils::StringToInt(id));
48 #endif
49     }
50 
51     ~MenuComponent() override = default;
52 
53     RefPtr<Element> CreateElement() override;
54 
InitTheme(const RefPtr<ThemeManager> & themeManager)55     void InitTheme(const RefPtr<ThemeManager>& themeManager)
56     {
57         if (popup_) {
58             popup_->InitTheme(themeManager);
59         }
60     }
61 
SetTheme(const RefPtr<SelectTheme> & theme)62     void SetTheme(const RefPtr<SelectTheme>& theme)
63     {
64         if (popup_) {
65             popup_->SetTheme(theme);
66         }
67     }
68 
SetTextDirection(TextDirection direction)69     void SetTextDirection(TextDirection direction) override
70     {
71         ComposedComponent::SetTextDirection(direction);
72         if (popup_) {
73             popup_->SetTextDirection(direction);
74         } else {
75             LOGE("popup component of select is null.");
76         }
77     }
78 
79     void ClearOptions();
80 
81     void AppendOption(const RefPtr<OptionComponent>& option);
82 
83     void RemoveOption(const RefPtr<OptionComponent>& option);
84 
85     void InsertOption(const RefPtr<OptionComponent>& option, uint32_t index);
86 
87     std::size_t GetOptionCount() const;
88 
89     RefPtr<OptionComponent> GetOption(std::size_t index) const;
90 
91     std::vector<RefPtr<OptionComponent>> GetOptions() const;
92 
GetTargetCallback()93     const std::function<void(const ComposeId&, const Offset&)>& GetTargetCallback() const
94     {
95         return targetCallback_;
96     }
97 
SetTargetCallback(const std::function<void (const ComposeId &,const Offset &)> & targetCallback)98     void SetTargetCallback(const std::function<void(const ComposeId&, const Offset&)>& targetCallback)
99     {
100         targetCallback_ = targetCallback;
101     }
102 
GetOnSuccess()103     const EventMarker& GetOnSuccess() const
104     {
105         return onSuccess_;
106     }
SetOnSuccess(const EventMarker & value)107     void SetOnSuccess(const EventMarker& value)
108     {
109         onSuccess_ = value;
110     }
111 
GetOnCancel()112     const EventMarker& GetOnCancel() const
113     {
114         return onCancel_;
115     }
SetOnCancel(const EventMarker & value)116     void SetOnCancel(const EventMarker& value)
117     {
118         onCancel_ = value;
119     }
120 
SetMenuShow(const MenuStatus & status)121     void SetMenuShow(const MenuStatus& status)
122     {
123         menuShow_ = status;
124     }
125 
GetMenuShow()126     const MenuStatus& GetMenuShow()
127     {
128         return menuShow_;
129     }
130 
SetPopupPosition(const Offset & position)131     void SetPopupPosition(const Offset& position)
132     {
133         popupPosition_ = position;
134     }
135 
GetPopupPosition()136     const Offset& GetPopupPosition()
137     {
138         return popupPosition_;
139     }
140 
141     std::string GetTitle() const;
142     void SetTitle(const std::string& value);
143 
144     const TextStyle& GetTitleStyle() const;
145     void SetTitleStyle(const TextStyle& value);
SetIsBindTarget(bool isBindTarget)146     void SetIsBindTarget(bool isBindTarget) {}
147     const RefPtr<SelectPopupComponent>& GetPopup() const;
148 
SetIsContextMenu(bool isContextMenu)149     void SetIsContextMenu(bool isContextMenu)
150     {
151         isContextMenu_ = isContextMenu;
152         if (popup_) {
153             popup_->SetIsContextMenu(isContextMenu);
154         }
155     }
156 
IsContextMenu()157     bool IsContextMenu()
158     {
159         return isContextMenu_;
160     }
161 
SetIsCustomMenu(bool isCustomMenu)162     void SetIsCustomMenu(bool isCustomMenu)
163     {
164         if (popup_) {
165             popup_->SetIsCustomMenu(isCustomMenu);
166         }
167     }
168 
169 private:
170     RefPtr<SelectPopupComponent> popup_ = AceType::MakeRefPtr<SelectPopupComponent>();
171     std::function<void(const ComposeId&, const Offset&)> targetCallback_;
172     EventMarker onSuccess_;
173     EventMarker onCancel_;
174     MenuStatus menuShow_ = MenuStatus::INIT;
175     Offset popupPosition_ = Offset(0, 0);
176     TextStyle textStyle_ = TextStyle();
177 
178     // This is mardked by contextMenu
179     bool isContextMenu_ = false;
180 };
181 
182 } // namespace OHOS::Ace
183 
184 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_MENU_MENU_COMPONENT_H
185