[Tizen] Add a new constructor to NUIApplication.
[platform/core/csapi/nui.git] / Tizen.NUI / src / internal / NUICoreBackend.cs
1 /*
2  * Copyright (c) 2017 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
21 using Tizen.Applications.CoreBackend;
22 using Tizen.Applications;
23 using Tizen.NUI;
24
25 namespace Tizen.NUI
26 {
27     class NUICoreBackend : ICoreBackend
28     {
29         /// <summary>
30         /// Application instance to connect event.
31         /// </summary>
32         protected Application _application;
33         private string _stylesheet = "";
34         private NUIApplication.WindowMode _windowMode = NUIApplication.WindowMode.Opaque;
35
36         /// <summary>
37         /// 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         /// Add NUIApplication event to Application.
67         /// Put each type of event callback in Dictionary.
68         /// </summary>
69         /// <param name="evType">Type of event</param>
70         /// <param name="handler">Event callback</param>
71         public void AddEventHandler(EventType evType, Action handler)
72         {
73             Handlers.Add(evType, handler);
74         }
75
76         /// <summary>
77         /// Add NUIApplication event to Application.
78         /// Put each type of event callback in Dictionary.
79         /// </summary>
80         /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
81         /// <param name="evType">Type of event</param>
82         /// <param name="handler">Event callback</param>
83         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
84         {
85             Handlers.Add(evType, handler);
86         }
87
88
89         /// <summary>
90         /// Dispose function.
91         /// </summary>
92         public void Dispose()
93         {
94             if(_application != null)
95             {
96                 _application.Dispose();
97             }
98         }
99
100         /// <summary>
101         /// Exit Application.
102         /// </summary>
103         public void Exit()
104         {
105             if(_application != null)
106             {
107                 _application.Quit();
108             }
109         }
110
111         /// <summary>
112         /// Run Application.
113         /// </summary>
114         /// <param name="args">Arguments from commandline.</param>
115         public void Run(string[] args)
116         {
117             TizenSynchronizationContext.Initialize();
118
119             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
120             if (args.Length == 1)
121             {
122                 _application = Application.NewApplication();
123             }
124             else if (args.Length > 1)
125             {
126                 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
127             }
128
129             _application.BatteryLow += OnBatteryLow;
130             _application.LanguageChanged += OnLanguageChanged;
131             _application.MemoryLow += OnMemoryLow;
132             _application.RegionChanged += OnRegionChanged;
133
134             _application.Initialized += OnInitialized;
135             _application.Resumed += OnResumed;
136             _application.Terminating += OnTerminated;
137             _application.Paused += OnPaused;
138             _application.AppControl += OnAppControl;
139
140             _application.MainLoop();
141         }
142
143         /// <summary>
144         /// Region changed event callback function.
145         /// </summary>
146         /// <param name="source">Application instance</param>
147         /// <param name="e">Event argument for RegionChanged</param>
148         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
149         {
150             Log.Debug("NUI", "NUICorebackend OnRegionChanged Called");
151             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
152             // Need to make new signal return in native to return right value.
153             handler?.Invoke( new RegionFormatChangedEventArgs(""));
154         }
155
156         /// <summary>
157         /// Memory Low event callback function.
158         /// </summary>
159         /// <param name="source">Application instance</param>
160         /// <param name="e">Event argument for MemoryLow</param>
161         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
162         {
163             Log.Debug("NUI", "NUICorebackend OnMemoryLow Called");
164             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
165             // Need to make new signal return in native to return right value.
166             handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
167         }
168
169         /// <summary>
170         /// Language changed event callback function.
171         /// </summary>
172         /// <param name="source">Application instance</param>
173         /// <param name="e">Event argument for LanguageChanged</param>
174         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
175         {
176             Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
177             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
178             // Need to make new signal return in native to return right value.
179             handler?.Invoke( new LocaleChangedEventArgs(""));
180         }
181
182         /// <summary>
183         /// Battery low event callback function.
184         /// </summary>
185         /// <param name="source">Application instance</param>
186         /// <param name="e">Event argument for BatteryLow</param>
187         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
188         {
189             Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
190             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
191             // Need to make new signal return in native to return right value.
192             handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
193         }
194
195         /// <summary>
196         /// Initialized event callback function.
197         /// </summary>
198         /// <param name="source">Application instance</param>
199         /// <param name="e">Event argument for Initialized</param>
200         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
201         {
202             Log.Debug("NUI", "NUICorebackend OnPreCreated Called");
203             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
204             preCreateHandler?.Invoke();
205
206             Log.Debug("NUI", "NUICorebackend OnCreate Called");
207             var createHandler = Handlers[EventType.Created] as Action;
208             createHandler?.Invoke();
209         }
210
211         /// <summary>
212         /// Terminated event callback function.
213         /// </summary>
214         /// <param name="source">Application instance</param>
215         /// <param name="e">Event argument for Terminated</param>
216         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
217         {
218             Log.Debug("NUI", "NUICorebackend OnTerminated Called");
219             var handler = Handlers[EventType.Terminated] as Action;
220             handler?.Invoke();
221         }
222
223         /// <summary>
224         /// Resumed event callback function.
225         /// </summary>
226         /// <param name="source">Application instance</param>
227         /// <param name="e">Event argument for Resumed</param>
228         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
229         {
230             Log.Debug("NUI", "NUICorebackend OnResumed Called");
231             var handler = Handlers[EventType.Resumed] as Action;
232             handler?.Invoke();
233         }
234
235         /// <summary>
236         /// App control event callback function.
237         /// </summary>
238         /// <param name="source">Application instance</param>
239         /// <param name="e">Event argument for AppControl</param>
240         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
241         {
242             Log.Debug("NUI", "NUICorebackend OnAppControl Called");
243             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;\r
244             SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);\r
245             handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
246         }
247
248         /// <summary>
249         /// Paused event callback function.
250         /// </summary>
251         /// <param name="source">Application instance</param>
252         /// <param name="e">Event argument for Paused</param>
253         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
254         {
255             Log.Debug("NUI", "NUICorebackend OnPaused Called");
256             var handler = Handlers[EventType.Paused] as Action;
257             handler?.Invoke();
258         }
259
260
261         internal Application ApplicationHandle
262         {
263                 get
264                 {
265                         return _application;
266                 }
267         }
268
269     }
270 }