Release 4.0.0-preview1-00304
[platform/core/csapi/tizenfx.git] / src / 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         /// The 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         /// 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         /// Adds NUIApplication event to Application.
67         /// Puts each type of event callback in Dictionary.
68         /// </summary>
69         /// <param name="evType">The type of event.</param>
70         /// <param name="handler">The event callback.</param>
71         public void AddEventHandler(EventType evType, Action handler)
72         {
73             Handlers.Add(evType, handler);
74         }
75
76         /// <summary>
77         /// Adds NUIApplication event to Application.
78         /// Puts each type of event callback in Dictionary.
79         /// </summary>
80         /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
81         /// <param name="evType">The type of event.</param>
82         /// <param name="handler">The 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         /// The Dispose function.
91         /// </summary>
92         public void Dispose()
93         {
94             if(_application != null)
95             {
96                 _application.Dispose();
97             }
98         }
99
100         /// <summary>
101         /// The Exit application.
102         /// </summary>
103         public void Exit()
104         {
105             if(_application != null)
106             {
107                 _application.Quit();
108             }
109         }
110
111         /// <summary>
112         /// Ensures that the function passed in is called from the main loop when it is idle.
113         /// </summary>
114         /// <param name="func">The function to call</param>
115         /// <returns>true if added successfully, false otherwise</returns>
116         public bool AddIdle(System.Delegate func)
117         {
118             return _application.AddIdle(func);
119         }
120
121         /// <summary>
122         /// The Run application.
123         /// </summary>
124         /// <param name="args">The arguments from commandline.</param>
125         public void Run(string[] args)
126         {
127             TizenSynchronizationContext.Initialize();
128
129             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
130             if (args.Length == 1)
131             {
132                 _application = Application.NewApplication();
133             }
134             else if (args.Length > 1)
135             {
136                 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
137             }
138
139             _application.BatteryLow += OnBatteryLow;
140             _application.LanguageChanged += OnLanguageChanged;
141             _application.MemoryLow += OnMemoryLow;
142             _application.RegionChanged += OnRegionChanged;
143
144             _application.Initialized += OnInitialized;
145             _application.Resumed += OnResumed;
146             _application.Terminating += OnTerminated;
147             _application.Paused += OnPaused;
148             _application.AppControl += OnAppControl;
149
150             _application.MainLoop();
151         }
152
153         /// <summary>
154         /// The Region changed event callback function.
155         /// </summary>
156         /// <param name="source">The application instance.</param>
157         /// <param name="e">The event argument for RegionChanged.</param>
158         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
159         {
160             Log.Debug("NUI", "NUICorebackend OnRegionChanged Called");
161             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
162             // Need to make new signal return in native to return right value.
163             handler?.Invoke( new RegionFormatChangedEventArgs(e.Application.GetRegion()));
164         }
165
166         /// <summary>
167         /// The Memory Low event callback function.
168         /// </summary>
169         /// <param name="source">The application instance.</param>
170         /// <param name="e">The event argument for MemoryLow.</param>
171         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
172         {
173             Log.Debug("NUI", "NUICorebackend OnMemoryLow Called");
174             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
175             // Need to make new signal return in native to return right value.
176             switch( e.MemoryStatus )
177             {
178                 case Application.MemoryStatus.Normal:
179                 {
180             handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
181                     break;
182                 }
183                 case Application.MemoryStatus.SoftWarning:
184                 {
185                     handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
186                     break;
187                 }
188                 case Application.MemoryStatus.HardWarning:
189                 {
190                     handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
191                     break;
192                 }
193             }
194         }
195
196         /// <summary>
197         /// The Language changed event callback function.
198         /// </summary>
199         /// <param name="source">The application instance.</param>
200         /// <param name="e">The event argument for LanguageChanged.</param>
201         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
202         {
203             Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
204             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
205             // Need to make new signal return in native to return right value.
206             handler?.Invoke( new LocaleChangedEventArgs(e.Application.GetLanguage()));
207         }
208
209         /// <summary>
210         /// The Battery Low event callback function.
211         /// </summary>
212         /// <param name="source">The application instance.</param>
213         /// <param name="e">The event argument for BatteryLow.</param>
214         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
215         {
216             Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
217             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
218             // Need to make new signal return in native to return right value.
219             switch( e.BatteryStatus )
220             {
221                 case Application.BatteryStatus.Normal:
222                 {
223             handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
224                     break;
225                 }
226                 case Application.BatteryStatus.CriticalLow:
227                 {
228                     handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
229                     break;
230                 }
231                 case Application.BatteryStatus.PowerOff:
232                 {
233                     handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
234                     break;
235                 }
236             }
237         }
238
239         /// <summary>
240         /// The Initialized event callback function.
241         /// </summary>
242         /// <param name="source">The application instance.</param>
243         /// <param name="e">The event argument for Initialized.</param>
244         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
245         {
246             Log.Debug("NUI", "NUICorebackend OnPreCreated Called");
247             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
248             preCreateHandler?.Invoke();
249
250             Log.Debug("NUI", "NUICorebackend OnCreate Called");
251             var createHandler = Handlers[EventType.Created] as Action;
252             createHandler?.Invoke();
253         }
254
255         /// <summary>
256         /// The Terminated event callback function.
257         /// </summary>
258         /// <param name="source">The application instance.</param>
259         /// <param name="e">The event argument for Terminated.</param>
260         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
261         {
262             Log.Debug("NUI", "NUICorebackend OnTerminated Called");
263             var handler = Handlers[EventType.Terminated] as Action;
264             handler?.Invoke();
265         }
266
267         /// <summary>
268         /// The Resumed event callback function.
269         /// </summary>
270         /// <param name="source">The application instance.</param>
271         /// <param name="e">The event argument for Resumed.</param>
272         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
273         {
274             Log.Debug("NUI", "NUICorebackend OnResumed Called");
275             var handler = Handlers[EventType.Resumed] as Action;
276             handler?.Invoke();
277         }
278
279         /// <summary>
280         /// The App control event callback function.
281         /// </summary>
282         /// <param name="source">The application instance.</param>
283         /// <param name="e">The event argument for AppControl.</param>
284         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
285         {
286             Log.Debug("NUI", "NUICorebackend OnAppControl Called");
287             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
288             SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
289             handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
290         }
291
292         /// <summary>
293         /// The Paused event callback function.
294         /// </summary>
295         /// <param name="source">The application instance.</param>
296         /// <param name="e">The event argument for Paused.</param>
297         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
298         {
299             Log.Debug("NUI", "NUICorebackend OnPaused Called");
300             var handler = Handlers[EventType.Paused] as Action;
301             handler?.Invoke();
302         }
303
304
305         internal Application ApplicationHandle
306         {
307                 get
308                 {
309                         return _application;
310                 }
311         }
312
313     }
314 }