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 [EditorBrowsable(EditorBrowsableState.Never)]
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 [EditorBrowsable(EditorBrowsableState.Never)]
54 public AlertDialog() : base()
60 /// Dispose AlertDialog and all children on it.
62 /// <param name="type">Dispose type.</param>
63 [EditorBrowsable(EditorBrowsableState.Never)]
64 protected override void Dispose(DisposeTypes type)
71 if (type == DisposeTypes.Explicit)
73 if (titleContent != null)
75 Utility.Dispose(titleContent);
80 Utility.Dispose(content);
83 if (actionContent != null)
85 Utility.Dispose(actionContent);
88 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
89 // Until the bug is fixed, padding view is added after action content.
90 if (defaultActionContentPadding != null)
92 Utility.Dispose(defaultActionContentPadding);
100 /// Applies style to AlertDialog.
102 /// <param name="viewStyle">The style to apply.</param>
103 [EditorBrowsable(EditorBrowsableState.Never)]
104 public override void ApplyStyle(ViewStyle viewStyle)
106 styleApplied = false;
108 base.ApplyStyle(viewStyle);
110 // Apply Title style.
111 if ((alertDialogStyle?.TitleTextLabel != null) && (DefaultTitleContent is TextLabel))
113 ((TextLabel)DefaultTitleContent)?.ApplyStyle(alertDialogStyle.TitleTextLabel);
116 // Apply Message style.
117 if ((alertDialogStyle?.MessageTextLabel != null) && (DefaultContent is TextLabel))
119 ((TextLabel)DefaultContent)?.ApplyStyle(alertDialogStyle.MessageTextLabel);
122 // Apply ActionContent style.
123 if (alertDialogStyle?.ActionContent != null)
125 DefaultActionContent?.ApplyStyle(alertDialogStyle.ActionContent);
130 // Calculate dialog position and children's positions based on padding sizes.
135 /// Title text of AlertDialog.
137 [EditorBrowsable(EditorBrowsableState.Never)]
153 if (TitleContent is TextLabel textLabel)
155 textLabel.Text = title;
161 /// Title content of AlertDialog. TitleContent is added to Children automatically.
163 [EditorBrowsable(EditorBrowsableState.Never)]
164 public View TitleContent
172 if (titleContent == value)
177 if (titleContent != null)
179 Remove(titleContent);
182 titleContent = value;
183 if (titleContent == null)
188 if (titleContent is TextLabel textLabel)
190 textLabel.Text = Title;
198 /// Message text of AlertDialog.
200 [EditorBrowsable(EditorBrowsableState.Never)]
201 public string Message
209 if (message == value)
216 if (Content is TextLabel textLabel)
218 textLabel.Text = message;
224 /// Content of AlertDialog. Content is added to Children automatically.
226 [EditorBrowsable(EditorBrowsableState.Never)]
235 if (content == value)
251 if (content is TextLabel textLabel)
253 textLabel.Text = message;
261 /// Action views of AlertDialog.
262 /// Action views are added to ActionContent of AlertDialog.
264 [EditorBrowsable(EditorBrowsableState.Never)]
265 public IEnumerable<View> Actions
269 return actionContentViews;
273 if (ActionContent == null)
275 actionContentViews = value;
279 if (actionContentViews != null)
281 foreach (var oldAction in actionContentViews)
283 if (ActionContent.Children?.Contains(oldAction) == true)
285 ActionContent.Children.Remove(oldAction);
290 actionContentViews = value;
292 if (actionContentViews == null)
297 foreach (var action in actionContentViews)
299 ActionContent.Add(action);
305 /// Action content of AlertDialog. ActionContent is added to Children automatically.
307 [EditorBrowsable(EditorBrowsableState.Never)]
308 public View ActionContent
312 return actionContent;
316 if (actionContent == value)
321 var oldActionContent = actionContent;
322 actionContent = value;
324 // Add views first before remove previous action content
325 // not to cause Garbage Collector collects views.
326 if ((actionContent != null) && (Actions != null))
328 foreach (var action in Actions)
330 actionContent.Add(action);
334 if (oldActionContent != null)
336 Remove(oldActionContent);
339 if (actionContent == null)
349 /// AccessibilityGetName.
351 [EditorBrowsable(EditorBrowsableState.Never)]
352 protected override string AccessibilityGetName()
354 if (!String.IsNullOrEmpty(Title))
365 /// Default title content of AlertDialog.
366 /// If Title is set, then default title content is automatically displayed.
368 [EditorBrowsable(EditorBrowsableState.Never)]
369 protected View DefaultTitleContent
373 if (defaultTitleContent == null)
375 defaultTitleContent = CreateDefaultTitleContent();
378 return defaultTitleContent;
383 /// Default content of AlertDialog.
384 /// If Message is set, then default content is automatically displayed.
386 [EditorBrowsable(EditorBrowsableState.Never)]
387 protected View DefaultContent
391 if (defaultContent == null)
393 defaultContent = CreateDefaultContent();
396 return defaultContent;
401 /// Default action content of AlertDialog.
402 /// If Actions are set, then default action content is automatically displayed.
404 [EditorBrowsable(EditorBrowsableState.Never)]
405 protected View DefaultActionContent
409 if (defaultActionContent == null)
411 defaultActionContent = CreateDefaultActionContent();
414 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
415 // Until the bug is fixed, padding view is added after action content.
416 if (defaultActionContentPadding == null)
418 defaultActionContentPadding = CreateDefaultActionContentPadding();
421 return defaultActionContent;
425 private void Initialize()
427 Layout = new LinearLayout()
429 LinearOrientation = LinearLayout.Orientation.Vertical,
432 this.Relayout += OnRelayout;
434 TitleContent = DefaultTitleContent;
436 Content = DefaultContent;
438 ActionContent = DefaultActionContent;
441 private void ResetContent()
443 //To keep the order of TitleContent, Content and ActionContent,
444 //the existing contents are removed and added again.
445 if (titleContent != null)
447 Remove(titleContent);
455 if (actionContent != null)
457 Remove(actionContent);
460 if (titleContent != null)
470 if (actionContent != null)
474 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
475 // Until the bug is fixed, padding view is added after action content.
476 if (actionContent == defaultActionContent)
478 if (defaultActionContentPadding != null)
480 Add(defaultActionContentPadding);
486 private TextLabel CreateDefaultTitleContent()
488 return new TextLabel();
491 private TextLabel CreateDefaultContent()
493 return new TextLabel();
496 private View CreateDefaultActionContent()
500 Layout = new LinearLayout()
502 LinearOrientation = LinearLayout.Orientation.Horizontal,
507 // FIXME: Now AlertDialog.Padding Top and Bottom increases AlertDialog size incorrectly.
508 // Until the bug is fixed, padding view is added after action content.
509 private View CreateDefaultActionContentPadding()
511 var layout = Layout as LinearLayout;
513 if ((layout == null) || (defaultActionContent == null))
518 View paddingView = null;
520 using (Size2D size = new Size2D(defaultActionContent.Size2D.Width, defaultActionContent.Size2D.Height))
522 if (layout.LinearOrientation == LinearLayout.Orientation.Horizontal)
531 paddingView = new View()
533 Size2D = new Size2D(size.Width, size.Height),
540 private void OnRelayout(object sender, EventArgs e)
542 // Calculate dialog position and children's positions based on padding sizes.
546 // Calculate dialog position and children's positions based on padding sizes.
547 private void CalculatePosition()
549 if (styleApplied == false)
554 CalculateActionsCellPadding();
557 var parent = GetParent();
560 if ((parent != null) && (parent is View))
562 parentSize = ((View)parent).Size;
566 parentSize = NUIApplication.GetDefaultWindow().Size;
569 Position2D = new Position2D((parentSize.Width - size.Width) / 2, (parentSize.Height - size.Height) / 2);
572 // Calculate CellPadding among Actions if ActionContent is LinearLayout.
573 private void CalculateActionsCellPadding()
575 if ((ActionContent != DefaultActionContent) || (ActionContent.Layout is LinearLayout == false))
586 var layout = ActionContent.Layout as LinearLayout;
589 if (layout.LinearOrientation == LinearLayout.Orientation.Horizontal)
591 int actionsWidth = 0;
593 foreach (var action in Actions)
595 actionsWidth += ((View)action).Size2D.Width + ((((View)action).Margin?.Start + ((View)action).Margin?.End) ?? 0);
601 actionsWidth += (Padding?.Start + Padding?.End) ?? 0;
602 var cellPaddingWidth = (size.Width - actionsWidth) / (count - 1);
603 layout.CellPadding = new Size2D(cellPaddingWidth , 0);
608 int actionsHeight = 0;
610 foreach (var action in Actions)
612 actionsHeight += ((View)action).Size2D.Height + ((((View)action).Margin?.Top + ((View)action).Margin?.Bottom) ?? 0);
618 actionsHeight += (Padding?.Top + Padding?.Bottom) ?? 0;
619 var cellPaddingHeight = (size.Height - actionsHeight) / (count - 1);
620 layout.CellPadding = new Size2D(0, cellPaddingHeight);