summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Native/Dialog.cs
blob: c59755f70b8197ace14c040ca6c4c28a6fad9ab8 (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
using System;
using ElmSharp;
using EButton = ElmSharp.Button;

namespace Xamarin.Forms.Platform.Tizen.Native
{
	/// <summary>
	/// Base class for Dialogs. 
	/// A dialog is a small window that prompts the user to make a decision or enter additional information.
	/// </summary>
	public class Dialog : Popup
	{
		EButton _positiveButton;
		EButton _neutralButton;
		EButton _negativeButton;
		EvasObject _content;
		string _title;

		/// <summary>
		/// Creates a dialog window that uses the default dialog theme.
		/// </summary>
		public Dialog(EvasObject parent) : base(parent)
		{
			Initialize();
		}

		/// <summary>
		/// Occurs when the hardware Back button is pressed.
		/// </summary>
		public event EventHandler BackButtonPressed;

		/// <summary>
		/// Occurs whenever the dialog is first displayed.
		/// </summary>
		public event EventHandler Shown;

		/// <summary>
		/// Enumerates the three valid positions of a dialog button.
		/// </summary>
		enum ButtonPosition
		{
			Positive,
			Neutral,
			Negative
		}

		/// <summary>
		/// Gets or sets the title of the dialog
		/// </summary>
		public string Title
		{
			get
			{
				return _title;
			}
			set
			{
				if (_title != value)
				{
					ApplyTitle(value);
				}
			}
		}

		/// <summary>
		/// Gets or sets the content to display in that dialog.
		/// </summary>
		public EvasObject Content
		{
			get
			{
				return _content;
			}
			set
			{
				if (_content != value)
				{
					ApplyContent(value);
				}
			}
		}

		/// <summary>
		/// Gets or sets the positive button used in the dialog
		/// </summary>
		public EButton PositiveButton
		{
			get
			{
				return _positiveButton;
			}
			set
			{
				if (_positiveButton != value)
				{
					ApplyButton(ButtonPosition.Positive, value);
				}
			}
		}

		/// <summary>
		/// Gets or sets the neutral button used in the dialog
		/// </summary>
		public EButton NeutralButton
		{
			get
			{
				return _neutralButton;
			}
			set
			{
				if (_neutralButton != value)
				{
					ApplyButton(ButtonPosition.Neutral, value);
				}
			}
		}

		/// <summary>
		/// Gets or sets the negative button used in the dialog
		/// </summary>
		public EButton NegativeButton
		{
			get
			{
				return _negativeButton;
			}
			set
			{
				if (_negativeButton != value)
				{
					ApplyButton(ButtonPosition.Negative, value);
				}
			}
		}

		/// <summary>
		/// Starts the dialog and displays it on screen.
		/// </summary>
		public new void Show()
		{
			base.Show();
			Shown?.Invoke(this, EventArgs.Empty);
		}

		/// <summary>
		/// Handles the disposing of a dialog widget.
		/// </summary>
		protected override void OnUnrealize()
		{
			_content?.Unrealize();

			ApplyButton(ButtonPosition.Positive, null);
			ApplyButton(ButtonPosition.Neutral, null);
			ApplyButton(ButtonPosition.Negative, null);
			ApplyContent(null);

			UngrabBackKey();

			base.OnUnrealize();
		}

		/// <summary>
		/// Called when the dialog is shown.
		/// </summary>
		/// <remarks>When shown, the dialog will register itself for the back key press event handling.</remarks>
		protected virtual void OnShown()
		{
			GrabBackKey();
		}

		/// <summary>
		/// Called when the dialog is dismissed.
		/// </summary>
		/// <remarks>When dismissed, the dialog will unregister itself from the back key press event handling.</remarks>
		protected virtual void OnDismissed()
		{
			UngrabBackKey();
		}

		/// <summary>
		/// Handles the initialization process.
		/// </summary>
		/// <remarks>Creates handlers for vital events</remarks>
		void Initialize()
		{
			// Adds a handler for the Dismissed event.
			// In effect, unregisters this instance from being affected by the hardware back key presses.
			Dismissed += (s, e) =>
			{
				OnDismissed();
				//Native control should be freed after dismissed event occurred. (this is EFL's law.)
				Unrealize();
			};

			// Adds a handler for the Shown event.
			// In effect, registers this instance to be affected by the hardware back key presses.
			Shown += (s, e) =>
			{
				OnShown();
			};

			// Adds a handler for the KeyUp event.
			// The handler checks whether the key just pressed is a back key
			// and if that is the case, invokes the back button press handler of this instance.
			KeyUp += (s, e) =>
			{
				if (e.KeyName == EvasKeyEventArgs.PlatformBackButtonName)
					BackButtonPressed?.Invoke(this, EventArgs.Empty);
			};
		}

		/// <summary>
		/// Changes the dialog title.
		/// </summary>
		/// <param name="title">New dialog title.</param>
		void ApplyTitle(string title)
		{
			_title = title;

			SetPartText("title,text", _title);
		}

		/// <summary>
		/// Puts the button in one of the three available slots.
		/// </summary>
		/// <param name="position">The slot to be occupied by the button expressed as a <see cref="ButtonPosition"/></param>
		/// <param name="button">The new button.</param>
		void ApplyButton(ButtonPosition position, EButton button)
		{
			if (button != null)
			{
				button.Style = "popup";
			}

			switch (position)
			{
				case ButtonPosition.Positive:
					_positiveButton = button;
					SetPartContent("button3", _positiveButton, true);
					break;

				case ButtonPosition.Neutral:
					_neutralButton = button;
					SetPartContent("button2", _neutralButton, true);
					break;

				case ButtonPosition.Negative:
					_negativeButton = button;
					SetPartContent("button1", _negativeButton, true);
					break;
			}
		}

		/// <summary>
		/// Updates the content of the dialog.
		/// </summary>
		/// <param name="content">New dialog content.</param>
		void ApplyContent(EvasObject content)
		{
			_content = content;

			SetPartContent("default", _content, true);
		}

		/// <summary>
		/// Registers this instance to be affected by pressing the hardware back key.
		/// </summary>
		void GrabBackKey()
		{
			KeyGrab(EvasKeyEventArgs.PlatformBackButtonName, true);
		}

		/// <summary>
		/// Unregisters this instance from being affected by pressing the hardware back key.
		/// </summary>
		void UngrabBackKey()
		{
			KeyUngrab(EvasKeyEventArgs.PlatformBackButtonName);
		}

	}
}