[NUI] Fix to disable ThemeManager in tv profile
[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         [EditorBrowsable(EditorBrowsableState.Never)]
248         public static string CurrentLoadedXaml
249         {
250             get
251             {
252                 return currentLoadedXaml;
253             }
254             set
255             {
256                 if (currentLoadedXaml != value)
257                 {
258                     currentLoadedXaml = value;
259                     XamlLoaded?.Invoke(value);
260                 }
261             }
262         }
263
264         /// <summary>
265         /// ResourceManager to handle multilingual.
266         /// </summary>
267         /// <since_tizen> 4 </since_tizen>
268         public static System.Resources.ResourceManager MultilingualResourceManager
269         {
270             get
271             {
272                 return resourceManager;
273             }
274             set
275             {
276                 resourceManager = value;
277             }
278         }
279
280         /// <summary>
281         /// Gets the window instance.
282         /// </summary>
283         /// <since_tizen> 3 </since_tizen>
284         [Obsolete("Please do not use! This will be deprecated!")]
285         [EditorBrowsable(EditorBrowsableState.Never)]
286         public Window Window
287         {
288             get
289             {
290                 return GetDefaultWindow();
291             }
292         }
293
294         /// <summary>
295         /// Gets the Application Id.
296         /// </summary>
297         /// <since_tizen> 6 </since_tizen>
298         [EditorBrowsable(EditorBrowsableState.Never)]
299         public string AppId
300         {
301             get
302             {
303                 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
304             }
305         }
306
307         /// <summary>
308         /// Gets the default window.
309         /// </summary>
310         /// <returns>The default Window.</returns>
311         /// <since_tizen> 6 </since_tizen>
312         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
313         [EditorBrowsable(EditorBrowsableState.Never)]
314         public static Window GetDefaultWindow()
315         {
316             return Window.Instance;
317         }
318
319         internal Application ApplicationHandle
320         {
321             get
322             {
323                 return ((NUICoreBackend)this.Backend).ApplicationHandle;
324             }
325         }
326
327         /// <summary>
328         /// Register the assembly to XAML.
329         /// </summary>
330         /// <since_tizen> 5 </since_tizen>
331         public static void RegisterAssembly(Assembly assembly)
332         {
333             XamlParser.s_assemblies.Add(assembly);
334         }
335
336         /// <summary>
337         /// Runs the NUIApplication.
338         /// </summary>
339         /// <param name="args">Arguments from commandline.</param>
340         /// <since_tizen> 4 </since_tizen>
341         public override void Run(string[] args)
342         {
343             Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
344             Backend.AddEventHandler(EventType.Resumed, OnResume);
345             Backend.AddEventHandler(EventType.Paused, OnPause);
346             base.Run(args);
347         }
348
349         /// <summary>
350         /// Exits the NUIApplication.
351         /// </summary>
352         /// <since_tizen> 4 </since_tizen>
353         public override void Exit()
354         {
355             base.Exit();
356         }
357
358         /// <summary>
359         /// Ensures that the function passed in is called from the main loop when it is idle.
360         /// </summary>
361         /// <param name="func">The function to call</param>
362         /// <returns>true if added successfully, false otherwise</returns>
363         /// <since_tizen> 4 </since_tizen>
364         public bool AddIdle(System.Delegate func)
365         {
366             return ((NUICoreBackend)this.Backend).AddIdle(func);
367         }
368
369         /// <summary>
370         /// Sets the number of frames per render.
371         /// </summary>
372         /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
373         /// <remarks>
374         /// Suggest this is a power of two:
375         /// 1 - render each vsync frame.
376         /// 2 - render every other vsync frame.
377         /// 4 - render every fourth vsync frame.
378         /// 8 - render every eighth vsync frame. <br />
379         /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
380         ///</remarks>
381         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
382         [EditorBrowsable(EditorBrowsableState.Never)]
383         public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
384         {
385             Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
386         }
387
388         /// <summary>
389         /// Overrides this method if you want to handle behavior.
390         /// </summary>
391         /// <since_tizen> 3 </since_tizen>
392         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
393         {
394             base.OnLocaleChanged(e);
395         }
396
397         /// <summary>
398         /// Overrides this method if you want to handle behavior.
399         /// </summary>
400         /// <since_tizen> 3 </since_tizen>
401         protected override void OnLowBattery(LowBatteryEventArgs e)
402         {
403             base.OnLowBattery(e);
404         }
405
406         /// <summary>
407         /// Overrides this method if you want to handle behavior.
408         /// </summary>
409         /// <since_tizen> 3 </since_tizen>
410         protected override void OnLowMemory(LowMemoryEventArgs e)
411         {
412             base.OnLowMemory(e);
413         }
414
415         /// <summary>
416         /// Overrides this method if you want to handle behavior.
417         /// </summary>
418         /// <since_tizen> 3 </since_tizen>
419         protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
420         {
421             base.OnRegionFormatChanged(e);
422         }
423
424         /// <summary>
425         /// Overrides this method if you want to handle behavior.
426         /// </summary>
427         /// <since_tizen> 3 </since_tizen>
428         protected override void OnTerminate()
429         {
430             base.OnTerminate();
431         }
432
433         /// <summary>
434         /// Overrides this method if you want to handle behavior.
435         /// </summary>
436         /// <since_tizen> 3 </since_tizen>
437         protected virtual void OnPause()
438         {
439             Paused?.Invoke(this, EventArgs.Empty);
440         }
441
442         /// <summary>
443         /// Overrides this method if you want to handle behavior.
444         /// </summary>
445         /// <since_tizen> 3 </since_tizen>
446         protected virtual void OnResume()
447         {
448             Resumed?.Invoke(this, EventArgs.Empty);
449         }
450
451         /// <summary>
452         /// Overrides this method if you want to handle behavior.
453         /// </summary>
454         /// <since_tizen> 3 </since_tizen>
455         protected virtual void OnPreCreate()
456         {
457         }
458
459         /// <summary>
460         /// Overrides this method if you want to handle behavior.
461         /// </summary>
462         /// <since_tizen> 3 </since_tizen>
463         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
464         {
465             if (e != null)
466             {
467                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
468                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
469             }
470             base.OnAppControlReceived(e);
471         }
472
473         /// <summary>
474         /// Overrides this method if you want to handle behavior.
475         /// </summary>
476         /// <since_tizen> 3 </since_tizen>
477         protected override void OnCreate()
478         {
479             base.OnCreate();
480         }
481
482         /// <summary>
483         /// This is used to improve application launch performance.
484         /// </summary>
485         [EditorBrowsable(EditorBrowsableState.Never)]
486         static public void Preload()
487         {
488             Interop.Application.PreInitialize();
489 #if ExternalThemeEnabled
490             ThemeManager.Preload();
491 #endif
492             IsPreload = true;
493         }
494
495         /// <summary>
496         /// This is used to improve application launch performance.
497         /// </summary>
498         [EditorBrowsable(EditorBrowsableState.Never)]
499         public void SendLaunchRequest(AppControl appControl)
500         {
501             TransitionOptions?.SendLaunchRequest(appControl);
502         }
503
504         /// <summary>
505         /// This is used to improve application launch performance.
506         /// </summary>
507         [EditorBrowsable(EditorBrowsableState.Never)]
508         public TransitionOptions TransitionOptions { get; set; }
509
510         /// <summary>
511         /// Check if it is loaded as dotnet-loader-nui.
512         /// </summary>
513         static internal bool IsPreload { get; set; }
514
515         private void ApplyThemeOption(ThemeOptions option)
516         {
517             if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
518             {
519                 ThemeManager.PlatformThemeEnabled = true;
520             }
521
522             if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
523             {
524                 ThemeManager.ApplicationThemeChangeSensitive = true;
525             }
526         }
527     }
528
529     /// <summary>
530     /// Graphics Backend Type.
531     /// </summary>
532     [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
533     [EditorBrowsable(EditorBrowsableState.Never)]
534     [Obsolete("Please do not use! This will be deprecated!")]
535     public class Graphics
536     {
537         /// <summary>
538         /// Graphics Backend Type.
539         /// </summary>
540         public enum BackendType
541         {
542             /// <summary>
543             /// The GLES backend.
544             /// </summary>
545             Gles,
546             /// <summary>
547             /// The Vulkan backend.
548             /// </summary>
549             Vulkan
550         }
551
552         /// <summary>
553         /// The backend used by the NUIApplication.
554         /// </summary>
555         [EditorBrowsable(EditorBrowsableState.Never)]
556         internal static BackendType Backend = BackendType.Gles;
557
558         internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
559         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
560     }
561 }