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;
45 private bool borderEnabled = false;
46 private IBorderInterface borderInterface = null;
49 /// Xaml loaded delegate.
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public delegate void XamlLoadedHandler(string xamlName);
54 static NUIApplication()
56 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
60 /// The default constructor.
62 /// <since_tizen> 3 </since_tizen>
63 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
64 public NUIApplication() : base(new NUICoreBackend())
69 /// The constructor with window size and position.
71 /// <param name="windowSize">The window size.</param>
72 /// <param name="windowPosition">The window position.</param>
73 /// <since_tizen> 5 </since_tizen>
74 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
75 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
82 /// The constructor with a stylesheet.
84 /// <param name="styleSheet">The styleSheet url.</param>
85 /// <since_tizen> 3 </since_tizen>
86 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
87 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
92 /// The constructor with a stylesheet, window size, and position.
94 /// <param name="styleSheet">The styleSheet URL.</param>
95 /// <param name="windowSize">The window size.</param>
96 /// <param name="windowPosition">The window position.</param>
97 /// <since_tizen> 5 </since_tizen>
98 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
99 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
100 [EditorBrowsable(EditorBrowsableState.Never)]
101 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
106 /// The constructor with a stylesheet and window mode.
108 /// <param name="styleSheet">The styleSheet url.</param>
109 /// <param name="windowMode">The windowMode.</param>
110 /// <since_tizen> 3 </since_tizen>
111 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
112 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
117 /// The constructor with a stylesheet, window mode, window size, and position.
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 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
126 [EditorBrowsable(EditorBrowsableState.Never)]
127 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
132 /// Internal inhouse constructor with Graphics Backend Type
134 /// <param name="backend"></param>
135 /// <param name="windowMode"></param>
136 /// <param name="windowSize"></param>
137 /// <param name="windowPosition"></param>
138 /// <param name="styleSheet"></param>
139 /// InhouseAPI, this could be opened in NextTizen
140 [Obsolete("Please do not use! This will be deprecated!")]
141 [EditorBrowsable(EditorBrowsableState.Never)]
142 public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
144 //windowMode and styleSheet will be added later. currently it's not working as expected.
145 Graphics.Backend = backend;
146 Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
150 /// The constructor with theme option.
152 /// <param name="option">The theme option.</param>
153 [EditorBrowsable(EditorBrowsableState.Never)]
154 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
155 public NUIApplication(ThemeOptions option) : base(new NUICoreBackend())
157 ApplyThemeOption(option);
161 /// The constructor with window size and position and theme option.
163 /// <param name="windowSize">The window size.</param>
164 /// <param name="windowPosition">The window position.</param>
165 /// <param name="option">The theme option.</param>
166 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
167 [EditorBrowsable(EditorBrowsableState.Never)]
168 public NUIApplication(Size2D windowSize, Position2D windowPosition, ThemeOptions option) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
170 ApplyThemeOption(option);
174 /// The constructor with a stylesheet, window mode and default window type.
175 /// It is the only way to create an IME window.
177 /// <param name="styleSheet">The styleSheet URL.</param>
178 /// <param name="windowMode">The windowMode.</param>
179 /// <param name="type">The default window type.</param>
180 /// <since_tizen> 9 </since_tizen>
181 public NUIApplication(string styleSheet, WindowMode windowMode, WindowType type) : base(new NUICoreBackend(styleSheet, windowMode, type))
183 ExternalThemeManager.Initialize();
187 /// The constructor with a stylesheet, window mode, size, position, theme option and boderInterface
188 /// It is the only way to create an IME window.
190 /// <param name="styleSheet">The styleSheet URL.</param>
191 /// <param name="windowMode">The windowMode.</param>
192 /// <param name="windowSize">The window size.</param>
193 /// <param name="windowPosition">The window position.</param>
194 /// <param name="borderInterface"><see cref="Tizen.NUI.IBorderInterface"/>If borderInterface is null, defaultBorder is enabled.</param>
195 [EditorBrowsable(EditorBrowsableState.Never)]
196 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition, IBorderInterface borderInterface, WindowMode windowMode = WindowMode.Opaque) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
198 borderEnabled = true;
199 this.borderInterface = borderInterface;
203 /// Occurs whenever the application is resumed.
205 /// <since_tizen> 4 </since_tizen>
206 public event EventHandler Resumed;
209 /// Occurs whenever the application is paused.
211 /// <since_tizen> 4 </since_tizen>
212 public event EventHandler Paused;
215 /// Xaml loaded event.
217 [EditorBrowsable(EditorBrowsableState.Never)]
218 public static event XamlLoadedHandler XamlLoaded;
221 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
223 /// <since_tizen> 3 </since_tizen>
224 public enum WindowMode
229 /// <since_tizen> 3 </since_tizen>
234 /// <since_tizen> 3 </since_tizen>
239 /// Enumeration for theme options of the NUIApplication.
242 [EditorBrowsable(EditorBrowsableState.Never)]
243 public enum ThemeOptions : int
246 /// No option specified.
248 [EditorBrowsable(EditorBrowsableState.Never)]
252 /// Enable platform theme.
253 /// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
255 [EditorBrowsable(EditorBrowsableState.Never)]
256 PlatformThemeEnabled = 1 << 0,
259 /// Sets the default value of View.ThemeChangeSensitive.
260 /// when this option is on, all views are made sensitive on theme changing by default.
262 [EditorBrowsable(EditorBrowsableState.Never)]
263 ThemeChangeSensitive = 1 << 1,
267 /// Current loaded xaml's full name.
269 [EditorBrowsable(EditorBrowsableState.Never)]
270 public static string CurrentLoadedXaml
274 return currentLoadedXaml;
278 if (currentLoadedXaml != value)
280 currentLoadedXaml = value;
281 XamlLoaded?.Invoke(value);
287 /// ResourceManager to handle multilingual.
289 /// <since_tizen> 4 </since_tizen>
290 public static System.Resources.ResourceManager MultilingualResourceManager
294 return resourceManager;
298 resourceManager = value;
303 /// Gets the window instance.
305 /// <since_tizen> 3 </since_tizen>
306 [Obsolete("Please do not use! This will be deprecated!")]
307 [EditorBrowsable(EditorBrowsableState.Never)]
312 return GetDefaultWindow();
317 /// Gets the Application Id.
319 /// <since_tizen> 6 </since_tizen>
320 [EditorBrowsable(EditorBrowsableState.Never)]
325 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
330 /// Gets the default window.
332 /// <returns>The default Window.</returns>
333 /// <since_tizen> 6 </since_tizen>
334 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
335 [EditorBrowsable(EditorBrowsableState.Never)]
336 public static Window GetDefaultWindow()
338 return Window.Instance;
341 internal Application ApplicationHandle
345 return ((NUICoreBackend)this.Backend).ApplicationHandle;
350 /// Register the assembly to XAML.
352 /// <since_tizen> 5 </since_tizen>
353 public static void RegisterAssembly(Assembly assembly)
355 XamlParser.s_assemblies.Add(assembly);
359 /// Runs the NUIApplication.
361 /// <param name="args">Arguments from commandline.</param>
362 /// <since_tizen> 4 </since_tizen>
363 public override void Run(string[] args)
365 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
366 Backend.AddEventHandler(EventType.Resumed, OnResume);
367 Backend.AddEventHandler(EventType.Paused, OnPause);
372 /// Exits the NUIApplication.
374 /// <since_tizen> 4 </since_tizen>
375 public override void Exit()
381 /// Ensures that the function passed in is called from the main loop when it is idle.
383 /// <param name="func">The function to call</param>
384 /// <returns>true if added successfully, false otherwise</returns>
385 /// <since_tizen> 4 </since_tizen>
386 public bool AddIdle(System.Delegate func)
388 return ((NUICoreBackend)this.Backend).AddIdle(func);
392 /// Sets the number of frames per render.
394 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
396 /// Suggest this is a power of two:
397 /// 1 - render each vsync frame.
398 /// 2 - render every other vsync frame.
399 /// 4 - render every fourth vsync frame.
400 /// 8 - render every eighth vsync frame. <br />
401 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
403 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
404 [EditorBrowsable(EditorBrowsableState.Never)]
405 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
407 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
411 /// Overrides this method if you want to handle behavior.
413 /// <since_tizen> 3 </since_tizen>
414 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
416 base.OnLocaleChanged(e);
420 /// Overrides this method if you want to handle behavior.
422 /// <since_tizen> 3 </since_tizen>
423 protected override void OnLowBattery(LowBatteryEventArgs e)
425 base.OnLowBattery(e);
429 /// Overrides this method if you want to handle behavior.
431 /// <since_tizen> 3 </since_tizen>
432 protected override void OnLowMemory(LowMemoryEventArgs e)
438 /// Overrides this method if you want to handle behavior.
440 /// <since_tizen> 3 </since_tizen>
441 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
443 base.OnRegionFormatChanged(e);
447 /// Overrides this method if you want to handle behavior.
449 /// <since_tizen> 3 </since_tizen>
450 protected override void OnTerminate()
456 /// Overrides this method if you want to handle behavior.
458 /// <since_tizen> 3 </since_tizen>
459 protected virtual void OnPause()
461 Paused?.Invoke(this, EventArgs.Empty);
465 /// Overrides this method if you want to handle behavior.
467 /// <since_tizen> 3 </since_tizen>
468 protected virtual void OnResume()
470 Resumed?.Invoke(this, EventArgs.Empty);
474 /// Overrides this method if you want to handle behavior.
476 /// <since_tizen> 3 </since_tizen>
477 protected virtual void OnPreCreate()
479 Tizen.Tracer.Begin("[NUI] OnPreCreate()");
483 GetDefaultWindow().EnableBorder(borderInterface);
490 /// Overrides this method if you want to handle behavior.
492 /// <since_tizen> 3 </since_tizen>
493 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
497 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
498 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
500 base.OnAppControlReceived(e);
504 /// Overrides this method if you want to handle behavior.
506 /// <since_tizen> 3 </since_tizen>
507 protected override void OnCreate()
509 Tizen.Tracer.Begin("[NUI] OnCreate()");
517 /// This is used to improve application launch performance.
519 [EditorBrowsable(EditorBrowsableState.Never)]
520 static public void Preload()
522 Interop.Application.PreInitialize();
523 #if ExternalThemeEnabled
524 ThemeManager.Preload();
530 /// Check if it is loaded as dotnet-loader-nui.
532 static internal bool IsPreload { get; set; }
534 private void ApplyThemeOption(ThemeOptions option)
536 if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
538 ThemeManager.PlatformThemeEnabled = true;
541 if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
543 ThemeManager.ApplicationThemeChangeSensitive = true;
549 /// Graphics Backend Type.
551 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
552 [EditorBrowsable(EditorBrowsableState.Never)]
553 [Obsolete("Please do not use! This will be deprecated!")]
554 public class Graphics
557 /// Graphics Backend Type.
559 public enum BackendType
562 /// The GLES backend.
566 /// The Vulkan backend.
572 /// The backend used by the NUIApplication.
574 [EditorBrowsable(EditorBrowsableState.Never)]
575 internal static BackendType Backend = BackendType.Gles;
577 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
578 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";