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;
23 using Tizen.NUI.BaseComponents;
27 class NUICoreBackend : ICoreTaskBackend
30 /// The Application instance to connect event.
32 protected Application application;
33 private string stylesheet = "";
34 private NUIApplication.WindowMode windowMode = NUIApplication.WindowMode.Opaque;
35 private Rectangle windowRectangle = null;
36 private WindowType defaultWindowType = WindowType.Normal;
37 private ICoreTask coreTask;
38 private WindowData windowData = null;
41 /// The Dictionary to contain each type of event callback.
43 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
44 protected IDictionary<EventType, object> TaskHandlers = new Dictionary<EventType, object>();
47 /// The default Constructor.
49 public NUICoreBackend()
54 /// The constructor with stylesheet.
56 public NUICoreBackend(string stylesheet)
58 this.stylesheet = stylesheet;
62 /// The constructor with stylesheet and window mode.
64 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
66 this.stylesheet = stylesheet;
67 this.windowMode = windowMode;
71 /// The constructor with stylesheet, window mode, window size and window position.
73 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
75 this.stylesheet = stylesheet;
76 this.windowMode = windowMode;
77 if (windowSize != null && windowPosition != null)
79 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
84 /// The constructor with stylesheet, window mode, window size, window position and default window type.
85 /// This will be hidden as inhouse API. Because it is only for internal IME window.
87 [EditorBrowsable(EditorBrowsableState.Never)]
88 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, WindowType type)
90 this.stylesheet = stylesheet;
91 this.windowMode = windowMode;
92 this.defaultWindowType = type;
95 [EditorBrowsable(EditorBrowsableState.Never)]
96 public NUICoreBackend(WindowData windowData)
98 //NOTE: windowMode, defaultWindowType, windowRectangle are not used.
99 this.windowData = windowData;
103 /// Adds NUIApplication event to Application.
104 /// Puts each type of event callback in Dictionary.
106 /// <param name="evType">The type of event.</param>
107 /// <param name="handler">The event callback.</param>
108 public void AddEventHandler(EventType evType, Action handler)
110 Handlers.Add(evType, handler);
114 /// Adds NUIApplication event to Application.
115 /// Puts each type of event callback in Dictionary.
117 /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
118 /// <param name="evType">The type of event.</param>
119 /// <param name="handler">The event callback.</param>
120 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
122 Handlers.Add(evType, handler);
126 /// The Dispose function.
128 public void Dispose()
130 if (application != null)
132 application.Dispose();
134 if (windowRectangle != null)
136 windowRectangle.Dispose();
141 /// The Exit application.
145 if (application != null)
152 /// Ensures that the function passed in is called from the main loop when it is idle.
154 /// <param name="func">The function to call</param>
155 /// <returns>true if added successfully, false otherwise</returns>
156 public bool AddIdle(System.Delegate func)
158 return application.AddIdle(func);
162 /// The Run application.
164 /// <param name="args">The arguments from commandline.</param>
165 public void Run(string[] args)
167 Tizen.Tracer.Begin("[NUI] NUICorebackend Run()");
169 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): TizenSynchronizationContext.Initialize() called");
170 TizenSynchronizationContext.Initialize();
173 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): args of main set, window type set");
174 if (Tizen.Applications.Application.Current?.ApplicationInfo != null)
176 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
178 if (string.IsNullOrEmpty(args[0]))
180 args[0] = this.GetType().Assembly.FullName.Replace(" ", "");
183 if (windowData != null)
185 bool enableUIThread = coreTask != null;
186 application = Application.NewApplication(args, stylesheet, enableUIThread, windowData);
188 else if (defaultWindowType != WindowType.Normal)
190 application = Application.NewApplication(args, stylesheet, windowMode, defaultWindowType);
194 if (windowRectangle != null)
196 if (coreTask != null)
198 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle, true);
202 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle);
207 if (coreTask != null)
209 // The Rectangle(0, 0, 0, 0) means that want to use the full screen size window at 0,0.
210 using (Rectangle rec = new Rectangle(0, 0, 0, 0))
212 application = Application.NewApplication(args, stylesheet, windowMode, rec, true);
217 application = Application.NewApplication(args, stylesheet, windowMode);
223 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): add application related events");
224 application.BatteryLow += OnBatteryLow;
225 application.LanguageChanged += OnLanguageChanged;
226 application.MemoryLow += OnMemoryLow;
227 application.RegionChanged += OnRegionChanged;
228 application.DeviceOrientationChanged += OnDeviceOrientationChanged;
230 application.Initialized += OnInitialized;
231 application.Resumed += OnResumed;
232 application.Terminating += OnTerminated;
233 application.Paused += OnPaused;
234 application.AppControl += OnAppControl;
239 if (coreTask != null)
241 application.TaskBatteryLow += OnTaskBatteryLow;
242 application.TaskLanguageChanged += OnTaskLanguageChanged;
243 application.TaskMemoryLow += OnTaskMemoryLow;
244 application.TaskRegionChanged += OnTaskRegionChanged;
245 application.TaskDeviceOrientationChanged += OnTaskDeviceOrientationChanged;
247 application.TaskInitialized += OnTaskInitialized;
248 application.TaskTerminating += OnTaskTerminated;
249 application.TaskAppControl += OnTaskAppControl;
250 // Note: UIEvent, DeviceOrientationChanged are not implemented.
253 application.MainLoop();
254 application.Dispose();
258 /// Sets the core task.
260 /// <param name="task">The core task interface.</param>
261 /// <since_tizen> 10 </since_tizen>
262 public void SetCoreTask(ICoreTask task)
268 /// The Region changed event callback function.
270 /// <param name="source">The application instance.</param>
271 /// <param name="e">The event argument for RegionChanged.</param>
272 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
274 Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
275 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
276 handler?.Invoke(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
280 /// The Memory Low event callback function.
282 /// <param name="source">The application instance.</param>
283 /// <param name="e">The event argument for MemoryLow.</param>
284 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
286 Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
287 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
289 switch (e.MemoryStatus)
291 case Application.MemoryStatus.Normal:
293 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
296 case Application.MemoryStatus.Low:
298 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
301 case Application.MemoryStatus.CriticallyLow:
303 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
310 /// The Language changed event callback function.
312 /// <param name="source">The application instance.</param>
313 /// <param name="e">The event argument for LanguageChanged.</param>
314 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
316 Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
317 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
318 handler?.Invoke(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
322 /// The Battery Low event callback function.
324 /// <param name="source">The application instance.</param>
325 /// <param name="e">The event argument for BatteryLow.</param>
326 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
328 Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
329 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
330 switch (e.BatteryStatus)
332 case Application.BatteryStatus.Normal:
334 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
337 case Application.BatteryStatus.CriticallyLow:
339 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
342 case Application.BatteryStatus.PowerOff:
344 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
351 /// The Device Orientation changed event callback function.
353 /// <param name="source">The application instance.</param>
354 /// <param name="e">The event argument for DeviceOrientationChanged.</param>
355 private void OnDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
357 Log.Info("NUI", "NUICorebackend OnDeviceOrientationChanged Called");
358 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
360 switch (e.DeviceOrientationStatus)
362 case Application.DeviceOrientationStatus.Orientation_0:
364 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
367 case Application.DeviceOrientationStatus.Orientation_90:
369 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
372 case Application.DeviceOrientationStatus.Orientation_180:
374 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
377 case Application.DeviceOrientationStatus.Orientation_270:
379 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
386 /// The Initialized event callback function.
388 /// <param name="source">The application instance.</param>
389 /// <param name="e">The event argument for Initialized.</param>
390 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
392 Tizen.Tracer.Begin("[NUI] OnInitialized()");
394 Log.Info("NUI", $"NUICorebackend OnPreCreated Called IsUsingXaml={NUIApplication.IsUsingXaml}");
396 #if REMOVE_READONLY_FOR_BINDABLE_PROPERTY
397 if(NUIApplication.IsUsingXaml)
399 View.CreateBindableProperties();
402 Tizen.Tracer.Begin("[NUI] OnInitialized(): OnPreCreated event handler");
403 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
404 preCreateHandler?.Invoke();
407 Log.Info("NUI", "NUICorebackend OnCreate Called");
409 Tizen.Tracer.Begin("[NUI] OnInitialized(): OnCreated event handler");
410 var createHandler = Handlers[EventType.Created] as Action;
411 createHandler?.Invoke();
418 /// The Terminated event callback function.
420 /// <param name="source">The application instance.</param>
421 /// <param name="e">The event argument for Terminated.</param>
422 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
424 Log.Info("NUI", "NUICorebackend OnTerminated Called");
425 var handler = Handlers[EventType.Terminated] as Action;
430 /// The Resumed event callback function.
432 /// <param name="source">The application instance.</param>
433 /// <param name="e">The event argument for Resumed.</param>
434 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
436 Tizen.Tracer.Begin("[NUI] OnResumed()");
438 Log.Info("NUI", "NUICorebackend OnResumed Called");
440 Tizen.Tracer.Begin("[NUI] OnResumed(): OnResumed event handler");
441 var handler = Handlers[EventType.Resumed] as Action;
449 /// The App control event callback function.
451 /// <param name="source">The application instance.</param>
452 /// <param name="e">The event argument for AppControl.</param>
453 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
455 Log.Info("NUI", "NUICorebackend OnAppControl Called");
456 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
457 using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
458 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
462 /// The Paused event callback function.
464 /// <param name="source">The application instance.</param>
465 /// <param name="e">The event argument for Paused.</param>
466 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
468 Log.Info("NUI", "NUICorebackend OnPaused Called");
469 var handler = Handlers[EventType.Paused] as Action;
474 /// The Region changed event callback function. The callback is emitted on the main thread.
476 /// <param name="source">The application instance.</param>
477 /// <param name="e">The event argument for RegionChanged.</param>
478 private void OnTaskRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
480 Log.Info("NUI", "NUICorebackend OnTaskRegionChanged Called");
481 coreTask.OnRegionFormatChanged(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
485 /// The Memory Low event callback function. The callback is emitted on the main thread.
487 /// <param name="source">The application instance.</param>
488 /// <param name="e">The event argument for MemoryLow.</param>
489 private void OnTaskMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
491 Log.Info("NUI", "NUICorebackend OnTaskMemoryLow Called");
492 switch (e.MemoryStatus)
494 case Application.MemoryStatus.Normal:
496 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.None));
499 case Application.MemoryStatus.Low:
501 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
504 case Application.MemoryStatus.CriticallyLow:
506 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
513 /// The Language changed event callback function. The callback is emitted on the main thread.
515 /// <param name="source">The application instance.</param>
516 /// <param name="e">The event argument for LanguageChanged.</param>
517 private void OnTaskLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
519 Log.Info("NUI", "NUICorebackend OnTaskLanguageChanged Called");
520 coreTask.OnLocaleChanged(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
524 /// The Battery Low event callback function. The callback is emitted on the main thread.
526 /// <param name="source">The application instance.</param>
527 /// <param name="e">The event argument for BatteryLow.</param>
528 private void OnTaskBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
530 Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
531 switch (e.BatteryStatus)
533 case Application.BatteryStatus.Normal:
535 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.None));
538 case Application.BatteryStatus.CriticallyLow:
540 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
543 case Application.BatteryStatus.PowerOff:
545 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
552 /// The Orientation Changed event callback function. The callback is emitted on the main thread.
554 /// <param name="source">The application instance.</param>
555 /// <param name="e">The event argument for changing device orientation.</param>
556 private void OnTaskDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
558 Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
559 switch (e.DeviceOrientationStatus)
561 case Application.DeviceOrientationStatus.Orientation_0:
563 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
566 case Application.DeviceOrientationStatus.Orientation_90:
568 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
571 case Application.DeviceOrientationStatus.Orientation_180:
573 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
576 case Application.DeviceOrientationStatus.Orientation_270:
578 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
585 /// The Initialized event callback function. The callback is emitted on the main thread.
587 /// <param name="source">The application instance.</param>
588 /// <param name="e">The event argument for Initialized.</param>
589 private void OnTaskInitialized(object source, NUIApplicationInitEventArgs e)
591 Log.Info("NUI", "NUICorebackend OnTaskInitialized Called");
596 /// The Terminated event callback function. The callback is emitted on the main thread.
598 /// <param name="source">The application instance.</param>
599 /// <param name="e">The event argument for Terminated.</param>
600 private void OnTaskTerminated(object source, NUIApplicationTerminatingEventArgs e)
602 Log.Info("NUI", "NUICorebackend OnTaskTerminated Called");
603 coreTask.OnTerminate();
607 /// The App control event callback function. The callback is emitted on the main thread.
609 /// <param name="source">The application instance.</param>
610 /// <param name="e">The event argument for AppControl.</param>
611 private void OnTaskAppControl(object source, NUIApplicationAppControlEventArgs e)
613 Log.Info("NUI", "NUICorebackend OnTaskAppControl Called");
614 using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
615 coreTask.OnAppControlReceived(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
618 internal Application ApplicationHandle