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