55c719373d3ec7f3f4da6d11433d19bd27b4f49f
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / NUIWidgetCoreBackend.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
24 namespace Tizen.NUI
25 {
26     class NUIWidgetCoreBackend : ICoreBackend
27     {
28         /// <summary>
29         /// Application instance to connect event.
30         /// </summary>
31         protected WidgetApplication _application;
32         private string _stylesheet = "";
33         Dictionary<System.Type, string> _widgetInfo;
34
35         /// <summary>
36         /// Dictionary to contain each type of event callback.
37         /// </summary>
38         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
39
40         /// <summary>
41         /// The default Constructor.
42         /// </summary>
43         public NUIWidgetCoreBackend()
44         {
45         }
46
47         /// <summary>
48         /// The constructor with stylesheet.
49         /// </summary>
50         public NUIWidgetCoreBackend(string stylesheet)
51         {
52             _stylesheet = stylesheet;
53         }
54
55         /// <summary>
56         /// Add NUIWidgetApplication event to Application.
57         /// Put each type of event callback in Dictionary.
58         /// </summary>
59         /// <param name="evType">Type of event</param>
60         /// <param name="handler">Event callback</param>
61         public void AddEventHandler(EventType evType, Action handler)
62         {
63             Handlers.Add(evType, handler);
64         }
65
66         /// <summary>
67         /// Add NUIWidgetApplication event to Application.
68         /// Put each type of event callback in Dictionary.
69         /// </summary>
70         /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
71         /// <param name="evType">Type of event</param>
72         /// <param name="handler">Event callback</param>
73         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
74         {
75             Handlers.Add(evType, handler);
76         }
77
78
79         /// <summary>
80         /// Dispose function.
81         /// </summary>
82         public void Dispose()
83         {
84             if (_application != null)
85             {
86                 _application.Dispose();
87             }
88         }
89
90         /// <summary>
91         /// Exit Application.
92         /// </summary>
93         public void Exit()
94         {
95             if (_application != null)
96             {
97                 _application.Quit();
98             }
99         }
100
101         public void RegisterWidgetInfo(Dictionary<System.Type, string> widgetInfo)
102         {
103             _widgetInfo = widgetInfo;
104         }
105
106         /// <summary>
107         /// Run Application.
108         /// </summary>
109         /// <param name="args">Arguments from commandline.</param>
110         public void Run(string[] args)
111         {
112             TizenSynchronizationContext.Initialize();
113
114             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
115             _application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
116             _application.RegisterWidgetInfo(_widgetInfo);
117
118             _application.BatteryLow += OnBatteryLow;
119             _application.LanguageChanged += OnLanguageChanged;
120             _application.MemoryLow += OnMemoryLow;
121             _application.RegionChanged += OnRegionChanged; ;
122             _application.Initialized += OnInitialized;
123             _application.Terminating += OnTerminated;
124
125             _application.MainLoop();
126         }
127
128         /// <summary>
129         /// The Initialized event callback function.
130         /// </summary>
131         /// <param name="source">The application instance.</param>
132         /// <param name="e">The event argument for Initialized.</param>
133         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
134         {
135             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
136             preCreateHandler?.Invoke();
137
138             var createHandler = Handlers[EventType.Created] as Action;
139             createHandler?.Invoke();
140             _application.RegisterWidgetCreatingFunction();
141         }
142
143         /// <summary>
144         /// The Terminated event callback function.
145         /// </summary>
146         /// <param name="source">The application instance.</param>
147         /// <param name="e">The event argument for Terminated.</param>
148         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
149         {
150             var handler = Handlers[EventType.Terminated] as Action;
151             handler?.Invoke();
152         }
153
154         /// <summary>
155         /// The Region changed event callback function.
156         /// </summary>
157         /// <param name="source">The application instance.</param>
158         /// <param name="e">The event argument for RegionChanged.</param>
159         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
160         {
161             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
162             handler?.Invoke(new RegionFormatChangedEventArgs(e.Application.GetRegion()));
163         }
164
165         /// <summary>
166         /// The Language changed event callback function.
167         /// </summary>
168         /// <param name="source">The application instance.</param>
169         /// <param name="e">The event argument for LanguageChanged.</param>
170         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
171         {
172             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
173             handler?.Invoke(new LocaleChangedEventArgs(e.Application.GetLanguage()));
174         }
175
176         /// <summary>
177         /// The Memory Low event callback function.
178         /// </summary>
179         /// <param name="source">The application instance.</param>
180         /// <param name="e">The event argument for MemoryLow.</param>
181         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
182         {
183             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
184
185             switch (e.MemoryStatus)
186             {
187                 case Application.MemoryStatus.Normal:
188                 {
189                     handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
190                     break;
191                 }
192                 case Application.MemoryStatus.Low:
193                 {
194                     handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
195                     break;
196                 }
197                 case Application.MemoryStatus.CriticallyLow:
198                 {
199                     handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
200                     break;
201                 }
202             }
203         }
204
205         /// <summary>
206         /// The Battery Low event callback function.
207         /// </summary>
208         /// <param name="source">The application instance.</param>
209         /// <param name="e">The event argument for BatteryLow.</param>
210         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
211         {
212             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
213             switch (e.BatteryStatus)
214             {
215                 case Application.BatteryStatus.Normal:
216                 {
217                     handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
218                     break;
219                 }
220                 case Application.BatteryStatus.CriticallyLow:
221                 {
222                     handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
223                     break;
224                 }
225                 case Application.BatteryStatus.PowerOff:
226                 {
227                     handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
228                     break;
229                 }
230             }
231         }
232
233         internal WidgetApplication WidgetApplicationHandle
234         {
235             get
236             {
237                 return _application;
238             }
239         }
240     }
241 }