Release 4.0.0-preview1-00235
[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(""));
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             handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
177         }
178
179         /// <summary>
180         /// The Language changed event callback function.
181         /// </summary>
182         /// <param name="source">The application instance.</param>
183         /// <param name="e">The event argument for LanguageChanged.</param>
184         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
185         {
186             Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
187             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
188             // Need to make new signal return in native to return right value.
189             handler?.Invoke( new LocaleChangedEventArgs(""));
190         }
191
192         /// <summary>
193         /// The Battery Low event callback function.
194         /// </summary>
195         /// <param name="source">The application instance.</param>
196         /// <param name="e">The event argument for BatteryLow.</param>
197         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
198         {
199             Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
200             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
201             // Need to make new signal return in native to return right value.
202             handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
203         }
204
205         /// <summary>
206         /// The Initialized event callback function.
207         /// </summary>
208         /// <param name="source">The application instance.</param>
209         /// <param name="e">The event argument for Initialized.</param>
210         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
211         {
212             Log.Debug("NUI", "NUICorebackend OnPreCreated Called");
213             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
214             preCreateHandler?.Invoke();
215
216             Log.Debug("NUI", "NUICorebackend OnCreate Called");
217             var createHandler = Handlers[EventType.Created] as Action;
218             createHandler?.Invoke();
219         }
220
221         /// <summary>
222         /// The Terminated event callback function.
223         /// </summary>
224         /// <param name="source">The application instance.</param>
225         /// <param name="e">The event argument for Terminated.</param>
226         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
227         {
228             Log.Debug("NUI", "NUICorebackend OnTerminated Called");
229             var handler = Handlers[EventType.Terminated] as Action;
230             handler?.Invoke();
231         }
232
233         /// <summary>
234         /// The Resumed event callback function.
235         /// </summary>
236         /// <param name="source">The application instance.</param>
237         /// <param name="e">The event argument for Resumed.</param>
238         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
239         {
240             Log.Debug("NUI", "NUICorebackend OnResumed Called");
241             var handler = Handlers[EventType.Resumed] as Action;
242             handler?.Invoke();
243         }
244
245         /// <summary>
246         /// The App control event callback function.
247         /// </summary>
248         /// <param name="source">The application instance.</param>
249         /// <param name="e">The event argument for AppControl.</param>
250         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
251         {
252             Log.Debug("NUI", "NUICorebackend OnAppControl Called");
253             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
254             SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
255             handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
256         }
257
258         /// <summary>
259         /// The Paused event callback function.
260         /// </summary>
261         /// <param name="source">The application instance.</param>
262         /// <param name="e">The event argument for Paused.</param>
263         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
264         {
265             Log.Debug("NUI", "NUICorebackend OnPaused Called");
266             var handler = Handlers[EventType.Paused] as Action;
267             handler?.Invoke();
268         }
269
270
271         internal Application ApplicationHandle
272         {
273                 get
274                 {
275                         return _application;
276                 }
277         }
278
279     }
280 }