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