[NUI] Sync with dalihub (#693)
[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())
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))
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))
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))
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         internal Application ApplicationHandle
204         {
205             get
206             {
207                 return ((NUICoreBackend)this.Backend).ApplicationHandle;
208             }
209         }
210
211         /// <summary>
212         /// Register the assembly to XAML.
213         /// </summary>
214         /// <since_tizen> 5 </since_tizen>
215         public static void RegisterAssembly(Assembly assembly)
216         {
217             XamlParser.s_assemblies.Add(assembly);
218         }
219
220         /// <summary>
221         /// Runs the NUIApplication.
222         /// </summary>
223         /// <param name="args">Arguments from commandline.</param>
224         /// <since_tizen> 4 </since_tizen>
225         public override void Run(string[] args)
226         {
227             Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
228             Backend.AddEventHandler(EventType.Resumed, OnResume);
229             Backend.AddEventHandler(EventType.Paused, OnPause);
230             base.Run(args);
231         }
232
233         /// <summary>
234         /// Exits the NUIApplication.
235         /// </summary>
236         /// <since_tizen> 4 </since_tizen>
237         public override void Exit()
238         {
239             base.Exit();
240         }
241
242         /// <summary>
243         /// Ensures that the function passed in is called from the main loop when it is idle.
244         /// </summary>
245         /// <param name="func">The function to call</param>
246         /// <returns>true if added successfully, false otherwise</returns>
247         /// <since_tizen> 4 </since_tizen>
248         public bool AddIdle(System.Delegate func)
249         {
250             return ((NUICoreBackend)this.Backend).AddIdle(func);
251         }
252
253         /// <summary>
254         /// Overrides this method if you want to handle behavior.
255         /// </summary>
256         /// <since_tizen> 3 </since_tizen>
257         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
258         {
259             base.OnLocaleChanged(e);
260         }
261
262         /// <summary>
263         /// Overrides this method if you want to handle behavior.
264         /// </summary>
265         /// <since_tizen> 3 </since_tizen>
266         protected override void OnLowBattery(LowBatteryEventArgs e)
267         {
268             base.OnLowBattery(e);
269         }
270
271         /// <summary>
272         /// Overrides this method if you want to handle behavior.
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         protected override void OnLowMemory(LowMemoryEventArgs e)
276         {
277             base.OnLowMemory(e);
278         }
279
280         /// <summary>
281         /// Overrides this method if you want to handle behavior.
282         /// </summary>
283         /// <since_tizen> 3 </since_tizen>
284         protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
285         {
286             base.OnRegionFormatChanged(e);
287         }
288
289         /// <summary>
290         /// Overrides this method if you want to handle behavior.
291         /// </summary>
292         /// <since_tizen> 3 </since_tizen>
293         protected override void OnTerminate()
294         {
295             base.OnTerminate();
296         }
297
298         /// <summary>
299         /// Overrides this method if you want to handle behavior.
300         /// </summary>
301         /// <since_tizen> 3 </since_tizen>
302         protected virtual void OnPause()
303         {
304             Paused?.Invoke(this, EventArgs.Empty);
305         }
306
307         /// <summary>
308         /// Overrides this method if you want to handle behavior.
309         /// </summary>
310         /// <since_tizen> 3 </since_tizen>
311         protected virtual void OnResume()
312         {
313             Resumed?.Invoke(this, EventArgs.Empty);
314         }
315
316         /// <summary>
317         /// Overrides this method if you want to handle behavior.
318         /// </summary>
319         /// <since_tizen> 3 </since_tizen>
320         protected virtual void OnPreCreate()
321         {
322             if (_windowSize2D != null)
323             {
324                 Window.Instance.WindowSize = _windowSize2D;
325             }
326             if (_windowPosition2D != null)
327             {
328                 Window.Instance.WindowPosition = _windowPosition2D;
329             }
330         }
331
332         /// <summary>
333         /// Overrides this method if you want to handle behavior.
334         /// </summary>
335         /// <since_tizen> 3 </since_tizen>
336         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
337         {
338             if (e != null)
339             {
340                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
341                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
342             }
343             base.OnAppControlReceived(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 OnCreate()
351         {
352             base.OnCreate();
353             Device.PlatformServices = new TizenPlatformServices();
354         }
355     }
356
357     /// <summary>
358     /// Graphics Backend Type.
359     /// </summary>
360     [EditorBrowsable(EditorBrowsableState.Never)]
361     [Obsolete("Please do not use! This will be deprecated!")]
362     public class Graphics
363     {
364         /// <summary>
365         /// Graphics Backend Type.
366         /// </summary>
367         public enum BackendType
368         {
369             /// <summary>
370             /// The GLES backend.
371             /// </summary>
372             Gles,
373             /// <summary>
374             /// The Vulkan backend.
375             /// </summary>
376             Vulkan
377         }
378
379         /// <summary>
380         /// The backend used by the NUIApplication.
381         /// </summary>
382         [EditorBrowsable(EditorBrowsableState.Never)]
383         public static BackendType Backend = BackendType.Gles;
384
385         internal const string GlesCSharpBinder = "libdali-csharp-binder.so";
386         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
387     }
388
389 }