[NUI] Add IsUsingXaml flag in properties of View and AnimatedVectorImageView
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Application / NUICoreBackend.cs
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.ComponentModel;
20 using System.Collections.Generic;
21 using Tizen.Applications.CoreBackend;
22 using Tizen.Applications;
23 using Tizen.NUI.BaseComponents;
24
25 namespace Tizen.NUI
26 {
27     class NUICoreBackend : ICoreTaskBackend
28     {
29         /// <summary>
30         /// The Application instance to connect event.
31         /// </summary>
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;
39
40         /// <summary>
41         /// The Dictionary to contain each type of event callback.
42         /// </summary>
43         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
44         protected IDictionary<EventType, object> TaskHandlers = new Dictionary<EventType, object>();
45
46         /// <summary>
47         /// The default Constructor.
48         /// </summary>
49         public NUICoreBackend()
50         {
51         }
52
53         /// <summary>
54         /// The constructor with stylesheet.
55         /// </summary>
56         public NUICoreBackend(string stylesheet)
57         {
58             this.stylesheet = stylesheet;
59         }
60
61         /// <summary>
62         /// The constructor with stylesheet and window mode.
63         /// </summary>
64         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
65         {
66             this.stylesheet = stylesheet;
67             this.windowMode = windowMode;
68         }
69
70         /// <summary>
71         /// The constructor with stylesheet, window mode, window size and window position.
72         /// </summary>
73         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
74         {
75             this.stylesheet = stylesheet;
76             this.windowMode = windowMode;
77             if (windowSize != null && windowPosition != null)
78             {
79                 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
80             }
81         }
82
83         /// <summary>
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.
86         /// </summary>
87         [EditorBrowsable(EditorBrowsableState.Never)]
88         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, WindowType type)
89         {
90             this.stylesheet = stylesheet;
91             this.windowMode = windowMode;
92             this.defaultWindowType = type;
93         }
94
95         [EditorBrowsable(EditorBrowsableState.Never)]
96         public NUICoreBackend(WindowData windowData)
97         {
98             //NOTE: windowMode, defaultWindowType, windowRectangle are not used.
99             this.windowData = windowData;
100         }
101
102         /// <summary>
103         /// Adds NUIApplication event to Application.
104         /// Puts each type of event callback in Dictionary.
105         /// </summary>
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)
109         {
110             Handlers.Add(evType, handler);
111         }
112
113         /// <summary>
114         /// Adds NUIApplication event to Application.
115         /// Puts each type of event callback in Dictionary.
116         /// </summary>
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
121         {
122             Handlers.Add(evType, handler);
123         }
124
125         /// <summary>
126         /// The Dispose function.
127         /// </summary>
128         public void Dispose()
129         {
130             if (application != null)
131             {
132                 application.Dispose();
133             }
134             if (windowRectangle != null)
135             {
136                 windowRectangle.Dispose();
137             }
138         }
139
140         /// <summary>
141         /// The Exit application.
142         /// </summary>
143         public void Exit()
144         {
145             if (application != null)
146             {
147                 application.Quit();
148             }
149         }
150
151         /// <summary>
152         /// Ensures that the function passed in is called from the main loop when it is idle.
153         /// </summary>
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)
157         {
158             return application.AddIdle(func);
159         }
160
161         /// <summary>
162         /// The Run application.
163         /// </summary>
164         /// <param name="args">The arguments from commandline.</param>
165         public void Run(string[] args)
166         {
167             Tizen.Tracer.Begin("[NUI] NUICorebackend Run()");
168
169             Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): TizenSynchronizationContext.Initialize() called");
170             TizenSynchronizationContext.Initialize();
171             Tizen.Tracer.End();
172
173             Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): args of main set, window type set");
174             if (Tizen.Applications.Application.Current?.ApplicationInfo != null)
175             {
176                 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
177             }
178             if (string.IsNullOrEmpty(args[0]))
179             {
180                 args[0] = this.GetType().Assembly.FullName.Replace(" ", "");
181             }
182
183             if (windowData != null)
184             {
185                 bool enableUIThread = coreTask != null;
186                 application = Application.NewApplication(args, stylesheet, enableUIThread, windowData);
187             }
188             else if (defaultWindowType != WindowType.Normal)
189             {
190                 application = Application.NewApplication(args, stylesheet, windowMode, defaultWindowType);
191             }
192             else
193             {
194                 if (windowRectangle != null)
195                 {
196                     if (coreTask != null)
197                     {
198                         application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle, true);
199                     }
200                     else
201                     {
202                         application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle);
203                     }
204                 }
205                 else
206                 {
207                     if (coreTask != null)
208                     {
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))
211                         {
212                             application = Application.NewApplication(args, stylesheet, windowMode, rec, true);
213                         }
214                     }
215                     else
216                     {
217                         application = Application.NewApplication(args, stylesheet, windowMode);
218                     }
219                 }
220             }
221             Tizen.Tracer.End();
222
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;
229
230             application.Initialized += OnInitialized;
231             application.Resumed += OnResumed;
232             application.Terminating += OnTerminated;
233             application.Paused += OnPaused;
234             application.AppControl += OnAppControl;
235             Tizen.Tracer.End();
236
237             Tizen.Tracer.End();
238
239             if (coreTask != null)
240             {
241                 application.TaskBatteryLow += OnTaskBatteryLow;
242                 application.TaskLanguageChanged += OnTaskLanguageChanged;
243                 application.TaskMemoryLow += OnTaskMemoryLow;
244                 application.TaskRegionChanged += OnTaskRegionChanged;
245                 application.TaskDeviceOrientationChanged += OnTaskDeviceOrientationChanged;
246
247                 application.TaskInitialized += OnTaskInitialized;
248                 application.TaskTerminating += OnTaskTerminated;
249                 application.TaskAppControl += OnTaskAppControl;
250                 // Note: UIEvent, DeviceOrientationChanged are not implemented.
251             }
252
253             application.MainLoop();
254             application.Dispose();
255         }
256
257         /// <summary>
258         /// Sets the core task.
259         /// </summary>
260         /// <param name="task">The core task interface.</param>
261         /// <since_tizen> 10 </since_tizen>
262         public void SetCoreTask(ICoreTask task)
263         {
264             coreTask = task;
265         }
266
267         /// <summary>
268         /// The Region changed event callback function.
269         /// </summary>
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)
273         {
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()));
277         }
278
279         /// <summary>
280         /// The Memory Low event callback function.
281         /// </summary>
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)
285         {
286             Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
287             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
288
289             switch (e.MemoryStatus)
290             {
291                 case Application.MemoryStatus.Normal:
292                     {
293                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
294                         break;
295                     }
296                 case Application.MemoryStatus.Low:
297                     {
298                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
299                         break;
300                     }
301                 case Application.MemoryStatus.CriticallyLow:
302                     {
303                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
304                         break;
305                     }
306             }
307         }
308
309         /// <summary>
310         /// The Language changed event callback function.
311         /// </summary>
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)
315         {
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()));
319         }
320
321         /// <summary>
322         /// The Battery Low event callback function.
323         /// </summary>
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)
327         {
328             Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
329             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
330             switch (e.BatteryStatus)
331             {
332                 case Application.BatteryStatus.Normal:
333                     {
334                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
335                         break;
336                     }
337                 case Application.BatteryStatus.CriticallyLow:
338                     {
339                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
340                         break;
341                     }
342                 case Application.BatteryStatus.PowerOff:
343                     {
344                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
345                         break;
346                     }
347             }
348         }
349
350         /// <summary>
351         /// The Device Orientation changed event callback function.
352         /// </summary>
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)
356         {
357             Log.Info("NUI", "NUICorebackend OnDeviceOrientationChanged Called");
358             var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
359
360             switch (e.DeviceOrientationStatus)
361             {
362                 case Application.DeviceOrientationStatus.Orientation_0:
363                     {
364                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
365                         break;
366                     }
367                 case Application.DeviceOrientationStatus.Orientation_90:
368                     {
369                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
370                         break;
371                     }
372                 case Application.DeviceOrientationStatus.Orientation_180:
373                     {
374                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
375                         break;
376                     }
377                 case Application.DeviceOrientationStatus.Orientation_270:
378                     {
379                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
380                         break;
381                     }
382             }
383         }
384
385         /// <summary>
386         /// The Initialized event callback function.
387         /// </summary>
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)
391         {
392             Tizen.Tracer.Begin("[NUI] OnInitialized()");
393
394             Log.Info("NUI", $"NUICorebackend OnPreCreated Called IsUsingXaml={NUIApplication.IsUsingXaml}");
395
396 #if REMOVE_READONLY_FOR_BINDABLE_PROPERTY
397             if(NUIApplication.IsUsingXaml)
398             {
399                 View.CreateBindableProperties();
400             }
401 #endif
402             Tizen.Tracer.Begin("[NUI] OnInitialized(): OnPreCreated event handler");
403             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
404             preCreateHandler?.Invoke();
405             Tizen.Tracer.End();
406
407             Log.Info("NUI", "NUICorebackend OnCreate Called");
408
409             Tizen.Tracer.Begin("[NUI] OnInitialized(): OnCreated event handler");
410             var createHandler = Handlers[EventType.Created] as Action;
411             createHandler?.Invoke();
412             Tizen.Tracer.End();
413
414             Tizen.Tracer.End();
415         }
416
417         /// <summary>
418         /// The Terminated event callback function.
419         /// </summary>
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)
423         {
424             Log.Info("NUI", "NUICorebackend OnTerminated Called");
425             var handler = Handlers[EventType.Terminated] as Action;
426             handler?.Invoke();
427         }
428
429         /// <summary>
430         /// The Resumed event callback function.
431         /// </summary>
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)
435         {
436             Tizen.Tracer.Begin("[NUI] OnResumed()");
437
438             Log.Info("NUI", "NUICorebackend OnResumed Called");
439
440             Tizen.Tracer.Begin("[NUI] OnResumed(): OnResumed event handler");
441             var handler = Handlers[EventType.Resumed] as Action;
442             handler?.Invoke();
443             Tizen.Tracer.End();
444
445             Tizen.Tracer.End();
446         }
447
448         /// <summary>
449         /// The App control event callback function.
450         /// </summary>
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)
454         {
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)));
459         }
460
461         /// <summary>
462         /// The Paused event callback function.
463         /// </summary>
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)
467         {
468             Log.Info("NUI", "NUICorebackend OnPaused Called");
469             var handler = Handlers[EventType.Paused] as Action;
470             handler?.Invoke();
471         }
472
473         /// <summary>
474         /// The Region changed event callback function. The callback is emitted on the main thread.
475         /// </summary>
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)
479         {
480             Log.Info("NUI", "NUICorebackend OnTaskRegionChanged Called");
481             coreTask.OnRegionFormatChanged(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
482         }
483
484         /// <summary>
485         /// The Memory Low event callback function. The callback is emitted on the main thread.
486         /// </summary>
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)
490         {
491             Log.Info("NUI", "NUICorebackend OnTaskMemoryLow Called");
492             switch (e.MemoryStatus)
493             {
494                 case Application.MemoryStatus.Normal:
495                     {
496                         coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.None));
497                         break;
498                     }
499                 case Application.MemoryStatus.Low:
500                     {
501                         coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
502                         break;
503                     }
504                 case Application.MemoryStatus.CriticallyLow:
505                     {
506                         coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
507                         break;
508                     }
509             }
510         }
511
512         /// <summary>
513         /// The Language changed event callback function. The callback is emitted on the main thread.
514         /// </summary>
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)
518         {
519             Log.Info("NUI", "NUICorebackend OnTaskLanguageChanged Called");
520             coreTask.OnLocaleChanged(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
521         }
522
523         /// <summary>
524         /// The Battery Low event callback function. The callback is emitted on the main thread.
525         /// </summary>
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)
529         {
530             Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
531             switch (e.BatteryStatus)
532             {
533                 case Application.BatteryStatus.Normal:
534                     {
535                         coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.None));
536                         break;
537                     }
538                 case Application.BatteryStatus.CriticallyLow:
539                     {
540                         coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
541                         break;
542                     }
543                 case Application.BatteryStatus.PowerOff:
544                     {
545                         coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
546                         break;
547                     }
548             }
549         }
550
551         /// <summary>
552         /// The Orientation Changed event callback function. The callback is emitted on the main thread.
553         /// </summary>
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)
557         {
558             Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
559             switch (e.DeviceOrientationStatus)
560             {
561                 case Application.DeviceOrientationStatus.Orientation_0:
562                     {
563                         coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
564                         break;
565                     }
566                 case Application.DeviceOrientationStatus.Orientation_90:
567                     {
568                         coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
569                         break;
570                     }
571                 case Application.DeviceOrientationStatus.Orientation_180:
572                     {
573                         coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
574                         break;
575                     }
576                 case Application.DeviceOrientationStatus.Orientation_270:
577                     {
578                         coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
579                         break;
580                     }
581             }
582         }
583
584         /// <summary>
585         /// The Initialized event callback function. The callback is emitted on the main thread.
586         /// </summary>
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)
590         {
591             Log.Info("NUI", "NUICorebackend OnTaskInitialized Called");
592             coreTask.OnCreate();
593         }
594
595         /// <summary>
596         /// The Terminated event callback function. The callback is emitted on the main thread.
597         /// </summary>
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)
601         {
602             Log.Info("NUI", "NUICorebackend OnTaskTerminated Called");
603             coreTask.OnTerminate();
604         }
605
606         /// <summary>
607         /// The App control event callback function. The callback is emitted on the main thread.
608         /// </summary>
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)
612         {
613             Log.Info("NUI", "NUICorebackend OnTaskAppControl Called");
614             using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
615             coreTask.OnAppControlReceived(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
616         }
617
618         internal Application ApplicationHandle
619         {
620             get
621             {
622                 return application;
623             }
624         }
625     }
626 }