Release 4.0.0-preview1-00051
[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         /// Application instance to connect event.
31         /// </summary>
32         protected Application _application;
33
34         /// <summary>
35         /// Dictionary to contain each type of event callback.
36         /// </summary>
37         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
38
39         /// <summary>
40         /// The default Constructor.
41         /// </summary>
42         public NUICoreBackend()
43         {
44             _application = Application.NewApplication();
45         }
46
47         /// <summary>
48         /// The constructor with stylesheet.
49         /// </summary>
50         public NUICoreBackend(string stylesheet)
51         {
52             _application = Application.NewApplication(stylesheet);
53         }
54
55         /// <summary>
56         /// The constructor with stylesheet and window mode.
57         /// </summary>
58         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
59         {
60             _application = Application.NewApplication(stylesheet, (Application.WindowMode)windowMode );
61         }
62
63         /// <summary>
64         /// Add NUIApplication event to Application.
65         /// Put each type of event callback in Dictionary.
66         /// </summary>
67         /// <param name="evType">Type of event</param>
68         /// <param name="handler">Event callback</param>
69         public void AddEventHandler(EventType evType, Action handler)
70         {
71             Handlers.Add(evType, handler);
72         }
73
74         /// <summary>
75         /// Add NUIApplication event to Application.
76         /// Put each type of event callback in Dictionary.
77         /// </summary>
78         /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
79         /// <param name="evType">Type of event</param>
80         /// <param name="handler">Event callback</param>
81         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
82         {
83             Handlers.Add(evType, handler);
84         }
85
86
87         /// <summary>
88         /// Dispose function.
89         /// </summary>
90         public void Dispose()
91         {
92             _application.Dispose();
93         }
94
95         /// <summary>
96         /// Exit Application.
97         /// </summary>
98         public void Exit()
99         {
100             _application.Quit();
101         }
102
103         /// <summary>
104         /// Run Application.
105         /// </summary>
106         /// <param name="args">Arguments from commandline.</param>
107         public void Run(string[] args)
108         {
109             TizenSynchronizationContext.Initialize();
110             _application.BatteryLow += OnBatteryLow;
111             _application.LanguageChanged += OnLanguageChanged;
112             _application.MemoryLow += OnMemoryLow;
113             _application.RegionChanged += OnRegionChanged;
114
115             _application.Initialized += OnInitialized;
116             _application.Resumed += OnResumed;
117             _application.Terminating += OnTerminated;
118             _application.Paused += OnPaused;
119             _application.AppControl += OnAppControl;
120
121             _application.MainLoop();
122         }
123
124         /// <summary>
125         /// Region changed event callback function.
126         /// </summary>
127         /// <param name="source">Application instance</param>
128         /// <param name="e">Event argument for RegionChanged</param>
129         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
130         {
131             Log.Debug("NUI", "NUICorebackend OnRegionChanged Called");
132             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
133             // Need to make new signal return in native to return right value.
134             handler?.Invoke( new RegionFormatChangedEventArgs(""));
135         }
136
137         /// <summary>
138         /// Memory Low event callback function.
139         /// </summary>
140         /// <param name="source">Application instance</param>
141         /// <param name="e">Event argument for MemoryLow</param>
142         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
143         {
144             Log.Debug("NUI", "NUICorebackend OnMemoryLow Called");
145             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
146             // Need to make new signal return in native to return right value.
147             handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
148         }
149
150         /// <summary>
151         /// Language changed event callback function.
152         /// </summary>
153         /// <param name="source">Application instance</param>
154         /// <param name="e">Event argument for LanguageChanged</param>
155         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
156         {
157             Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
158             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
159             // Need to make new signal return in native to return right value.
160             handler?.Invoke( new LocaleChangedEventArgs(""));
161         }
162
163         /// <summary>
164         /// Battery low event callback function.
165         /// </summary>
166         /// <param name="source">Application instance</param>
167         /// <param name="e">Event argument for BatteryLow</param>
168         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
169         {
170             Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
171             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
172             // Need to make new signal return in native to return right value.
173             handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
174         }
175
176         /// <summary>
177         /// Initialized event callback function.
178         /// </summary>
179         /// <param name="source">Application instance</param>
180         /// <param name="e">Event argument for Initialized</param>
181         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
182         {
183             Log.Debug("NUI", "NUICorebackend OnPreCreated Called");
184             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
185             preCreateHandler?.Invoke();
186
187             Log.Debug("NUI", "NUICorebackend OnCreate Called");
188             var createHandler = Handlers[EventType.Created] as Action;
189             createHandler?.Invoke();
190         }
191
192         /// <summary>
193         /// Terminated event callback function.
194         /// </summary>
195         /// <param name="source">Application instance</param>
196         /// <param name="e">Event argument for Terminated</param>
197         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
198         {
199             Log.Debug("NUI", "NUICorebackend OnTerminated Called");
200             var handler = Handlers[EventType.Terminated] as Action;
201             handler?.Invoke();
202         }
203
204         /// <summary>
205         /// Resumed event callback function.
206         /// </summary>
207         /// <param name="source">Application instance</param>
208         /// <param name="e">Event argument for Resumed</param>
209         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
210         {
211             Log.Debug("NUI", "NUICorebackend OnResumed Called");
212             var handler = Handlers[EventType.Resumed] as Action;
213             handler?.Invoke();
214         }
215
216         /// <summary>
217         /// App control event callback function.
218         /// </summary>
219         /// <param name="source">Application instance</param>
220         /// <param name="e">Event argument for AppControl</param>
221         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
222         {
223             Log.Debug("NUI", "NUICorebackend OnAppControl Called");
224             /* can invoke after making new api which getting control handle at application.
225             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
226             handler?.Invoke();
227             */
228         }
229
230         /// <summary>
231         /// Paused event callback function.
232         /// </summary>
233         /// <param name="source">Application instance</param>
234         /// <param name="e">Event argument for Paused</param>
235         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
236         {
237             Log.Debug("NUI", "NUICorebackend OnPaused Called");
238             var handler = Handlers[EventType.Paused] as Action;
239             handler?.Invoke();
240         }
241
242
243         internal Application ApplicationHandle
244         {
245                 get
246                 {
247                         return _application;
248                 }
249         }
250
251     }
252 }