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_CUSTOM_DIALOG_CUSTOM_DIALOG_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_DIALOG_CUSTOM_DIALOG_COMPONENT_H
18 
19 #include <functional>
20 
21 #include "base/geometry/dimension.h"
22 #include "core/components/common/properties/edge.h"
23 #include "core/event/ace_event_handler.h"
24 #include "core/pipeline/base/composed_component.h"
25 
26 namespace OHOS::Ace {
27 
28 using BuildDialogImpl = std::function<void()>;
29 
30 // CustomDialogController is used in CustomDialogElement to build a dialog
31 class CustomDialogController : public virtual AceType {
32     DECLARE_ACE_TYPE(CustomDialogController, AceType);
33 
34 public:
ShowDialog()35     void ShowDialog()
36     {
37         if (showDialogImpl_) {
38             showDialogImpl_();
39         }
40     }
41 
CloseDialog()42     void CloseDialog()
43     {
44         if (closeDialogImpl_) {
45             closeDialogImpl_();
46         }
47     }
48 
SetShowDialogImpl(const BuildDialogImpl & showDialogImpl)49     void SetShowDialogImpl(const BuildDialogImpl& showDialogImpl)
50     {
51         showDialogImpl_ = showDialogImpl;
52     }
53 
SetCloseDialogImpl(const BuildDialogImpl & closeDialogImpl)54     void SetCloseDialogImpl(const BuildDialogImpl& closeDialogImpl)
55     {
56         closeDialogImpl_ = closeDialogImpl;
57     }
58 
59 private:
60     BuildDialogImpl showDialogImpl_;
61     BuildDialogImpl closeDialogImpl_;
62 };
63 
64 class CustomDialogComponent : public ComposedComponent {
65     DECLARE_ACE_TYPE(CustomDialogComponent, ComposedComponent);
66 
67 public:
CustomDialogComponent(const ComposeId & id,const std::string & name,const RefPtr<Component> & child)68     CustomDialogComponent(const ComposeId& id, const std::string& name, const RefPtr<Component>& child)
69         : ComposedComponent(id, name, child)
70     {
71         dialogController_ = AceType::MakeRefPtr<CustomDialogController>();
72     };
73 
CustomDialogComponent(const ComposeId & id,const std::string & name)74     CustomDialogComponent(const ComposeId& id, const std::string& name) : ComposedComponent(id, name)
75     {
76         dialogController_ = AceType::MakeRefPtr<CustomDialogController>();
77     };
78 
79     ~CustomDialogComponent() override = default;
80 
81     RefPtr<Element> CreateElement() override;
82 
GetDialogController()83     const RefPtr<CustomDialogController>& GetDialogController() const
84     {
85         return dialogController_;
86     }
87 
GetOnCancel()88     const EventMarker& GetOnCancel() const
89     {
90         return onCancel_;
91     }
92 
SetOnCancel(const EventMarker & value)93     void SetOnCancel(const EventMarker& value)
94     {
95         onCancel_ = value;
96     }
97 
GetOnShow()98     const EventMarker& GetOnShow() const
99     {
100         return onShow_;
101     }
102 
SetOnShow(const EventMarker & value)103     void SetOnShow(const EventMarker& value)
104     {
105         onShow_ = value;
106     }
107 
GetOnClose()108     const EventMarker& GetOnClose() const
109     {
110         return onClose_;
111     }
112 
SetOnClose(const EventMarker & value)113     void SetOnClose(const EventMarker& value)
114     {
115         onClose_ = value;
116     }
117 
GetHeight()118     const Dimension& GetHeight() const
119     {
120         return height_;
121     }
122 
SetHeight(const Dimension & height)123     void SetHeight(const Dimension& height)
124     {
125         height_ = height;
126     }
127 
GetWidth()128     const Dimension& GetWidth() const
129     {
130         return width_;
131     }
132 
SetWidth(const Dimension & width)133     void SetWidth(const Dimension& width)
134     {
135         width_ = width;
136     }
137 
SetMargin(const Edge & margin)138     void SetMargin(const Edge& margin)
139     {
140         margin_ = margin;
141         isSetMargin_ = true;
142     }
143 
GetMargin()144     const Edge& GetMargin() const
145     {
146         return margin_;
147     }
148 
IsSetMargin()149     bool IsSetMargin() const
150     {
151         return isSetMargin_;
152     }
153 
SetIsDragable(bool isDragable)154     void SetIsDragable(bool isDragable)
155     {
156         isDragable_ = isDragable;
157     }
158 
IsDragable()159     bool IsDragable() const
160     {
161         return isDragable_;
162     }
163 
164 private:
165     RefPtr<CustomDialogController> dialogController_;
166     EventMarker onCancel_;
167     EventMarker onShow_;
168     EventMarker onClose_;
169     // initialize height and width to default case
170     Dimension height_;
171     Dimension width_;
172     Edge margin_;
173     bool isSetMargin_ = false;
174     bool isDragable_ = false;
175 };
176 
177 } // namespace OHOS::Ace
178 
179 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_DIALOG_CUSTOM_DIALOG_COMPONENT_H
180