2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Collections.Generic;
20 using Tizen.Applications.CoreBackend;
21 using Tizen.Applications;
25 class NUIWidgetCoreBackend : ICoreBackend
28 /// Application instance to connect event.
30 protected WidgetApplication application;
31 private string stylesheet = "";
32 private Dictionary<System.Type, string> widgetInfo;
35 /// Dictionary to contain each type of event callback.
37 protected IDictionary<EventType, object> handlers = new Dictionary<EventType, object>();
40 /// The default Constructor.
42 public NUIWidgetCoreBackend()
47 /// The constructor with stylesheet.
49 public NUIWidgetCoreBackend(string stylesheet)
51 this.stylesheet = stylesheet;
55 /// Add NUIWidgetApplication event to Application.
56 /// Put each type of event callback in Dictionary.
58 /// <param name="evType">Type of event</param>
59 /// <param name="handler">Event callback</param>
60 public void AddEventHandler(EventType evType, Action handler)
62 handlers.Add(evType, handler);
66 /// Add NUIWidgetApplication event to Application.
67 /// Put each type of event callback in Dictionary.
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
74 handlers.Add(evType, handler);
83 application?.Dispose();
94 public void RegisterWidgetInfo(Dictionary<System.Type, string> widgetInfo)
96 this.widgetInfo = widgetInfo;
99 public void AddWidgetInfo(Dictionary<System.Type, string> widgetInfo)
101 application?.AddWidgetInfo(widgetInfo);
107 /// <param name="args">Arguments from commandline.</param>
108 public void Run(string[] args)
110 TizenSynchronizationContext.Initialize();
112 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
113 application = WidgetApplication.NewWidgetApplication(args, stylesheet);
114 application.RegisterWidgetInfo(widgetInfo);
116 application.BatteryLow += OnBatteryLow;
117 application.LanguageChanged += OnLanguageChanged;
118 application.MemoryLow += OnMemoryLow;
119 application.RegionChanged += OnRegionChanged;
120 application.DeviceOrientationChanged += OnDeviceOrientationChanged;
121 application.Initialized += OnInitialized;
122 application.Terminating += OnTerminated;
124 application.MainLoop();
125 application.Dispose();
129 /// The Initialized event callback function.
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)
135 var preCreateHandler = handlers[EventType.PreCreated] as Action;
136 preCreateHandler?.Invoke();
138 var createHandler = handlers[EventType.Created] as Action;
139 createHandler?.Invoke();
140 application.RegisterWidgetCreatingFunction();
144 /// The Terminated event callback function.
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)
150 var handler = handlers[EventType.Terminated] as Action;
155 /// The Region changed event callback function.
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)
161 var handler = handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
162 handler?.Invoke(new RegionFormatChangedEventArgs(e.Application.GetRegion()));
166 /// The Language changed event callback function.
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)
172 var handler = handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
173 handler?.Invoke(new LocaleChangedEventArgs(e.Application.GetLanguage()));
177 /// The Memory Low event callback function.
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)
183 var handler = handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
185 switch (e.MemoryStatus)
187 case Application.MemoryStatus.Normal:
189 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
192 case Application.MemoryStatus.Low:
194 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
197 case Application.MemoryStatus.CriticallyLow:
199 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
206 /// The Battery Low event callback function.
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)
212 var handler = handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
213 switch (e.BatteryStatus)
215 case Application.BatteryStatus.Normal:
217 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
220 case Application.BatteryStatus.CriticallyLow:
222 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
225 case Application.BatteryStatus.PowerOff:
227 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
234 /// The Device Orientation changed event callback function.
236 /// <param name="source">The application instance.</param>
237 /// <param name="e">The event argument for DeviceOrientationChanged.</param>
238 private void OnDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
240 var handler = handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
242 switch (e.DeviceOrientationStatus)
244 case Application.DeviceOrientationStatus.Orientation_0:
246 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
249 case Application.DeviceOrientationStatus.Orientation_90:
251 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
254 case Application.DeviceOrientationStatus.Orientation_180:
256 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
259 case Application.DeviceOrientationStatus.Orientation_270:
261 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
267 internal WidgetApplication WidgetApplicationHandle