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