[NUI] Update theme system
[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         /// Occurs whenever the application is resumed.
162         /// </summary>
163         /// <since_tizen> 4 </since_tizen>
164         public event EventHandler Resumed;
165
166         /// <summary>
167         /// Occurs whenever the application is paused.
168         /// </summary>
169         /// <since_tizen> 4 </since_tizen>
170         public event EventHandler Paused;
171
172         /// <summary>
173         /// Enumeration for deciding whether a NUI application window is opaque or transparent.
174         /// </summary>
175         /// <since_tizen> 3 </since_tizen>
176         public enum WindowMode
177         {
178             /// <summary>
179             /// Opaque
180             /// </summary>
181             /// <since_tizen> 3 </since_tizen>
182             Opaque = 0,
183             /// <summary>
184             /// Transparent
185             /// </summary>
186             /// <since_tizen> 3 </since_tizen>
187             Transparent = 1
188         }
189
190         /// <summary>
191         /// Enumeration for theme options of the NUIApplication.
192         /// </summary>
193         [Flags]
194         [EditorBrowsable(EditorBrowsableState.Never)]
195         public enum ThemeOptions : int
196         {
197             /// <summary>
198             /// No option specified.
199             /// </summary>
200             [EditorBrowsable(EditorBrowsableState.Never)]
201             None = 0,
202
203             /// <summary>
204             /// Enable platform theme.
205             /// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
206             /// </summary>
207             [EditorBrowsable(EditorBrowsableState.Never)]
208             PlatformThemeEnabled = 1 << 0,
209
210             /// <summary>
211             /// Sets the default value of View.ThemeChangeSensitive.
212             /// when this option is on, all views are made sensitive on theme changing by default.
213             /// </summary>
214             [EditorBrowsable(EditorBrowsableState.Never)]
215             ThemeChangeSensitive = 1 << 1,
216         };
217
218         /// <summary>
219         /// ResourceManager to handle multilingual.
220         /// </summary>
221         /// <since_tizen> 4 </since_tizen>
222         public static System.Resources.ResourceManager MultilingualResourceManager
223         {
224             get
225             {
226                 return resourceManager;
227             }
228             set
229             {
230                 resourceManager = value;
231             }
232         }
233
234         /// <summary>
235         /// Gets the window instance.
236         /// </summary>
237         /// <since_tizen> 3 </since_tizen>
238         [Obsolete("Please do not use! This will be deprecated!")]
239         [EditorBrowsable(EditorBrowsableState.Never)]
240         public Window Window
241         {
242             get
243             {
244                 return GetDefaultWindow();
245             }
246         }
247
248         /// <summary>
249         /// Gets the Application Id.
250         /// </summary>
251         /// <since_tizen> 6 </since_tizen>
252         [EditorBrowsable(EditorBrowsableState.Never)]
253         public string AppId
254         {
255             get
256             {
257                 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
258             }
259         }
260
261         /// <summary>
262         /// Gets the default window.
263         /// </summary>
264         /// <returns>The default Window.</returns>
265         /// <since_tizen> 6 </since_tizen>
266         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
267         [EditorBrowsable(EditorBrowsableState.Never)]
268         public static Window GetDefaultWindow()
269         {
270             return Window.Instance;
271         }
272
273         internal Application ApplicationHandle
274         {
275             get
276             {
277                 return ((NUICoreBackend)this.Backend).ApplicationHandle;
278             }
279         }
280
281         /// <summary>
282         /// Register the assembly to XAML.
283         /// </summary>
284         /// <since_tizen> 5 </since_tizen>
285         public static void RegisterAssembly(Assembly assembly)
286         {
287             XamlParser.s_assemblies.Add(assembly);
288         }
289
290         /// <summary>
291         /// Runs the NUIApplication.
292         /// </summary>
293         /// <param name="args">Arguments from commandline.</param>
294         /// <since_tizen> 4 </since_tizen>
295         public override void Run(string[] args)
296         {
297             Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
298             Backend.AddEventHandler(EventType.Resumed, OnResume);
299             Backend.AddEventHandler(EventType.Paused, OnPause);
300             base.Run(args);
301         }
302
303         /// <summary>
304         /// Exits the NUIApplication.
305         /// </summary>
306         /// <since_tizen> 4 </since_tizen>
307         public override void Exit()
308         {
309             base.Exit();
310         }
311
312         /// <summary>
313         /// Ensures that the function passed in is called from the main loop when it is idle.
314         /// </summary>
315         /// <param name="func">The function to call</param>
316         /// <returns>true if added successfully, false otherwise</returns>
317         /// <since_tizen> 4 </since_tizen>
318         public bool AddIdle(System.Delegate func)
319         {
320             return ((NUICoreBackend)this.Backend).AddIdle(func);
321         }
322
323         /// <summary>
324         /// Sets the number of frames per render.
325         /// </summary>
326         /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
327         /// <remarks>
328         /// Suggest this is a power of two:
329         /// 1 - render each vsync frame.
330         /// 2 - render every other vsync frame.
331         /// 4 - render every fourth vsync frame.
332         /// 8 - render every eighth vsync frame. <br />
333         /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
334         ///</remarks>
335         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
336         [EditorBrowsable(EditorBrowsableState.Never)]
337         public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
338         {
339             Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
340         }
341
342         /// <summary>
343         /// Overrides this method if you want to handle behavior.
344         /// </summary>
345         /// <since_tizen> 3 </since_tizen>
346         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
347         {
348             base.OnLocaleChanged(e);
349         }
350
351         /// <summary>
352         /// Overrides this method if you want to handle behavior.
353         /// </summary>
354         /// <since_tizen> 3 </since_tizen>
355         protected override void OnLowBattery(LowBatteryEventArgs e)
356         {
357             base.OnLowBattery(e);
358         }
359
360         /// <summary>
361         /// Overrides this method if you want to handle behavior.
362         /// </summary>
363         /// <since_tizen> 3 </since_tizen>
364         protected override void OnLowMemory(LowMemoryEventArgs e)
365         {
366             base.OnLowMemory(e);
367         }
368
369         /// <summary>
370         /// Overrides this method if you want to handle behavior.
371         /// </summary>
372         /// <since_tizen> 3 </since_tizen>
373         protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
374         {
375             base.OnRegionFormatChanged(e);
376         }
377
378         /// <summary>
379         /// Overrides this method if you want to handle behavior.
380         /// </summary>
381         /// <since_tizen> 3 </since_tizen>
382         protected override void OnTerminate()
383         {
384             base.OnTerminate();
385         }
386
387         /// <summary>
388         /// Overrides this method if you want to handle behavior.
389         /// </summary>
390         /// <since_tizen> 3 </since_tizen>
391         protected virtual void OnPause()
392         {
393             Paused?.Invoke(this, EventArgs.Empty);
394         }
395
396         /// <summary>
397         /// Overrides this method if you want to handle behavior.
398         /// </summary>
399         /// <since_tizen> 3 </since_tizen>
400         protected virtual void OnResume()
401         {
402             Resumed?.Invoke(this, EventArgs.Empty);
403         }
404
405         /// <summary>
406         /// Overrides this method if you want to handle behavior.
407         /// </summary>
408         /// <since_tizen> 3 </since_tizen>
409         protected virtual void OnPreCreate()
410         {
411         }
412
413         /// <summary>
414         /// Overrides this method if you want to handle behavior.
415         /// </summary>
416         /// <since_tizen> 3 </since_tizen>
417         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
418         {
419             if (e != null)
420             {
421                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
422                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
423             }
424             base.OnAppControlReceived(e);
425         }
426
427         /// <summary>
428         /// Overrides this method if you want to handle behavior.
429         /// </summary>
430         /// <since_tizen> 3 </since_tizen>
431         protected override void OnCreate()
432         {
433             base.OnCreate();
434         }
435
436         /// <summary>
437         /// This is used to improve application launch performance.
438         /// </summary>
439         [EditorBrowsable(EditorBrowsableState.Never)]
440         static public void Preload()
441         {
442             Interop.Application.PreInitialize();
443             ThemeManager.Preload();
444             IsPreload = true;
445         }
446
447         /// <summary>
448         /// This is used to improve application launch performance.
449         /// </summary>
450         [EditorBrowsable(EditorBrowsableState.Never)]
451         public void SendLaunchRequest(AppControl appControl)
452         {
453             TransitionOptions?.SendLaunchRequest(appControl);
454         }
455
456         /// <summary>
457         /// This is used to improve application launch performance.
458         /// </summary>
459         [EditorBrowsable(EditorBrowsableState.Never)]
460         public TransitionOptions TransitionOptions { get; set; }
461
462         /// <summary>
463         /// Check if it is loaded as dotnet-loader-nui.
464         /// </summary>
465         static internal bool IsPreload { get; set; }
466
467         private void ApplyThemeOption(ThemeOptions option)
468         {
469             if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
470             {
471                 ThemeManager.PlatformThemeEnabled = true;
472             }
473
474             if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
475             {
476                 ThemeManager.ApplicationThemeChangeSensitive = true;
477             }
478         }
479     }
480
481     /// <summary>
482     /// Graphics Backend Type.
483     /// </summary>
484     [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
485     [EditorBrowsable(EditorBrowsableState.Never)]
486     [Obsolete("Please do not use! This will be deprecated!")]
487     public class Graphics
488     {
489         /// <summary>
490         /// Graphics Backend Type.
491         /// </summary>
492         public enum BackendType
493         {
494             /// <summary>
495             /// The GLES backend.
496             /// </summary>
497             Gles,
498             /// <summary>
499             /// The Vulkan backend.
500             /// </summary>
501             Vulkan
502         }
503
504         /// <summary>
505         /// The backend used by the NUIApplication.
506         /// </summary>
507         [EditorBrowsable(EditorBrowsableState.Never)]
508         internal static BackendType Backend = BackendType.Gles;
509
510         internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
511         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
512     }
513 }