129d86b475f7e9562cd4c4a4bfb1c232b344e2d1
[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         /// <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         /// Overrides this method if you want to handle behavior.
267         /// </summary>
268         /// <since_tizen> 3 </since_tizen>
269         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
270         {
271             base.OnLocaleChanged(e);
272         }
273
274         /// <summary>
275         /// Overrides this method if you want to handle behavior.
276         /// </summary>
277         /// <since_tizen> 3 </since_tizen>
278         protected override void OnLowBattery(LowBatteryEventArgs e)
279         {
280             base.OnLowBattery(e);
281         }
282
283         /// <summary>
284         /// Overrides this method if you want to handle behavior.
285         /// </summary>
286         /// <since_tizen> 3 </since_tizen>
287         protected override void OnLowMemory(LowMemoryEventArgs e)
288         {
289             base.OnLowMemory(e);
290         }
291
292         /// <summary>
293         /// Overrides this method if you want to handle behavior.
294         /// </summary>
295         /// <since_tizen> 3 </since_tizen>
296         protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
297         {
298             base.OnRegionFormatChanged(e);
299         }
300
301         /// <summary>
302         /// Overrides this method if you want to handle behavior.
303         /// </summary>
304         /// <since_tizen> 3 </since_tizen>
305         protected override void OnTerminate()
306         {
307             base.OnTerminate();
308         }
309
310         /// <summary>
311         /// Overrides this method if you want to handle behavior.
312         /// </summary>
313         /// <since_tizen> 3 </since_tizen>
314         protected virtual void OnPause()
315         {
316             Paused?.Invoke(this, EventArgs.Empty);
317         }
318
319         /// <summary>
320         /// Overrides this method if you want to handle behavior.
321         /// </summary>
322         /// <since_tizen> 3 </since_tizen>
323         protected virtual void OnResume()
324         {
325             Resumed?.Invoke(this, EventArgs.Empty);
326         }
327
328         /// <summary>
329         /// Overrides this method if you want to handle behavior.
330         /// </summary>
331         /// <since_tizen> 3 </since_tizen>
332         protected virtual void OnPreCreate()
333         {
334         }
335
336         /// <summary>
337         /// Overrides this method if you want to handle behavior.
338         /// </summary>
339         /// <since_tizen> 3 </since_tizen>
340         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
341         {
342             if (e != null)
343             {
344                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
345                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
346             }
347             base.OnAppControlReceived(e);
348         }
349
350         /// <summary>
351         /// Overrides this method if you want to handle behavior.
352         /// </summary>
353         /// <since_tizen> 3 </since_tizen>
354         protected override void OnCreate()
355         {
356             base.OnCreate();
357             Device.PlatformServices = new TizenPlatformServices();
358         }
359     }
360
361     /// <summary>
362     /// Graphics Backend Type.
363     /// </summary>
364     [EditorBrowsable(EditorBrowsableState.Never)]
365     [Obsolete("Please do not use! This will be deprecated!")]
366     public class Graphics
367     {
368         /// <summary>
369         /// Graphics Backend Type.
370         /// </summary>
371         public enum BackendType
372         {
373             /// <summary>
374             /// The GLES backend.
375             /// </summary>
376             Gles,
377             /// <summary>
378             /// The Vulkan backend.
379             /// </summary>
380             Vulkan
381         }
382
383         /// <summary>
384         /// The backend used by the NUIApplication.
385         /// </summary>
386         [EditorBrowsable(EditorBrowsableState.Never)]
387         public static BackendType Backend = BackendType.Gles;
388
389         internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
390         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
391     }
392
393 }