Make Dialog APIs public
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Navigation / DialogPage.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.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using System.Diagnostics.CodeAnalysis;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// The DialogPage class is a class which shows a dialog on the page.
26     /// DialogPage contains dialog and dimmed scrim behind the dialog.
27     /// </summary>
28     /// <since_tizen> 9 </since_tizen>
29     public class DialogPage : Page
30     {
31         private View content = null;
32         private View scrim = null;
33         private bool enableScrim = true;
34
35         /// <summary>
36         /// Creates a new instance of a DialogPage.
37         /// </summary>
38         /// <since_tizen> 9 </since_tizen>
39         public DialogPage() : base()
40         {
41             Layout = new AbsoluteLayout();
42
43             // DialogPage fills to parent by default.
44             WidthResizePolicy = ResizePolicyType.FillToParent;
45             HeightResizePolicy = ResizePolicyType.FillToParent;
46
47             // FIXME: To pass touch event when Scrim is disabled.
48             //        When proper way to pass touch event is introduced, this code should be fixed.
49             EnableControlState = false;
50
51             Scrim = CreateDefaultScrim();
52         }
53
54         /// <summary>
55         /// Dispose DialogPage and all children on it.
56         /// </summary>
57         /// <param name="type">Dispose type.</param>
58         [EditorBrowsable(EditorBrowsableState.Never)]
59         protected override void Dispose(DisposeTypes type)
60         {
61             if (disposed)
62             {
63                 return;
64             }
65
66             if (type == DisposeTypes.Explicit)
67             {
68                 if (content != null)
69                 {
70                     Utility.Dispose(content);
71                 }
72
73                 if (scrim != null)
74                 {
75                     Utility.Dispose(scrim);
76                 }
77             }
78
79             base.Dispose(type);
80         }
81
82         /// <summary>
83         /// Content of DialogPage.
84         /// Content is used as dialog, so Content is displayed above the dimmed scrim.
85         /// Content is added as a child of DialogPage automatically.
86         /// </summary>
87         /// <since_tizen> 9 </since_tizen>
88         public View Content
89         {
90             get
91             {
92                 return content;
93             }
94             set
95             {
96                 if (content == value)
97                 {
98                     return;
99                 }
100
101                 if (content != null)
102                 {
103                     Remove(content);
104                 }
105
106                 content = value;
107                 if (content == null)
108                 {
109                     return;
110                 }
111
112                 Add(content);
113
114                 if (Scrim != null)
115                 {
116                     foreach (var child in Children)
117                     {
118                         if (child != Scrim)
119                         {
120                             // All children are above Scrim not to be covered by Scrim.
121                             child.RaiseAbove(Scrim);
122                         }
123                     }
124                 }
125             }
126         }
127
128         /// <summary>
129         /// Scrim of DialogPage.
130         /// Scrim is the dimmed screen region behind dialog.
131         /// Scrim is added as a child of DialogPage automatically.
132         /// </summary>
133         /// <since_tizen> 9 </since_tizen>
134         protected View Scrim
135         {
136             get
137             {
138                 return scrim;
139             }
140             set
141             {
142                 if (scrim == value)
143                 {
144                     return;
145                 }
146
147                 if (scrim != null)
148                 {
149                     Remove(scrim);
150                 }
151
152                 scrim = value;
153                 if (scrim == null)
154                 {
155                     return;
156                 }
157
158                 Add(scrim);
159
160                 foreach (var child in Children)
161                 {
162                     if (child != scrim)
163                     {
164                         // All children are above Scrim not to be covered by Scrim.
165                         child.RaiseAbove(scrim);
166                     }
167                 }
168
169                 if (EnableScrim != Scrim.Visibility)
170                 {
171                     if (EnableScrim == true)
172                     {
173                         scrim.Show();
174                     }
175                     else
176                     {
177                         scrim.Hide();
178                     }
179                 }
180             }
181         }
182
183         /// <summary>
184         /// Indicates to show scrim behind dialog.
185         /// </summary>
186         /// <since_tizen> 9 </since_tizen>
187         public bool EnableScrim
188         {
189             get
190             {
191                 return enableScrim;
192             }
193             set
194             {
195                 if (enableScrim == value)
196                 {
197                     return;
198                 }
199
200                 enableScrim = value;
201
202                 if ((Scrim != null) && (enableScrim != Scrim.Visibility))
203                 {
204                     if (enableScrim == true)
205                     {
206                         Scrim.Show();
207                     }
208                     else
209                     {
210                         Scrim.Hide();
211                     }
212                 }
213             }
214         }
215
216         /// <summary>
217         /// Indicates to dismiss dialog by touching on scrim.
218         /// </summary>
219         /// <since_tizen> 9 </since_tizen>
220         public bool EnableDismissOnScrim { get; set; } = true;
221
222         /// <summary>
223         /// The color of scrim.
224         /// </summary>
225         /// <since_tizen> 9 </since_tizen>
226         public Color ScrimColor
227         {
228             get
229             {
230                 return Scrim?.BackgroundColor;
231             }
232             set
233             {
234                 if (Scrim != null)
235                 {
236                     Scrim.BackgroundColor = value;
237                 }
238             }
239         }
240
241         private View CreateDefaultScrim()
242         {
243             //FIXME: Needs to separate GUI implementation codes to style cs file.
244             var scrim = new VisualView();
245             scrim.BackgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.5f);
246             //FIXME: Needs to set proper size to Scrim.
247             scrim.Size = NUIApplication.GetDefaultWindow().Size;
248             scrim.TouchEvent += (object source, TouchEventArgs e) =>
249             {
250                 if ((EnableDismissOnScrim == true) && (e.Touch.GetState(0) == PointStateType.Up))
251                 {
252                     this.Navigator.Pop();
253                 }
254                 return true;
255             };
256
257             return scrim;
258         }
259
260         /// <summary>
261         /// Shows a dialog by pushing a dialog page containing dialog to default navigator.
262         /// </summary>
263         /// <param name="content">The content of Dialog.</param>
264         /// <since_tizen> 9 </since_tizen>
265         [SuppressMessage("Microsoft.Reliability",
266                          "CA2000:DisposeObjectsBeforeLosingScope",
267                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
268         public static void ShowDialog(View content)
269         {
270             var dialogPage = new DialogPage()
271             {
272                 Content = new Dialog()
273                 {
274                     Content = content,
275                 },
276             };
277
278             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
279         }
280
281         /// <summary>
282         /// Shows an alert dialog by pushing a page containing the alert dialog
283         /// to default navigator.
284         /// </summary>
285         /// <param name="title">The title of AlertDialog.</param>
286         /// <param name="message">The message of AlertDialog.</param>
287         /// <param name="actions">The action views of AlertDialog.</param>
288         /// <since_tizen> 9 </since_tizen>
289         [SuppressMessage("Microsoft.Reliability",
290                          "CA2000:DisposeObjectsBeforeLosingScope",
291                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
292         public static void ShowAlertDialog(string title, string message, params View[] actions)
293         {
294             var dialogPage = new DialogPage()
295             {
296                 Content = new AlertDialog()
297                 {
298                     Title = title,
299                     Message = message,
300                     Actions =  actions,
301                 },
302             };
303
304             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
305         }
306
307         /// <summary>
308         /// Shows a menu by pushing a dialog page containing menu to default navigator.
309         /// </summary>
310         /// <param name="anchor">The anchor view where menu is displayed.</param>
311         /// <param name="items">The menu items.</param>
312         [EditorBrowsable(EditorBrowsableState.Never)]
313         [SuppressMessage("Microsoft.Reliability",
314                          "CA2000:DisposeObjectsBeforeLosingScope",
315                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
316         public static void ShowMenu(View anchor, params MenuItem[] items)
317         {
318             if (items == null)
319             {
320                 return;
321             }
322
323             Position2D anchorPosition = new Position2D((int)(anchor?.ScreenPosition.X ?? 0), (int)(anchor?.ScreenPosition.Y ?? 0) + (anchor?.Size2D.Height ?? 0) + (anchor?.Margin.Bottom ?? 0));
324
325             var dialogPage = new DialogPage()
326             {
327                 Content = new Menu()
328                 {
329                     Items = items,
330                     AnchorPosition = anchorPosition,
331                 },
332                 ScrimColor = Color.Transparent,
333             };
334
335             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
336         }
337
338         /// <summary>
339         /// Shows a menu by pushing a dialog page containing menu to default navigator.
340         /// </summary>
341         /// <param name="anchorPosition">The anchor position where menu is displayed.</param>
342         /// <param name="items">The menu items.</param>
343         [EditorBrowsable(EditorBrowsableState.Never)]
344         [SuppressMessage("Microsoft.Reliability",
345                          "CA2000:DisposeObjectsBeforeLosingScope",
346                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
347         public static void ShowMenu(Position2D anchorPosition, params MenuItem[] items)
348         {
349             if (items == null)
350             {
351                 return;
352             }
353
354             var dialogPage = new DialogPage()
355             {
356                 Content = new Menu()
357                 {
358                     Items = items,
359                     AnchorPosition = anchorPosition,
360                 },
361                 ScrimColor = Color.Transparent,
362             };
363
364             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
365         }
366     }
367 }