2 * Copyright (c) 2018 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.ComponentModel;
20 using System.Collections.Generic;
21 using Tizen.Applications.CoreBackend;
22 using Tizen.Applications;
26 class NUICoreBackend : ICoreBackend
29 /// The Application instance to connect event.
31 protected Application application;
32 private string stylesheet = "";
33 private NUIApplication.WindowMode windowMode = NUIApplication.WindowMode.Opaque;
34 private Rectangle windowRectangle = null;
35 private WindowType defaultWindowType = WindowType.Normal;
38 /// The Dictionary to contain each type of event callback.
40 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
43 /// The default Constructor.
45 public NUICoreBackend()
50 /// The constructor with stylesheet.
52 public NUICoreBackend(string stylesheet)
54 this.stylesheet = stylesheet;
58 /// The constructor with stylesheet and window mode.
60 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
62 this.stylesheet = stylesheet;
63 this.windowMode = windowMode;
67 /// The constructor with stylesheet, window mode, window size and window position.
69 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
71 this.stylesheet = stylesheet;
72 this.windowMode = windowMode;
73 if (windowSize != null && windowPosition != null)
75 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
80 /// The constructor with stylesheet, window mode, window size, window position and default window type.
81 /// This will be hidden as inhouse API. Because it is only for internal IME window.
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition, WindowType type)
86 this.stylesheet = stylesheet;
87 this.windowMode = windowMode;
88 if (windowSize != null && windowPosition != null)
90 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
92 this.defaultWindowType = type;
96 /// Adds NUIApplication event to Application.
97 /// Puts each type of event callback in Dictionary.
99 /// <param name="evType">The type of event.</param>
100 /// <param name="handler">The event callback.</param>
101 public void AddEventHandler(EventType evType, Action handler)
103 Handlers.Add(evType, handler);
107 /// Adds NUIApplication event to Application.
108 /// Puts each type of event callback in Dictionary.
110 /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
111 /// <param name="evType">The type of event.</param>
112 /// <param name="handler">The event callback.</param>
113 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
115 Handlers.Add(evType, handler);
119 /// The Dispose function.
121 public void Dispose()
123 if (application != null)
125 application.Dispose();
127 if (windowRectangle != null)
129 windowRectangle.Dispose();
134 /// The Exit application.
138 if (application != null)
145 /// Ensures that the function passed in is called from the main loop when it is idle.
147 /// <param name="func">The function to call</param>
148 /// <returns>true if added successfully, false otherwise</returns>
149 public bool AddIdle(System.Delegate func)
151 return application.AddIdle(func);
155 /// The Run application.
157 /// <param name="args">The arguments from commandline.</param>
158 public void Run(string[] args)
160 TizenSynchronizationContext.Initialize();
162 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
163 if (string.IsNullOrEmpty(args[0]))
165 args[0] = this.GetType().Assembly.FullName.Replace(" ", "");
168 if(defaultWindowType != WindowType.Normal)
170 application = Application.NewApplication(stylesheet, windowMode, windowRectangle, defaultWindowType);
174 if (windowRectangle != null)
176 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle);
180 application = Application.NewApplication(args, stylesheet, windowMode);
184 application.BatteryLow += OnBatteryLow;
185 application.LanguageChanged += OnLanguageChanged;
186 application.MemoryLow += OnMemoryLow;
187 application.RegionChanged += OnRegionChanged;
189 application.Initialized += OnInitialized;
190 application.Resumed += OnResumed;
191 application.Terminating += OnTerminated;
192 application.Paused += OnPaused;
193 application.AppControl += OnAppControl;
195 application.MainLoop();
196 application.Dispose();
200 /// The Region changed event callback function.
202 /// <param name="source">The application instance.</param>
203 /// <param name="e">The event argument for RegionChanged.</param>
204 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
206 Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
207 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
208 handler?.Invoke(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
212 /// The Memory Low event callback function.
214 /// <param name="source">The application instance.</param>
215 /// <param name="e">The event argument for MemoryLow.</param>
216 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
218 Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
219 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
221 switch (e.MemoryStatus)
223 case Application.MemoryStatus.Normal:
225 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
228 case Application.MemoryStatus.Low:
230 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
233 case Application.MemoryStatus.CriticallyLow:
235 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
242 /// The Language changed event callback function.
244 /// <param name="source">The application instance.</param>
245 /// <param name="e">The event argument for LanguageChanged.</param>
246 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
248 Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
249 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
250 handler?.Invoke(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
254 /// The Battery Low event callback function.
256 /// <param name="source">The application instance.</param>
257 /// <param name="e">The event argument for BatteryLow.</param>
258 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
260 Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
261 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
262 switch (e.BatteryStatus)
264 case Application.BatteryStatus.Normal:
266 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
269 case Application.BatteryStatus.CriticallyLow:
271 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
274 case Application.BatteryStatus.PowerOff:
276 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
283 /// The Initialized event callback function.
285 /// <param name="source">The application instance.</param>
286 /// <param name="e">The event argument for Initialized.</param>
287 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
289 Log.Info("NUI", "NUICorebackend OnPreCreated Called");
290 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
291 preCreateHandler?.Invoke();
293 Log.Info("NUI", "NUICorebackend OnCreate Called");
294 var createHandler = Handlers[EventType.Created] as Action;
295 createHandler?.Invoke();
299 /// The Terminated event callback function.
301 /// <param name="source">The application instance.</param>
302 /// <param name="e">The event argument for Terminated.</param>
303 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
305 Log.Info("NUI", "NUICorebackend OnTerminated Called");
306 var handler = Handlers[EventType.Terminated] as Action;
311 /// The Resumed event callback function.
313 /// <param name="source">The application instance.</param>
314 /// <param name="e">The event argument for Resumed.</param>
315 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
317 Log.Info("NUI", "NUICorebackend OnResumed Called");
318 var handler = Handlers[EventType.Resumed] as Action;
323 /// The App control event callback function.
325 /// <param name="source">The application instance.</param>
326 /// <param name="e">The event argument for AppControl.</param>
327 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
329 Log.Info("NUI", "NUICorebackend OnAppControl Called");
330 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
331 SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
332 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
336 /// The Paused event callback function.
338 /// <param name="source">The application instance.</param>
339 /// <param name="e">The event argument for Paused.</param>
340 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
342 Log.Info("NUI", "NUICorebackend OnPaused Called");
343 var handler = Handlers[EventType.Paused] as Action;
347 internal Application ApplicationHandle