2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.ComponentModel;
20 using System.Collections.Generic;
21 using Tizen.NUI.BaseComponents;
23 namespace Tizen.NUI.Components
26 /// AlertDialog class shows a dialog with title, message and action buttons.
28 /// <since_tizen> 9 </since_tizen>
29 public class AlertDialog : Control
31 private string title = null;
32 private string message = null;
34 private View titleContent = null;
35 private View content = null;
36 private View actionContent = null;
37 private IEnumerable<View> actionContentViews = null;
39 private View defaultTitleContent = null;
40 private View defaultContent = null;
41 private View defaultActionContent = null;
42 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
43 // Until the bug is fixed, padding view is added after action content.
44 private View defaultActionContentPadding = null;
46 private AlertDialogStyle alertDialogStyle => ViewStyle as AlertDialogStyle;
48 private bool styleApplied = false;
51 /// Creates a new instance of AlertDialog.
53 /// <since_tizen> 9 </since_tizen>
54 public AlertDialog() : base()
60 /// Creates a new instance of AlertDialog.
62 /// <param name="style">Creates AlertDialog by special style defined in UX.</param>
63 /// <since_tizen> 9 </since_tizen>
64 public AlertDialog(string style) : base(style)
70 /// Creates a new instance of AlertDialog.
72 /// <param name="alertDialogStyle">Creates AlertDialog by style customized by user.</param>
73 /// <since_tizen> 9 </since_tizen>
74 public AlertDialog(AlertDialogStyle alertDialogStyle) : base(alertDialogStyle)
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 protected override void Dispose(DisposeTypes type)
88 if (type == DisposeTypes.Explicit)
90 if (titleContent != null)
92 Utility.Dispose(titleContent);
97 Utility.Dispose(content);
100 if (actionContent != null)
102 Utility.Dispose(actionContent);
105 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
106 // Until the bug is fixed, padding view is added after action content.
107 if (defaultActionContentPadding != null)
109 Utility.Dispose(defaultActionContentPadding);
117 /// Applies style to AlertDialog.
119 /// <param name="viewStyle">The style to apply.</param>
120 /// <since_tizen> 9 </since_tizen>
121 public override void ApplyStyle(ViewStyle viewStyle)
123 styleApplied = false;
125 base.ApplyStyle(viewStyle);
127 // Apply Title style.
128 if ((alertDialogStyle?.TitleTextLabel != null) && (DefaultTitleContent is TextLabel))
130 ((TextLabel)DefaultTitleContent)?.ApplyStyle(alertDialogStyle.TitleTextLabel);
133 // Apply Message style.
134 if ((alertDialogStyle?.MessageTextLabel != null) && (DefaultContent is TextLabel))
136 ((TextLabel)DefaultContent)?.ApplyStyle(alertDialogStyle.MessageTextLabel);
139 // Apply ActionContent style.
140 if (alertDialogStyle?.ActionContent != null)
142 DefaultActionContent?.ApplyStyle(alertDialogStyle.ActionContent);
147 // Calculate dialog position and children's positions based on padding sizes.
152 /// Title text of AlertDialog.
153 /// Title text is set to TitleContent's Text if TitleContent is TextLabel.
154 /// If TitleContent's Text is set manually by user, then it is not guaranteed that TitleContent's Text is the same with Title text.
156 /// <since_tizen> 9 </since_tizen>
172 if (TitleContent is TextLabel textLabel)
174 textLabel.Text = title;
180 /// Title content of AlertDialog.
181 /// TitleContent is added as a child of AlertDialog automatically.
182 /// Title text is set to TitleContent's Text if TitleContent is TextLabel.
183 /// If TitleContent's Text is set manually by user, then it is not guaranteed that TitleContent's Text is the same with Title text.
185 /// <since_tizen> 9 </since_tizen>
186 public View TitleContent
194 if (titleContent == value)
199 if (titleContent != null)
201 Remove(titleContent);
204 titleContent = value;
205 if (titleContent == null)
210 if (titleContent is TextLabel textLabel)
212 textLabel.Text = Title;
220 /// Message text of AlertDialog.
221 /// Message text is set to Content's Text if Content is TextLabel.
222 /// If Content's Text is set manually by user, then it is not guaranteed that Content's Text is the same with Message text.
224 /// <since_tizen> 9 </since_tizen>
225 public string Message
233 if (message == value)
240 if (Content is TextLabel textLabel)
242 textLabel.Text = message;
248 /// Content of AlertDialog.
249 /// Content is added as a child of AlertDialog automatically.
250 /// Message text is set to Content's Text if Content is TextLabel.
251 /// If Content's Text is set manually by user, then it is not guaranteed that Content's Text is the same with Message text.
253 /// <since_tizen> 9 </since_tizen>
262 if (content == value)
278 if (content is TextLabel textLabel)
280 textLabel.Text = message;
288 /// Action views of AlertDialog.
289 /// Action views are added as children of ActionContent.
290 /// When Actions are set, previous Actions are removed from ActionContent.
292 /// <since_tizen> 9 </since_tizen>
293 public IEnumerable<View> Actions
297 return actionContentViews;
301 if (ActionContent == null)
303 actionContentViews = value;
307 if (actionContentViews != null)
309 foreach (var oldAction in actionContentViews)
311 if (ActionContent.Children?.Contains(oldAction) == true)
313 ActionContent.Children.Remove(oldAction);
318 actionContentViews = value;
320 if (actionContentViews == null)
325 foreach (var action in actionContentViews)
327 ActionContent.Add(action);
333 /// Action content of AlertDialog.
334 /// ActionContent is added as a child of AlertDialog automatically.
335 /// Actions are added as children of ActionContent.
337 /// <since_tizen> 9 </since_tizen>
338 public View ActionContent
342 return actionContent;
346 if (actionContent == value)
351 var oldActionContent = actionContent;
352 actionContent = value;
354 // Add views first before remove previous action content
355 // not to cause Garbage Collector collects views.
356 if ((actionContent != null) && (Actions != null))
358 foreach (var action in Actions)
360 actionContent.Add(action);
364 if (oldActionContent != null)
366 Remove(oldActionContent);
369 if (actionContent == null)
379 /// AccessibilityGetName.
381 [EditorBrowsable(EditorBrowsableState.Never)]
382 protected override string AccessibilityGetName()
384 if (!String.IsNullOrEmpty(Title))
395 /// Default title content of AlertDialog.
396 /// If Title is set, then default title content is automatically displayed.
398 [EditorBrowsable(EditorBrowsableState.Never)]
399 protected View DefaultTitleContent
403 if (defaultTitleContent == null)
405 defaultTitleContent = CreateDefaultTitleContent();
408 return defaultTitleContent;
413 /// Default content of AlertDialog.
414 /// If Message is set, then default content is automatically displayed.
416 [EditorBrowsable(EditorBrowsableState.Never)]
417 protected View DefaultContent
421 if (defaultContent == null)
423 defaultContent = CreateDefaultContent();
426 return defaultContent;
431 /// Default action content of AlertDialog.
432 /// If Actions are set, then default action content is automatically displayed.
434 [EditorBrowsable(EditorBrowsableState.Never)]
435 protected View DefaultActionContent
439 if (defaultActionContent == null)
441 defaultActionContent = CreateDefaultActionContent();
444 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
445 // Until the bug is fixed, padding view is added after action content.
446 if (defaultActionContentPadding == null)
448 defaultActionContentPadding = CreateDefaultActionContentPadding();
451 return defaultActionContent;
455 private void Initialize()
457 Layout = new LinearLayout()
459 LinearOrientation = LinearLayout.Orientation.Vertical,
462 this.Relayout += OnRelayout;
464 TitleContent = DefaultTitleContent;
466 Content = DefaultContent;
468 ActionContent = DefaultActionContent;
471 private void ResetContent()
473 //To keep the order of TitleContent, Content and ActionContent,
474 //the existing contents are removed and added again.
475 if (titleContent != null)
477 Remove(titleContent);
485 if (actionContent != null)
487 Remove(actionContent);
490 if (titleContent != null)
500 if (actionContent != null)
504 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
505 // Until the bug is fixed, padding view is added after action content.
506 if (actionContent == defaultActionContent)
508 if (defaultActionContentPadding != null)
510 Add(defaultActionContentPadding);
516 private TextLabel CreateDefaultTitleContent()
518 return new TextLabel();
521 private TextLabel CreateDefaultContent()
523 return new TextLabel();
526 private View CreateDefaultActionContent()
530 Layout = new LinearLayout()
532 LinearOrientation = LinearLayout.Orientation.Horizontal,
537 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
538 // Until the bug is fixed, padding view is added after action content.
539 private View CreateDefaultActionContentPadding()
541 var layout = Layout as LinearLayout;
543 if ((layout == null) || (defaultActionContent == null))
548 View paddingView = null;
550 using (Size2D size = new Size2D(defaultActionContent.Size2D.Width, defaultActionContent.Size2D.Height))
552 if (layout.LinearOrientation == LinearLayout.Orientation.Horizontal)
561 paddingView = new View()
563 Size2D = new Size2D(size.Width, size.Height),
570 private void OnRelayout(object sender, EventArgs e)
572 // Calculate dialog position and children's positions based on padding sizes.
576 // Calculate dialog position and children's positions based on padding sizes.
577 private void CalculatePosition()
579 if (styleApplied == false)
584 CalculateActionsCellPadding();
587 var parent = GetParent();
590 if ((parent != null) && (parent is View))
592 parentSize = ((View)parent).Size;
596 parentSize = NUIApplication.GetDefaultWindow().Size;
599 Position2D = new Position2D((parentSize.Width - size.Width) / 2, (parentSize.Height - size.Height) / 2);
602 // Calculate CellPadding among Actions if ActionContent is LinearLayout.
603 private void CalculateActionsCellPadding()
605 if ((ActionContent != DefaultActionContent) || (ActionContent.Layout is LinearLayout == false))
616 var layout = ActionContent.Layout as LinearLayout;
619 if (layout.LinearOrientation == LinearLayout.Orientation.Horizontal)
621 int actionsWidth = 0;
623 foreach (var action in Actions)
625 actionsWidth += ((View)action).Size2D.Width + ((((View)action).Margin?.Start + ((View)action).Margin?.End) ?? 0);
631 actionsWidth += (Padding?.Start + Padding?.End) ?? 0;
632 var cellPaddingWidth = (size.Width - actionsWidth) / (count - 1);
633 layout.CellPadding = new Size2D(cellPaddingWidth , 0);
638 int actionsHeight = 0;
640 foreach (var action in Actions)
642 actionsHeight += ((View)action).Size2D.Height + ((((View)action).Margin?.Top + ((View)action).Margin?.Bottom) ?? 0);
648 actionsHeight += (Padding?.Top + Padding?.Bottom) ?? 0;
649 var cellPaddingHeight = (size.Height - actionsHeight) / (count - 1);
650 layout.CellPadding = new Size2D(0, cellPaddingHeight);