Revert "[NUI] Dialog and AlertDialog code refactoring with adding DialogPage"
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Dialog.cs
1 /*
2  * Copyright(c) 2020 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;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// Dialog class shows a popup content with background scrim.
26     /// </summary>
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class Dialog : Control
29     {
30         private View popupContent = null;
31         private View scrim = null;
32         private bool enableScrim = true;
33
34         /// <summary>
35         /// Creates a new instance of Dialog.
36         /// </summary>
37         /// <param name="content">The content to set to Content of Dialog.</param>
38         [EditorBrowsable(EditorBrowsableState.Never)]
39         public Dialog(View content = null) : base()
40         {
41             EnableScrim = true;
42             EnableDismissOnScrim = true;
43
44             //FIXME: Needs to separate GUI implementation codes to style cs file.
45             var scrim = new VisualView();
46             scrim.BackgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.5f);
47             //FIXME: Needs to set proper size to Scrim.
48             scrim.Size = NUIApplication.GetDefaultWindow().Size;
49             scrim.TouchEvent += (object source, TouchEventArgs e) =>
50             {
51                 if ((EnableDismissOnScrim == true) && (e.Touch.GetState(0) == PointStateType.Up))
52                 {
53                     //TODO: To show hide animation.
54                     this.Hide();
55                     this.Dispose();
56                 }
57                 return true;
58             };
59
60             Scrim = scrim;
61
62             if (content != null)
63             {
64                 content.RaiseAbove(scrim);
65                 Content = content;
66             }
67         }
68
69         /// <summary>
70         /// Popup content of Dialog. Content is added to Children automatically.
71         /// </summary>
72         [EditorBrowsable(EditorBrowsableState.Never)]
73         public View Content
74         {
75             get
76             {
77                 return popupContent;
78             }
79             set
80             {
81                 if (popupContent == value)
82                 {
83                     return;
84                 }
85
86                 if (popupContent != null)
87                 {
88                     Remove(popupContent);
89                 }
90
91                 popupContent = value;
92                 if (popupContent == null)
93                 {
94                     return;
95                 }
96
97                 popupContent.Relayout += PopupContentRelayout;
98
99                 //FIXME: Needs to separate GUI implementation codes to style cs file.
100                 CalculateContentPosition();
101
102                 Add(popupContent);
103
104                 if (Scrim != null)
105                 {
106                     popupContent.RaiseAbove(Scrim);
107                 }
108             }
109         }
110
111         private void PopupContentRelayout(object sender, EventArgs e)
112         {
113             //FIXME: Needs to separate GUI implementation codes to style cs file.
114             CalculateContentPosition();
115         }
116
117         private void CalculateContentPosition()
118         {
119             var size = popupContent.Size2D;
120             var parent = GetParent();
121             Size2D parentSize;
122
123             if ((parent != null) && (parent is View))
124             {
125                 parentSize = ((View)parent).Size;
126             }
127             else
128             {
129                 parentSize = NUIApplication.GetDefaultWindow().Size;
130             }
131
132             popupContent.Position2D = new Position2D((parentSize.Width - size.Width) / 2, (parentSize.Height - size.Height) / 2);
133         }
134
135         /// <summary>
136         /// Indicates to show scrim behind popup content.
137         /// </summary>
138         [EditorBrowsable(EditorBrowsableState.Never)]
139         public bool EnableScrim
140         {
141             get
142             {
143                 return enableScrim;
144             }
145             set
146             {
147                 if (enableScrim == value)
148                 {
149                     return;
150                 }
151
152                 enableScrim = value;
153
154                 if ((Scrim != null) && (enableScrim != Scrim.Visibility))
155                 {
156                     if (enableScrim)
157                     {
158                         Scrim.Show();
159                     }
160                     else
161                     {
162                         Scrim.Hide();
163                     }
164                 }
165             }
166         }
167
168         /// <summary>
169         /// Indicates to dismiss popup content by touching on scrim.
170         /// </summary>
171         [EditorBrowsable(EditorBrowsableState.Never)]
172         public bool EnableDismissOnScrim { get; set; }
173
174         /// <summary>
175         /// Scrim covers background behind popup content.
176         /// </summary>
177         [EditorBrowsable(EditorBrowsableState.Never)]
178         protected internal View Scrim
179         {
180             get
181             {
182                 return scrim;
183             }
184             set
185             {
186                 if (scrim == value)
187                 {
188                     return;
189                 }
190
191                 if (scrim != null)
192                 {
193                     Remove(scrim);
194                 }
195
196                 scrim = value;
197                 if (scrim == null)
198                 {
199                     return;
200                 }
201
202                 Add(scrim);
203
204                 if (Content != null)
205                 {
206                     Content.RaiseAbove(scrim);
207                 }
208             }
209         }
210
211         /// <summary>
212         /// Dispose Dialog and all children on it.
213         /// </summary>
214         /// <param name="type">Dispose type.</param>
215         [EditorBrowsable(EditorBrowsableState.Never)]
216         protected override void Dispose(DisposeTypes type)
217         {
218             if (disposed)
219             {
220                 return;
221             }
222
223             if (type == DisposeTypes.Explicit)
224             {
225                 if (popupContent != null)
226                 {
227                     popupContent.Relayout -= PopupContentRelayout;
228                     Utility.Dispose(popupContent);
229                 }
230
231                 if (scrim != null)
232                 {
233                     Utility.Dispose(scrim);
234                 }
235             }
236
237             base.Dispose(type);
238         }
239
240         /// <summary>
241         /// Initialize AT-SPI object.
242         /// </summary>
243         [EditorBrowsable(EditorBrowsableState.Never)]
244         public override void OnInitialize()
245         {
246             base.OnInitialize();
247             SetAccessibilityConstructor(Role.Dialog);
248             AppendAccessibilityAttribute("sub-role", "Alert");
249             Show(); // calls AddPopup()
250         }
251
252         /// <summary>
253         /// Informs AT-SPI bridge about the set of AT-SPI states associated with this object.
254         /// </summary>
255         [EditorBrowsable(EditorBrowsableState.Never)]
256         protected override AccessibilityStates AccessibilityCalculateStates()
257         {
258             var states = base.AccessibilityCalculateStates();
259             states.Set(AccessibilityState.Modal, true);
260             return states;
261         }
262     }
263 }