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