2 * Copyright (c) 2021 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;
31 /// Represents an application that have a UI screen. The NUIApplication class has a default stage.
33 /// <since_tizen> 3 </since_tizen>
34 public class NUIApplication : CoreApplication
37 /// The instance of ResourceManager.
39 private static System.Resources.ResourceManager resourceManager = null;
41 static NUIApplication()
43 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
47 /// The default constructor.
49 /// <since_tizen> 3 </since_tizen>
50 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
51 public NUIApplication() : base(new NUICoreBackend())
53 ExternalThemeManager.Initialize();
57 /// The constructor with window size and position.
59 /// <param name="windowSize">The window size.</param>
60 /// <param name="windowPosition">The window position.</param>
61 /// <since_tizen> 5 </since_tizen>
62 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
63 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
64 [EditorBrowsable(EditorBrowsableState.Never)]
65 public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
67 ExternalThemeManager.Initialize();
71 /// The constructor with a stylesheet.
73 /// <param name="styleSheet">The styleSheet url.</param>
74 /// <since_tizen> 3 </since_tizen>
75 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
76 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
78 ExternalThemeManager.Initialize();
82 /// The constructor with a stylesheet, window size, and position.
84 /// <param name="styleSheet">The styleSheet URL.</param>
85 /// <param name="windowSize">The window size.</param>
86 /// <param name="windowPosition">The window position.</param>
87 /// <since_tizen> 5 </since_tizen>
88 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
89 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
93 ExternalThemeManager.Initialize();
97 /// The constructor with a stylesheet and window mode.
99 /// <param name="styleSheet">The styleSheet url.</param>
100 /// <param name="windowMode">The windowMode.</param>
101 /// <since_tizen> 3 </since_tizen>
102 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
103 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
105 ExternalThemeManager.Initialize();
109 /// The constructor with a stylesheet, window mode, window size, and position.
111 /// <param name="styleSheet">The styleSheet URL.</param>
112 /// <param name="windowMode">The windowMode.</param>
113 /// <param name="windowSize">The window size.</param>
114 /// <param name="windowPosition">The window position.</param>
115 /// <since_tizen> 5 </since_tizen>
116 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
117 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
118 [EditorBrowsable(EditorBrowsableState.Never)]
119 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
121 ExternalThemeManager.Initialize();
125 /// Internal inhouse constructor with Graphics Backend Type
127 /// <param name="backend"></param>
128 /// <param name="windowMode"></param>
129 /// <param name="windowSize"></param>
130 /// <param name="windowPosition"></param>
131 /// <param name="styleSheet"></param>
132 /// InhouseAPI, this could be opened in NextTizen
133 [Obsolete("Please do not use! This will be deprecated!")]
134 [EditorBrowsable(EditorBrowsableState.Never)]
135 public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
137 //windowMode and styleSheet will be added later. currently it's not working as expected.
138 Graphics.Backend = backend;
139 Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
140 ExternalThemeManager.Initialize();
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();
398 ThemeManager.AddPackageTheme(DefaultThemeCreator.Instance);
403 /// This is used to improve application launch performance.
405 [EditorBrowsable(EditorBrowsableState.Never)]
406 public void SendLaunchRequest(AppControl appControl)
408 TransitionOptions?.SendLaunchRequest(appControl);
412 /// This is used to improve application launch performance.
414 [EditorBrowsable(EditorBrowsableState.Never)]
415 public TransitionOptions TransitionOptions { get; set; }
418 /// Check if it is loaded as dotnet-loader-nui.
420 static internal bool IsPreload { get; set; }
424 /// Graphics Backend Type.
426 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
427 [EditorBrowsable(EditorBrowsableState.Never)]
428 [Obsolete("Please do not use! This will be deprecated!")]
429 public class Graphics
432 /// Graphics Backend Type.
434 public enum BackendType
437 /// The GLES backend.
441 /// The Vulkan backend.
447 /// The backend used by the NUIApplication.
449 [EditorBrowsable(EditorBrowsableState.Never)]
450 internal static BackendType Backend = BackendType.Gles;
452 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
453 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";