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