[NUI] Add Tizen.NUI.Components.preload (#2663)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Application / NUIApplication.cs
1 /*
2  * Copyright (c) 2019 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.Diagnostics.CodeAnalysis;
21 using System.Threading;
22 using System.Reflection;
23 using Tizen.Applications;
24 using Tizen.Applications.CoreBackend;
25 using Tizen.NUI.Binding;
26 using Tizen.NUI.Xaml;
27 using System.Collections.Generic;
28
29 namespace Tizen.NUI
30 {
31
32     /// <summary>
33     /// Represents an application that have a UI screen. The NUIApplication class has a default stage.
34     /// </summary>
35     /// <since_tizen> 3 </since_tizen>
36     public class NUIApplication : CoreApplication
37     {
38         /// <summary>
39         /// The instance of ResourceManager.
40         /// </summary>
41         private static System.Resources.ResourceManager resourceManager = null;
42         private Size2D _windowSize2D = null;
43         private Position2D _windowPosition2D = null;
44         private TransitionOptions transitionOptions;
45         private string appId = null;
46         internal static NUIApplication me;
47
48         private static bool isPreload = false;
49
50         // TODO Enable this after tizen-theme-manager is released.
51         // private readonly ThemeLoader themeLoader = new ThemeLoader();
52
53         /// <summary>
54         /// The default constructor.
55         /// </summary>
56         /// <since_tizen> 3 </since_tizen>
57         public NUIApplication() : base(new NUICoreBackend())
58         {
59             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
60             me = this;
61
62             // TODO Enable this after tizen-theme-manager is released.
63             // themeLoader.ThemeChanged += TizenThemeChanged;
64         }
65
66         /// <summary>
67         /// The constructor with window size and position.
68         /// </summary>
69         /// <param name="windowSize">The window size.</param>
70         /// <param name="windowPosition">The window position.</param>
71         /// <since_tizen> 5 </since_tizen>
72         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
73         [EditorBrowsable(EditorBrowsableState.Never)]
74         public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", WindowMode.Opaque, windowSize, windowPosition))
75         {
76             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
77             // TODO Enable this after tizen-theme-manager is released.
78             // themeLoader.ThemeChanged += TizenThemeChanged;
79             _windowSize2D = windowSize;
80             _windowPosition2D = windowPosition;
81             me = this;
82         }
83
84         /// <summary>
85         /// The constructor with a stylesheet.
86         /// </summary>
87         /// <param name="styleSheet">The styleSheet url.</param>
88         /// <since_tizen> 3 </since_tizen>
89         public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
90         {
91             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
92             me = this;
93             // TODO Enable this after tizen-theme-manager is released.
94             // themeLoader.ThemeChanged += TizenThemeChanged;
95         }
96
97         /// <summary>
98         /// The constructor with a stylesheet, window size, and position.
99         /// </summary>
100         /// <param name="styleSheet">The styleSheet URL.</param>
101         /// <param name="windowSize">The window size.</param>
102         /// <param name="windowPosition">The window position.</param>
103         /// <since_tizen> 5 </since_tizen>
104         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
105         [EditorBrowsable(EditorBrowsableState.Never)]
106         public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
107         {
108             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
109             // TODO Enable this after tizen-theme-manager is released.
110             // themeLoader.ThemeChanged += TizenThemeChanged;
111             _windowSize2D = windowSize;
112             _windowPosition2D = windowPosition;
113             me = this;
114         }
115
116         /// <summary>
117         /// The constructor with a stylesheet and window mode.
118         /// </summary>
119         /// <param name="styleSheet">The styleSheet url.</param>
120         /// <param name="windowMode">The windowMode.</param>
121         /// <since_tizen> 3 </since_tizen>
122         public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
123         {
124             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
125             me = this;
126             // TODO Enable this after tizen-theme-manager is released.
127             // themeLoader.ThemeChanged += TizenThemeChanged;
128         }
129
130         /// <summary>
131         /// The constructor with a stylesheet, window mode, window size, and position.
132         /// </summary>
133         /// <param name="styleSheet">The styleSheet URL.</param>
134         /// <param name="windowMode">The windowMode.</param>
135         /// <param name="windowSize">The window size.</param>
136         /// <param name="windowPosition">The window position.</param>
137         /// <since_tizen> 5 </since_tizen>
138         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
139         [EditorBrowsable(EditorBrowsableState.Never)]
140         public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
141         {
142             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
143             // TODO Enable this after tizen-theme-manager is released.
144             // themeLoader.ThemeChanged += TizenThemeChanged;
145             _windowSize2D = windowSize;
146             _windowPosition2D = windowPosition;
147             me = this;
148         }
149
150         /// <summary>
151         /// Internal inhouse constructor with Graphics Backend Type
152         /// </summary>
153         /// <param name="backend"></param>
154         /// <param name="windowMode"></param>
155         /// <param name="windowSize"></param>
156         /// <param name="windowPosition"></param>
157         /// <param name="styleSheet"></param>
158         /// InhouseAPI, this could be opend in NextTizen
159         [Obsolete("Please do not use! This will be deprecated!")]
160         [EditorBrowsable(EditorBrowsableState.Never)]
161         public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
162         {
163             //windowMode and styleSheet will be added later. currenlty it's not working as expected.
164             Graphics.Backend = backend;
165             Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
166
167             if (windowSize != null) { _windowSize2D = windowSize; }
168             if (windowPosition != null) { _windowPosition2D = windowPosition; }
169             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
170             me = this;
171             // TODO Enable this after tizen-theme-manager is released.
172             // themeLoader.ThemeChanged += TizenThemeChanged;
173         }
174
175         /// <summary>
176         /// Occurs whenever the application is resumed.
177         /// </summary>
178         /// <since_tizen> 4 </since_tizen>
179         public event EventHandler Resumed;
180
181         /// <summary>
182         /// Occurs whenever the application is paused.
183         /// </summary>
184         /// <since_tizen> 4 </since_tizen>
185         public event EventHandler Paused;
186
187         /// <summary>
188         /// Enumeration for deciding whether a NUI application window is opaque or transparent.
189         /// </summary>
190         /// <since_tizen> 3 </since_tizen>
191         public enum WindowMode
192         {
193             /// <summary>
194             /// Opaque
195             /// </summary>
196             /// <since_tizen> 3 </since_tizen>
197             Opaque = 0,
198             /// <summary>
199             /// Transparent
200             /// </summary>
201             /// <since_tizen> 3 </since_tizen>
202             Transparent = 1
203         }
204
205         /// <summary>
206         /// ResourceManager to handle multilingual.
207         /// </summary>
208         /// <since_tizen> 4 </since_tizen>
209         public static System.Resources.ResourceManager MultilingualResourceManager
210         {
211             get
212             {
213                 return resourceManager;
214             }
215             set
216             {
217                 resourceManager = value;
218             }
219         }
220
221         /// <summary>
222         /// Gets the window instance.
223         /// </summary>
224         /// <since_tizen> 3 </since_tizen>
225         [Obsolete("Please do not use! This will be deprecated!")]
226         [EditorBrowsable(EditorBrowsableState.Never)]
227         public Window Window
228         {
229             get
230             {
231                 return GetDefaultWindow();
232             }
233         }
234
235         /// <summary>
236         /// Gets the Application Id.
237         /// </summary>
238         /// <since_tizen> 6 </since_tizen>
239         [EditorBrowsable(EditorBrowsableState.Never)]
240         public string AppId
241         {
242             get
243             {
244                 return appId;
245             }
246         }
247
248         /// <summary>
249         /// Gets the default window.
250         /// </summary>
251         /// <returns>The default Window.</returns>
252         /// <since_tizen> 6 </since_tizen>
253         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
254         [EditorBrowsable(EditorBrowsableState.Never)]
255         public static Window GetDefaultWindow()
256         {
257             return Window.Instance;
258         }
259
260         internal Application ApplicationHandle
261         {
262             get
263             {
264                 return ((NUICoreBackend)this.Backend).ApplicationHandle;
265             }
266         }
267
268         /// <summary>
269         /// Register the assembly to XAML.
270         /// </summary>
271         /// <since_tizen> 5 </since_tizen>
272         public static void RegisterAssembly(Assembly assembly)
273         {
274             XamlParser.s_assemblies.Add(assembly);
275         }
276
277         /// <summary>
278         /// Runs the NUIApplication.
279         /// </summary>
280         /// <param name="args">Arguments from commandline.</param>
281         /// <since_tizen> 4 </since_tizen>
282         public override void Run(string[] args)
283         {
284             Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
285             Backend.AddEventHandler(EventType.Resumed, OnResume);
286             Backend.AddEventHandler(EventType.Paused, OnPause);
287             base.Run(args);
288         }
289
290         /// <summary>
291         /// Exits the NUIApplication.
292         /// </summary>
293         /// <since_tizen> 4 </since_tizen>
294         public override void Exit()
295         {
296             base.Exit();
297         }
298
299         /// <summary>
300         /// Ensures that the function passed in is called from the main loop when it is idle.
301         /// </summary>
302         /// <param name="func">The function to call</param>
303         /// <returns>true if added successfully, false otherwise</returns>
304         /// <since_tizen> 4 </since_tizen>
305         public bool AddIdle(System.Delegate func)
306         {
307             return ((NUICoreBackend)this.Backend).AddIdle(func);
308         }
309
310         /// <summary>
311         /// Sets the number of frames per render.
312         /// </summary>
313         /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
314         /// <remarks>
315         /// Suggest this is a power of two:
316         /// 1 - render each vsync frame.
317         /// 2 - render every other vsync frame.
318         /// 4 - render every fourth vsync frame.
319         /// 8 - render every eighth vsync frame. <br />
320         /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
321         ///</remarks>
322         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
323         [EditorBrowsable(EditorBrowsableState.Never)]
324         public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
325         {
326             Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
327         }
328
329         /// <summary>
330         /// Overrides this method if you want to handle behavior.
331         /// </summary>
332         /// <since_tizen> 3 </since_tizen>
333         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
334         {
335             base.OnLocaleChanged(e);
336         }
337
338         /// <summary>
339         /// Overrides this method if you want to handle behavior.
340         /// </summary>
341         /// <since_tizen> 3 </since_tizen>
342         protected override void OnLowBattery(LowBatteryEventArgs e)
343         {
344             base.OnLowBattery(e);
345         }
346
347         /// <summary>
348         /// Overrides this method if you want to handle behavior.
349         /// </summary>
350         /// <since_tizen> 3 </since_tizen>
351         protected override void OnLowMemory(LowMemoryEventArgs e)
352         {
353             base.OnLowMemory(e);
354         }
355
356         /// <summary>
357         /// Overrides this method if you want to handle behavior.
358         /// </summary>
359         /// <since_tizen> 3 </since_tizen>
360         protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
361         {
362             base.OnRegionFormatChanged(e);
363         }
364
365         /// <summary>
366         /// Overrides this method if you want to handle behavior.
367         /// </summary>
368         /// <since_tizen> 3 </since_tizen>
369         protected override void OnTerminate()
370         {
371             base.OnTerminate();
372         }
373
374         /// <summary>
375         /// Overrides this method if you want to handle behavior.
376         /// </summary>
377         /// <since_tizen> 3 </since_tizen>
378         protected virtual void OnPause()
379         {
380             Paused?.Invoke(this, EventArgs.Empty);
381         }
382
383         /// <summary>
384         /// Overrides this method if you want to handle behavior.
385         /// </summary>
386         /// <since_tizen> 3 </since_tizen>
387         protected virtual void OnResume()
388         {
389             Resumed?.Invoke(this, EventArgs.Empty);
390         }
391
392         /// <summary>
393         /// Overrides this method if you want to handle behavior.
394         /// </summary>
395         /// <since_tizen> 3 </since_tizen>
396         protected virtual void OnPreCreate()
397         {
398         }
399
400         /// <summary>
401         /// Overrides this method if you want to handle behavior.
402         /// </summary>
403         /// <since_tizen> 3 </since_tizen>
404         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
405         {
406             if (e != null)
407             {
408                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
409                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
410             }
411             base.OnAppControlReceived(e);
412         }
413
414         /// <summary>
415         /// Overrides this method if you want to handle behavior.
416         /// </summary>
417         /// <since_tizen> 3 </since_tizen>
418         protected override void OnCreate()
419         {
420             base.OnCreate();
421             appId = Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
422         }
423
424         /// <summary>
425         /// This is used to improve application launch performance.
426         /// </summary>
427         [EditorBrowsable(EditorBrowsableState.Never)]
428         static public void Preload()
429         {
430             Interop.Application.PreInitialize();
431             isPreload = true;
432         }
433
434         /// <summary>
435         /// This is used to improve application launch performance.
436         /// </summary>
437         [EditorBrowsable(EditorBrowsableState.Never)]
438         public void SendLaunchRequest(AppControl appControl)
439         {
440             transitionOptions.SendLaunchRequest(appControl);
441         }
442
443         /// <summary>
444         /// This is used to improve application launch performance.
445         /// </summary>
446         [EditorBrowsable(EditorBrowsableState.Never)]
447         public TransitionOptions TransitionOptions
448         {
449             get
450             {
451                 return transitionOptions;
452             }
453             set
454             {
455                 transitionOptions = value;
456             }
457         }
458
459         // TODO Enable this after tizen-theme-manager is released.
460         // private void TizenThemeChanged(object sender, ThemeEventArgs e)
461         // {
462         //     string prefix = "/theme/";
463
464         //     Dictionary<string, string> changedResources = new Dictionary<string, string>();
465         //     foreach (string key in ThemeManager.DefaultTheme.Resources.Keys)
466         //     {
467         //         // NOTE Need improve this code by checking HasKey
468         //         string newValue = null;
469         //         try
470         //         {
471         //             newValue = e.Theme.GetString(prefix + key);
472         //         }
473         //         catch { }
474         //         if (newValue != null)
475         //         {
476         //             changedResources[key] = newValue;
477         //         }
478         //     }
479         //     ThemeManager.UpdateCurrentThemeResources(changedResources);
480         // }
481
482         /// <summary>
483         /// Check if it is loaded as dotnet-loader-nui.
484         /// </summary>
485         static internal bool IsPreload
486         {
487             get
488             {
489                 return isPreload;
490             }
491         }
492     }
493
494     /// <summary>
495     /// Graphics Backend Type.
496     /// </summary>
497     [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
498     [EditorBrowsable(EditorBrowsableState.Never)]
499     [Obsolete("Please do not use! This will be deprecated!")]
500     public class Graphics
501     {
502         /// <summary>
503         /// Graphics Backend Type.
504         /// </summary>
505         public enum BackendType
506         {
507             /// <summary>
508             /// The GLES backend.
509             /// </summary>
510             Gles,
511             /// <summary>
512             /// The Vulkan backend.
513             /// </summary>
514             Vulkan
515         }
516
517         /// <summary>
518         /// The backend used by the NUIApplication.
519         /// </summary>
520         [EditorBrowsable(EditorBrowsableState.Never)]
521         internal static BackendType Backend = BackendType.Gles;
522
523         internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
524         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
525     }
526
527 }