2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.ComponentModel;
20 using System.Diagnostics.CodeAnalysis;
21 using System.Threading;
22 using System.Reflection;
23 using Tizen.Applications;
24 using Tizen.Applications.CoreBackend;
25 using Tizen.NUI.Binding;
27 using System.Collections.Generic;
33 /// Represents an application that have a UI screen. The NUIApplication class has a default stage.
35 /// <since_tizen> 3 </since_tizen>
36 public class NUIApplication : CoreApplication
39 /// The instance of ResourceManager.
41 private static System.Resources.ResourceManager resourceManager = null;
43 // TODO Enable this after tizen-theme-manager is released.
44 // private readonly ThemeLoader themeLoader = new ThemeLoader();
46 static NUIApplication()
48 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
49 // TODO Enable this after tizen-theme-manager is released.
50 // themeLoader.ThemeChanged += TizenThemeChanged;
54 /// The default constructor.
56 /// <since_tizen> 3 </since_tizen>
57 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
58 public NUIApplication() : base(new NUICoreBackend())
63 /// The constructor with window size and position.
65 /// <param name="windowSize">The window size.</param>
66 /// <param name="windowPosition">The window position.</param>
67 /// <since_tizen> 5 </since_tizen>
68 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
69 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
70 [EditorBrowsable(EditorBrowsableState.Never)]
71 public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
76 /// The constructor with a stylesheet.
78 /// <param name="styleSheet">The styleSheet url.</param>
79 /// <since_tizen> 3 </since_tizen>
80 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
81 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
86 /// The constructor with a stylesheet, window size, and position.
88 /// <param name="styleSheet">The styleSheet URL.</param>
89 /// <param name="windowSize">The window size.</param>
90 /// <param name="windowPosition">The window position.</param>
91 /// <since_tizen> 5 </since_tizen>
92 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
93 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
94 [EditorBrowsable(EditorBrowsableState.Never)]
95 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
100 /// The constructor with a stylesheet and window mode.
102 /// <param name="styleSheet">The styleSheet url.</param>
103 /// <param name="windowMode">The windowMode.</param>
104 /// <since_tizen> 3 </since_tizen>
105 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
106 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
111 /// The constructor with a stylesheet, window mode, window size, and position.
113 /// <param name="styleSheet">The styleSheet URL.</param>
114 /// <param name="windowMode">The windowMode.</param>
115 /// <param name="windowSize">The window size.</param>
116 /// <param name="windowPosition">The window position.</param>
117 /// <since_tizen> 5 </since_tizen>
118 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
119 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
120 [EditorBrowsable(EditorBrowsableState.Never)]
121 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
126 /// Internal inhouse constructor with Graphics Backend Type
128 /// <param name="backend"></param>
129 /// <param name="windowMode"></param>
130 /// <param name="windowSize"></param>
131 /// <param name="windowPosition"></param>
132 /// <param name="styleSheet"></param>
133 /// InhouseAPI, this could be opend in NextTizen
134 [Obsolete("Please do not use! This will be deprecated!")]
135 [EditorBrowsable(EditorBrowsableState.Never)]
136 public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
138 //windowMode and styleSheet will be added later. currenlty it's not working as expected.
139 Graphics.Backend = backend;
140 Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
144 /// Occurs whenever the application is resumed.
146 /// <since_tizen> 4 </since_tizen>
147 public event EventHandler Resumed;
150 /// Occurs whenever the application is paused.
152 /// <since_tizen> 4 </since_tizen>
153 public event EventHandler Paused;
156 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
158 /// <since_tizen> 3 </since_tizen>
159 public enum WindowMode
164 /// <since_tizen> 3 </since_tizen>
169 /// <since_tizen> 3 </since_tizen>
174 /// ResourceManager to handle multilingual.
176 /// <since_tizen> 4 </since_tizen>
177 public static System.Resources.ResourceManager MultilingualResourceManager
181 return resourceManager;
185 resourceManager = value;
190 /// Gets the window instance.
192 /// <since_tizen> 3 </since_tizen>
193 [Obsolete("Please do not use! This will be deprecated!")]
194 [EditorBrowsable(EditorBrowsableState.Never)]
199 return GetDefaultWindow();
204 /// Gets the Application Id.
206 /// <since_tizen> 6 </since_tizen>
207 [EditorBrowsable(EditorBrowsableState.Never)]
212 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
217 /// Gets the default window.
219 /// <returns>The default Window.</returns>
220 /// <since_tizen> 6 </since_tizen>
221 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
222 [EditorBrowsable(EditorBrowsableState.Never)]
223 public static Window GetDefaultWindow()
225 return Window.Instance;
228 internal Application ApplicationHandle
232 return ((NUICoreBackend)this.Backend).ApplicationHandle;
237 /// Register the assembly to XAML.
239 /// <since_tizen> 5 </since_tizen>
240 public static void RegisterAssembly(Assembly assembly)
242 XamlParser.s_assemblies.Add(assembly);
246 /// Runs the NUIApplication.
248 /// <param name="args">Arguments from commandline.</param>
249 /// <since_tizen> 4 </since_tizen>
250 public override void Run(string[] args)
252 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
253 Backend.AddEventHandler(EventType.Resumed, OnResume);
254 Backend.AddEventHandler(EventType.Paused, OnPause);
259 /// Exits the NUIApplication.
261 /// <since_tizen> 4 </since_tizen>
262 public override void Exit()
268 /// Ensures that the function passed in is called from the main loop when it is idle.
270 /// <param name="func">The function to call</param>
271 /// <returns>true if added successfully, false otherwise</returns>
272 /// <since_tizen> 4 </since_tizen>
273 public bool AddIdle(System.Delegate func)
275 return ((NUICoreBackend)this.Backend).AddIdle(func);
279 /// Sets the number of frames per render.
281 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
283 /// Suggest this is a power of two:
284 /// 1 - render each vsync frame.
285 /// 2 - render every other vsync frame.
286 /// 4 - render every fourth vsync frame.
287 /// 8 - render every eighth vsync frame. <br />
288 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
290 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
291 [EditorBrowsable(EditorBrowsableState.Never)]
292 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
294 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
298 /// Overrides this method if you want to handle behavior.
300 /// <since_tizen> 3 </since_tizen>
301 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
303 base.OnLocaleChanged(e);
307 /// Overrides this method if you want to handle behavior.
309 /// <since_tizen> 3 </since_tizen>
310 protected override void OnLowBattery(LowBatteryEventArgs e)
312 base.OnLowBattery(e);
316 /// Overrides this method if you want to handle behavior.
318 /// <since_tizen> 3 </since_tizen>
319 protected override void OnLowMemory(LowMemoryEventArgs e)
325 /// Overrides this method if you want to handle behavior.
327 /// <since_tizen> 3 </since_tizen>
328 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
330 base.OnRegionFormatChanged(e);
334 /// Overrides this method if you want to handle behavior.
336 /// <since_tizen> 3 </since_tizen>
337 protected override void OnTerminate()
343 /// Overrides this method if you want to handle behavior.
345 /// <since_tizen> 3 </since_tizen>
346 protected virtual void OnPause()
348 Paused?.Invoke(this, EventArgs.Empty);
352 /// Overrides this method if you want to handle behavior.
354 /// <since_tizen> 3 </since_tizen>
355 protected virtual void OnResume()
357 Resumed?.Invoke(this, EventArgs.Empty);
361 /// Overrides this method if you want to handle behavior.
363 /// <since_tizen> 3 </since_tizen>
364 protected virtual void OnPreCreate()
369 /// Overrides this method if you want to handle behavior.
371 /// <since_tizen> 3 </since_tizen>
372 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
376 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
377 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
379 base.OnAppControlReceived(e);
383 /// Overrides this method if you want to handle behavior.
385 /// <since_tizen> 3 </since_tizen>
386 protected override void OnCreate()
392 /// This is used to improve application launch performance.
394 [EditorBrowsable(EditorBrowsableState.Never)]
395 static public void Preload()
397 Interop.Application.PreInitialize();
402 /// This is used to improve application launch performance.
404 [EditorBrowsable(EditorBrowsableState.Never)]
405 public void SendLaunchRequest(AppControl appControl)
407 TransitionOptions?.SendLaunchRequest(appControl);
411 /// This is used to improve application launch performance.
413 [EditorBrowsable(EditorBrowsableState.Never)]
414 public TransitionOptions TransitionOptions { get; set; }
416 // TODO Enable this after tizen-theme-manager is released.
417 // private void TizenThemeChanged(object sender, ThemeEventArgs e)
419 // string prefix = "/theme/";
421 // Dictionary<string, string> changedResources = new Dictionary<string, string>();
422 // foreach (string key in ThemeManager.DefaultTheme.Resources.Keys)
424 // // NOTE Need improve this code by checking HasKey
425 // string newValue = null;
428 // newValue = e.Theme.GetString(prefix + key);
431 // if (newValue != null)
433 // changedResources[key] = newValue;
436 // ThemeManager.UpdateCurrentThemeResources(changedResources);
440 /// Check if it is loaded as dotnet-loader-nui.
442 static internal bool IsPreload { get; set; }
446 /// Graphics Backend Type.
448 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
449 [EditorBrowsable(EditorBrowsableState.Never)]
450 [Obsolete("Please do not use! This will be deprecated!")]
451 public class Graphics
454 /// Graphics Backend Type.
456 public enum BackendType
459 /// The GLES backend.
463 /// The Vulkan backend.
469 /// The backend used by the NUIApplication.
471 [EditorBrowsable(EditorBrowsableState.Never)]
472 internal static BackendType Backend = BackendType.Gles;
474 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
475 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";