[NUI] Fix svace issues in EXaml and Accessibility (#2936)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Accessibility / Accessibility.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 System.Runtime.InteropServices;
21 using Tizen.NUI.BaseComponents;
22 using System.Diagnostics.CodeAnalysis;
23 #if (NUI_DEBUG_ON)
24 using tlog = Tizen.Log;
25 #endif
26
27 namespace Tizen.NUI.Accessibility
28 {
29     /// <summary>
30     /// Accessibility provides Dali-ATSPI interface which has functionality of Screen-Reader and general accessibility
31     /// </summary>
32     // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
33     [SuppressMessage("Microsoft.Design", "CA1724: Type names should not match namespaces")]
34     [SuppressMessage("Microsoft.Design", "CA1001:Types that own disposable fields should be disposable", Justification = "This is a singleton class and is not disposed")]
35     [EditorBrowsable(EditorBrowsableState.Never)]
36     public class Accessibility
37     {
38         #region Constructor, Destructor, Dispose
39         private Accessibility()
40         {
41             dummy = new View();
42             dummy.Name = "dali-atspi-singleton";
43         }
44         /// <summary>
45         /// destructor. This is HiddenAPI. recommended not to use in public.
46         /// </summary>
47         ~Accessibility()
48         {
49             Tizen.Log.Debug("NUI", $"Accessibility is destroyed\n");
50         }
51         #endregion Constructor, Destructor, Dispose
52
53
54         #region Property
55         /// <summary>
56         /// Instance for singleton
57         /// </summary>
58         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         public static Accessibility Instance
61         {
62             get => accessibility;
63         }
64         #endregion Property
65
66
67         #region Method
68         /// <summary>
69         /// Get the current status
70         /// </summary>
71         /// <returns>Current enabled status</returns>
72         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
73         [EditorBrowsable(EditorBrowsableState.Never)]
74         static public bool GetStatus()
75         {
76             return true;
77         }
78
79         /// <summary>
80         /// Start to speak
81         /// </summary>
82         /// <param name="sentence">Content to be spoken</param>
83         /// <param name="discardable">true to be stopped and discarded when other Say is triggered</param>
84         /// <returns></returns>
85         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
86         [EditorBrowsable(EditorBrowsableState.Never)]
87         public bool Say(string sentence, bool discardable)
88         {
89             IntPtr callbackIntPtr = IntPtr.Zero;
90             if (sayFinishedEventHandler != null)
91             {
92                 callback = SayFinishedEventCallback;
93                 callbackIntPtr = Marshal.GetFunctionPointerForDelegate<Delegate>(callback);
94             }
95             bool ret = Interop.Accessibility.Say(View.getCPtr(dummy), sentence, discardable, callbackIntPtr);
96             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
97             return ret;
98         }
99
100         /// <summary>
101         /// To make Say be paused or resumed
102         /// </summary>
103         /// <param name="pause">true to be paused, false to be resumed</param>
104         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
105         [EditorBrowsable(EditorBrowsableState.Never)]
106         public void PauseResume(bool pause)
107         {
108             Interop.Accessibility.PauseResume(View.getCPtr(dummy), pause);
109             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
110         }
111
112         /// <summary>
113         /// Cancels anything screen-reader is reading / has queued to read
114         /// </summary>
115         /// <param name="alsoNonDiscardable">whether to cancel non-discardable readings as well</param>
116         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
117         [EditorBrowsable(EditorBrowsableState.Never)]
118         public void StopReading(bool alsoNonDiscardable)
119         {
120             Interop.Accessibility.StopReading(View.getCPtr(dummy), alsoNonDiscardable);
121             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
122         }
123
124         /// <summary>
125         ///  Get View that is used to highlight widget.
126         /// </summary>
127         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
128         [EditorBrowsable(EditorBrowsableState.Never)]
129         public View GetHighlightFrameView()
130         {
131             var ptr = Interop.ControlDevel.DaliAccessibilityAccessibleGetHighlightActor();
132             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
133             if (ptr == IntPtr.Zero)
134                 return null;
135             return new View(ptr, true);
136         }
137
138         /// <summary>
139         ///  Set view that will be used to highlight widget.
140         /// </summary>
141         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
142         [EditorBrowsable(EditorBrowsableState.Never)]
143         public void SetHighlightFrameView(View view)
144         {
145             Interop.ControlDevel.DaliAccessibilityAccessibleSetHighlightActor(View.getCPtr(view));
146             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
147         }
148
149         /// <summary>
150         ///  Get highligted View.
151         /// </summary>
152         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
153         [EditorBrowsable(EditorBrowsableState.Never)]
154         public View GetCurrentlyHighlightedView()
155         {
156             var ptr = Interop.ControlDevel.DaliAccessibilityAccessibleGetCurrentlyHighlightedActor();
157             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
158             if (ptr == IntPtr.Zero)
159                 return null;
160             return new View(ptr, true);
161         }
162
163         /// <summary>
164         ///  Clear highlight.
165         /// </summary>
166         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
167         [EditorBrowsable(EditorBrowsableState.Never)]
168         public bool ClearCurrentlyHighlightedView()
169         {
170             using (View view = GetCurrentlyHighlightedView())
171             {
172                 return view?.ClearAccessibilityHighlight() ?? false;
173             }
174         }
175         #endregion Method
176
177
178         #region Event, Enum, Struct, ETC
179         /// <summary>
180         ///  Say Finished event arguments
181         /// </summary>
182         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
183         [EditorBrowsable(EditorBrowsableState.Never)]
184         public class SayFinishedEventArgs : EventArgs
185         {
186             /// <summary>
187             /// The state of Say finished
188             /// </summary>
189             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
190             [EditorBrowsable(EditorBrowsableState.Never)]
191             public SayFinishedState State
192             {
193                 private set;
194                 get;
195             }
196
197             internal SayFinishedEventArgs(int result)
198             {
199                 State = (SayFinishedState)(result);
200                 tlog.Fatal(tag, $"SayFinishedEventArgs Constructor! State={State}");
201             }
202         }
203
204         /// <summary>
205         /// Enum of Say finished event argument status
206         /// </summary>
207         [Obsolete("Please do not use! This will be removed. Please use Accessibility.SayFinishedState instead!")]
208         [EditorBrowsable(EditorBrowsableState.Never)]
209         public enum SayFinishedStates
210         {
211             /// <summary>
212             /// Invalid
213             /// </summary>
214             [Obsolete("Please do not use! This will be removed. Please use Accessibility.SayFinishedState.Invalid instead!")]
215             [EditorBrowsable(EditorBrowsableState.Never)]
216             Invalid = -1,
217             /// <summary>
218             /// Cancelled
219             /// </summary>
220             [Obsolete("Please do not use! This will be removed. Please use Accessibility.SayFinishedState.Invalid instead!")]
221             [EditorBrowsable(EditorBrowsableState.Never)]
222             Cancelled = 1,
223             /// <summary>
224             /// Stopped
225             /// </summary>
226             [Obsolete("Please do not use! This will be removed. Please use Accessibility.SayFinishedState.Invalid instead!")]
227             [EditorBrowsable(EditorBrowsableState.Never)]
228             Stopped = 2,
229             /// <summary>
230             /// Skipped
231             /// </summary>
232             [Obsolete("Please do not use! This will be removed. Please use Accessibility.SayFinishedState.Invalid instead!")]
233             [EditorBrowsable(EditorBrowsableState.Never)]
234             Skipped = 3
235         }
236
237         /// <summary>
238         /// Enum of Say finished event argument status
239         /// </summary>
240         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
241         [EditorBrowsable(EditorBrowsableState.Never)]
242         public enum SayFinishedState
243         {
244             /// <summary>
245             /// Invalid
246             /// </summary>
247             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
248             [EditorBrowsable(EditorBrowsableState.Never)]
249             Invalid = -1,
250             /// <summary>
251             /// Cancelled
252             /// </summary>
253             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
254             [EditorBrowsable(EditorBrowsableState.Never)]
255             Cancelled = 1,
256             /// <summary>
257             /// Stopped
258             /// </summary>
259             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
260             [EditorBrowsable(EditorBrowsableState.Never)]
261             Stopped = 2,
262             /// <summary>
263             /// Skipped
264             /// </summary>
265             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
266             [EditorBrowsable(EditorBrowsableState.Never)]
267             Skipped = 3,
268             /// <summary>
269             /// Paused
270             /// </summary>
271             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
272             [EditorBrowsable(EditorBrowsableState.Never)]
273             Paused = 4,
274             /// <summary>
275             /// Resumed
276             /// </summary>
277             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
278             [EditorBrowsable(EditorBrowsableState.Never)]
279             Resumed = 5
280         }
281
282         /// <summary>
283         /// When Say is finished, this event is triggered
284         /// </summary>
285         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
286         [EditorBrowsable(EditorBrowsableState.Never)]
287         public event EventHandler<SayFinishedEventArgs> SayFinished
288         {
289             add => sayFinishedEventHandler += value;
290             remove => sayFinishedEventHandler -= value;
291         }
292         #endregion Event, Enum, Struct, ETC
293
294
295         #region Internal
296         internal void PauseResume(View target, bool pause)
297         {
298             Interop.Accessibility.PauseResume(View.getCPtr(target), pause);
299             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
300         }
301
302         internal bool Say(View target, string sentence, bool discardable)
303         {
304             IntPtr callbackIntPtr = IntPtr.Zero;
305             if (sayFinishedEventHandler != null)
306             {
307                 callback = SayFinishedEventCallback;
308                 callbackIntPtr = Marshal.GetFunctionPointerForDelegate<Delegate>(callback);
309             }
310             bool ret = Interop.Accessibility.Say(View.getCPtr(target), sentence, discardable, callbackIntPtr);
311             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
312             return ret;
313         }
314         #endregion Internal
315
316
317         #region Private
318         private static readonly Accessibility accessibility = new Accessibility();
319
320         private event EventHandler<SayFinishedEventArgs> sayFinishedEventHandler;
321
322         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
323         private delegate void SayFinishedEventCallbackType(int result);
324
325         private SayFinishedEventCallbackType callback = null;
326
327         private void SayFinishedEventCallback(int result)
328         {
329             tlog.Fatal(tag, $"sayFinishedEventCallback(res={result}) called!");
330             sayFinishedEventHandler?.Invoke(this, new SayFinishedEventArgs(result));
331         }
332
333         private View dummy;
334
335         private static string tag = "NUITEST";
336         #endregion Private
337     }
338 }