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 : ICoreTaskBackend
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;
36 private ICoreTask coreTask;
39 /// The Dictionary to contain each type of event callback.
41 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
42 protected IDictionary<EventType, object> TaskHandlers = new Dictionary<EventType, object>();
45 /// The default Constructor.
47 public NUICoreBackend()
52 /// The constructor with stylesheet.
54 public NUICoreBackend(string stylesheet)
56 this.stylesheet = stylesheet;
60 /// The constructor with stylesheet and window mode.
62 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
64 this.stylesheet = stylesheet;
65 this.windowMode = windowMode;
69 /// The constructor with stylesheet, window mode, window size and window position.
71 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
73 this.stylesheet = stylesheet;
74 this.windowMode = windowMode;
75 if (windowSize != null && windowPosition != null)
77 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
82 /// The constructor with stylesheet, window mode, window size, window position and default window type.
83 /// This will be hidden as inhouse API. Because it is only for internal IME window.
85 [EditorBrowsable(EditorBrowsableState.Never)]
86 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, WindowType type)
88 this.stylesheet = stylesheet;
89 this.windowMode = windowMode;
90 this.defaultWindowType = type;
94 /// Adds NUIApplication event to Application.
95 /// Puts each type of event callback in Dictionary.
97 /// <param name="evType">The type of event.</param>
98 /// <param name="handler">The event callback.</param>
99 public void AddEventHandler(EventType evType, Action handler)
101 Handlers.Add(evType, handler);
105 /// Adds NUIApplication event to Application.
106 /// Puts each type of event callback in Dictionary.
108 /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
109 /// <param name="evType">The type of event.</param>
110 /// <param name="handler">The event callback.</param>
111 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
113 Handlers.Add(evType, handler);
117 /// The Dispose function.
119 public void Dispose()
121 if (application != null)
123 application.Dispose();
125 if (windowRectangle != null)
127 windowRectangle.Dispose();
132 /// The Exit application.
136 if (application != null)
143 /// Ensures that the function passed in is called from the main loop when it is idle.
145 /// <param name="func">The function to call</param>
146 /// <returns>true if added successfully, false otherwise</returns>
147 public bool AddIdle(System.Delegate func)
149 return application.AddIdle(func);
153 /// The Run application.
155 /// <param name="args">The arguments from commandline.</param>
156 public void Run(string[] args)
158 Tizen.Tracer.Begin("[NUI] NUICorebackend Run()");
160 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): TizenSynchronizationContext.Initialize() called");
161 TizenSynchronizationContext.Initialize();
164 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): args of main set, window type set");
165 if (Tizen.Applications.Application.Current?.ApplicationInfo != null)
167 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
169 if (string.IsNullOrEmpty(args[0]))
171 args[0] = this.GetType().Assembly.FullName.Replace(" ", "");
174 if (defaultWindowType != WindowType.Normal)
176 application = Application.NewApplication(stylesheet, windowMode, defaultWindowType);
180 if (windowRectangle != null)
182 if (coreTask != null)
184 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle, true);
188 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle);
193 if (coreTask != null)
195 // The Rectangle(0, 0, 0, 0) means that want to use the full screen size window at 0,0.
196 using (Rectangle rec = new Rectangle(0, 0, 0, 0))
198 application = Application.NewApplication(args, stylesheet, windowMode, rec, true);
203 application = Application.NewApplication(args, stylesheet, windowMode);
209 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): add application related events");
210 application.BatteryLow += OnBatteryLow;
211 application.LanguageChanged += OnLanguageChanged;
212 application.MemoryLow += OnMemoryLow;
213 application.RegionChanged += OnRegionChanged;
214 application.DeviceOrientationChanged += OnDeviceOrientationChanged;
216 application.Initialized += OnInitialized;
217 application.Resumed += OnResumed;
218 application.Terminating += OnTerminated;
219 application.Paused += OnPaused;
220 application.AppControl += OnAppControl;
225 if (coreTask != null)
227 application.TaskBatteryLow += OnTaskBatteryLow;
228 application.TaskLanguageChanged += OnTaskLanguageChanged;
229 application.TaskMemoryLow += OnTaskMemoryLow;
230 application.TaskRegionChanged += OnTaskRegionChanged;
231 application.TaskDeviceOrientationChanged += OnTaskDeviceOrientationChanged;
233 application.TaskInitialized += OnTaskInitialized;
234 application.TaskTerminating += OnTaskTerminated;
235 application.TaskAppControl += OnTaskAppControl;
236 // Note: UIEvent, DeviceOrientationChanged are not implemented.
239 application.MainLoop();
240 application.Dispose();
244 /// Sets the core task.
246 /// <param name="task">The core task interface.</param>
247 /// <since_tizen> 10 </since_tizen>
248 public void SetCoreTask(ICoreTask task)
254 /// The Region changed event callback function.
256 /// <param name="source">The application instance.</param>
257 /// <param name="e">The event argument for RegionChanged.</param>
258 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
260 Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
261 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
262 handler?.Invoke(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
266 /// The Memory Low event callback function.
268 /// <param name="source">The application instance.</param>
269 /// <param name="e">The event argument for MemoryLow.</param>
270 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
272 Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
273 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
275 switch (e.MemoryStatus)
277 case Application.MemoryStatus.Normal:
279 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
282 case Application.MemoryStatus.Low:
284 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
287 case Application.MemoryStatus.CriticallyLow:
289 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
296 /// The Language changed event callback function.
298 /// <param name="source">The application instance.</param>
299 /// <param name="e">The event argument for LanguageChanged.</param>
300 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
302 Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
303 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
304 handler?.Invoke(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
308 /// The Battery Low event callback function.
310 /// <param name="source">The application instance.</param>
311 /// <param name="e">The event argument for BatteryLow.</param>
312 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
314 Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
315 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
316 switch (e.BatteryStatus)
318 case Application.BatteryStatus.Normal:
320 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
323 case Application.BatteryStatus.CriticallyLow:
325 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
328 case Application.BatteryStatus.PowerOff:
330 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
337 /// The Device Orientation changed event callback function.
339 /// <param name="source">The application instance.</param>
340 /// <param name="e">The event argument for DeviceOrientationChanged.</param>
341 private void OnDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
343 Log.Info("NUI", "NUICorebackend OnDeviceOrientationChanged Called");
344 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
346 switch (e.DeviceOrientationStatus)
348 case Application.DeviceOrientationStatus.Orientation_0:
350 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
353 case Application.DeviceOrientationStatus.Orientation_90:
355 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
358 case Application.DeviceOrientationStatus.Orientation_180:
360 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
363 case Application.DeviceOrientationStatus.Orientation_270:
365 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
372 /// The Initialized event callback function.
374 /// <param name="source">The application instance.</param>
375 /// <param name="e">The event argument for Initialized.</param>
376 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
378 Tizen.Tracer.Begin("[NUI] OnInitialized()");
380 Log.Info("NUI", "NUICorebackend OnPreCreated Called");
382 Tizen.Tracer.Begin("[NUI] OnInitialized(): OnPreCreated event handler");
383 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
384 preCreateHandler?.Invoke();
387 Log.Info("NUI", "NUICorebackend OnCreate Called");
389 Tizen.Tracer.Begin("[NUI] OnInitialized(): OnCreated event handler");
390 var createHandler = Handlers[EventType.Created] as Action;
391 createHandler?.Invoke();
398 /// The Terminated event callback function.
400 /// <param name="source">The application instance.</param>
401 /// <param name="e">The event argument for Terminated.</param>
402 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
404 Log.Info("NUI", "NUICorebackend OnTerminated Called");
405 var handler = Handlers[EventType.Terminated] as Action;
410 /// The Resumed event callback function.
412 /// <param name="source">The application instance.</param>
413 /// <param name="e">The event argument for Resumed.</param>
414 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
416 Tizen.Tracer.Begin("[NUI] OnResumed()");
418 Log.Info("NUI", "NUICorebackend OnResumed Called");
420 Tizen.Tracer.Begin("[NUI] OnResumed(): OnResumed event handler");
421 var handler = Handlers[EventType.Resumed] as Action;
429 /// The App control event callback function.
431 /// <param name="source">The application instance.</param>
432 /// <param name="e">The event argument for AppControl.</param>
433 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
435 Log.Info("NUI", "NUICorebackend OnAppControl Called");
436 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
437 using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
438 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
442 /// The Paused event callback function.
444 /// <param name="source">The application instance.</param>
445 /// <param name="e">The event argument for Paused.</param>
446 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
448 Log.Info("NUI", "NUICorebackend OnPaused Called");
449 var handler = Handlers[EventType.Paused] as Action;
454 /// The Region changed event callback function. The callback is emitted on the main thread.
456 /// <param name="source">The application instance.</param>
457 /// <param name="e">The event argument for RegionChanged.</param>
458 private void OnTaskRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
460 Log.Info("NUI", "NUICorebackend OnTaskRegionChanged Called");
461 coreTask.OnRegionFormatChanged(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
465 /// The Memory Low event callback function. The callback is emitted on the main thread.
467 /// <param name="source">The application instance.</param>
468 /// <param name="e">The event argument for MemoryLow.</param>
469 private void OnTaskMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
471 Log.Info("NUI", "NUICorebackend OnTaskMemoryLow Called");
472 switch (e.MemoryStatus)
474 case Application.MemoryStatus.Normal:
476 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.None));
479 case Application.MemoryStatus.Low:
481 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
484 case Application.MemoryStatus.CriticallyLow:
486 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
493 /// The Language changed event callback function. The callback is emitted on the main thread.
495 /// <param name="source">The application instance.</param>
496 /// <param name="e">The event argument for LanguageChanged.</param>
497 private void OnTaskLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
499 Log.Info("NUI", "NUICorebackend OnTaskLanguageChanged Called");
500 coreTask.OnLocaleChanged(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
504 /// The Battery Low event callback function. The callback is emitted on the main thread.
506 /// <param name="source">The application instance.</param>
507 /// <param name="e">The event argument for BatteryLow.</param>
508 private void OnTaskBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
510 Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
511 switch (e.BatteryStatus)
513 case Application.BatteryStatus.Normal:
515 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.None));
518 case Application.BatteryStatus.CriticallyLow:
520 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
523 case Application.BatteryStatus.PowerOff:
525 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
532 /// The Orientation Changed event callback function. The callback is emitted on the main thread.
534 /// <param name="source">The application instance.</param>
535 /// <param name="e">The event argument for changing device orientation.</param>
536 private void OnTaskDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
538 Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
539 switch (e.DeviceOrientationStatus)
541 case Application.DeviceOrientationStatus.Orientation_0:
543 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
546 case Application.DeviceOrientationStatus.Orientation_90:
548 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
551 case Application.DeviceOrientationStatus.Orientation_180:
553 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
556 case Application.DeviceOrientationStatus.Orientation_270:
558 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
565 /// The Initialized event callback function. The callback is emitted on the main thread.
567 /// <param name="source">The application instance.</param>
568 /// <param name="e">The event argument for Initialized.</param>
569 private void OnTaskInitialized(object source, NUIApplicationInitEventArgs e)
571 Log.Info("NUI", "NUICorebackend OnTaskInitialized Called");
576 /// The Terminated event callback function. The callback is emitted on the main thread.
578 /// <param name="source">The application instance.</param>
579 /// <param name="e">The event argument for Terminated.</param>
580 private void OnTaskTerminated(object source, NUIApplicationTerminatingEventArgs e)
582 Log.Info("NUI", "NUICorebackend OnTaskTerminated Called");
583 coreTask.OnTerminate();
587 /// The App control event callback function. The callback is emitted on the main thread.
589 /// <param name="source">The application instance.</param>
590 /// <param name="e">The event argument for AppControl.</param>
591 private void OnTaskAppControl(object source, NUIApplicationAppControlEventArgs e)
593 Log.Info("NUI", "NUICorebackend OnTaskAppControl Called");
594 using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
595 coreTask.OnAppControlReceived(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
598 internal Application ApplicationHandle