summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/Popup.cs
blob: 26f736d2597bc657354aa1785067588882c46723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;

namespace ElmSharp
{
    /// <summary>
    /// Enumeration for the popup orientation type.
    /// </summary>
    public enum PopupOrientation
    {
        /// <summary>
        /// Appears in the top of parent, default.
        /// </summary>
        Top,
        /// <summary>
        /// Appears in the center of parent.
        /// </summary>
        Center,
        /// <summary>
        /// Appears in the bottom of parent.
        /// </summary>
        Bottom,
        /// <summary>
        /// Appears in the left of parent.
        /// </summary>
        Left,
        /// <summary>
        /// Appears in the right of parent.
        /// </summary>
        Right,
        /// <summary>
        /// Appears in the top left of parent.
        /// </summary>
        TopLeft,
        /// <summary>
        /// Appears in the top right of parent.
        /// </summary>
        TopRight,
        /// <summary>
        /// Appears in the bottom left of parent.
        /// </summary>
        BottomLeft,
        /// <summary>
        /// Appears in the bottom right of parent.
        /// </summary>
        BottomRight
    }

    /// <summary>
    /// The Popup is a widget that is an enhancement of Notify.
    /// In addition to content area, there are two optional sections, namely title area and action area.
    /// </summary>
    public class Popup : Layout
    {
        HashSet<PopupItem> _children = new HashSet<PopupItem>();
        SmartEvent _dismissed;
        SmartEvent _blockClicked;
        SmartEvent _timeout;
        SmartEvent _showFinished;

        /// <summary>
        /// Creates and initializes a new instance of the Popup class.
        /// </summary>
        /// <param name="parent">The EvasObject to which the new Popup will be attached as a child.</param>
        public Popup(EvasObject parent) : base(parent)
        {
            _dismissed = new SmartEvent(this, "dismissed");
            _dismissed.On += (sender, e) =>
            {
                Dismissed?.Invoke(this, EventArgs.Empty);
            };

            _blockClicked = new SmartEvent(this, "block,clicked");
            _blockClicked.On += (sender, e) =>
            {
                OutsideClicked?.Invoke(this, EventArgs.Empty);
            };

            _timeout = new SmartEvent(this, "timeout");
            _timeout.On += (sender, e) =>
            {
                TimedOut?.Invoke(this, EventArgs.Empty);
            };

            _showFinished = new SmartEvent(this, "show,finished");
            _showFinished.On += (sender, e) =>
            {
                ShowAnimationFinished?.Invoke(this, EventArgs.Empty);
            };
        }

        /// <summary>
        /// Dismissed will be triggered when Popup have been dismissed.
        /// </summary>
        public event EventHandler Dismissed;

        /// <summary>
        /// OutsideClicked will be triggered when users taps on the outside of Popup.
        /// </summary>
        public event EventHandler OutsideClicked;

        /// <summary>
        /// OutsideClicked will be triggered when Popup is closed as a result of timeout.
        /// </summary>
        public event EventHandler TimedOut;

        /// <summary>
        /// OutsideClicked will be triggered when the Popup transition is finished in showing.
        /// </summary>
        public event EventHandler ShowAnimationFinished;

        /// <summary>
        /// Sets or gets the position in which Popup will appear in its parent.
        /// </summary>
        public PopupOrientation Orientation
        {
            get
            {
                return (PopupOrientation)Interop.Elementary.elm_popup_orient_get(Handle);
            }
            set
            {
                Interop.Elementary.elm_popup_orient_set(Handle, (int)value);
            }
        }

        /// <summary>
        /// Sets or gets the wrapping type of content text packed in content area of Popup widget.
        /// </summary>
        public WrapType ContentTextWrapType
        {
            get
            {
                return (WrapType)Interop.Elementary.elm_popup_content_text_wrap_type_get(Handle);
            }
            set
            {
                Interop.Elementary.elm_popup_content_text_wrap_type_set(Handle, (int)value);
            }
        }

        /// <summary>
        /// Sets or gets the timeout value set to the Popup(in seconds).
        /// </summary>
        /// <remarks>
        /// Since calling Show() on a popup restarts the timer controlling when it is hidden,
        /// setting this before the popup is shown will in effect mean starting the timer when the popup is shown.
        /// TimedOut is called afterwards which can be handled if needed.
        /// Set a value <= 0.0 to disable a running timer.If the value > 0.0 and the popup is previously visible,
        /// the timer will be started with this value, canceling any running timer.
        /// </remarks>
        public double Timeout
        {
            get
            {
                return Interop.Elementary.elm_popup_timeout_get(Handle);
            }
            set
            {
                Interop.Elementary.elm_popup_timeout_set(Handle, value);
            }
        }

        /// <summary>
        /// Sets or gets whether events should be passed to event blocked area by a click outside.
        /// </summary>
        /// <remarks>
        /// The visible region of popup is surrounded by a translucent region called Blocked Event area.
        /// </remarks>
        public bool AllowEvents
        {
            get
            {
                return Interop.Elementary.elm_popup_allow_events_get(Handle);
            }
            set
            {
                Interop.Elementary.elm_popup_allow_events_set(Handle, value);
            }
        }

        /// <summary>
        /// Sets or gets the AlignmentX in which the popup will appear in its parent.
        /// </summary>
        public override double AlignmentX
        {
            get
            {
                return Interop.Elementary.GetPopupAlignX(Handle);
            }
            set
            {
                Interop.Elementary.SetPopupAlignX(Handle, value);
            }
        }

        /// <summary>
        /// Sets or gets the AlignmentY in which the popup will appear in its parent.
        /// </summary>
        public override double AlignmentY
        {
            get
            {
                return Interop.Elementary.GetPopupAlignY(Handle);
            }
            set
            {
                Interop.Elementary.SetPopupAlignY(Handle, value);
            }
        }

        /// <summary>
        /// Gets the Opacity value of the Popup.
        /// </summary>
        public override int Opacity
        {
            get
            {
                return Color.Default.A;
            }

            set
            {
                Console.WriteLine("Popup instance doesn't support to set Opacity.");
            }
        }

        /// <summary>
        /// Adds label to a Popup widget.
        /// </summary>
        /// <param name="label"></param>
        /// <returns>The new PopupItem which contains label .</returns>
        public PopupItem Append(string label)
        {
            return Append(label, null);
        }

        /// <summary>
        /// Adds Label and icon to a Popup widget.
        /// </summary>
        /// <param name="label">The Label which will be added into a new PopupItem. </param>
        /// <param name="icon">The icon which will be added into a new PopupItem. </param>
        /// <returns>The new PopupItem which contains label and icon.</returns>
        public PopupItem Append(string label, EvasObject icon)
        {
            PopupItem item = new PopupItem(label, icon);
            item.Handle = Interop.Elementary.elm_popup_item_append(Handle, label, icon, null, (IntPtr)item.Id);
            AddInternal(item);
            return item;
        }

        /// <summary>
        /// Uses this function to dismiss the popup in hide effect.
        /// when the Popup is dismissed, the "dismissed" signal will be emitted.
        /// </summary>
        public void Dismiss()
        {
            Interop.Elementary.elm_popup_dismiss(Handle);
        }

        protected override IntPtr CreateHandle(EvasObject parent)
        {
            return Interop.Elementary.elm_popup_add(parent.Handle);
        }
        void AddInternal(PopupItem item)
        {
            _children.Add(item);
            item.Deleted += Item_Deleted;
        }
        void Item_Deleted(object sender, EventArgs e)
        {
            _children.Remove((PopupItem)sender);
        }
    }
}