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(XamlLoadedEventArgs args);
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 public static string CurrentLoadedXaml
251 return currentLoadedXaml;
255 if (currentLoadedXaml != value)
257 currentLoadedXaml = value;
258 XamlLoaded?.Invoke(new XamlLoadedEventArgs(){XamlName = value});
264 /// ResourceManager to handle multilingual.
266 /// <since_tizen> 4 </since_tizen>
267 public static System.Resources.ResourceManager MultilingualResourceManager
271 return resourceManager;
275 resourceManager = value;
280 /// Gets the window instance.
282 /// <since_tizen> 3 </since_tizen>
283 [Obsolete("Please do not use! This will be deprecated!")]
284 [EditorBrowsable(EditorBrowsableState.Never)]
289 return GetDefaultWindow();
294 /// Gets the Application Id.
296 /// <since_tizen> 6 </since_tizen>
297 [EditorBrowsable(EditorBrowsableState.Never)]
302 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
307 /// Gets the default window.
309 /// <returns>The default Window.</returns>
310 /// <since_tizen> 6 </since_tizen>
311 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
312 [EditorBrowsable(EditorBrowsableState.Never)]
313 public static Window GetDefaultWindow()
315 return Window.Instance;
318 internal Application ApplicationHandle
322 return ((NUICoreBackend)this.Backend).ApplicationHandle;
327 /// Register the assembly to XAML.
329 /// <since_tizen> 5 </since_tizen>
330 public static void RegisterAssembly(Assembly assembly)
332 XamlParser.s_assemblies.Add(assembly);
336 /// Runs the NUIApplication.
338 /// <param name="args">Arguments from commandline.</param>
339 /// <since_tizen> 4 </since_tizen>
340 public override void Run(string[] args)
342 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
343 Backend.AddEventHandler(EventType.Resumed, OnResume);
344 Backend.AddEventHandler(EventType.Paused, OnPause);
349 /// Exits the NUIApplication.
351 /// <since_tizen> 4 </since_tizen>
352 public override void Exit()
358 /// Ensures that the function passed in is called from the main loop when it is idle.
360 /// <param name="func">The function to call</param>
361 /// <returns>true if added successfully, false otherwise</returns>
362 /// <since_tizen> 4 </since_tizen>
363 public bool AddIdle(System.Delegate func)
365 return ((NUICoreBackend)this.Backend).AddIdle(func);
369 /// Sets the number of frames per render.
371 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
373 /// Suggest this is a power of two:
374 /// 1 - render each vsync frame.
375 /// 2 - render every other vsync frame.
376 /// 4 - render every fourth vsync frame.
377 /// 8 - render every eighth vsync frame. <br />
378 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
380 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
381 [EditorBrowsable(EditorBrowsableState.Never)]
382 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
384 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
388 /// Overrides this method if you want to handle behavior.
390 /// <since_tizen> 3 </since_tizen>
391 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
393 base.OnLocaleChanged(e);
397 /// Overrides this method if you want to handle behavior.
399 /// <since_tizen> 3 </since_tizen>
400 protected override void OnLowBattery(LowBatteryEventArgs e)
402 base.OnLowBattery(e);
406 /// Overrides this method if you want to handle behavior.
408 /// <since_tizen> 3 </since_tizen>
409 protected override void OnLowMemory(LowMemoryEventArgs e)
415 /// Overrides this method if you want to handle behavior.
417 /// <since_tizen> 3 </since_tizen>
418 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
420 base.OnRegionFormatChanged(e);
424 /// Overrides this method if you want to handle behavior.
426 /// <since_tizen> 3 </since_tizen>
427 protected override void OnTerminate()
433 /// Overrides this method if you want to handle behavior.
435 /// <since_tizen> 3 </since_tizen>
436 protected virtual void OnPause()
438 Paused?.Invoke(this, EventArgs.Empty);
442 /// Overrides this method if you want to handle behavior.
444 /// <since_tizen> 3 </since_tizen>
445 protected virtual void OnResume()
447 Resumed?.Invoke(this, EventArgs.Empty);
451 /// Overrides this method if you want to handle behavior.
453 /// <since_tizen> 3 </since_tizen>
454 protected virtual void OnPreCreate()
459 /// Overrides this method if you want to handle behavior.
461 /// <since_tizen> 3 </since_tizen>
462 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
466 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
467 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
469 base.OnAppControlReceived(e);
473 /// Overrides this method if you want to handle behavior.
475 /// <since_tizen> 3 </since_tizen>
476 protected override void OnCreate()
482 /// This is used to improve application launch performance.
484 [EditorBrowsable(EditorBrowsableState.Never)]
485 static public void Preload()
487 Interop.Application.PreInitialize();
488 ThemeManager.Preload();
493 /// Check if it is loaded as dotnet-loader-nui.
495 static internal bool IsPreload { get; set; }
497 private void ApplyThemeOption(ThemeOptions option)
499 if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
501 ThemeManager.PlatformThemeEnabled = true;
504 if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
506 ThemeManager.ApplicationThemeChangeSensitive = true;
512 /// Graphics Backend Type.
514 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
515 [EditorBrowsable(EditorBrowsableState.Never)]
516 [Obsolete("Please do not use! This will be deprecated!")]
517 public class Graphics
520 /// Graphics Backend Type.
522 public enum BackendType
525 /// The GLES backend.
529 /// The Vulkan backend.
535 /// The backend used by the NUIApplication.
537 [EditorBrowsable(EditorBrowsableState.Never)]
538 internal static BackendType Backend = BackendType.Gles;
540 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
541 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
545 /// Xaml loaded event args.
547 [EditorBrowsable(EditorBrowsableState.Never)]
548 public class XamlLoadedEventArgs : EventArgs
550 public string XamlName {get; set;}