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_POPUP_POPUP_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_POPUP_POPUP_COMPONENT_H
18 
19 #include "base/geometry/dimension.h"
20 #include "core/components/common/properties/border.h"
21 #include "core/components/common/properties/color.h"
22 #include "core/components/common/properties/edge.h"
23 #include "core/components/common/properties/placement.h"
24 #include "core/components/common/properties/popup_param.h"
25 #include "core/pipeline/base/component.h"
26 #include "core/pipeline/base/composed_component.h"
27 #include "core/pipeline/base/sole_child_component.h"
28 
29 namespace OHOS::Ace {
30 
31 using PopupImpl = std::function<void()>;
32 
33 class PopupController : public virtual AceType {
34     DECLARE_ACE_TYPE(PopupController, AceType);
35 
36 public:
ShowPopup()37     void ShowPopup()
38     {
39         if (showPopupImpl_) {
40             showPopupImpl_();
41         }
42     }
43 
SetShowPopupImpl(const PopupImpl & showPopupImpl)44     void SetShowPopupImpl(const PopupImpl& showPopupImpl)
45     {
46         showPopupImpl_ = showPopupImpl;
47     }
48 
CancelPopup()49     void CancelPopup()
50     {
51         if (cancelPopupImpl_) {
52             cancelPopupImpl_();
53         }
54     }
55 
SetCancelPopupImpl(const PopupImpl & cancelPopupImpl)56     void SetCancelPopupImpl(const PopupImpl& cancelPopupImpl)
57     {
58         cancelPopupImpl_ = cancelPopupImpl;
59     }
60 
61 private:
62     PopupImpl showPopupImpl_;
63     PopupImpl cancelPopupImpl_;
64 };
65 
66 class PopupComponent : public ComposedComponent {
67     DECLARE_ACE_TYPE(PopupComponent, ComposedComponent);
68 
69 public:
PopupComponent(const ComposeId & id,const std::string & name,const RefPtr<Component> & child)70     PopupComponent(const ComposeId& id, const std::string& name, const RefPtr<Component>& child)
71         : ComposedComponent(id, name, child)
72     {
73         popupParam_ = MakeRefPtr<PopupParam>();
74         popupController_ = MakeRefPtr<PopupController>();
75     };
PopupComponent(const ComposeId & id,const std::string & name)76     PopupComponent(const ComposeId& id, const std::string& name) : ComposedComponent(id, name)
77     {
78         popupParam_ = MakeRefPtr<PopupParam>();
79         popupController_ = MakeRefPtr<PopupController>();
80     };
81     ~PopupComponent() override = default;
82 
83     RefPtr<Element> CreateElement() override;
84 
GetPopupController()85     const RefPtr<PopupController>& GetPopupController() const
86     {
87         return popupController_;
88     }
89 
SetPopupParam(const RefPtr<PopupParam> & popupParam)90     void SetPopupParam(const RefPtr<PopupParam>& popupParam)
91     {
92         popupParam_ = popupParam;
93     }
94 
GetPopupParam()95     const RefPtr<PopupParam>& GetPopupParam() const
96     {
97         return popupParam_;
98     }
99 
IsShow()100     bool IsShow() const
101     {
102         if (popupParam_) {
103             return popupParam_->IsShow();
104         }
105         return true;
106     }
107 
108 private:
109     RefPtr<PopupParam> popupParam_;
110     RefPtr<PopupController> popupController_;
111 };
112 
113 } // namespace OHOS::Ace
114 
115 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_POPUP_POPUP_COMPONENT_H
116