[NUI] Set EnableDismissOnScrim true by default
[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     [EditorBrowsable(EditorBrowsableState.Never)]
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         [EditorBrowsable(EditorBrowsableState.Never)]
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. Content is added to Children automatically.
84         /// </summary>
85         [EditorBrowsable(EditorBrowsableState.Never)]
86         public View Content
87         {
88             get
89             {
90                 return content;
91             }
92             set
93             {
94                 if (content == value)
95                 {
96                     return;
97                 }
98
99                 if (content != null)
100                 {
101                     Remove(content);
102                 }
103
104                 content = value;
105                 if (content == null)
106                 {
107                     return;
108                 }
109
110                 Add(content);
111
112                 if (Scrim != null)
113                 {
114                     content.RaiseAbove(Scrim);
115                 }
116             }
117         }
118
119         /// <summary>
120         /// Scrim of DialogPage. Scrim is added to Children automatically.
121         /// </summary>
122         [EditorBrowsable(EditorBrowsableState.Never)]
123         protected View Scrim
124         {
125             get
126             {
127                 return scrim;
128             }
129             set
130             {
131                 if (scrim == value)
132                 {
133                     return;
134                 }
135
136                 if (scrim != null)
137                 {
138                     Remove(scrim);
139                 }
140
141                 scrim = value;
142                 if (scrim == null)
143                 {
144                     return;
145                 }
146
147                 Add(scrim);
148
149                 if (Content != null)
150                 {
151                     Content.RaiseAbove(scrim);
152                 }
153
154                 if (EnableScrim != Scrim.Visibility)
155                 {
156                     if (EnableScrim == true)
157                     {
158                         scrim.Show();
159                     }
160                     else
161                     {
162                         scrim.Hide();
163                     }
164                 }
165             }
166         }
167
168         /// <summary>
169         /// Indicates to show scrim behind dialog.
170         /// </summary>
171         [EditorBrowsable(EditorBrowsableState.Never)]
172         public bool EnableScrim
173         {
174             get
175             {
176                 return enableScrim;
177             }
178             set
179             {
180                 if (enableScrim == value)
181                 {
182                     return;
183                 }
184
185                 enableScrim = value;
186
187                 if ((Scrim != null) && (enableScrim != Scrim.Visibility))
188                 {
189                     if (enableScrim == true)
190                     {
191                         Scrim.Show();
192                     }
193                     else
194                     {
195                         Scrim.Hide();
196                     }
197                 }
198             }
199         }
200
201         /// <summary>
202         /// Indicates to dismiss dialog by touching on scrim.
203         /// </summary>
204         [EditorBrowsable(EditorBrowsableState.Never)]
205         public bool EnableDismissOnScrim { get; set; } = true;
206
207         /// <summary>
208         /// The color of scrim.
209         /// </summary>
210         [EditorBrowsable(EditorBrowsableState.Never)]
211         public Color ScrimColor
212         {
213             get
214             {
215                 return Scrim?.BackgroundColor;
216             }
217             set
218             {
219                 if (Scrim != null)
220                 {
221                     Scrim.BackgroundColor = value;
222                 }
223             }
224         }
225
226         private View CreateDefaultScrim()
227         {
228             //FIXME: Needs to separate GUI implementation codes to style cs file.
229             var scrim = new VisualView();
230             scrim.BackgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.5f);
231             //FIXME: Needs to set proper size to Scrim.
232             scrim.Size = NUIApplication.GetDefaultWindow().Size;
233             scrim.TouchEvent += (object source, TouchEventArgs e) =>
234             {
235                 if ((EnableDismissOnScrim == true) && (e.Touch.GetState(0) == PointStateType.Up))
236                 {
237                     this.Navigator.Pop();
238                 }
239                 return true;
240             };
241
242             return scrim;
243         }
244
245         /// <summary>
246         /// Shows a dialog by pushing a dialog page containing dialog to default navigator.
247         /// </summary>
248         /// <param name="content">The content of Dialog.</param>
249         [EditorBrowsable(EditorBrowsableState.Never)]
250         [SuppressMessage("Microsoft.Reliability",
251                          "CA2000:DisposeObjectsBeforeLosingScope",
252                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
253         public static void ShowDialog(View content)
254         {
255             var dialogPage = new DialogPage()
256             {
257                 Content = new Dialog()
258                 {
259                     Content = content,
260                 },
261             };
262
263             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
264         }
265
266         /// <summary>
267         /// Shows an alert dialog by pushing a page containing the alert dialog
268         /// to default navigator.
269         /// </summary>
270         /// <param name="title">The title of AlertDialog.</param>
271         /// <param name="message">The message of AlertDialog.</param>
272         /// <param name="actions">The action views of AlertDialog.</param>
273         [EditorBrowsable(EditorBrowsableState.Never)]
274         [SuppressMessage("Microsoft.Reliability",
275                          "CA2000:DisposeObjectsBeforeLosingScope",
276                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
277         public static void ShowAlertDialog(string title, string message, params View[] actions)
278         {
279             var dialogPage = new DialogPage()
280             {
281                 Content = new AlertDialog()
282                 {
283                     Title = title,
284                     Message = message,
285                     Actions =  actions,
286                 },
287             };
288
289             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
290         }
291
292         /// <summary>
293         /// Shows a menu by pushing a dialog page containing menu to default navigator.
294         /// </summary>
295         /// <param name="anchor">The anchor view where menu is displayed.</param>
296         /// <param name="items">The menu items.</param>
297         [EditorBrowsable(EditorBrowsableState.Never)]
298         [SuppressMessage("Microsoft.Reliability",
299                          "CA2000:DisposeObjectsBeforeLosingScope",
300                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
301         public static void ShowMenu(View anchor, params MenuItem[] items)
302         {
303             if (items == null)
304             {
305                 return;
306             }
307
308             Position2D anchorPosition = new Position2D((int)(anchor?.ScreenPosition.X ?? 0), (int)(anchor?.ScreenPosition.Y ?? 0) + (anchor?.Size2D.Height ?? 0) + (anchor?.Margin.Bottom ?? 0));
309
310             var dialogPage = new DialogPage()
311             {
312                 Content = new Menu()
313                 {
314                     Items = items,
315                     AnchorPosition = anchorPosition,
316                 },
317                 ScrimColor = Color.Transparent,
318             };
319
320             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
321         }
322
323         /// <summary>
324         /// Shows a menu by pushing a dialog page containing menu to default navigator.
325         /// </summary>
326         /// <param name="anchorPosition">The anchor position where menu is displayed.</param>
327         /// <param name="items">The menu items.</param>
328         [EditorBrowsable(EditorBrowsableState.Never)]
329         [SuppressMessage("Microsoft.Reliability",
330                          "CA2000:DisposeObjectsBeforeLosingScope",
331                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
332         public static void ShowMenu(Position2D anchorPosition, params MenuItem[] items)
333         {
334             if (items == null)
335             {
336                 return;
337             }
338
339             var dialogPage = new DialogPage()
340             {
341                 Content = new Menu()
342                 {
343                     Items = items,
344                     AnchorPosition = anchorPosition,
345                 },
346                 ScrimColor = Color.Transparent,
347             };
348
349             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
350         }
351     }
352 }