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;
37 private WindowData windowData = null;
40 /// The Dictionary to contain each type of event callback.
42 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
43 protected IDictionary<EventType, object> TaskHandlers = new Dictionary<EventType, object>();
46 /// The default Constructor.
48 public NUICoreBackend()
53 /// The constructor with stylesheet.
55 public NUICoreBackend(string stylesheet)
57 this.stylesheet = stylesheet;
61 /// The constructor with stylesheet and window mode.
63 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
65 this.stylesheet = stylesheet;
66 this.windowMode = windowMode;
70 /// The constructor with stylesheet, window mode, window size and window position.
72 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
74 this.stylesheet = stylesheet;
75 this.windowMode = windowMode;
76 if (windowSize != null && windowPosition != null)
78 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
83 /// The constructor with stylesheet, window mode, window size, window position and default window type.
84 /// This will be hidden as inhouse API. Because it is only for internal IME window.
86 [EditorBrowsable(EditorBrowsableState.Never)]
87 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, WindowType type)
89 this.stylesheet = stylesheet;
90 this.windowMode = windowMode;
91 this.defaultWindowType = type;
94 [EditorBrowsable(EditorBrowsableState.Never)]
95 public NUICoreBackend(WindowData windowData)
97 //NOTE: windowMode, defaultWindowType, windowRectangle are not used.
98 this.windowData = windowData;
102 /// Adds NUIApplication event to Application.
103 /// Puts each type of event callback in Dictionary.
105 /// <param name="evType">The type of event.</param>
106 /// <param name="handler">The event callback.</param>
107 public void AddEventHandler(EventType evType, Action handler)
109 Handlers.Add(evType, handler);
113 /// Adds NUIApplication event to Application.
114 /// Puts each type of event callback in Dictionary.
116 /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
117 /// <param name="evType">The type of event.</param>
118 /// <param name="handler">The event callback.</param>
119 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
121 Handlers.Add(evType, handler);
125 /// The Dispose function.
127 public void Dispose()
129 if (application != null)
131 application.Dispose();
133 if (windowRectangle != null)
135 windowRectangle.Dispose();
140 /// The Exit application.
144 if (application != null)
151 /// Ensures that the function passed in is called from the main loop when it is idle.
153 /// <param name="func">The function to call</param>
154 /// <returns>true if added successfully, false otherwise</returns>
155 public bool AddIdle(System.Delegate func)
157 return application.AddIdle(func);
161 /// The Run application.
163 /// <param name="args">The arguments from commandline.</param>
164 public void Run(string[] args)
166 Tizen.Tracer.Begin("[NUI] NUICorebackend Run()");
168 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): TizenSynchronizationContext.Initialize() called");
169 TizenSynchronizationContext.Initialize();
172 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): args of main set, window type set");
173 if (Tizen.Applications.Application.Current?.ApplicationInfo != null)
175 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
177 if (string.IsNullOrEmpty(args[0]))
179 args[0] = this.GetType().Assembly.FullName.Replace(" ", "");
182 if (windowData != null)
184 bool enableUIThread = coreTask != null;
185 application = Application.NewApplication(args, stylesheet, enableUIThread, windowData);
187 else if (defaultWindowType != WindowType.Normal)
189 application = Application.NewApplication(args, stylesheet, windowMode, defaultWindowType);
193 if (windowRectangle != null)
195 if (coreTask != null)
197 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle, true);
201 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle);
206 if (coreTask != null)
208 // The Rectangle(0, 0, 0, 0) means that want to use the full screen size window at 0,0.
209 using (Rectangle rec = new Rectangle(0, 0, 0, 0))
211 application = Application.NewApplication(args, stylesheet, windowMode, rec, true);
216 application = Application.NewApplication(args, stylesheet, windowMode);
222 Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): add application related events");
223 application.BatteryLow += OnBatteryLow;
224 application.LanguageChanged += OnLanguageChanged;
225 application.MemoryLow += OnMemoryLow;
226 application.RegionChanged += OnRegionChanged;
227 application.DeviceOrientationChanged += OnDeviceOrientationChanged;
229 application.Initialized += OnInitialized;
230 application.Resumed += OnResumed;
231 application.Terminating += OnTerminated;
232 application.Paused += OnPaused;
233 application.AppControl += OnAppControl;
238 if (coreTask != null)
240 application.TaskBatteryLow += OnTaskBatteryLow;
241 application.TaskLanguageChanged += OnTaskLanguageChanged;
242 application.TaskMemoryLow += OnTaskMemoryLow;
243 application.TaskRegionChanged += OnTaskRegionChanged;
244 application.TaskDeviceOrientationChanged += OnTaskDeviceOrientationChanged;
246 application.TaskInitialized += OnTaskInitialized;
247 application.TaskTerminating += OnTaskTerminated;
248 application.TaskAppControl += OnTaskAppControl;
249 // Note: UIEvent, DeviceOrientationChanged are not implemented.
252 application.MainLoop();
253 application.Dispose();
257 /// Sets the core task.
259 /// <param name="task">The core task interface.</param>
260 /// <since_tizen> 10 </since_tizen>
261 public void SetCoreTask(ICoreTask task)
267 /// The Region changed event callback function.
269 /// <param name="source">The application instance.</param>
270 /// <param name="e">The event argument for RegionChanged.</param>
271 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
273 Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
274 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
275 handler?.Invoke(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
279 /// The Memory Low event callback function.
281 /// <param name="source">The application instance.</param>
282 /// <param name="e">The event argument for MemoryLow.</param>
283 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
285 Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
286 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
288 switch (e.MemoryStatus)
290 case Application.MemoryStatus.Normal:
292 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
295 case Application.MemoryStatus.Low:
297 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
300 case Application.MemoryStatus.CriticallyLow:
302 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
309 /// The Language changed event callback function.
311 /// <param name="source">The application instance.</param>
312 /// <param name="e">The event argument for LanguageChanged.</param>
313 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
315 Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
316 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
317 handler?.Invoke(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
321 /// The Battery Low event callback function.
323 /// <param name="source">The application instance.</param>
324 /// <param name="e">The event argument for BatteryLow.</param>
325 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
327 Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
328 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
329 switch (e.BatteryStatus)
331 case Application.BatteryStatus.Normal:
333 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
336 case Application.BatteryStatus.CriticallyLow:
338 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
341 case Application.BatteryStatus.PowerOff:
343 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
350 /// The Device Orientation changed event callback function.
352 /// <param name="source">The application instance.</param>
353 /// <param name="e">The event argument for DeviceOrientationChanged.</param>
354 private void OnDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
356 Log.Info("NUI", "NUICorebackend OnDeviceOrientationChanged Called");
357 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
359 switch (e.DeviceOrientationStatus)
361 case Application.DeviceOrientationStatus.Orientation_0:
363 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
366 case Application.DeviceOrientationStatus.Orientation_90:
368 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
371 case Application.DeviceOrientationStatus.Orientation_180:
373 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
376 case Application.DeviceOrientationStatus.Orientation_270:
378 handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
385 /// The Initialized event callback function.
387 /// <param name="source">The application instance.</param>
388 /// <param name="e">The event argument for Initialized.</param>
389 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
391 Tizen.Tracer.Begin("[NUI] OnInitialized()");
393 Log.Info("NUI", "NUICorebackend OnPreCreated Called");
395 Tizen.Tracer.Begin("[NUI] OnInitialized(): OnPreCreated event handler");
396 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
397 preCreateHandler?.Invoke();
400 Log.Info("NUI", "NUICorebackend OnCreate Called");
402 Tizen.Tracer.Begin("[NUI] OnInitialized(): OnCreated event handler");
403 var createHandler = Handlers[EventType.Created] as Action;
404 createHandler?.Invoke();
411 /// The Terminated event callback function.
413 /// <param name="source">The application instance.</param>
414 /// <param name="e">The event argument for Terminated.</param>
415 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
417 Log.Info("NUI", "NUICorebackend OnTerminated Called");
418 var handler = Handlers[EventType.Terminated] as Action;
423 /// The Resumed event callback function.
425 /// <param name="source">The application instance.</param>
426 /// <param name="e">The event argument for Resumed.</param>
427 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
429 Tizen.Tracer.Begin("[NUI] OnResumed()");
431 Log.Info("NUI", "NUICorebackend OnResumed Called");
433 Tizen.Tracer.Begin("[NUI] OnResumed(): OnResumed event handler");
434 var handler = Handlers[EventType.Resumed] as Action;
442 /// The App control event callback function.
444 /// <param name="source">The application instance.</param>
445 /// <param name="e">The event argument for AppControl.</param>
446 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
448 Log.Info("NUI", "NUICorebackend OnAppControl Called");
449 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
450 using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
451 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
455 /// The Paused event callback function.
457 /// <param name="source">The application instance.</param>
458 /// <param name="e">The event argument for Paused.</param>
459 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
461 Log.Info("NUI", "NUICorebackend OnPaused Called");
462 var handler = Handlers[EventType.Paused] as Action;
467 /// The Region changed event callback function. The callback is emitted on the main thread.
469 /// <param name="source">The application instance.</param>
470 /// <param name="e">The event argument for RegionChanged.</param>
471 private void OnTaskRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
473 Log.Info("NUI", "NUICorebackend OnTaskRegionChanged Called");
474 coreTask.OnRegionFormatChanged(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
478 /// The Memory Low event callback function. The callback is emitted on the main thread.
480 /// <param name="source">The application instance.</param>
481 /// <param name="e">The event argument for MemoryLow.</param>
482 private void OnTaskMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
484 Log.Info("NUI", "NUICorebackend OnTaskMemoryLow Called");
485 switch (e.MemoryStatus)
487 case Application.MemoryStatus.Normal:
489 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.None));
492 case Application.MemoryStatus.Low:
494 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
497 case Application.MemoryStatus.CriticallyLow:
499 coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
506 /// The Language changed event callback function. The callback is emitted on the main thread.
508 /// <param name="source">The application instance.</param>
509 /// <param name="e">The event argument for LanguageChanged.</param>
510 private void OnTaskLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
512 Log.Info("NUI", "NUICorebackend OnTaskLanguageChanged Called");
513 coreTask.OnLocaleChanged(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
517 /// The Battery Low event callback function. The callback is emitted on the main thread.
519 /// <param name="source">The application instance.</param>
520 /// <param name="e">The event argument for BatteryLow.</param>
521 private void OnTaskBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
523 Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
524 switch (e.BatteryStatus)
526 case Application.BatteryStatus.Normal:
528 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.None));
531 case Application.BatteryStatus.CriticallyLow:
533 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
536 case Application.BatteryStatus.PowerOff:
538 coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
545 /// The Orientation Changed event callback function. The callback is emitted on the main thread.
547 /// <param name="source">The application instance.</param>
548 /// <param name="e">The event argument for changing device orientation.</param>
549 private void OnTaskDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
551 Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
552 switch (e.DeviceOrientationStatus)
554 case Application.DeviceOrientationStatus.Orientation_0:
556 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
559 case Application.DeviceOrientationStatus.Orientation_90:
561 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
564 case Application.DeviceOrientationStatus.Orientation_180:
566 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
569 case Application.DeviceOrientationStatus.Orientation_270:
571 coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
578 /// The Initialized event callback function. The callback is emitted on the main thread.
580 /// <param name="source">The application instance.</param>
581 /// <param name="e">The event argument for Initialized.</param>
582 private void OnTaskInitialized(object source, NUIApplicationInitEventArgs e)
584 Log.Info("NUI", "NUICorebackend OnTaskInitialized Called");
589 /// The Terminated event callback function. The callback is emitted on the main thread.
591 /// <param name="source">The application instance.</param>
592 /// <param name="e">The event argument for Terminated.</param>
593 private void OnTaskTerminated(object source, NUIApplicationTerminatingEventArgs e)
595 Log.Info("NUI", "NUICorebackend OnTaskTerminated Called");
596 coreTask.OnTerminate();
600 /// The App control event callback function. The callback is emitted on the main thread.
602 /// <param name="source">The application instance.</param>
603 /// <param name="e">The event argument for AppControl.</param>
604 private void OnTaskAppControl(object source, NUIApplicationAppControlEventArgs e)
606 Log.Info("NUI", "NUICorebackend OnTaskAppControl Called");
607 using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
608 coreTask.OnAppControlReceived(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
611 internal Application ApplicationHandle