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