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