[NUI] Modify preload (#2046)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / 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.Threading;
21 using System.Reflection;
22 using Tizen.Applications;
23 using Tizen.Applications.CoreBackend;
24 using Tizen.NUI.Binding;
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         private Size2D _windowSize2D = null;
41         private Position2D _windowPosition2D = null;
42         private TransitionOptions transitionOptions;
43
44
45         /// <summary>
46         /// The default constructor.
47         /// </summary>
48         /// <since_tizen> 3 </since_tizen>
49         public NUIApplication() : base(new NUICoreBackend())
50         {
51             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
52         }
53
54         /// <summary>
55         /// The constructor with window size and position.
56         /// </summary>
57         /// <param name="windowSize">The window size.</param>
58         /// <param name="windowPosition">The window position.</param>
59         /// <since_tizen> 5 </since_tizen>
60         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
61         [EditorBrowsable(EditorBrowsableState.Never)]
62         public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", WindowMode.Opaque, windowSize, windowPosition))
63         {
64             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
65             _windowSize2D = windowSize;
66             _windowPosition2D = windowPosition;
67         }
68
69         /// <summary>
70         /// The constructor with a stylesheet.
71         /// </summary>
72         /// <param name="styleSheet">The styleSheet url.</param>
73         /// <since_tizen> 3 </since_tizen>
74         public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
75         {
76             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
77         }
78
79         /// <summary>
80         /// The constructor with a stylesheet, window size, and position.
81         /// </summary>
82         /// <param name="styleSheet">The styleSheet URL.</param>
83         /// <param name="windowSize">The window size.</param>
84         /// <param name="windowPosition">The window position.</param>
85         /// <since_tizen> 5 </since_tizen>
86         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
87         [EditorBrowsable(EditorBrowsableState.Never)]
88         public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
89         {
90             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
91             _windowSize2D = windowSize;
92             _windowPosition2D = windowPosition;
93         }
94
95         /// <summary>
96         /// The constructor with a stylesheet and window mode.
97         /// </summary>
98         /// <param name="styleSheet">The styleSheet url.</param>
99         /// <param name="windowMode">The windowMode.</param>
100         /// <since_tizen> 3 </since_tizen>
101         public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
102         {
103             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
104         }
105
106         /// <summary>
107         /// The constructor with a stylesheet, window mode, window size, and position.
108         /// </summary>
109         /// <param name="styleSheet">The styleSheet URL.</param>
110         /// <param name="windowMode">The windowMode.</param>
111         /// <param name="windowSize">The window size.</param>
112         /// <param name="windowPosition">The window position.</param>
113         /// <since_tizen> 5 </since_tizen>
114         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
115         [EditorBrowsable(EditorBrowsableState.Never)]
116         public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
117         {
118             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
119             _windowSize2D = windowSize;
120             _windowPosition2D = windowPosition;
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             if (windowSize != null) { _windowSize2D = windowSize; }
141             if (windowPosition != null) { _windowPosition2D = windowPosition; }
142             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
143         }
144
145         /// <summary>
146         /// Occurs whenever the application is resumed.
147         /// </summary>
148         /// <since_tizen> 4 </since_tizen>
149         public event EventHandler Resumed;
150
151         /// <summary>
152         /// Occurs whenever the application is paused.
153         /// </summary>
154         /// <since_tizen> 4 </since_tizen>
155         public event EventHandler Paused;
156
157         /// <summary>
158         /// Enumeration for deciding whether a NUI application window is opaque or transparent.
159         /// </summary>
160         /// <since_tizen> 3 </since_tizen>
161         public enum WindowMode
162         {
163             /// <summary>
164             /// Opaque
165             /// </summary>
166             /// <since_tizen> 3 </since_tizen>
167             Opaque = 0,
168             /// <summary>
169             /// Transparent
170             /// </summary>
171             /// <since_tizen> 3 </since_tizen>
172             Transparent = 1
173         }
174
175         /// <summary>
176         /// ResourceManager to handle multilingual.
177         /// </summary>
178         /// <since_tizen> 4 </since_tizen>
179         public static System.Resources.ResourceManager MultilingualResourceManager
180         {
181             get
182             {
183                 return resourceManager;
184             }
185             set
186             {
187                 resourceManager = value;
188             }
189         }
190
191         /// <summary>
192         /// Gets the window instance.
193         /// </summary>
194         /// <since_tizen> 3 </since_tizen>
195         [Obsolete("Please do not use! This will be deprecated!")]
196         [EditorBrowsable(EditorBrowsableState.Never)]
197         public Window Window
198         {
199             get
200             {
201                 return GetDefaultWindow();
202             }
203         }
204
205         /// <summary>
206         /// Gets the default window.
207         /// </summary>
208         /// <returns>The default Window.</returns>
209         /// <since_tizen> 6 </since_tizen>
210         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
211         [EditorBrowsable(EditorBrowsableState.Never)]
212         public static Window GetDefaultWindow()
213         {
214             return Window.Instance;
215         }
216
217         internal Application ApplicationHandle
218         {
219             get
220             {
221                 return ((NUICoreBackend)this.Backend).ApplicationHandle;
222             }
223         }
224
225         /// <summary>
226         /// Register the assembly to XAML.
227         /// </summary>
228         /// <since_tizen> 5 </since_tizen>
229         public static void RegisterAssembly(Assembly assembly)
230         {
231             XamlParser.s_assemblies.Add(assembly);
232         }
233
234         /// <summary>
235         /// Runs the NUIApplication.
236         /// </summary>
237         /// <param name="args">Arguments from commandline.</param>
238         /// <since_tizen> 4 </since_tizen>
239         public override void Run(string[] args)
240         {
241             Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
242             Backend.AddEventHandler(EventType.Resumed, OnResume);
243             Backend.AddEventHandler(EventType.Paused, OnPause);
244             base.Run(args);
245         }
246
247         /// <summary>
248         /// Exits the NUIApplication.
249         /// </summary>
250         /// <since_tizen> 4 </since_tizen>
251         public override void Exit()
252         {
253             base.Exit();
254         }
255
256         /// <summary>
257         /// Ensures that the function passed in is called from the main loop when it is idle.
258         /// </summary>
259         /// <param name="func">The function to call</param>
260         /// <returns>true if added successfully, false otherwise</returns>
261         /// <since_tizen> 4 </since_tizen>
262         public bool AddIdle(System.Delegate func)
263         {
264             return ((NUICoreBackend)this.Backend).AddIdle(func);
265         }
266
267         /// <summary>
268         /// Sets the number of frames per render.
269         /// </summary>
270         /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
271         /// <remarks>
272         /// Suggest this is a power of two:
273         /// 1 - render each vsync frame.
274         /// 2 - render every other vsync frame.
275         /// 4 - render every fourth vsync frame.
276         /// 8 - render every eighth vsync frame. <br />
277         /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
278         ///</remarks>
279         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
280         [EditorBrowsable(EditorBrowsableState.Never)]
281         public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
282         {
283             Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
284         }
285
286         /// <summary>
287         /// Overrides this method if you want to handle behavior.
288         /// </summary>
289         /// <since_tizen> 3 </since_tizen>
290         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
291         {
292             base.OnLocaleChanged(e);
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 OnLowBattery(LowBatteryEventArgs e)
300         {
301             base.OnLowBattery(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 OnLowMemory(LowMemoryEventArgs e)
309         {
310             base.OnLowMemory(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 OnRegionFormatChanged(RegionFormatChangedEventArgs e)
318         {
319             base.OnRegionFormatChanged(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 OnTerminate()
327         {
328             base.OnTerminate();
329         }
330
331         /// <summary>
332         /// Overrides this method if you want to handle behavior.
333         /// </summary>
334         /// <since_tizen> 3 </since_tizen>
335         protected virtual void OnPause()
336         {
337             Paused?.Invoke(this, EventArgs.Empty);
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 OnResume()
345         {
346             Resumed?.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 OnPreCreate()
354         {
355         }
356
357         /// <summary>
358         /// Overrides this method if you want to handle behavior.
359         /// </summary>
360         /// <since_tizen> 3 </since_tizen>
361         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
362         {
363             if (e != null)
364             {
365                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
366                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
367             }
368             base.OnAppControlReceived(e);
369         }
370
371         /// <summary>
372         /// Overrides this method if you want to handle behavior.
373         /// </summary>
374         /// <since_tizen> 3 </since_tizen>
375         protected override void OnCreate()
376         {
377             base.OnCreate();
378         }
379
380         /// <summary>
381         /// This is used to improve application launch performance.
382         /// </summary>
383         [EditorBrowsable(EditorBrowsableState.Never)]
384         static public void PreLoad()
385         {
386             Interop.Application.Application_PreInitialize();
387         }
388
389         /// <summary>
390         /// This is used to improve application launch performance.
391         /// </summary>
392         [EditorBrowsable(EditorBrowsableState.Never)]
393         public void SendLaunchRequest(AppControl appControl)
394         {
395             transitionOptions.SendLaunchRequest(appControl);
396         }
397
398         /// <summary>
399         /// This is used to improve application launch performance.
400         /// </summary>
401         [EditorBrowsable(EditorBrowsableState.Never)]
402         public TransitionOptions TransitionOptions
403         {
404             get
405             {
406                 return transitionOptions;
407             }
408             set
409             {
410                 transitionOptions = value;
411             }
412         }
413     }
414
415     /// <summary>
416     /// Graphics Backend Type.
417     /// </summary>
418     [EditorBrowsable(EditorBrowsableState.Never)]
419     [Obsolete("Please do not use! This will be deprecated!")]
420     public class Graphics
421     {
422         /// <summary>
423         /// Graphics Backend Type.
424         /// </summary>
425         public enum BackendType
426         {
427             /// <summary>
428             /// The GLES backend.
429             /// </summary>
430             Gles,
431             /// <summary>
432             /// The Vulkan backend.
433             /// </summary>
434             Vulkan
435         }
436
437         /// <summary>
438         /// The backend used by the NUIApplication.
439         /// </summary>
440         [EditorBrowsable(EditorBrowsableState.Never)]
441         public static BackendType Backend = BackendType.Gles;
442
443         internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
444         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
445     }
446
447 }