24ee8eeaffc72ce733bf17b0392cd082c0e7ad0f
[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         /// Suppress reading of screen-reader
126         /// </summary>
127         /// <param name="suppress">whether to suppress reading of screen-reader</param>
128         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
129         [EditorBrowsable(EditorBrowsableState.Never)]
130         public bool SuppressScreenReader(bool suppress)
131         {
132             bool ret = Interop.Accessibility.SuppressScreenReader(View.getCPtr(dummy), suppress);
133             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
134             return ret;
135         }
136
137         /// <summary>
138         ///  Get View that is used to highlight widget.
139         /// </summary>
140         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
141         [EditorBrowsable(EditorBrowsableState.Never)]
142         public View GetHighlightFrameView()
143         {
144             var ptr = Interop.ControlDevel.DaliAccessibilityAccessibleGetHighlightActor();
145             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
146             if (ptr == IntPtr.Zero)
147                 return null;
148             return new View(ptr, true);
149         }
150
151         /// <summary>
152         ///  Set view that will be used to highlight widget.
153         /// </summary>
154         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
155         [EditorBrowsable(EditorBrowsableState.Never)]
156         public void SetHighlightFrameView(View view)
157         {
158             Interop.ControlDevel.DaliAccessibilityAccessibleSetHighlightActor(View.getCPtr(view));
159             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
160         }
161
162         /// <summary>
163         ///  Get highligted View.
164         /// </summary>
165         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
166         [EditorBrowsable(EditorBrowsableState.Never)]
167         public View GetCurrentlyHighlightedView()
168         {
169             var ptr = Interop.ControlDevel.DaliAccessibilityAccessibleGetCurrentlyHighlightedActor();
170             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
171             if (ptr == IntPtr.Zero)
172                 return null;
173             return new View(ptr, true);
174         }
175
176         /// <summary>
177         ///  Clear highlight.
178         /// </summary>
179         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
180         [EditorBrowsable(EditorBrowsableState.Never)]
181         public bool ClearCurrentlyHighlightedView()
182         {
183             using (View view = GetCurrentlyHighlightedView())
184             {
185                 return view?.ClearAccessibilityHighlight() ?? false;
186             }
187         }
188         #endregion Method
189
190
191         #region Event, Enum, Struct, ETC
192         /// <summary>
193         /// Enum of Say finished event argument status
194         /// </summary>
195         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
196         [EditorBrowsable(EditorBrowsableState.Never)]
197         public enum SayFinishedState
198         {
199             /// <summary>
200             /// Invalid
201             /// </summary>
202             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
203             [EditorBrowsable(EditorBrowsableState.Never)]
204             Invalid = -1,
205             /// <summary>
206             /// Cancelled
207             /// </summary>
208             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
209             [EditorBrowsable(EditorBrowsableState.Never)]
210             Cancelled = 1,
211             /// <summary>
212             /// Stopped
213             /// </summary>
214             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
215             [EditorBrowsable(EditorBrowsableState.Never)]
216             Stopped = 2,
217             /// <summary>
218             /// Skipped
219             /// </summary>
220             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
221             [EditorBrowsable(EditorBrowsableState.Never)]
222             Skipped = 3,
223             /// <summary>
224             /// Paused
225             /// </summary>
226             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
227             [EditorBrowsable(EditorBrowsableState.Never)]
228             Paused = 4,
229             /// <summary>
230             /// Resumed
231             /// </summary>
232             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
233             [EditorBrowsable(EditorBrowsableState.Never)]
234             Resumed = 5
235         }
236
237         /// <summary>
238         /// When Say is finished, this event is triggered
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 event EventHandler<SayFinishedEventArgs> SayFinished
243         {
244             add => sayFinishedEventHandler += value;
245             remove => sayFinishedEventHandler -= value;
246         }
247         #endregion Event, Enum, Struct, ETC
248
249
250         #region Internal
251         internal void PauseResume(View target, bool pause)
252         {
253             Interop.Accessibility.PauseResume(View.getCPtr(target), pause);
254             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
255         }
256
257         internal bool Say(View target, string sentence, bool discardable)
258         {
259             IntPtr callbackIntPtr = IntPtr.Zero;
260             if (sayFinishedEventHandler != null)
261             {
262                 callback = SayFinishedEventCallback;
263                 callbackIntPtr = Marshal.GetFunctionPointerForDelegate<Delegate>(callback);
264             }
265             bool ret = Interop.Accessibility.Say(View.getCPtr(target), sentence, discardable, callbackIntPtr);
266             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267             return ret;
268         }
269         #endregion Internal
270
271
272         #region Private
273         private static readonly Accessibility accessibility = new Accessibility();
274
275         private event EventHandler<SayFinishedEventArgs> sayFinishedEventHandler;
276
277         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
278         private delegate void SayFinishedEventCallbackType(int result);
279
280         private SayFinishedEventCallbackType callback = null;
281
282         private void SayFinishedEventCallback(int result)
283         {
284             tlog.Fatal(tag, $"sayFinishedEventCallback(res={result}) called!");
285             sayFinishedEventHandler?.Invoke(this, new SayFinishedEventArgs(result));
286         }
287
288         private View dummy;
289
290         private static string tag = "NUITEST";
291         #endregion Private
292     }
293
294     /// <summary>
295     ///  Say Finished event arguments
296     /// </summary>
297     // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
298     [EditorBrowsable(EditorBrowsableState.Never)]
299     public class SayFinishedEventArgs : EventArgs
300     {
301         /// <summary>
302         /// The state of Say finished
303         /// </summary>
304         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
305         [EditorBrowsable(EditorBrowsableState.Never)]
306         public Accessibility.SayFinishedState State
307         {
308             private set;
309             get;
310         }
311
312         internal SayFinishedEventArgs(int result)
313         {
314             State = (Accessibility.SayFinishedState)(result);
315             tlog.Fatal("NUITEST", $"SayFinishedEventArgs Constructor! State={State}");
316         }
317     }
318 }