[NUI] Support Device orientation and window orientation event.
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Application / NUIWidgetCoreBackend.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.Collections.Generic;
20 using Tizen.Applications.CoreBackend;
21 using Tizen.Applications;
22
23 namespace Tizen.NUI
24 {
25     class NUIWidgetCoreBackend : ICoreBackend
26     {
27         /// <summary>
28         /// Application instance to connect event.
29         /// </summary>
30         protected WidgetApplication application;
31         private string stylesheet = "";
32         private Dictionary<System.Type, string> widgetInfo;
33
34         /// <summary>
35         /// Dictionary to contain each type of event callback.
36         /// </summary>
37         protected IDictionary<EventType, object> handlers = new Dictionary<EventType, object>();
38
39         /// <summary>
40         /// The default Constructor.
41         /// </summary>
42         public NUIWidgetCoreBackend()
43         {
44         }
45
46         /// <summary>
47         /// The constructor with stylesheet.
48         /// </summary>
49         public NUIWidgetCoreBackend(string stylesheet)
50         {
51             this.stylesheet = stylesheet;
52         }
53
54         /// <summary>
55         /// Add NUIWidgetApplication event to Application.
56         /// Put each type of event callback in Dictionary.
57         /// </summary>
58         /// <param name="evType">Type of event</param>
59         /// <param name="handler">Event callback</param>
60         public void AddEventHandler(EventType evType, Action handler)
61         {
62             handlers.Add(evType, handler);
63         }
64
65         /// <summary>
66         /// Add NUIWidgetApplication event to Application.
67         /// Put each type of event callback in Dictionary.
68         /// </summary>
69         /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
70         /// <param name="evType">Type of event</param>
71         /// <param name="handler">Event callback</param>
72         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
73         {
74             handlers.Add(evType, handler);
75         }
76
77
78         /// <summary>
79         /// Dispose function.
80         /// </summary>
81         public void Dispose()
82         {
83             application?.Dispose();
84         }
85
86         /// <summary>
87         /// Exit Application.
88         /// </summary>
89         public void Exit()
90         {
91             application?.Quit();
92         }
93
94         public void RegisterWidgetInfo(Dictionary<System.Type, string> widgetInfo)
95         {
96             this.widgetInfo = widgetInfo;
97         }
98
99         public void AddWidgetInfo(Dictionary<System.Type, string> widgetInfo)
100         {
101             application?.AddWidgetInfo(widgetInfo);
102         }
103
104         /// <summary>
105         /// Run Application.
106         /// </summary>
107         /// <param name="args">Arguments from commandline.</param>
108         public void Run(string[] args)
109         {
110             TizenSynchronizationContext.Initialize();
111
112             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
113             application = WidgetApplication.NewWidgetApplication(args, stylesheet);
114             application.RegisterWidgetInfo(widgetInfo);
115
116             application.BatteryLow += OnBatteryLow;
117             application.LanguageChanged += OnLanguageChanged;
118             application.MemoryLow += OnMemoryLow;
119             application.RegionChanged += OnRegionChanged;
120             application.DeviceOrientationChanged += OnDeviceOrientationChanged;
121             application.Initialized += OnInitialized;
122             application.Terminating += OnTerminated;
123
124             application.MainLoop();
125             application.Dispose();
126         }
127
128         /// <summary>
129         /// The Initialized event callback function.
130         /// </summary>
131         /// <param name="source">The application instance.</param>
132         /// <param name="e">The event argument for Initialized.</param>
133         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
134         {
135             var preCreateHandler = handlers[EventType.PreCreated] as Action;
136             preCreateHandler?.Invoke();
137
138             var createHandler = handlers[EventType.Created] as Action;
139             createHandler?.Invoke();
140             application.RegisterWidgetCreatingFunction();
141         }
142
143         /// <summary>
144         /// The Terminated event callback function.
145         /// </summary>
146         /// <param name="source">The application instance.</param>
147         /// <param name="e">The event argument for Terminated.</param>
148         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
149         {
150             var handler = handlers[EventType.Terminated] as Action;
151             handler?.Invoke();
152         }
153
154         /// <summary>
155         /// The Region changed event callback function.
156         /// </summary>
157         /// <param name="source">The application instance.</param>
158         /// <param name="e">The event argument for RegionChanged.</param>
159         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
160         {
161             var handler = handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
162             handler?.Invoke(new RegionFormatChangedEventArgs(e.Application.GetRegion()));
163         }
164
165         /// <summary>
166         /// The Language changed event callback function.
167         /// </summary>
168         /// <param name="source">The application instance.</param>
169         /// <param name="e">The event argument for LanguageChanged.</param>
170         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
171         {
172             var handler = handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
173             handler?.Invoke(new LocaleChangedEventArgs(e.Application.GetLanguage()));
174         }
175
176         /// <summary>
177         /// The Memory Low event callback function.
178         /// </summary>
179         /// <param name="source">The application instance.</param>
180         /// <param name="e">The event argument for MemoryLow.</param>
181         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
182         {
183             var handler = handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
184
185             switch (e.MemoryStatus)
186             {
187                 case Application.MemoryStatus.Normal:
188                     {
189                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
190                         break;
191                     }
192                 case Application.MemoryStatus.Low:
193                     {
194                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
195                         break;
196                     }
197                 case Application.MemoryStatus.CriticallyLow:
198                     {
199                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
200                         break;
201                     }
202             }
203         }
204
205         /// <summary>
206         /// The Battery Low event callback function.
207         /// </summary>
208         /// <param name="source">The application instance.</param>
209         /// <param name="e">The event argument for BatteryLow.</param>
210         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
211         {
212             var handler = handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
213             switch (e.BatteryStatus)
214             {
215                 case Application.BatteryStatus.Normal:
216                     {
217                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
218                         break;
219                     }
220                 case Application.BatteryStatus.CriticallyLow:
221                     {
222                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
223                         break;
224                     }
225                 case Application.BatteryStatus.PowerOff:
226                     {
227                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
228                         break;
229                     }
230             }
231         }
232
233         /// <summary>
234         /// The Device Orientation changed event callback function.
235         /// </summary>
236         /// <param name="source">The application instance.</param>
237         /// <param name="e">The event argument for DeviceOrientationChanged.</param>
238         private void OnDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
239         {
240             var handler = handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
241
242             switch (e.DeviceOrientationStatus)
243             {
244                 case Application.DeviceOrientationStatus.Orientation_0:
245                     {
246                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
247                         break;
248                     }
249                 case Application.DeviceOrientationStatus.Orientation_90:
250                     {
251                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
252                         break;
253                     }
254                 case Application.DeviceOrientationStatus.Orientation_180:
255                     {
256                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
257                         break;
258                     }
259                 case Application.DeviceOrientationStatus.Orientation_270:
260                     {
261                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
262                         break;
263                     }
264             }
265         }
266
267         internal WidgetApplication WidgetApplicationHandle
268         {
269             get
270             {
271                 return application;
272             }
273         }
274     }
275 }