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 /// The constructor with a stylesheet, window mode and default window type.
162 /// It is the only way to create an IME window.
164 /// <param name="styleSheet">The styleSheet URL.</param>
165 /// <param name="windowMode">The windowMode.</param>
166 /// <param name="type">The default window type.</param>
167 /// <since_tizen> 9 </since_tizen>
168 public NUIApplication(string styleSheet, WindowMode windowMode, WindowType type) : base(new NUICoreBackend(styleSheet, windowMode, type))
170 ExternalThemeManager.Initialize();
174 /// Occurs whenever the application is resumed.
176 /// <since_tizen> 4 </since_tizen>
177 public event EventHandler Resumed;
180 /// Occurs whenever the application is paused.
182 /// <since_tizen> 4 </since_tizen>
183 public event EventHandler Paused;
186 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
188 /// <since_tizen> 3 </since_tizen>
189 public enum WindowMode
194 /// <since_tizen> 3 </since_tizen>
199 /// <since_tizen> 3 </since_tizen>
204 /// Enumeration for theme options of the NUIApplication.
207 [EditorBrowsable(EditorBrowsableState.Never)]
208 public enum ThemeOptions : int
211 /// No option specified.
213 [EditorBrowsable(EditorBrowsableState.Never)]
217 /// Enable platform theme.
218 /// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 PlatformThemeEnabled = 1 << 0,
224 /// Sets the default value of View.ThemeChangeSensitive.
225 /// when this option is on, all views are made sensitive on theme changing by default.
227 [EditorBrowsable(EditorBrowsableState.Never)]
228 ThemeChangeSensitive = 1 << 1,
232 /// ResourceManager to handle multilingual.
234 /// <since_tizen> 4 </since_tizen>
235 public static System.Resources.ResourceManager MultilingualResourceManager
239 return resourceManager;
243 resourceManager = value;
248 /// Gets the window instance.
250 /// <since_tizen> 3 </since_tizen>
251 [Obsolete("Please do not use! This will be deprecated!")]
252 [EditorBrowsable(EditorBrowsableState.Never)]
257 return GetDefaultWindow();
262 /// Gets the Application Id.
264 /// <since_tizen> 6 </since_tizen>
265 [EditorBrowsable(EditorBrowsableState.Never)]
270 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
275 /// Gets the default window.
277 /// <returns>The default Window.</returns>
278 /// <since_tizen> 6 </since_tizen>
279 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
280 [EditorBrowsable(EditorBrowsableState.Never)]
281 public static Window GetDefaultWindow()
283 return Window.Instance;
286 internal Application ApplicationHandle
290 return ((NUICoreBackend)this.Backend).ApplicationHandle;
295 /// Register the assembly to XAML.
297 /// <since_tizen> 5 </since_tizen>
298 public static void RegisterAssembly(Assembly assembly)
300 XamlParser.s_assemblies.Add(assembly);
304 /// Runs the NUIApplication.
306 /// <param name="args">Arguments from commandline.</param>
307 /// <since_tizen> 4 </since_tizen>
308 public override void Run(string[] args)
310 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
311 Backend.AddEventHandler(EventType.Resumed, OnResume);
312 Backend.AddEventHandler(EventType.Paused, OnPause);
317 /// Exits the NUIApplication.
319 /// <since_tizen> 4 </since_tizen>
320 public override void Exit()
326 /// Ensures that the function passed in is called from the main loop when it is idle.
328 /// <param name="func">The function to call</param>
329 /// <returns>true if added successfully, false otherwise</returns>
330 /// <since_tizen> 4 </since_tizen>
331 public bool AddIdle(System.Delegate func)
333 return ((NUICoreBackend)this.Backend).AddIdle(func);
337 /// Sets the number of frames per render.
339 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
341 /// Suggest this is a power of two:
342 /// 1 - render each vsync frame.
343 /// 2 - render every other vsync frame.
344 /// 4 - render every fourth vsync frame.
345 /// 8 - render every eighth vsync frame. <br />
346 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
348 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
349 [EditorBrowsable(EditorBrowsableState.Never)]
350 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
352 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
356 /// Overrides this method if you want to handle behavior.
358 /// <since_tizen> 3 </since_tizen>
359 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
361 base.OnLocaleChanged(e);
365 /// Overrides this method if you want to handle behavior.
367 /// <since_tizen> 3 </since_tizen>
368 protected override void OnLowBattery(LowBatteryEventArgs e)
370 base.OnLowBattery(e);
374 /// Overrides this method if you want to handle behavior.
376 /// <since_tizen> 3 </since_tizen>
377 protected override void OnLowMemory(LowMemoryEventArgs e)
383 /// Overrides this method if you want to handle behavior.
385 /// <since_tizen> 3 </since_tizen>
386 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
388 base.OnRegionFormatChanged(e);
392 /// Overrides this method if you want to handle behavior.
394 /// <since_tizen> 3 </since_tizen>
395 protected override void OnTerminate()
401 /// Overrides this method if you want to handle behavior.
403 /// <since_tizen> 3 </since_tizen>
404 protected virtual void OnPause()
406 Paused?.Invoke(this, EventArgs.Empty);
410 /// Overrides this method if you want to handle behavior.
412 /// <since_tizen> 3 </since_tizen>
413 protected virtual void OnResume()
415 Resumed?.Invoke(this, EventArgs.Empty);
419 /// Overrides this method if you want to handle behavior.
421 /// <since_tizen> 3 </since_tizen>
422 protected virtual void OnPreCreate()
427 /// Overrides this method if you want to handle behavior.
429 /// <since_tizen> 3 </since_tizen>
430 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
434 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
435 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
437 base.OnAppControlReceived(e);
441 /// Overrides this method if you want to handle behavior.
443 /// <since_tizen> 3 </since_tizen>
444 protected override void OnCreate()
450 /// This is used to improve application launch performance.
452 [EditorBrowsable(EditorBrowsableState.Never)]
453 static public void Preload()
455 Interop.Application.PreInitialize();
456 ThemeManager.Preload();
461 /// Check if it is loaded as dotnet-loader-nui.
463 static internal bool IsPreload { get; set; }
465 private void ApplyThemeOption(ThemeOptions option)
467 if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
469 ThemeManager.PlatformThemeEnabled = true;
472 if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
474 ThemeManager.ApplicationThemeChangeSensitive = true;
480 /// Graphics Backend Type.
482 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
483 [EditorBrowsable(EditorBrowsableState.Never)]
484 [Obsolete("Please do not use! This will be deprecated!")]
485 public class Graphics
488 /// Graphics Backend Type.
490 public enum BackendType
493 /// The GLES backend.
497 /// The Vulkan backend.
503 /// The backend used by the NUIApplication.
505 [EditorBrowsable(EditorBrowsableState.Never)]
506 internal static BackendType Backend = BackendType.Gles;
508 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
509 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";