2575bf923c7ffc9d63cc089e9b92c3cbbcd9fd07
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / NUIApplication.cs
1 /*
2  * Copyright (c) 2018 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         /// Occurs whenever the application is resumed.
38         /// </summary>
39         /// <since_tizen> 4 </since_tizen>
40         public event EventHandler Resumed;
41
42         /// <summary>
43         /// Occurs whenever the application is paused.
44         /// </summary>
45         /// <since_tizen> 4 </since_tizen>
46         public event EventHandler Paused;
47
48         /// <summary>
49         /// The instance of ResourceManager.
50         /// </summary>
51         private static System.Resources.ResourceManager resourceManager = null;
52
53         /// <summary>
54         /// The default constructor.
55         /// </summary>
56         /// <since_tizen> 3 </since_tizen>
57         public NUIApplication() : base(new NUICoreBackend())
58         {
59             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
60         }
61
62         private Size2D _windowSize2D = null;
63         private Position2D _windowPosition2D = null;
64         /// <summary>
65         /// The constructor with window size and position.
66         /// </summary>
67         /// <param name="windowSize">The window size.</param>
68         /// <param name="windowPosition">The window position.</param>
69         /// <since_tizen> 5 </since_tizen>
70         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend())
73         {
74             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
75             _windowSize2D = windowSize;
76             _windowPosition2D = windowPosition;
77         }
78
79         /// <summary>
80         /// The constructor with a stylesheet.
81         /// </summary>
82         /// <param name="styleSheet">The styleSheet url.</param>
83         /// <since_tizen> 3 </since_tizen>
84         public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
85         {
86             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
87         }
88
89         /// <summary>
90         /// The constructor with a stylesheet, window size, and position.
91         /// </summary>
92         /// <param name="styleSheet">The styleSheet URL.</param>
93         /// <param name="windowSize">The window size.</param>
94         /// <param name="windowPosition">The window position.</param>
95         /// <since_tizen> 5 </since_tizen>
96         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
97         [EditorBrowsable(EditorBrowsableState.Never)]
98         public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet))
99         {
100             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
101             _windowSize2D = windowSize;
102             _windowPosition2D = windowPosition;
103         }
104
105         /// <summary>
106         /// The constructor with a stylesheet and window mode.
107         /// </summary>
108         /// <param name="styleSheet">The styleSheet url.</param>
109         /// <param name="windowMode">The windowMode.</param>
110         /// <since_tizen> 3 </since_tizen>
111         public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
112         {
113             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
114         }
115
116         /// <summary>
117         /// The constructor with a stylesheet, window mode, window size, and position.
118         /// </summary>
119         /// <param name="styleSheet">The styleSheet URL.</param>
120         /// <param name="windowMode">The windowMode.</param>
121         /// <param name="windowSize">The window size.</param>
122         /// <param name="windowPosition">The window position.</param>
123         /// <since_tizen> 5 </since_tizen>
124         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
125         [EditorBrowsable(EditorBrowsableState.Never)]
126         public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode))
127         {
128             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
129             _windowSize2D = windowSize;
130             _windowPosition2D = windowPosition;
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         [EditorBrowsable(EditorBrowsableState.Never)]
143         public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode))
144         {
145             //windowMode and styleSheet will be added later. currenlty it's not working as expected.
146             Graphics.Backend = backend;
147             if (windowSize != null) { _windowSize2D = windowSize; }
148             if (windowPosition != null) { _windowPosition2D = windowPosition; }
149             Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
150         }
151
152         /// <summary>
153         /// Overrides this method if you want to handle behavior.
154         /// </summary>
155         /// <since_tizen> 3 </since_tizen>
156         protected override void OnLocaleChanged(LocaleChangedEventArgs e)
157         {
158             base.OnLocaleChanged(e);
159         }
160
161         /// <summary>
162         /// Overrides this method if you want to handle behavior.
163         /// </summary>
164         /// <since_tizen> 3 </since_tizen>
165         protected override void OnLowBattery(LowBatteryEventArgs e)
166         {
167             base.OnLowBattery(e);
168         }
169
170         /// <summary>
171         /// Overrides this method if you want to handle behavior.
172         /// </summary>
173         /// <since_tizen> 3 </since_tizen>
174         protected override void OnLowMemory(LowMemoryEventArgs e)
175         {
176             base.OnLowMemory(e);
177         }
178
179         /// <summary>
180         /// Overrides this method if you want to handle behavior.
181         /// </summary>
182         /// <since_tizen> 3 </since_tizen>
183         protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
184         {
185             base.OnRegionFormatChanged(e);
186         }
187
188         /// <summary>
189         /// Overrides this method if you want to handle behavior.
190         /// </summary>
191         /// <since_tizen> 3 </since_tizen>
192         protected override void OnTerminate()
193         {
194             base.OnTerminate();
195         }
196
197         /// <summary>
198         /// Overrides this method if you want to handle behavior.
199         /// </summary>
200         /// <since_tizen> 3 </since_tizen>
201         protected virtual void OnPause()
202         {
203             Paused?.Invoke(this, EventArgs.Empty);
204         }
205
206         /// <summary>
207         /// Overrides this method if you want to handle behavior.
208         /// </summary>
209         /// <since_tizen> 3 </since_tizen>
210         protected virtual void OnResume()
211         {
212             Resumed?.Invoke(this, EventArgs.Empty);
213         }
214
215         /// <summary>
216         /// Overrides this method if you want to handle behavior.
217         /// </summary>
218         /// <since_tizen> 3 </since_tizen>
219         protected virtual void OnPreCreate()
220         {
221             if (_windowSize2D != null)
222             {
223                 Window.Instance.WindowSize = _windowSize2D;
224             }
225             if (_windowPosition2D != null)
226             {
227                 Window.Instance.WindowPosition = _windowPosition2D;
228             }
229         }
230
231         /// <summary>
232         /// Overrides this method if you want to handle behavior.
233         /// </summary>
234         /// <since_tizen> 3 </since_tizen>
235         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
236         {
237             if (e != null)
238             {
239                 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
240                 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + "   IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
241             }
242             base.OnAppControlReceived(e);
243         }
244
245         /// <summary>
246         /// Overrides this method if you want to handle behavior.
247         /// </summary>
248         /// <since_tizen> 3 </since_tizen>
249         protected override void OnCreate()
250         {
251             base.OnCreate();
252             Device.PlatformServices = new TizenPlatformServices();
253         }
254
255         /// <summary>
256         /// Runs the NUIApplication.
257         /// </summary>
258         /// <param name="args">Arguments from commandline.</param>
259         /// <since_tizen> 4 </since_tizen>
260         public override void Run(string[] args)
261         {
262             Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
263             Backend.AddEventHandler(EventType.Resumed, OnResume);
264             Backend.AddEventHandler(EventType.Paused, OnPause);
265             base.Run(args);
266         }
267
268         /// <summary>
269         /// Exits the NUIApplication.
270         /// </summary>
271         /// <since_tizen> 4 </since_tizen>
272         public override void Exit()
273         {
274             base.Exit();
275         }
276
277         /// <summary>
278         /// Ensures that the function passed in is called from the main loop when it is idle.
279         /// </summary>
280         /// <param name="func">The function to call</param>
281         /// <returns>true if added successfully, false otherwise</returns>
282         /// <since_tizen> 4 </since_tizen>
283         public bool AddIdle(System.Delegate func)
284         {
285             return ((NUICoreBackend)this.Backend).AddIdle(func);
286         }
287
288         /// <summary>
289         /// Enumeration for deciding whether a NUI application window is opaque or transparent.
290         /// </summary>
291         /// <since_tizen> 3 </since_tizen>
292         public enum WindowMode
293         {
294             /// <summary>
295             /// Opaque
296             /// </summary>
297             /// <since_tizen> 3 </since_tizen>
298             Opaque = 0,
299             /// <summary>
300             /// Transparent
301             /// </summary>
302             /// <since_tizen> 3 </since_tizen>
303             Transparent = 1
304         }
305
306
307         internal Application ApplicationHandle
308         {
309             get
310             {
311                 return ((NUICoreBackend)this.Backend).ApplicationHandle;
312             }
313         }
314
315         /// <summary>
316         /// ResourceManager to handle multilingual.
317         /// </summary>
318         /// <since_tizen> 4 </since_tizen>
319         public static System.Resources.ResourceManager MultilingualResourceManager
320         {
321             get
322             {
323                 return resourceManager;
324             }
325             set
326             {
327                 resourceManager = value;
328             }
329         }
330
331         /// <summary>
332         /// Register the assembly to XAML.
333         /// </summary>
334         /// <since_tizen> 5 </since_tizen>
335         public static void RegisterAssembly(Assembly assembly)
336         {
337             XamlParser.s_assemblies.Add(assembly);
338         }
339
340         /// <summary>
341         /// Gets the window instance.
342         /// </summary>
343         /// <since_tizen> 3 </since_tizen>
344         [Obsolete("Please do not use! This will be deprecated!")]
345         [EditorBrowsable(EditorBrowsableState.Never)]
346         public Window Window
347         {
348             get
349             {
350                 return Window.Instance;
351             }
352         }
353     }
354
355     /// <summary>
356     /// Graphics BackendType
357     /// </summary>
358     /// InhouseAPI, this could be opend in NextTizen
359     [EditorBrowsable(EditorBrowsableState.Never)]
360     public class Graphics
361     {
362         public enum BackendType
363         {
364             Gles,
365             Vulkan
366         }
367         public static BackendType Backend = BackendType.Gles;
368         internal const string GlesCSharpBinder = "libdali-csharp-binder.so";
369         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
370     }
371
372 }