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