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