[NUI][AT-SPI] Do not set Popup's sub-role to "Alert"
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Dialog.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;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Binding;
22
23 namespace Tizen.NUI.Components
24 {
25     /// <summary>
26     /// Dialog class shows a dialog with content.
27     /// </summary>
28     /// <since_tizen> 9 </since_tizen>
29     public class Dialog : Control
30     {
31         /// <summary>
32         /// ContentProperty
33         /// </summary>
34         [EditorBrowsable(EditorBrowsableState.Never)]
35         public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(Dialog), null, propertyChanged: (bindable, oldValue, newValue) =>
36         {
37             var instance = (Dialog)bindable;
38             if (newValue != null)
39             {
40                 instance.InternalContent = newValue as View;
41             }
42         },
43         defaultValueCreator: (bindable) =>
44         {
45             var instance = (Dialog)bindable;
46             return instance.InternalContent;
47         });
48
49         private View content = null;
50
51         /// <summary>
52         /// Creates a new instance of Dialog.
53         /// </summary>
54         /// <since_tizen> 9 </since_tizen>
55         public Dialog() : base()
56         {
57             Layout = new AbsoluteLayout();
58
59             this.Relayout += OnRelayout;
60         }
61
62         /// <inheritdoc/>
63         [EditorBrowsable(EditorBrowsableState.Never)]
64         protected override void Dispose(DisposeTypes type)
65         {
66             if (disposed)
67             {
68                 return;
69             }
70
71             if (type == DisposeTypes.Explicit)
72             {
73                 this.Relayout -= OnRelayout;
74
75                 if (content != null)
76                 {
77                     Utility.Dispose(content);
78                 }
79             }
80
81             base.Dispose(type);
82         }
83
84         /// <summary>
85         /// Popup content of Dialog.
86         /// Content is added as a child of Dialog automatically.
87         /// </summary>
88         /// <since_tizen> 9 </since_tizen>
89         public View Content
90         {
91             get
92             {
93                 return GetValue(ContentProperty) as View;
94             }
95             set
96             {
97                 SetValue(ContentProperty, value);
98                 NotifyPropertyChanged();
99             }
100         }
101         private View InternalContent
102         {
103             get
104             {
105                 return content;
106             }
107             set
108             {
109                 if (content == value)
110                 {
111                     return;
112                 }
113
114                 if (content != null)
115                 {
116                     Remove(content);
117                 }
118
119                 content = value;
120                 if (content == null)
121                 {
122                     return;
123                 }
124
125                 Add(content);
126             }
127         }
128
129         /// <summary>
130         /// Initialize AT-SPI object.
131         /// </summary>
132         [EditorBrowsable(EditorBrowsableState.Never)]
133         public override void OnInitialize()
134         {
135             base.OnInitialize();
136             AccessibilityRole = Role.Dialog;
137             Show(); // calls RegisterDefaultLabel(); Hide() will call UnregisterDefaultLabel()
138         }
139
140         /// <summary>
141         /// Informs AT-SPI bridge about the set of AT-SPI states associated with this object.
142         /// </summary>
143         [EditorBrowsable(EditorBrowsableState.Never)]
144         protected override AccessibilityStates AccessibilityCalculateStates()
145         {
146             var states = base.AccessibilityCalculateStates();
147
148             states[AccessibilityState.Modal] = true;
149
150             return states;
151         }
152
153         private void OnRelayout(object sender, EventArgs e)
154         {
155             //FIXME: Needs to separate GUI implementation codes to style cs file.
156             CalculateContentPosition();
157         }
158
159         private void CalculateContentPosition()
160         {
161             var size = Size2D;
162             var parent = GetParent();
163             Size2D parentSize;
164
165             if ((parent != null) && (parent is View))
166             {
167                 parentSize = ((View)parent).Size;
168             }
169             else
170             {
171                 parentSize = NUIApplication.GetDefaultWindow().Size;
172             }
173
174             Position2D = new Position2D((parentSize.Width - size.Width) / 2, (parentSize.Height - size.Height) / 2);
175         }
176     }
177 }