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())
56 /// The constructor with window size and position.
58 /// <param name="windowSize">The window size.</param>
59 /// <param name="windowPosition">The window position.</param>
60 /// <since_tizen> 5 </since_tizen>
61 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
62 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
63 [EditorBrowsable(EditorBrowsableState.Never)]
64 public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
69 /// The constructor with a stylesheet.
71 /// <param name="styleSheet">The styleSheet url.</param>
72 /// <since_tizen> 3 </since_tizen>
73 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
74 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
79 /// The constructor with a stylesheet, window size, and position.
81 /// <param name="styleSheet">The styleSheet URL.</param>
82 /// <param name="windowSize">The window size.</param>
83 /// <param name="windowPosition">The window position.</param>
84 /// <since_tizen> 5 </since_tizen>
85 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
86 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
87 [EditorBrowsable(EditorBrowsableState.Never)]
88 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
93 /// The constructor with a stylesheet and window mode.
95 /// <param name="styleSheet">The styleSheet url.</param>
96 /// <param name="windowMode">The windowMode.</param>
97 /// <since_tizen> 3 </since_tizen>
98 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
99 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
104 /// The constructor with a stylesheet, window mode, window size, and position.
106 /// <param name="styleSheet">The styleSheet URL.</param>
107 /// <param name="windowMode">The windowMode.</param>
108 /// <param name="windowSize">The window size.</param>
109 /// <param name="windowPosition">The window position.</param>
110 /// <since_tizen> 5 </since_tizen>
111 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
112 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
113 [EditorBrowsable(EditorBrowsableState.Never)]
114 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
119 /// Internal inhouse constructor with Graphics Backend Type
121 /// <param name="backend"></param>
122 /// <param name="windowMode"></param>
123 /// <param name="windowSize"></param>
124 /// <param name="windowPosition"></param>
125 /// <param name="styleSheet"></param>
126 /// InhouseAPI, this could be opened in NextTizen
127 [Obsolete("Please do not use! This will be deprecated!")]
128 [EditorBrowsable(EditorBrowsableState.Never)]
129 public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
131 //windowMode and styleSheet will be added later. currently it's not working as expected.
132 Graphics.Backend = backend;
133 Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
137 /// The constructor with theme option.
139 /// <param name="option">The theme option.</param>
140 [EditorBrowsable(EditorBrowsableState.Never)]
141 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
142 public NUIApplication(ThemeOptions option) : base(new NUICoreBackend())
144 ApplyThemeOption(option);
148 /// The constructor with window size and position and theme option.
150 /// <param name="windowSize">The window size.</param>
151 /// <param name="windowPosition">The window position.</param>
152 /// <param name="option">The theme option.</param>
153 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
154 [EditorBrowsable(EditorBrowsableState.Never)]
155 public NUIApplication(Size2D windowSize, Position2D windowPosition, ThemeOptions option) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
157 ApplyThemeOption(option);
161 /// Occurs whenever the application is resumed.
163 /// <since_tizen> 4 </since_tizen>
164 public event EventHandler Resumed;
167 /// Occurs whenever the application is paused.
169 /// <since_tizen> 4 </since_tizen>
170 public event EventHandler Paused;
173 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
175 /// <since_tizen> 3 </since_tizen>
176 public enum WindowMode
181 /// <since_tizen> 3 </since_tizen>
186 /// <since_tizen> 3 </since_tizen>
191 /// Enumeration for theme options of the NUIApplication.
194 [EditorBrowsable(EditorBrowsableState.Never)]
195 public enum ThemeOptions : int
198 /// No option specified.
200 [EditorBrowsable(EditorBrowsableState.Never)]
204 /// Enable platform theme.
205 /// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
207 [EditorBrowsable(EditorBrowsableState.Never)]
208 PlatformThemeEnabled = 1 << 0,
211 /// Sets the default value of View.ThemeChangeSensitive.
212 /// when this option is on, all views are made sensitive on theme changing by default.
214 [EditorBrowsable(EditorBrowsableState.Never)]
215 ThemeChangeSensitive = 1 << 1,
219 /// ResourceManager to handle multilingual.
221 /// <since_tizen> 4 </since_tizen>
222 public static System.Resources.ResourceManager MultilingualResourceManager
226 return resourceManager;
230 resourceManager = value;
235 /// Gets the window instance.
237 /// <since_tizen> 3 </since_tizen>
238 [Obsolete("Please do not use! This will be deprecated!")]
239 [EditorBrowsable(EditorBrowsableState.Never)]
244 return GetDefaultWindow();
249 /// Gets the Application Id.
251 /// <since_tizen> 6 </since_tizen>
252 [EditorBrowsable(EditorBrowsableState.Never)]
257 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
262 /// Gets the default window.
264 /// <returns>The default Window.</returns>
265 /// <since_tizen> 6 </since_tizen>
266 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
267 [EditorBrowsable(EditorBrowsableState.Never)]
268 public static Window GetDefaultWindow()
270 return Window.Instance;
273 internal Application ApplicationHandle
277 return ((NUICoreBackend)this.Backend).ApplicationHandle;
282 /// Register the assembly to XAML.
284 /// <since_tizen> 5 </since_tizen>
285 public static void RegisterAssembly(Assembly assembly)
287 XamlParser.s_assemblies.Add(assembly);
291 /// Runs the NUIApplication.
293 /// <param name="args">Arguments from commandline.</param>
294 /// <since_tizen> 4 </since_tizen>
295 public override void Run(string[] args)
297 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
298 Backend.AddEventHandler(EventType.Resumed, OnResume);
299 Backend.AddEventHandler(EventType.Paused, OnPause);
304 /// Exits the NUIApplication.
306 /// <since_tizen> 4 </since_tizen>
307 public override void Exit()
313 /// Ensures that the function passed in is called from the main loop when it is idle.
315 /// <param name="func">The function to call</param>
316 /// <returns>true if added successfully, false otherwise</returns>
317 /// <since_tizen> 4 </since_tizen>
318 public bool AddIdle(System.Delegate func)
320 return ((NUICoreBackend)this.Backend).AddIdle(func);
324 /// Sets the number of frames per render.
326 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
328 /// Suggest this is a power of two:
329 /// 1 - render each vsync frame.
330 /// 2 - render every other vsync frame.
331 /// 4 - render every fourth vsync frame.
332 /// 8 - render every eighth vsync frame. <br />
333 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
335 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
336 [EditorBrowsable(EditorBrowsableState.Never)]
337 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
339 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
343 /// Overrides this method if you want to handle behavior.
345 /// <since_tizen> 3 </since_tizen>
346 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
348 base.OnLocaleChanged(e);
352 /// Overrides this method if you want to handle behavior.
354 /// <since_tizen> 3 </since_tizen>
355 protected override void OnLowBattery(LowBatteryEventArgs e)
357 base.OnLowBattery(e);
361 /// Overrides this method if you want to handle behavior.
363 /// <since_tizen> 3 </since_tizen>
364 protected override void OnLowMemory(LowMemoryEventArgs e)
370 /// Overrides this method if you want to handle behavior.
372 /// <since_tizen> 3 </since_tizen>
373 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
375 base.OnRegionFormatChanged(e);
379 /// Overrides this method if you want to handle behavior.
381 /// <since_tizen> 3 </since_tizen>
382 protected override void OnTerminate()
388 /// Overrides this method if you want to handle behavior.
390 /// <since_tizen> 3 </since_tizen>
391 protected virtual void OnPause()
393 Paused?.Invoke(this, EventArgs.Empty);
397 /// Overrides this method if you want to handle behavior.
399 /// <since_tizen> 3 </since_tizen>
400 protected virtual void OnResume()
402 Resumed?.Invoke(this, EventArgs.Empty);
406 /// Overrides this method if you want to handle behavior.
408 /// <since_tizen> 3 </since_tizen>
409 protected virtual void OnPreCreate()
414 /// Overrides this method if you want to handle behavior.
416 /// <since_tizen> 3 </since_tizen>
417 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
421 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
422 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
424 base.OnAppControlReceived(e);
428 /// Overrides this method if you want to handle behavior.
430 /// <since_tizen> 3 </since_tizen>
431 protected override void OnCreate()
437 /// This is used to improve application launch performance.
439 [EditorBrowsable(EditorBrowsableState.Never)]
440 static public void Preload()
442 Interop.Application.PreInitialize();
443 ThemeManager.Preload();
448 /// This is used to improve application launch performance.
450 [EditorBrowsable(EditorBrowsableState.Never)]
451 public void SendLaunchRequest(AppControl appControl)
453 TransitionOptions?.SendLaunchRequest(appControl);
457 /// This is used to improve application launch performance.
459 [EditorBrowsable(EditorBrowsableState.Never)]
460 public TransitionOptions TransitionOptions { get; set; }
463 /// Check if it is loaded as dotnet-loader-nui.
465 static internal bool IsPreload { get; set; }
467 private void ApplyThemeOption(ThemeOptions option)
469 if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
471 ThemeManager.PlatformThemeEnabled = true;
474 if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
476 ThemeManager.ApplicationThemeChangeSensitive = true;
482 /// Graphics Backend Type.
484 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
485 [EditorBrowsable(EditorBrowsableState.Never)]
486 [Obsolete("Please do not use! This will be deprecated!")]
487 public class Graphics
490 /// Graphics Backend Type.
492 public enum BackendType
495 /// The GLES backend.
499 /// The Vulkan backend.
505 /// The backend used by the NUIApplication.
507 [EditorBrowsable(EditorBrowsableState.Never)]
508 internal static BackendType Backend = BackendType.Gles;
510 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
511 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";