[NUI] Integreation from dalihub (#988)
[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
43         /// <summary>
44         /// The default constructor.
45         /// </summary>
46         /// <since_tizen> 3 </since_tizen>
47         public NUIApplication() : base(new NUICoreBackend())
48         {
49             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
50         }
51
52         /// <summary>
53         /// The constructor with window size and position.
54         /// </summary>
55         /// <param name="windowSize">The window size.</param>
56         /// <param name="windowPosition">The window position.</param>
57         /// <since_tizen> 5 </since_tizen>
58         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", WindowMode.Opaque, windowSize, windowPosition))
61         {
62             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
63             _windowSize2D = windowSize;
64             _windowPosition2D = windowPosition;
65         }
66
67         /// <summary>
68         /// The constructor with a stylesheet.
69         /// </summary>
70         /// <param name="styleSheet">The styleSheet url.</param>
71         /// <since_tizen> 3 </since_tizen>
72         public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
73         {
74             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
75         }
76
77         /// <summary>
78         /// The constructor with a stylesheet, window size, and position.
79         /// </summary>
80         /// <param name="styleSheet">The styleSheet URL.</param>
81         /// <param name="windowSize">The window size.</param>
82         /// <param name="windowPosition">The window position.</param>
83         /// <since_tizen> 5 </since_tizen>
84         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
85         [EditorBrowsable(EditorBrowsableState.Never)]
86         public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
87         {
88             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
89             _windowSize2D = windowSize;
90             _windowPosition2D = windowPosition;
91         }
92
93         /// <summary>
94         /// The constructor with a stylesheet and window mode.
95         /// </summary>
96         /// <param name="styleSheet">The styleSheet url.</param>
97         /// <param name="windowMode">The windowMode.</param>
98         /// <since_tizen> 3 </since_tizen>
99         public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
100         {
101             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
102         }
103
104         /// <summary>
105         /// The constructor with a stylesheet, window mode, window size, and position.
106         /// </summary>
107         /// <param name="styleSheet">The styleSheet URL.</param>
108         /// <param name="windowMode">The windowMode.</param>
109         /// <param name="windowSize">The window size.</param>
110         /// <param name="windowPosition">The window position.</param>
111         /// <since_tizen> 5 </since_tizen>
112         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
113         [EditorBrowsable(EditorBrowsableState.Never)]
114         public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
115         {
116             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
117             _windowSize2D = windowSize;
118             _windowPosition2D = windowPosition;
119         }
120
121         /// <summary>
122         /// Internal inhouse constructor with Graphics Backend Type
123         /// </summary>
124         /// <param name="backend"></param>
125         /// <param name="windowMode"></param>
126         /// <param name="windowSize"></param>
127         /// <param name="windowPosition"></param>
128         /// <param name="styleSheet"></param>
129         /// InhouseAPI, this could be opend in NextTizen
130         [Obsolete("Please do not use! This will be deprecated!")]
131         [EditorBrowsable(EditorBrowsableState.Never)]
132         public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
133         {
134             //windowMode and styleSheet will be added later. currenlty it's not working as expected.
135             Graphics.Backend = backend;
136             Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
137
138             if (windowSize != null) { _windowSize2D = windowSize; }
139             if (windowPosition != null) { _windowPosition2D = windowPosition; }
140             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
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 Window.Instance;
200             }
201         }
202
203         /// <summary>
204         /// Gets the default window.
205         /// </summary>
206         /// <returns>The default Window.</returns>
207         /// <since_tizen> 6 </since_tizen>
208         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
209         [EditorBrowsable(EditorBrowsableState.Never)]
210         public static Window GetDefaultWindow()
211         {
212             return Window.Instance;
213         }
214
215         internal Application ApplicationHandle
216         {
217             get
218             {
219                 return ((NUICoreBackend)this.Backend).ApplicationHandle;
220             }
221         }
222
223         /// <summary>
224         /// Register the assembly to XAML.
225         /// </summary>
226         /// <since_tizen> 5 </since_tizen>
227         public static void RegisterAssembly(Assembly assembly)
228         {
229             XamlParser.s_assemblies.Add(assembly);
230         }
231
232         /// <summary>
233         /// Runs the NUIApplication.
234         /// </summary>
235         /// <param name="args">Arguments from commandline.</param>
236         /// <since_tizen> 4 </since_tizen>
237         public override void Run(string[] args)
238         {
239             Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
240             Backend.AddEventHandler(EventType.Resumed, OnResume);
241             Backend.AddEventHandler(EventType.Paused, OnPause);
242             base.Run(args);
243         }
244
245         /// <summary>
246         /// Exits the NUIApplication.
247         /// </summary>
248         /// <since_tizen> 4 </since_tizen>
249         public override void Exit()
250         {
251             base.Exit();
252         }
253
254         /// <summary>
255         /// Ensures that the function passed in is called from the main loop when it is idle.
256         /// </summary>
257         /// <param name="func">The function to call</param>
258         /// <returns>true if added successfully, false otherwise</returns>
259         /// <since_tizen> 4 </since_tizen>
260         public bool AddIdle(System.Delegate func)
261         {
262             return ((NUICoreBackend)this.Backend).AddIdle(func);
263         }
264
265         /// <summary>
266         /// Sets the number of frames per render.
267         /// </summary>
268         /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
269         /// <remarks>
270         /// Suggest this is a power of two:
271         /// 1 - render each vsync frame.
272         /// 2 - render every other vsync frame.
273         /// 4 - render every fourth vsync frame.
274         /// 8 - render every eighth vsync frame. <br />
275         /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
276         ///</remarks>
277         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
278         [EditorBrowsable(EditorBrowsableState.Never)]
279         public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
280         {
281             Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
282         }
283
284         /// <summary>
285         /// Overrides this method if you want to handle behavior.
286         /// </summary>
287         /// <since_tizen> 3 </since_tizen>
288         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
289         {
290             base.OnLocaleChanged(e);
291         }
292
293         /// <summary>
294         /// Overrides this method if you want to handle behavior.
295         /// </summary>
296         /// <since_tizen> 3 </since_tizen>
297         protected override void OnLowBattery(LowBatteryEventArgs e)
298         {
299             base.OnLowBattery(e);
300         }
301
302         /// <summary>
303         /// Overrides this method if you want to handle behavior.
304         /// </summary>
305         /// <since_tizen> 3 </since_tizen>
306         protected override void OnLowMemory(LowMemoryEventArgs e)
307         {
308             base.OnLowMemory(e);
309         }
310
311         /// <summary>
312         /// Overrides this method if you want to handle behavior.
313         /// </summary>
314         /// <since_tizen> 3 </since_tizen>
315         protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
316         {
317             base.OnRegionFormatChanged(e);
318         }
319
320         /// <summary>
321         /// Overrides this method if you want to handle behavior.
322         /// </summary>
323         /// <since_tizen> 3 </since_tizen>
324         protected override void OnTerminate()
325         {
326             base.OnTerminate();
327         }
328
329         /// <summary>
330         /// Overrides this method if you want to handle behavior.
331         /// </summary>
332         /// <since_tizen> 3 </since_tizen>
333         protected virtual void OnPause()
334         {
335             Paused?.Invoke(this, EventArgs.Empty);
336         }
337
338         /// <summary>
339         /// Overrides this method if you want to handle behavior.
340         /// </summary>
341         /// <since_tizen> 3 </since_tizen>
342         protected virtual void OnResume()
343         {
344             Resumed?.Invoke(this, EventArgs.Empty);
345         }
346
347         /// <summary>
348         /// Overrides this method if you want to handle behavior.
349         /// </summary>
350         /// <since_tizen> 3 </since_tizen>
351         protected virtual void OnPreCreate()
352         {
353         }
354
355         /// <summary>
356         /// Overrides this method if you want to handle behavior.
357         /// </summary>
358         /// <since_tizen> 3 </since_tizen>
359         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
360         {
361             if (e != null)
362             {
363                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
364                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
365             }
366             base.OnAppControlReceived(e);
367         }
368
369         /// <summary>
370         /// Overrides this method if you want to handle behavior.
371         /// </summary>
372         /// <since_tizen> 3 </since_tizen>
373         protected override void OnCreate()
374         {
375             base.OnCreate();
376         }
377     }
378
379     /// <summary>
380     /// Graphics Backend Type.
381     /// </summary>
382     [EditorBrowsable(EditorBrowsableState.Never)]
383     [Obsolete("Please do not use! This will be deprecated!")]
384     public class Graphics
385     {
386         /// <summary>
387         /// Graphics Backend Type.
388         /// </summary>
389         public enum BackendType
390         {
391             /// <summary>
392             /// The GLES backend.
393             /// </summary>
394             Gles,
395             /// <summary>
396             /// The Vulkan backend.
397             /// </summary>
398             Vulkan
399         }
400
401         /// <summary>
402         /// The backend used by the NUIApplication.
403         /// </summary>
404         [EditorBrowsable(EditorBrowsableState.Never)]
405         public static BackendType Backend = BackendType.Gles;
406
407         internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
408         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
409     }
410
411 }