[NUI] Add WindowSystem softkey APIs (#1836)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.WindowSystem / src / public / SoftkeyClient.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
21 namespace Tizen.NUI.WindowSystem.Shell
22 {
23     /// <summary>
24     /// Class for the Tizen softkey client.
25     /// </summary>
26     /// This class is need to be hidden as inhouse API.
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class SoftkeyClient : IDisposable
29     {
30         private TizenShell _tzsh;
31         private IntPtr _softkeyClient;
32         private int _tzshWin;
33         private bool disposed = false;
34         private bool isDisposeQueued = false;
35
36         /// <summary>
37         /// Creates a new Softkey Client handle.
38         /// </summary>
39         /// <param name="tzShell">The TizenShell instance.</param>
40         /// <param name="win">The window to provide service of the quickpanel.</param>
41         /// <privilege>http://tizen.org/privilege/windowsystem.admin</privilege>
42         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
43         /// <exception cref="ArgumentNullException">Thrown when a argument is null.</exception>
44         /// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation or no service.</exception>
45         /// <exception cref="UnauthorizedAccessException">Thrown when the caller does not have privilege to use this method.</exception>
46         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
47         public SoftkeyClient(TizenShell tzShell, Window win)
48         {
49             if (tzShell == null)
50             {
51                 throw new ArgumentNullException(nameof(tzShell));
52             }
53             if (tzShell.GetNativeHandle() == IntPtr.Zero)
54             {
55                 throw new ArgumentException("tzShell is not initialized.");
56             }
57             if (win == null)
58             {
59                 throw new ArgumentNullException(nameof(win));
60             }
61
62             _tzsh = tzShell;
63             _tzshWin = win.GetNativeId();
64             _softkeyClient = Interop.SoftkeyClient.Create(_tzsh.GetNativeHandle(), (IntPtr)_tzshWin);
65             if (_softkeyClient == IntPtr.Zero)
66             {
67                 int err = Tizen.Internals.Errors.ErrorFacts.GetLastResult();
68                 _tzsh.ErrorCodeThrow(err);
69             }
70         }
71
72         /// <summary>
73         /// Destructor.
74         /// </summary>
75         ~SoftkeyClient()
76         {
77             if (!isDisposeQueued)
78             {
79                 isDisposeQueued = true;
80                 DisposeQueue.Instance.Add(this);
81             }
82         }
83
84         /// <summary>
85         /// Dispose.
86         /// </summary>
87         public void Dispose()
88         {
89             if (isDisposeQueued)
90             {
91                 Dispose(DisposeTypes.Implicit);
92             }
93             else
94             {
95                 Dispose(DisposeTypes.Explicit);
96                 GC.SuppressFinalize(this);
97             }
98         }
99
100         /// <inheritdoc/>
101         protected virtual void Dispose(DisposeTypes type)
102         {
103             if (!disposed)
104             {
105                 if (_softkeyClient != IntPtr.Zero)
106                 {
107                     int res = Interop.SoftkeyClient.Destroy(_softkeyClient);
108                     _softkeyClient = IntPtr.Zero;
109                 }
110                 disposed = true;
111             }
112         }
113
114         /// <summary>
115         /// Gets the visible state of a softkey service window.
116         /// </summary>
117         /// <returns>The visible state of the softkey service window.</returns>
118         /// <exception cref="ArgumentException" > Thrown when failed of invalid argument.</exception>
119         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
120         /// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation or no service.</exception>
121         public SoftkeyVisibleState Visible
122         {
123             get
124             {
125                 return GetVisible();
126             }
127         }
128
129         /// <summary>
130         /// Gets the expand state of a softkey service window.
131         /// </summary>
132         /// <returns>The expand state of the softkey service window.</returns>
133         /// <exception cref="ArgumentException" > Thrown when failed of invalid argument.</exception>
134         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
135         /// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation or no service.</exception>
136         public SoftkeyExpandState Expand
137         {
138             get
139             {
140                 return GetExpand();
141             }
142             set
143             {
144                 SetExpand(value);
145             }
146         }
147
148         /// <summary>
149         /// Gets the opacity state of a softkey service window.
150         /// </summary>
151         /// <returns>The opacity state of the softkey service window.</returns>
152         /// <exception cref="ArgumentException" > Thrown when failed of invalid argument.</exception>
153         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
154         /// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation or no service.</exception>
155         public SoftkeyOpacityState Opacity
156         {
157             get
158             {
159                 return GetOpacity();
160             }
161             set
162             {
163                 SetOpacity(value);
164             }
165         }
166
167         /// <summary>
168         /// Requests to show the softkey service window.
169         /// </summary>
170         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
171         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
172         /// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation or no service.</exception>
173         public void Show()
174         {
175             int res = Interop.SoftkeyClient.Show(_softkeyClient);
176             _tzsh.ErrorCodeThrow(res);
177         }
178
179         /// <summary>
180         /// Requests to hide the softkey service window.
181         /// </summary>
182         /// <exception cref="ArgumentException" > Thrown when failed of invalid argument.</exception>
183         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
184         /// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation or no service.</exception>
185         public void Hide()
186         {
187             int res = Interop.SoftkeyClient.Hide(_softkeyClient);
188             _tzsh.ErrorCodeThrow(res);
189         }
190
191         private SoftkeyVisibleState GetVisible()
192         {
193             int res = Interop.SoftkeyClient.GetVisibleState(_softkeyClient, out int vis);
194
195             _tzsh.ErrorCodeThrow(res);
196
197             return ChangeVisibleStateToPublic((Interop.SoftkeyClient.VisibleState)vis);
198         }
199
200         private SoftkeyExpandState GetExpand()
201         {
202             int res = Interop.SoftkeyClient.GetExpandState(_softkeyClient, out int expand);
203
204             _tzsh.ErrorCodeThrow(res);
205
206             return ChangeExpandStateToPublic((Interop.SoftkeyClient.ExpandState)expand);
207         }
208
209         private void SetExpand(SoftkeyExpandState expand)
210         {
211             int res = Interop.SoftkeyClient.SetExpandState(_softkeyClient, (int)(ChangeExpandStateToInternal(expand)));
212
213             _tzsh.ErrorCodeThrow(res);
214         }
215
216         private SoftkeyOpacityState GetOpacity()
217         {
218             int res = Interop.SoftkeyClient.GetOpacityState(_softkeyClient, out int opacity);
219
220             _tzsh.ErrorCodeThrow(res);
221
222             return ChangeOpacityStateToPublic((Interop.SoftkeyClient.OpacityState)opacity);
223         }
224
225         private void SetOpacity(SoftkeyOpacityState opacity)
226         {
227             int res = Interop.SoftkeyClient.SetOpacityState(_softkeyClient, (int)(ChangeOpacityStateToInternal(opacity)));
228
229             _tzsh.ErrorCodeThrow(res);
230         }
231
232         private SoftkeyVisibleState ChangeVisibleStateToPublic(Interop.SoftkeyClient.VisibleState state)
233         {
234             if (state == Interop.SoftkeyClient.VisibleState.Shown)
235                 return SoftkeyVisibleState.Shown;
236             else if (state == Interop.SoftkeyClient.VisibleState.Hidden)
237                 return SoftkeyVisibleState.Hidden;
238             else
239                 return SoftkeyVisibleState.Unknown;
240         }
241
242         private Interop.SoftkeyClient.VisibleState ChangeVisibleStateToInternal(SoftkeyVisibleState state)
243         {
244             if (state == SoftkeyVisibleState.Shown)
245                 return Interop.SoftkeyClient.VisibleState.Shown;
246             else if (state == SoftkeyVisibleState.Hidden)
247                 return Interop.SoftkeyClient.VisibleState.Hidden;
248             else
249                 return Interop.SoftkeyClient.VisibleState.Unknown;
250         }
251
252         private SoftkeyExpandState ChangeExpandStateToPublic(Interop.SoftkeyClient.ExpandState state)
253         {
254             if (state == Interop.SoftkeyClient.ExpandState.On)
255                 return SoftkeyExpandState.On;
256             else if (state == Interop.SoftkeyClient.ExpandState.Off)
257                 return SoftkeyExpandState.Off;
258             else
259                 return SoftkeyExpandState.Unknown;
260         }
261
262         private Interop.SoftkeyClient.ExpandState ChangeExpandStateToInternal(SoftkeyExpandState state)
263         {
264             if (state == SoftkeyExpandState.On)
265                 return Interop.SoftkeyClient.ExpandState.On;
266             else if (state == SoftkeyExpandState.Off)
267                 return Interop.SoftkeyClient.ExpandState.Off;
268             else
269                 return Interop.SoftkeyClient.ExpandState.Unknown;
270         }
271
272         private SoftkeyOpacityState ChangeOpacityStateToPublic(Interop.SoftkeyClient.OpacityState state)
273         {
274             if (state == Interop.SoftkeyClient.OpacityState.Opaque)
275                 return SoftkeyOpacityState.Opaque;
276             else if (state == Interop.SoftkeyClient.OpacityState.Transparent)
277                 return SoftkeyOpacityState.Transparent;
278             else
279                 return SoftkeyOpacityState.Unknown;
280         }
281
282         private Interop.SoftkeyClient.OpacityState ChangeOpacityStateToInternal(SoftkeyOpacityState state)
283         {
284             if (state == SoftkeyOpacityState.Opaque)
285                 return Interop.SoftkeyClient.OpacityState.Opaque;
286             else if (state == SoftkeyOpacityState.Transparent)
287                 return Interop.SoftkeyClient.OpacityState.Transparent;
288             else
289                 return Interop.SoftkeyClient.OpacityState.Unknown;
290         }
291     }
292 }