Follow formatting NUI
[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         public void AddWidgetInfo(Dictionary<System.Type, string> widgetInfo)
106         {
107             if (_application != null)
108             {
109                 _application.AddWidgetInfo(widgetInfo);
110             }
111         }
112
113         /// <summary>
114         /// Run Application.
115         /// </summary>
116         /// <param name="args">Arguments from commandline.</param>
117         public void Run(string[] args)
118         {
119             TizenSynchronizationContext.Initialize();
120
121             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
122             _application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
123             _application.RegisterWidgetInfo(_widgetInfo);
124
125             _application.BatteryLow += OnBatteryLow;
126             _application.LanguageChanged += OnLanguageChanged;
127             _application.MemoryLow += OnMemoryLow;
128             _application.RegionChanged += OnRegionChanged; ;
129             _application.Initialized += OnInitialized;
130             _application.Terminating += OnTerminated;
131
132             _application.MainLoop();
133         }
134
135         /// <summary>
136         /// The Initialized event callback function.
137         /// </summary>
138         /// <param name="source">The application instance.</param>
139         /// <param name="e">The event argument for Initialized.</param>
140         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
141         {
142             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
143             preCreateHandler?.Invoke();
144
145             var createHandler = Handlers[EventType.Created] as Action;
146             createHandler?.Invoke();
147             _application.RegisterWidgetCreatingFunction();
148         }
149
150         /// <summary>
151         /// The Terminated event callback function.
152         /// </summary>
153         /// <param name="source">The application instance.</param>
154         /// <param name="e">The event argument for Terminated.</param>
155         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
156         {
157             var handler = Handlers[EventType.Terminated] as Action;
158             handler?.Invoke();
159         }
160
161         /// <summary>
162         /// The Region changed event callback function.
163         /// </summary>
164         /// <param name="source">The application instance.</param>
165         /// <param name="e">The event argument for RegionChanged.</param>
166         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
167         {
168             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
169             handler?.Invoke(new RegionFormatChangedEventArgs(e.Application.GetRegion()));
170         }
171
172         /// <summary>
173         /// The Language changed event callback function.
174         /// </summary>
175         /// <param name="source">The application instance.</param>
176         /// <param name="e">The event argument for LanguageChanged.</param>
177         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
178         {
179             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
180             handler?.Invoke(new LocaleChangedEventArgs(e.Application.GetLanguage()));
181         }
182
183         /// <summary>
184         /// The Memory Low event callback function.
185         /// </summary>
186         /// <param name="source">The application instance.</param>
187         /// <param name="e">The event argument for MemoryLow.</param>
188         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
189         {
190             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
191
192             switch (e.MemoryStatus)
193             {
194                 case Application.MemoryStatus.Normal:
195                     {
196                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
197                         break;
198                     }
199                 case Application.MemoryStatus.Low:
200                     {
201                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
202                         break;
203                     }
204                 case Application.MemoryStatus.CriticallyLow:
205                     {
206                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
207                         break;
208                     }
209             }
210         }
211
212         /// <summary>
213         /// The Battery Low event callback function.
214         /// </summary>
215         /// <param name="source">The application instance.</param>
216         /// <param name="e">The event argument for BatteryLow.</param>
217         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
218         {
219             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
220             switch (e.BatteryStatus)
221             {
222                 case Application.BatteryStatus.Normal:
223                     {
224                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
225                         break;
226                     }
227                 case Application.BatteryStatus.CriticallyLow:
228                     {
229                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
230                         break;
231                     }
232                 case Application.BatteryStatus.PowerOff:
233                     {
234                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
235                         break;
236                     }
237             }
238         }
239
240         internal WidgetApplication WidgetApplicationHandle
241         {
242             get
243             {
244                 return _application;
245             }
246         }
247     }
248 }