988e95abec97a1a85dd3ad20c0aa6220eba2d557
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / AlertDialog.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.ComponentModel;
20 using System.Collections.Generic;
21 using Tizen.NUI.BaseComponents;
22
23 namespace Tizen.NUI.Components
24 {
25     /// <summary>
26     /// AlertDialog class shows a dialog with title, message and action buttons.
27     /// </summary>
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class AlertDialog : Control
30     {
31         private string title = null;
32         private string message = null;
33
34         private View titleContent = null;
35         private View content = null;
36         private View actionContent = null;
37         private IEnumerable<View> actionContentViews = null;
38
39         private View defaultTitleContent = null;
40         private View defaultContent = null;
41         private View defaultActionContent = null;
42
43         /// <summary>
44         /// Creates a new instance of AlertDialog.
45         /// </summary>
46         [EditorBrowsable(EditorBrowsableState.Never)]
47         public AlertDialog() : base()
48         {
49             Initialize();
50         }
51
52         /// <summary>
53         /// Dispose AlertDialog and all children on it.
54         /// </summary>
55         /// <param name="type">Dispose type.</param>
56         [EditorBrowsable(EditorBrowsableState.Never)]
57         protected override void Dispose(DisposeTypes type)
58         {
59             if (disposed)
60             {
61                 return;
62             }
63
64             if (type == DisposeTypes.Explicit)
65             {
66                 if (titleContent != null)
67                 {
68                     Utility.Dispose(titleContent);
69                 }
70
71                 if (content != null)
72                 {
73                     Utility.Dispose(content);
74                 }
75
76                 if (actionContent != null)
77                 {
78                     Utility.Dispose(actionContent);
79                 }
80             }
81
82             base.Dispose(type);
83         }
84
85         /// <summary>
86         /// Title text of AlertDialog.
87         /// </summary>
88         [EditorBrowsable(EditorBrowsableState.Never)]
89         public string Title
90         {
91             get
92             {
93                 return title;
94             }
95             set
96             {
97                 if (title == value)
98                 {
99                     return;
100                 }
101
102                 title = value;
103
104                 if (TitleContent is TextLabel textLabel)
105                 {
106                     textLabel.Text = title;
107                 }
108             }
109         }
110
111         /// <summary>
112         /// Title content of AlertDialog. TitleContent is added to Children automatically.
113         /// </summary>
114         [EditorBrowsable(EditorBrowsableState.Never)]
115         public View TitleContent
116         {
117             get
118             {
119                 return titleContent;
120             }
121             set
122             {
123                 if (titleContent == value)
124                 {
125                     return;
126                 }
127
128                 if (titleContent != null)
129                 {
130                     Remove(titleContent);
131                 }
132
133                 titleContent = value;
134                 if (titleContent == null)
135                 {
136                     return;
137                 }
138
139                 if (titleContent is TextLabel textLabel)
140                 {
141                     textLabel.Text = Title;
142                 }
143
144                 ResetContent();
145             }
146         }
147
148         /// <summary>
149         /// Message text of AlertDialog.
150         /// </summary>
151         [EditorBrowsable(EditorBrowsableState.Never)]
152         public string Message
153         {
154             get
155             {
156                 return message;
157             }
158             set
159             {
160                 if (message == value)
161                 {
162                     return;
163                 }
164
165                 message = value;
166
167                 if (Content is TextLabel textLabel)
168                 {
169                     textLabel.Text = message;
170                 }
171             }
172         }
173
174         /// <summary>
175         /// Content of AlertDialog. Content is added to Children automatically.
176         /// </summary>
177         [EditorBrowsable(EditorBrowsableState.Never)]
178         public View Content
179         {
180             get
181             {
182                 return content;
183             }
184             set
185             {
186                 if (content == value)
187                 {
188                     return;
189                 }
190
191                 if (content != null)
192                 {
193                     Remove(content);
194                 }
195
196                 content = value;
197                 if (content == null)
198                 {
199                     return;
200                 }
201
202                 if (content is TextLabel textLabel)
203                 {
204                     textLabel.Text = message;
205                 }
206
207                 ResetContent();
208             }
209         }
210
211         /// <summary>
212         /// Action views of AlertDialog.
213         /// Action views are added to ActionContent of AlertDialog.
214         /// </summary>
215         [EditorBrowsable(EditorBrowsableState.Never)]
216         public IEnumerable<View> Actions
217         {
218             get
219             {
220                 return actionContentViews;
221             }
222             set
223             {
224                 if (ActionContent == null)
225                 {
226                     actionContentViews = value;
227                     return;
228                 }
229
230                 if (actionContentViews != null)
231                 {
232                     foreach (var oldAction in actionContentViews)
233                     {
234                         if (ActionContent.Children?.Contains(oldAction) == true)
235                         {
236                             ActionContent.Children.Remove(oldAction);
237                         }
238                     }
239                 }
240
241                 actionContentViews = value;
242
243                 if (actionContentViews == null)
244                 {
245                     return;
246                 }
247
248                 foreach (var action in actionContentViews)
249                 {
250                     ActionContent.Add(action);
251                 }
252             }
253         }
254
255         /// <summary>
256         /// Action content of AlertDialog. ActionContent is added to Children automatically.
257         /// </summary>
258         [EditorBrowsable(EditorBrowsableState.Never)]
259         public View ActionContent
260         {
261              get
262              {
263                 return actionContent;
264              }
265              set
266              {
267                 if (actionContent == value)
268                  {
269                      return;
270                  }
271
272                 var oldActionContent = actionContent;
273                 actionContent = value;
274
275                 // Add views first before remove previous action content
276                 // not to cause Garbage Collector collects views.
277                 if ((actionContent != null) && (Actions != null))
278                 {
279                     foreach (var action in Actions)
280                     {
281                         actionContent.Add(action);
282                     }
283                 }
284
285                 if (oldActionContent != null)
286                 {
287                     Remove(oldActionContent);
288                 }
289
290                 if (actionContent == null)
291                 {
292                     return;
293                 }
294
295                 ResetContent();
296             }
297         }
298
299         /// <summary>
300         /// AccessibilityGetName.
301         /// </summary>
302         [EditorBrowsable(EditorBrowsableState.Never)]
303         protected override string AccessibilityGetName()
304         {
305             if (!String.IsNullOrEmpty(Title))
306             {
307                 return Title;
308             }
309             else
310             {
311                 return Message;
312             }
313         }
314
315         /// <summary>
316         /// Default title content of AlertDialog.
317         /// If Title is set, then default title content is automatically displayed.
318         /// </summary>
319         [EditorBrowsable(EditorBrowsableState.Never)]
320         protected View DefaultTitleContent
321         {
322             get
323             {
324                 if (defaultTitleContent == null)
325                 {
326                     defaultTitleContent = CreateDefaultTitleContent();
327                 }
328
329                 return defaultTitleContent;
330             }
331         }
332
333         /// <summary>
334         /// Default content of AlertDialog.
335         /// If Message is set, then default content is automatically displayed.
336         /// </summary>
337         [EditorBrowsable(EditorBrowsableState.Never)]
338         protected View DefaultContent
339         {
340             get
341             {
342                 if (defaultContent == null)
343                 {
344                     defaultContent = CreateDefaultContent();
345                 }
346
347                 return defaultContent;
348             }
349         }
350
351         /// <summary>
352         /// Default action content of AlertDialog.
353         /// If Actions are set, then default action content is automatically displayed.
354         /// </summary>
355         [EditorBrowsable(EditorBrowsableState.Never)]
356         protected View DefaultActionContent
357         {
358             get
359             {
360                 if (defaultActionContent == null)
361                 {
362                     defaultActionContent = CreateDefaultActionContent();
363                 }
364
365                 return defaultActionContent;
366             }
367         }
368
369         private void Initialize()
370         {
371             Layout = new LinearLayout()
372             {
373                 LinearOrientation = LinearLayout.Orientation.Vertical,
374             };
375
376             this.Relayout += OnRelayout;
377
378             TitleContent = DefaultTitleContent;
379
380             Content = DefaultContent;
381
382             ActionContent = DefaultActionContent;
383         }
384
385         private void ResetContent()
386         {
387             //To keep the order of TitleContent, Content and ActionContent,
388             //the existing contents are removed and added again.
389             if (titleContent != null)
390             {
391                 Remove(titleContent);
392             }
393
394             if (content != null)
395             {
396                 Remove(content);
397             }
398
399             if (actionContent != null)
400             {
401                 Remove(actionContent);
402             }
403
404             if (titleContent != null)
405             {
406                 Add(titleContent);
407             }
408
409             if (content != null)
410             {
411                 Add(content);
412             }
413
414             if (actionContent != null)
415             {
416                 Add(actionContent);
417             }
418         }
419
420         private TextLabel CreateDefaultTitleContent()
421         {
422             //FIXME: Needs to separate GUI implementation codes to style cs file.
423             return new TextLabel()
424             {
425                 HorizontalAlignment = HorizontalAlignment.Center,
426                 VerticalAlignment = VerticalAlignment.Center,
427                 BackgroundColor = new Color(1.0f, 1.0f, 1.0f, 1.0f),
428                 Size = new Size(360, 80),
429             };
430         }
431
432         private TextLabel CreateDefaultContent()
433         {
434             //FIXME: Needs to separate GUI implementation codes to style cs file.
435             return new TextLabel()
436             {
437                 HorizontalAlignment = HorizontalAlignment.Center,
438                 VerticalAlignment = VerticalAlignment.Center,
439                 BackgroundColor = new Color(1.0f, 1.0f, 1.0f, 1.0f),
440                 Size = new Size(360, 200),
441             };
442         }
443
444         private View CreateDefaultActionContent()
445         {
446             //FIXME: Needs to separate GUI implementation codes to style cs file.
447             return new Control()
448             {
449                 BackgroundColor = new Color(1.0f, 1.0f, 1.0f, 1.0f),
450                 Size = new Size(360, 80),
451                 Layout = new LinearLayout()
452                 {
453                     LinearOrientation = LinearLayout.Orientation.Horizontal,
454                     LinearAlignment = LinearLayout.Alignment.CenterHorizontal,
455                     CellPadding = new Size2D(10, 0),
456                 },
457             };
458         }
459
460         private void OnRelayout(object sender, EventArgs e)
461         {
462             //FIXME: Needs to separate GUI implementation codes to style cs file.
463             CalculatePosition();
464         }
465
466         private void CalculatePosition()
467         {
468             var size = Size2D;
469             var parent = GetParent();
470             Size2D parentSize;
471
472             if ((parent != null) && (parent is View))
473             {
474                 parentSize = ((View)parent).Size;
475             }
476             else
477             {
478                 parentSize = NUIApplication.GetDefaultWindow().Size;
479             }
480
481             Position2D = new Position2D((parentSize.Width - size.Width) / 2, (parentSize.Height - size.Height) / 2);
482         }
483     }
484 }