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;
40 private static string currentLoadedXaml = null;
43 /// Xaml loaded delegate.
45 [EditorBrowsable(EditorBrowsableState.Never)]
46 public delegate void XamlLoadedHandler(string xamlName);
48 static NUIApplication()
50 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
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 opened 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. currently 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 /// The constructor with theme option.
146 /// <param name="option">The theme option.</param>
147 [EditorBrowsable(EditorBrowsableState.Never)]
148 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
149 public NUIApplication(ThemeOptions option) : base(new NUICoreBackend())
151 ApplyThemeOption(option);
155 /// The constructor with window size and position and theme option.
157 /// <param name="windowSize">The window size.</param>
158 /// <param name="windowPosition">The window position.</param>
159 /// <param name="option">The theme option.</param>
160 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
161 [EditorBrowsable(EditorBrowsableState.Never)]
162 public NUIApplication(Size2D windowSize, Position2D windowPosition, ThemeOptions option) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
164 ApplyThemeOption(option);
168 /// The constructor with a stylesheet, window mode and default window type.
169 /// It is the only way to create an IME window.
171 /// <param name="styleSheet">The styleSheet URL.</param>
172 /// <param name="windowMode">The windowMode.</param>
173 /// <param name="type">The default window type.</param>
174 /// <since_tizen> 9 </since_tizen>
175 public NUIApplication(string styleSheet, WindowMode windowMode, WindowType type) : base(new NUICoreBackend(styleSheet, windowMode, type))
177 ExternalThemeManager.Initialize();
181 /// Occurs whenever the application is resumed.
183 /// <since_tizen> 4 </since_tizen>
184 public event EventHandler Resumed;
187 /// Occurs whenever the application is paused.
189 /// <since_tizen> 4 </since_tizen>
190 public event EventHandler Paused;
193 /// Xaml loaded event.
195 [EditorBrowsable(EditorBrowsableState.Never)]
196 public static event XamlLoadedHandler XamlLoaded;
199 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
201 /// <since_tizen> 3 </since_tizen>
202 public enum WindowMode
207 /// <since_tizen> 3 </since_tizen>
212 /// <since_tizen> 3 </since_tizen>
217 /// Enumeration for theme options of the NUIApplication.
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 public enum ThemeOptions : int
224 /// No option specified.
226 [EditorBrowsable(EditorBrowsableState.Never)]
230 /// Enable platform theme.
231 /// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
233 [EditorBrowsable(EditorBrowsableState.Never)]
234 PlatformThemeEnabled = 1 << 0,
237 /// Sets the default value of View.ThemeChangeSensitive.
238 /// when this option is on, all views are made sensitive on theme changing by default.
240 [EditorBrowsable(EditorBrowsableState.Never)]
241 ThemeChangeSensitive = 1 << 1,
245 /// Current loaded xaml's full name.
247 [EditorBrowsable(EditorBrowsableState.Never)]
248 public static string CurrentLoadedXaml
252 return currentLoadedXaml;
256 if (currentLoadedXaml != value)
258 currentLoadedXaml = value;
259 XamlLoaded?.Invoke(value);
265 /// ResourceManager to handle multilingual.
267 /// <since_tizen> 4 </since_tizen>
268 public static System.Resources.ResourceManager MultilingualResourceManager
272 return resourceManager;
276 resourceManager = value;
281 /// Gets the window instance.
283 /// <since_tizen> 3 </since_tizen>
284 [Obsolete("Please do not use! This will be deprecated!")]
285 [EditorBrowsable(EditorBrowsableState.Never)]
290 return GetDefaultWindow();
295 /// Gets the Application Id.
297 /// <since_tizen> 6 </since_tizen>
298 [EditorBrowsable(EditorBrowsableState.Never)]
303 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
308 /// Gets the default window.
310 /// <returns>The default Window.</returns>
311 /// <since_tizen> 6 </since_tizen>
312 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
313 [EditorBrowsable(EditorBrowsableState.Never)]
314 public static Window GetDefaultWindow()
316 return Window.Instance;
319 internal Application ApplicationHandle
323 return ((NUICoreBackend)this.Backend).ApplicationHandle;
328 /// Register the assembly to XAML.
330 /// <since_tizen> 5 </since_tizen>
331 public static void RegisterAssembly(Assembly assembly)
333 XamlParser.s_assemblies.Add(assembly);
337 /// Runs the NUIApplication.
339 /// <param name="args">Arguments from commandline.</param>
340 /// <since_tizen> 4 </since_tizen>
341 public override void Run(string[] args)
343 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
344 Backend.AddEventHandler(EventType.Resumed, OnResume);
345 Backend.AddEventHandler(EventType.Paused, OnPause);
350 /// Exits the NUIApplication.
352 /// <since_tizen> 4 </since_tizen>
353 public override void Exit()
359 /// Ensures that the function passed in is called from the main loop when it is idle.
361 /// <param name="func">The function to call</param>
362 /// <returns>true if added successfully, false otherwise</returns>
363 /// <since_tizen> 4 </since_tizen>
364 public bool AddIdle(System.Delegate func)
366 return ((NUICoreBackend)this.Backend).AddIdle(func);
370 /// Sets the number of frames per render.
372 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
374 /// Suggest this is a power of two:
375 /// 1 - render each vsync frame.
376 /// 2 - render every other vsync frame.
377 /// 4 - render every fourth vsync frame.
378 /// 8 - render every eighth vsync frame. <br />
379 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
381 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
382 [EditorBrowsable(EditorBrowsableState.Never)]
383 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
385 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
389 /// Overrides this method if you want to handle behavior.
391 /// <since_tizen> 3 </since_tizen>
392 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
394 base.OnLocaleChanged(e);
398 /// Overrides this method if you want to handle behavior.
400 /// <since_tizen> 3 </since_tizen>
401 protected override void OnLowBattery(LowBatteryEventArgs e)
403 base.OnLowBattery(e);
407 /// Overrides this method if you want to handle behavior.
409 /// <since_tizen> 3 </since_tizen>
410 protected override void OnLowMemory(LowMemoryEventArgs e)
416 /// Overrides this method if you want to handle behavior.
418 /// <since_tizen> 3 </since_tizen>
419 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
421 base.OnRegionFormatChanged(e);
425 /// Overrides this method if you want to handle behavior.
427 /// <since_tizen> 3 </since_tizen>
428 protected override void OnTerminate()
434 /// Overrides this method if you want to handle behavior.
436 /// <since_tizen> 3 </since_tizen>
437 protected virtual void OnPause()
439 Paused?.Invoke(this, EventArgs.Empty);
443 /// Overrides this method if you want to handle behavior.
445 /// <since_tizen> 3 </since_tizen>
446 protected virtual void OnResume()
448 Resumed?.Invoke(this, EventArgs.Empty);
452 /// Overrides this method if you want to handle behavior.
454 /// <since_tizen> 3 </since_tizen>
455 protected virtual void OnPreCreate()
460 /// Overrides this method if you want to handle behavior.
462 /// <since_tizen> 3 </since_tizen>
463 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
467 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
468 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
470 base.OnAppControlReceived(e);
474 /// Overrides this method if you want to handle behavior.
476 /// <since_tizen> 3 </since_tizen>
477 protected override void OnCreate()
483 /// This is used to improve application launch performance.
485 [EditorBrowsable(EditorBrowsableState.Never)]
486 static public void Preload()
488 Interop.Application.PreInitialize();
489 #if ExternalThemeEnabled
490 ThemeManager.Preload();
496 /// This is used to improve application launch performance.
498 [EditorBrowsable(EditorBrowsableState.Never)]
499 public void SendLaunchRequest(AppControl appControl)
501 TransitionOptions?.SendLaunchRequest(appControl);
505 /// This is used to improve application launch performance.
507 [EditorBrowsable(EditorBrowsableState.Never)]
508 public TransitionOptions TransitionOptions { get; set; }
511 /// Check if it is loaded as dotnet-loader-nui.
513 static internal bool IsPreload { get; set; }
515 private void ApplyThemeOption(ThemeOptions option)
517 if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
519 ThemeManager.PlatformThemeEnabled = true;
522 if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
524 ThemeManager.ApplicationThemeChangeSensitive = true;
530 /// Graphics Backend Type.
532 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
533 [EditorBrowsable(EditorBrowsableState.Never)]
534 [Obsolete("Please do not use! This will be deprecated!")]
535 public class Graphics
538 /// Graphics Backend Type.
540 public enum BackendType
543 /// The GLES backend.
547 /// The Vulkan backend.
553 /// The backend used by the NUIApplication.
555 [EditorBrowsable(EditorBrowsableState.Never)]
556 internal static BackendType Backend = BackendType.Gles;
558 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
559 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";