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;
44 /// Creates a new instance of AlertDialog.
46 [EditorBrowsable(EditorBrowsableState.Never)]
47 public AlertDialog() : base()
53 /// Dispose AlertDialog and all children on it.
55 /// <param name="type">Dispose type.</param>
56 [EditorBrowsable(EditorBrowsableState.Never)]
57 protected override void Dispose(DisposeTypes type)
64 if (type == DisposeTypes.Explicit)
66 if (titleContent != null)
68 Utility.Dispose(titleContent);
73 Utility.Dispose(content);
76 if (actionContent != null)
78 Utility.Dispose(actionContent);
86 /// Title text of AlertDialog.
88 [EditorBrowsable(EditorBrowsableState.Never)]
104 if (TitleContent is TextLabel textLabel)
106 textLabel.Text = title;
112 /// Title content of AlertDialog. TitleContent is added to Children automatically.
114 [EditorBrowsable(EditorBrowsableState.Never)]
115 public View TitleContent
123 if (titleContent == value)
128 if (titleContent != null)
130 Remove(titleContent);
133 titleContent = value;
134 if (titleContent == null)
139 if (titleContent is TextLabel textLabel)
141 textLabel.Text = Title;
149 /// Message text of AlertDialog.
151 [EditorBrowsable(EditorBrowsableState.Never)]
152 public string Message
160 if (message == value)
167 if (Content is TextLabel textLabel)
169 textLabel.Text = message;
175 /// Content of AlertDialog. Content is added to Children automatically.
177 [EditorBrowsable(EditorBrowsableState.Never)]
186 if (content == value)
202 if (content is TextLabel textLabel)
204 textLabel.Text = message;
212 /// Action views of AlertDialog.
213 /// Action views are added to ActionContent of AlertDialog.
215 [EditorBrowsable(EditorBrowsableState.Never)]
216 public IEnumerable<View> Actions
220 return actionContentViews;
224 if (ActionContent == null)
226 actionContentViews = value;
230 if (actionContentViews != null)
232 foreach (var oldAction in actionContentViews)
234 if (ActionContent.Children?.Contains(oldAction) == true)
236 ActionContent.Children.Remove(oldAction);
241 actionContentViews = value;
243 if (actionContentViews == null)
248 foreach (var action in actionContentViews)
250 ActionContent.Add(action);
256 /// Action content of AlertDialog. ActionContent is added to Children automatically.
258 [EditorBrowsable(EditorBrowsableState.Never)]
259 public View ActionContent
263 return actionContent;
267 if (actionContent == value)
272 var oldActionContent = actionContent;
273 actionContent = value;
275 // Add views first before remove previous action content
276 // not to cause Garbage Collector collects views.
277 if ((actionContent != null) && (Actions != null))
279 foreach (var action in Actions)
281 actionContent.Add(action);
285 if (oldActionContent != null)
287 Remove(oldActionContent);
290 if (actionContent == null)
300 /// AccessibilityGetName.
302 [EditorBrowsable(EditorBrowsableState.Never)]
303 protected override string AccessibilityGetName()
305 if (!String.IsNullOrEmpty(Title))
316 /// Default title content of AlertDialog.
317 /// If Title is set, then default title content is automatically displayed.
319 [EditorBrowsable(EditorBrowsableState.Never)]
320 protected View DefaultTitleContent
324 if (defaultTitleContent == null)
326 defaultTitleContent = CreateDefaultTitleContent();
329 return defaultTitleContent;
334 /// Default content of AlertDialog.
335 /// If Message is set, then default content is automatically displayed.
337 [EditorBrowsable(EditorBrowsableState.Never)]
338 protected View DefaultContent
342 if (defaultContent == null)
344 defaultContent = CreateDefaultContent();
347 return defaultContent;
352 /// Default action content of AlertDialog.
353 /// If Actions are set, then default action content is automatically displayed.
355 [EditorBrowsable(EditorBrowsableState.Never)]
356 protected View DefaultActionContent
360 if (defaultActionContent == null)
362 defaultActionContent = CreateDefaultActionContent();
365 return defaultActionContent;
369 private void Initialize()
371 Layout = new LinearLayout()
373 LinearOrientation = LinearLayout.Orientation.Vertical,
376 this.Relayout += OnRelayout;
378 TitleContent = DefaultTitleContent;
380 Content = DefaultContent;
382 ActionContent = DefaultActionContent;
385 private void ResetContent()
387 //To keep the order of TitleContent, Content and ActionContent,
388 //the existing contents are removed and added again.
389 if (titleContent != null)
391 Remove(titleContent);
399 if (actionContent != null)
401 Remove(actionContent);
404 if (titleContent != null)
414 if (actionContent != null)
420 private TextLabel CreateDefaultTitleContent()
422 //FIXME: Needs to separate GUI implementation codes to style cs file.
423 return new TextLabel()
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),
432 private TextLabel CreateDefaultContent()
434 //FIXME: Needs to separate GUI implementation codes to style cs file.
435 return new TextLabel()
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),
444 private View CreateDefaultActionContent()
446 //FIXME: Needs to separate GUI implementation codes to style cs file.
449 BackgroundColor = new Color(1.0f, 1.0f, 1.0f, 1.0f),
450 Size = new Size(360, 80),
451 Layout = new LinearLayout()
453 LinearOrientation = LinearLayout.Orientation.Horizontal,
454 LinearAlignment = LinearLayout.Alignment.CenterHorizontal,
455 CellPadding = new Size2D(10, 0),
460 private void OnRelayout(object sender, EventArgs e)
462 //FIXME: Needs to separate GUI implementation codes to style cs file.
466 private void CalculatePosition()
469 var parent = GetParent();
472 if ((parent != null) && (parent is View))
474 parentSize = ((View)parent).Size;
478 parentSize = NUIApplication.GetDefaultWindow().Size;
481 Position2D = new Position2D((parentSize.Width - size.Width) / 2, (parentSize.Height - size.Height) / 2);