56c553f69a4b6673cdad6486b442d4a1b13f3d49
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Application / NUIApplication.cs
1 /*
2  * Copyright (c) 2019 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.Binding;
26 using Tizen.NUI.Xaml;
27 using System.Collections.Generic;
28
29 namespace Tizen.NUI
30 {
31
32     /// <summary>
33     /// Represents an application that have a UI screen. The NUIApplication class has a default stage.
34     /// </summary>
35     /// <since_tizen> 3 </since_tizen>
36     public class NUIApplication : CoreApplication
37     {
38         /// <summary>
39         /// The instance of ResourceManager.
40         /// </summary>
41         private static System.Resources.ResourceManager resourceManager = null;
42
43         // TODO Enable this after tizen-theme-manager is released.
44         // private readonly ThemeLoader themeLoader = new ThemeLoader();
45
46         static NUIApplication()
47         {
48             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
49             // TODO Enable this after tizen-theme-manager is released.
50             // themeLoader.ThemeChanged += TizenThemeChanged;
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 opend 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. currenlty 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         /// Occurs whenever the application is resumed.
145         /// </summary>
146         /// <since_tizen> 4 </since_tizen>
147         public event EventHandler Resumed;
148
149         /// <summary>
150         /// Occurs whenever the application is paused.
151         /// </summary>
152         /// <since_tizen> 4 </since_tizen>
153         public event EventHandler Paused;
154
155         /// <summary>
156         /// Enumeration for deciding whether a NUI application window is opaque or transparent.
157         /// </summary>
158         /// <since_tizen> 3 </since_tizen>
159         public enum WindowMode
160         {
161             /// <summary>
162             /// Opaque
163             /// </summary>
164             /// <since_tizen> 3 </since_tizen>
165             Opaque = 0,
166             /// <summary>
167             /// Transparent
168             /// </summary>
169             /// <since_tizen> 3 </since_tizen>
170             Transparent = 1
171         }
172
173         /// <summary>
174         /// ResourceManager to handle multilingual.
175         /// </summary>
176         /// <since_tizen> 4 </since_tizen>
177         public static System.Resources.ResourceManager MultilingualResourceManager
178         {
179             get
180             {
181                 return resourceManager;
182             }
183             set
184             {
185                 resourceManager = value;
186             }
187         }
188
189         /// <summary>
190         /// Gets the window instance.
191         /// </summary>
192         /// <since_tizen> 3 </since_tizen>
193         [Obsolete("Please do not use! This will be deprecated!")]
194         [EditorBrowsable(EditorBrowsableState.Never)]
195         public Window Window
196         {
197             get
198             {
199                 return GetDefaultWindow();
200             }
201         }
202
203         /// <summary>
204         /// Gets the Application Id.
205         /// </summary>
206         /// <since_tizen> 6 </since_tizen>
207         [EditorBrowsable(EditorBrowsableState.Never)]
208         public string AppId
209         {
210             get
211             {
212                 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
213             }
214         }
215
216         /// <summary>
217         /// Gets the default window.
218         /// </summary>
219         /// <returns>The default Window.</returns>
220         /// <since_tizen> 6 </since_tizen>
221         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
222         [EditorBrowsable(EditorBrowsableState.Never)]
223         public static Window GetDefaultWindow()
224         {
225             return Window.Instance;
226         }
227
228         internal Application ApplicationHandle
229         {
230             get
231             {
232                 return ((NUICoreBackend)this.Backend).ApplicationHandle;
233             }
234         }
235
236         /// <summary>
237         /// Register the assembly to XAML.
238         /// </summary>
239         /// <since_tizen> 5 </since_tizen>
240         public static void RegisterAssembly(Assembly assembly)
241         {
242             XamlParser.s_assemblies.Add(assembly);
243         }
244
245         /// <summary>
246         /// Runs the NUIApplication.
247         /// </summary>
248         /// <param name="args">Arguments from commandline.</param>
249         /// <since_tizen> 4 </since_tizen>
250         public override void Run(string[] args)
251         {
252             Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
253             Backend.AddEventHandler(EventType.Resumed, OnResume);
254             Backend.AddEventHandler(EventType.Paused, OnPause);
255             base.Run(args);
256         }
257
258         /// <summary>
259         /// Exits the NUIApplication.
260         /// </summary>
261         /// <since_tizen> 4 </since_tizen>
262         public override void Exit()
263         {
264             base.Exit();
265         }
266
267         /// <summary>
268         /// Ensures that the function passed in is called from the main loop when it is idle.
269         /// </summary>
270         /// <param name="func">The function to call</param>
271         /// <returns>true if added successfully, false otherwise</returns>
272         /// <since_tizen> 4 </since_tizen>
273         public bool AddIdle(System.Delegate func)
274         {
275             return ((NUICoreBackend)this.Backend).AddIdle(func);
276         }
277
278         /// <summary>
279         /// Sets the number of frames per render.
280         /// </summary>
281         /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
282         /// <remarks>
283         /// Suggest this is a power of two:
284         /// 1 - render each vsync frame.
285         /// 2 - render every other vsync frame.
286         /// 4 - render every fourth vsync frame.
287         /// 8 - render every eighth vsync frame. <br />
288         /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
289         ///</remarks>
290         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
291         [EditorBrowsable(EditorBrowsableState.Never)]
292         public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
293         {
294             Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
295         }
296
297         /// <summary>
298         /// Overrides this method if you want to handle behavior.
299         /// </summary>
300         /// <since_tizen> 3 </since_tizen>
301         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
302         {
303             base.OnLocaleChanged(e);
304         }
305
306         /// <summary>
307         /// Overrides this method if you want to handle behavior.
308         /// </summary>
309         /// <since_tizen> 3 </since_tizen>
310         protected override void OnLowBattery(LowBatteryEventArgs e)
311         {
312             base.OnLowBattery(e);
313         }
314
315         /// <summary>
316         /// Overrides this method if you want to handle behavior.
317         /// </summary>
318         /// <since_tizen> 3 </since_tizen>
319         protected override void OnLowMemory(LowMemoryEventArgs e)
320         {
321             base.OnLowMemory(e);
322         }
323
324         /// <summary>
325         /// Overrides this method if you want to handle behavior.
326         /// </summary>
327         /// <since_tizen> 3 </since_tizen>
328         protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
329         {
330             base.OnRegionFormatChanged(e);
331         }
332
333         /// <summary>
334         /// Overrides this method if you want to handle behavior.
335         /// </summary>
336         /// <since_tizen> 3 </since_tizen>
337         protected override void OnTerminate()
338         {
339             base.OnTerminate();
340         }
341
342         /// <summary>
343         /// Overrides this method if you want to handle behavior.
344         /// </summary>
345         /// <since_tizen> 3 </since_tizen>
346         protected virtual void OnPause()
347         {
348             Paused?.Invoke(this, EventArgs.Empty);
349         }
350
351         /// <summary>
352         /// Overrides this method if you want to handle behavior.
353         /// </summary>
354         /// <since_tizen> 3 </since_tizen>
355         protected virtual void OnResume()
356         {
357             Resumed?.Invoke(this, EventArgs.Empty);
358         }
359
360         /// <summary>
361         /// Overrides this method if you want to handle behavior.
362         /// </summary>
363         /// <since_tizen> 3 </since_tizen>
364         protected virtual void OnPreCreate()
365         {
366         }
367
368         /// <summary>
369         /// Overrides this method if you want to handle behavior.
370         /// </summary>
371         /// <since_tizen> 3 </since_tizen>
372         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
373         {
374             if (e != null)
375             {
376                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
377                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
378             }
379             base.OnAppControlReceived(e);
380         }
381
382         /// <summary>
383         /// Overrides this method if you want to handle behavior.
384         /// </summary>
385         /// <since_tizen> 3 </since_tizen>
386         protected override void OnCreate()
387         {
388             base.OnCreate();
389         }
390
391         /// <summary>
392         /// This is used to improve application launch performance.
393         /// </summary>
394         [EditorBrowsable(EditorBrowsableState.Never)]
395         static public void Preload()
396         {
397             Interop.Application.PreInitialize();
398             IsPreload = true;
399         }
400
401         /// <summary>
402         /// This is used to improve application launch performance.
403         /// </summary>
404         [EditorBrowsable(EditorBrowsableState.Never)]
405         public void SendLaunchRequest(AppControl appControl)
406         {
407             TransitionOptions?.SendLaunchRequest(appControl);
408         }
409
410         /// <summary>
411         /// This is used to improve application launch performance.
412         /// </summary>
413         [EditorBrowsable(EditorBrowsableState.Never)]
414         public TransitionOptions TransitionOptions { get; set; }
415
416         // TODO Enable this after tizen-theme-manager is released.
417         // private void TizenThemeChanged(object sender, ThemeEventArgs e)
418         // {
419         //     string prefix = "/theme/";
420
421         //     Dictionary<string, string> changedResources = new Dictionary<string, string>();
422         //     foreach (string key in ThemeManager.DefaultTheme.Resources.Keys)
423         //     {
424         //         // NOTE Need improve this code by checking HasKey
425         //         string newValue = null;
426         //         try
427         //         {
428         //             newValue = e.Theme.GetString(prefix + key);
429         //         }
430         //         catch { }
431         //         if (newValue != null)
432         //         {
433         //             changedResources[key] = newValue;
434         //         }
435         //     }
436         //     ThemeManager.UpdateCurrentThemeResources(changedResources);
437         // }
438
439         /// <summary>
440         /// Check if it is loaded as dotnet-loader-nui.
441         /// </summary>
442         static internal bool IsPreload { get; set; }
443     }
444
445     /// <summary>
446     /// Graphics Backend Type.
447     /// </summary>
448     [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
449     [EditorBrowsable(EditorBrowsableState.Never)]
450     [Obsolete("Please do not use! This will be deprecated!")]
451     public class Graphics
452     {
453         /// <summary>
454         /// Graphics Backend Type.
455         /// </summary>
456         public enum BackendType
457         {
458             /// <summary>
459             /// The GLES backend.
460             /// </summary>
461             Gles,
462             /// <summary>
463             /// The Vulkan backend.
464             /// </summary>
465             Vulkan
466         }
467
468         /// <summary>
469         /// The backend used by the NUIApplication.
470         /// </summary>
471         [EditorBrowsable(EditorBrowsableState.Never)]
472         internal static BackendType Backend = BackendType.Gles;
473
474         internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
475         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
476     }
477
478 }