ec05e4319994a74c783fe69522cd3b9f06df386e
[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
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             //Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend called");
45             //_application = WidgetApplication.NewWidgetApplication();
46         }
47
48         /// <summary>
49         /// The constructor with stylesheet.
50         /// </summary>
51         public NUIWidgetCoreBackend(string stylesheet)
52         {
53             _stylesheet = stylesheet;
54             //_application = WidgetApplication.NewWidgetApplication(stylesheet);
55         }
56
57         /// <summary>
58         /// Add NUIWidgetApplication event to Application.
59         /// Put each type of event callback in Dictionary.
60         /// </summary>
61         /// <param name="evType">Type of event</param>
62         /// <param name="handler">Event callback</param>
63         public void AddEventHandler(EventType evType, Action handler)
64         {
65             Handlers.Add(evType, handler);
66         }
67
68         /// <summary>
69         /// Add NUIWidgetApplication event to Application.
70         /// Put each type of event callback in Dictionary.
71         /// </summary>
72         /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
73         /// <param name="evType">Type of event</param>
74         /// <param name="handler">Event callback</param>
75         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
76         {
77             Handlers.Add(evType, handler);
78         }
79
80
81         /// <summary>
82         /// Dispose function.
83         /// </summary>
84         public void Dispose()
85         {
86             Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend Dispose called");
87             if (_application != null)
88             {
89                 _application.Dispose();
90             }
91         }
92
93         /// <summary>
94         /// Exit Application.
95         /// </summary>
96         public void Exit()
97         {
98             Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend Exit called");
99             if (_application != null)
100             {
101                 _application.Quit();
102             }
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             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
112             _application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
113
114             TizenSynchronizationContext.Initialize();
115             _application.BatteryLow += OnBatteryLow;
116             _application.LanguageChanged += OnLanguageChanged;
117             _application.MemoryLow += OnMemoryLow;
118             _application.RegionChanged += OnRegionChanged; ;
119
120             _application.Init += OnInit;
121             _application.Terminate += OnTerminate;
122
123             _application.MainLoop();
124         }
125
126         private void OnInit(object sender, WidgetApplication.WidgetApplicationEventArgs e)
127         {
128             Log.Fatal("NUI", "NUIWidgetApplication OnPreCreated Called");
129             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
130             preCreateHandler?.Invoke();
131
132             Log.Fatal("NUI", "NUIWidgetApplication OnCreate Called");
133             var createHandler = Handlers[EventType.Created] as Action;
134             createHandler?.Invoke();
135
136         }
137
138         private void OnTerminate(object sender, WidgetApplication.WidgetApplicationEventArgs e)
139         {
140             Log.Fatal("NUI", "NUIWidgetApplication OnTerminated Called");
141             var handler = Handlers[EventType.Terminated] as Action;
142             handler?.Invoke();
143         }
144
145         /// <summary>
146         /// Region changed event callback function.
147         /// </summary>
148         /// <param name="source">Application instance</param>
149         /// <param name="e">Event argument for RegionChanged</param>
150         private void OnRegionChanged(object sender, WidgetApplication.WidgetApplicationEventArgs e)
151         {
152             Log.Fatal("NUI", "NUIWidgetApplication OnRegionChanged Called");
153             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
154             // Need to make new signal return in native to return right value.
155             handler?.Invoke(new RegionFormatChangedEventArgs(""));
156         }
157
158         /// <summary>
159         /// Memory Low event callback function.
160         /// </summary>
161         /// <param name="source">Application instance</param>
162         /// <param name="e">Event argument for MemoryLow</param>
163         private void OnMemoryLow(object sender, WidgetApplication.WidgetApplicationEventArgs e)
164         {
165             Log.Fatal("NUI", "NUIWidgetApplication OnMemoryLow Called");
166             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
167             // Need to make new signal return in native to return right value.
168             handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
169         }
170
171         /// <summary>
172         /// Language changed event callback function.
173         /// </summary>
174         /// <param name="source">Application instance</param>
175         /// <param name="e">Event argument for LanguageChanged</param>
176         private void OnLanguageChanged(object sender, WidgetApplication.WidgetApplicationEventArgs e)
177         {
178
179             Log.Fatal("NUI", "NUIWidgetApplication OnLanguageChanged Called");
180             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
181             // Need to make new signal return in native to return right value.
182             handler?.Invoke(new LocaleChangedEventArgs(""));
183
184         }
185
186         /// <summary>
187         /// Battery low event callback function.
188         /// </summary>
189         /// <param name="source">Application instance</param>
190         /// <param name="e">Event argument for BatteryLow</param>
191         private void OnBatteryLow(object sender, WidgetApplication.WidgetApplicationEventArgs e)
192         {
193             Log.Fatal("NUI", "NUIWidgetApplication OnBatteryLow Called");
194             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
195             // Need to make new signal return in native to return right value.
196             handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
197
198         }
199
200         internal WidgetApplication WidgetApplicationHandle
201         {
202             get
203             {
204                 return _application;
205             }
206         }
207     }
208 }