268b69f99d4e11e8307cc6ebdd8e0a2553743294
[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             Scrim = CreateDefaultScrim();
48         }
49
50         /// <summary>
51         /// Dispose DialogPage and all children on it.
52         /// </summary>
53         /// <param name="type">Dispose type.</param>
54         [EditorBrowsable(EditorBrowsableState.Never)]
55         protected override void Dispose(DisposeTypes type)
56         {
57             if (disposed)
58             {
59                 return;
60             }
61
62             if (type == DisposeTypes.Explicit)
63             {
64                 if (content != null)
65                 {
66                     Utility.Dispose(content);
67                 }
68
69                 if (scrim != null)
70                 {
71                     Utility.Dispose(scrim);
72                 }
73             }
74
75             base.Dispose(type);
76         }
77
78         /// <summary>
79         /// Content of DialogPage. Content is added to Children automatically.
80         /// </summary>
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         public View Content
83         {
84             get
85             {
86                 return content;
87             }
88             set
89             {
90                 if (content == value)
91                 {
92                     return;
93                 }
94
95                 if (content != null)
96                 {
97                     Remove(content);
98                 }
99
100                 content = value;
101                 if (content == null)
102                 {
103                     return;
104                 }
105
106                 Add(content);
107
108                 if (Scrim != null)
109                 {
110                     content.RaiseAbove(Scrim);
111                 }
112             }
113         }
114
115         /// <summary>
116         /// Scrim of DialogPage. Scrim is added to Children automatically.
117         /// </summary>
118         [EditorBrowsable(EditorBrowsableState.Never)]
119         protected View Scrim
120         {
121             get
122             {
123                 return scrim;
124             }
125             set
126             {
127                 if (scrim == value)
128                 {
129                     return;
130                 }
131
132                 if (scrim != null)
133                 {
134                     Remove(scrim);
135                 }
136
137                 scrim = value;
138                 if (scrim == null)
139                 {
140                     return;
141                 }
142
143                 Add(scrim);
144
145                 if (Content != null)
146                 {
147                     Content.RaiseAbove(scrim);
148                 }
149
150                 if (EnableScrim != Scrim.Visibility)
151                 {
152                     if (EnableScrim == true)
153                     {
154                         scrim.Show();
155                     }
156                     else
157                     {
158                         scrim.Hide();
159                     }
160                 }
161             }
162         }
163
164         /// <summary>
165         /// Indicates to show scrim behind dialog.
166         /// </summary>
167         [EditorBrowsable(EditorBrowsableState.Never)]
168         public bool EnableScrim
169         {
170             get
171             {
172                 return enableScrim;
173             }
174             set
175             {
176                 if (enableScrim == value)
177                 {
178                     return;
179                 }
180
181                 enableScrim = value;
182
183                 if ((Scrim != null) && (enableScrim != Scrim.Visibility))
184                 {
185                     if (enableScrim == true)
186                     {
187                         Scrim.Show();
188                     }
189                     else
190                     {
191                         Scrim.Hide();
192                     }
193                 }
194             }
195         }
196
197         /// <summary>
198         /// Indicates to dismiss dialog by touching on scrim.
199         /// </summary>
200         [EditorBrowsable(EditorBrowsableState.Never)]
201         public bool EnableDismissOnScrim { get; set; }
202
203         private View CreateDefaultScrim()
204         {
205             //FIXME: Needs to separate GUI implementation codes to style cs file.
206             var scrim = new VisualView();
207             scrim.BackgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.5f);
208             //FIXME: Needs to set proper size to Scrim.
209             scrim.Size = NUIApplication.GetDefaultWindow().Size;
210             scrim.TouchEvent += (object source, TouchEventArgs e) =>
211             {
212                 if ((EnableDismissOnScrim == true) && (e.Touch.GetState(0) == PointStateType.Up))
213                 {
214                     this.Navigator.Pop();
215                 }
216                 return true;
217             };
218
219             return scrim;
220         }
221
222         /// <summary>
223         /// Shows a dialog by pushing a dialog page containing dialog to default navigator.
224         /// </summary>
225         /// <param name="content">The content of Dialog.</param>
226         [EditorBrowsable(EditorBrowsableState.Never)]
227         [SuppressMessage("Microsoft.Reliability",
228                          "CA2000:DisposeObjectsBeforeLosingScope",
229                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
230         public static void ShowDialog(View content)
231         {
232             var dialogPage = new DialogPage()
233             {
234                 Content = new Dialog()
235                 {
236                     Content = content,
237                 },
238             };
239
240             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
241         }
242
243         /// <summary>
244         /// Shows an alert dialog by pushing a page containing the alert dialog
245         /// to default navigator.
246         /// </summary>
247         /// <param name="title">The title of AlertDialog.</param>
248         /// <param name="message">The message of AlertDialog.</param>
249         /// <param name="actions">The action views of AlertDialog.</param>
250         [EditorBrowsable(EditorBrowsableState.Never)]
251         [SuppressMessage("Microsoft.Reliability",
252                          "CA2000:DisposeObjectsBeforeLosingScope",
253                          Justification = "The pushed views are added to NavigationPages and are disposed in Navigator.Dispose().")]
254         public static void ShowAlertDialog(string title, string message, params View[] actions)
255         {
256             var dialogPage = new DialogPage()
257             {
258                 Content = new AlertDialog()
259                 {
260                     Title = title,
261                     Message = message,
262                     Actions =  actions,
263                 },
264             };
265
266             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(dialogPage);
267         }
268     }
269 }