4fc4d389b6a0f20f0f55bf20146057b4cc84bca9
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Application / NUICoreBackend.cs
1 /*
2  * Copyright (c) 2018 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 NUICoreBackend : ICoreBackend
26     {
27         /// <summary>
28         /// The Application instance to connect event.
29         /// </summary>
30         protected Application application;
31         private string stylesheet = "";
32         private NUIApplication.WindowMode windowMode = NUIApplication.WindowMode.Opaque;
33         private Rectangle windowRectangle = null;
34
35         /// <summary>
36         /// The Dictionary to contain each type of event callback.
37         /// </summary>
38         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
39
40         /// <summary>
41         /// The default Constructor.
42         /// </summary>
43         public NUICoreBackend()
44         {
45         }
46
47         /// <summary>
48         /// The constructor with stylesheet.
49         /// </summary>
50         public NUICoreBackend(string stylesheet)
51         {
52             this.stylesheet = stylesheet;
53         }
54
55         /// <summary>
56         /// The constructor with stylesheet and window mode.
57         /// </summary>
58         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
59         {
60             this.stylesheet = stylesheet;
61             this.windowMode = windowMode;
62         }
63
64         /// <summary>
65         /// The constructor with stylesheet, window mode, window size and window position.
66         /// </summary>
67         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
68         {
69             this.stylesheet = stylesheet;
70             this.windowMode = windowMode;
71             if (windowSize != null && windowPosition != null)
72             {
73                 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
74             }
75         }
76
77         /// <summary>
78         /// Adds NUIApplication event to Application.
79         /// Puts each type of event callback in Dictionary.
80         /// </summary>
81         /// <param name="evType">The type of event.</param>
82         /// <param name="handler">The event callback.</param>
83         public void AddEventHandler(EventType evType, Action handler)
84         {
85             Handlers.Add(evType, handler);
86         }
87
88         /// <summary>
89         /// Adds NUIApplication event to Application.
90         /// Puts each type of event callback in Dictionary.
91         /// </summary>
92         /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
93         /// <param name="evType">The type of event.</param>
94         /// <param name="handler">The event callback.</param>
95         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
96         {
97             Handlers.Add(evType, handler);
98         }
99
100         /// <summary>
101         /// The Dispose function.
102         /// </summary>
103         public void Dispose()
104         {
105             if (application != null)
106             {
107                 application.Dispose();
108             }
109             if (windowRectangle != null)
110             {
111                 windowRectangle.Dispose();
112             }
113         }
114
115         /// <summary>
116         /// The Exit application.
117         /// </summary>
118         public void Exit()
119         {
120             if (application != null)
121             {
122                 application.Quit();
123             }
124         }
125
126         /// <summary>
127         /// Ensures that the function passed in is called from the main loop when it is idle.
128         /// </summary>
129         /// <param name="func">The function to call</param>
130         /// <returns>true if added successfully, false otherwise</returns>
131         public bool AddIdle(System.Delegate func)
132         {
133             return application.AddIdle(func);
134         }
135
136         /// <summary>
137         /// The Run application.
138         /// </summary>
139         /// <param name="args">The arguments from commandline.</param>
140         public void Run(string[] args)
141         {
142             TizenSynchronizationContext.Initialize();
143
144             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
145             if (string.IsNullOrEmpty(args[0]))
146             {
147                 args[0] = this.GetType().Assembly.FullName.Replace(" ", "");
148             }
149
150             if (windowRectangle != null)
151             {
152                 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle);
153             }
154             else
155             {
156                 application = Application.NewApplication(args, stylesheet, windowMode);
157             }
158
159             application.BatteryLow += OnBatteryLow;
160             application.LanguageChanged += OnLanguageChanged;
161             application.MemoryLow += OnMemoryLow;
162             application.RegionChanged += OnRegionChanged;
163
164             application.Initialized += OnInitialized;
165             application.Resumed += OnResumed;
166             application.Terminating += OnTerminated;
167             application.Paused += OnPaused;
168             application.AppControl += OnAppControl;
169
170             application.MainLoop();
171             application.Dispose();
172         }
173
174         /// <summary>
175         /// The Region changed event callback function.
176         /// </summary>
177         /// <param name="source">The application instance.</param>
178         /// <param name="e">The event argument for RegionChanged.</param>
179         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
180         {
181             Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
182             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
183             handler?.Invoke(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
184         }
185
186         /// <summary>
187         /// The Memory Low event callback function.
188         /// </summary>
189         /// <param name="source">The application instance.</param>
190         /// <param name="e">The event argument for MemoryLow.</param>
191         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
192         {
193             Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
194             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
195
196             switch (e.MemoryStatus)
197             {
198                 case Application.MemoryStatus.Normal:
199                     {
200                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
201                         break;
202                     }
203                 case Application.MemoryStatus.Low:
204                     {
205                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
206                         break;
207                     }
208                 case Application.MemoryStatus.CriticallyLow:
209                     {
210                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
211                         break;
212                     }
213             }
214         }
215
216         /// <summary>
217         /// The Language changed event callback function.
218         /// </summary>
219         /// <param name="source">The application instance.</param>
220         /// <param name="e">The event argument for LanguageChanged.</param>
221         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
222         {
223             Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
224             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
225             handler?.Invoke(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
226         }
227
228         /// <summary>
229         /// The Battery Low event callback function.
230         /// </summary>
231         /// <param name="source">The application instance.</param>
232         /// <param name="e">The event argument for BatteryLow.</param>
233         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
234         {
235             Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
236             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
237             switch (e.BatteryStatus)
238             {
239                 case Application.BatteryStatus.Normal:
240                     {
241                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
242                         break;
243                     }
244                 case Application.BatteryStatus.CriticallyLow:
245                     {
246                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
247                         break;
248                     }
249                 case Application.BatteryStatus.PowerOff:
250                     {
251                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
252                         break;
253                     }
254             }
255         }
256
257         /// <summary>
258         /// The Initialized event callback function.
259         /// </summary>
260         /// <param name="source">The application instance.</param>
261         /// <param name="e">The event argument for Initialized.</param>
262         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
263         {
264             Log.Info("NUI", "NUICorebackend OnPreCreated Called");
265             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
266             preCreateHandler?.Invoke();
267
268             Log.Info("NUI", "NUICorebackend OnCreate Called");
269             var createHandler = Handlers[EventType.Created] as Action;
270             createHandler?.Invoke();
271         }
272
273         /// <summary>
274         /// The Terminated event callback function.
275         /// </summary>
276         /// <param name="source">The application instance.</param>
277         /// <param name="e">The event argument for Terminated.</param>
278         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
279         {
280             Log.Info("NUI", "NUICorebackend OnTerminated Called");
281             var handler = Handlers[EventType.Terminated] as Action;
282             handler?.Invoke();
283         }
284
285         /// <summary>
286         /// The Resumed event callback function.
287         /// </summary>
288         /// <param name="source">The application instance.</param>
289         /// <param name="e">The event argument for Resumed.</param>
290         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
291         {
292             Log.Info("NUI", "NUICorebackend OnResumed Called");
293             var handler = Handlers[EventType.Resumed] as Action;
294             handler?.Invoke();
295         }
296
297         /// <summary>
298         /// The App control event callback function.
299         /// </summary>
300         /// <param name="source">The application instance.</param>
301         /// <param name="e">The event argument for AppControl.</param>
302         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
303         {
304             Log.Info("NUI", "NUICorebackend OnAppControl Called");
305             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
306             SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
307             handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
308         }
309
310         /// <summary>
311         /// The Paused event callback function.
312         /// </summary>
313         /// <param name="source">The application instance.</param>
314         /// <param name="e">The event argument for Paused.</param>
315         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
316         {
317             Log.Info("NUI", "NUICorebackend OnPaused Called");
318             var handler = Handlers[EventType.Paused] as Action;
319             handler?.Invoke();
320         }
321
322         internal Application ApplicationHandle
323         {
324             get
325             {
326                 return application;
327             }
328         }
329     }
330 }