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, window size, position and default window type.
163 /// <param name="styleSheet">The styleSheet URL.</param>
164 /// <param name="windowMode">The windowMode.</param>
165 /// <param name="windowSize">The window size.</param>
166 /// <param name="windowPosition">The window position.</param>
167 /// <param name="type">The default window type.</param>
168 /// <since_tizen> 9 </since_tizen>
169 /// This should be hidden. Because the major purpose is to support IME window as keyboard application.
170 [EditorBrowsable(EditorBrowsableState.Never)]
171 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition, WindowType type) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition, type))
173 ExternalThemeManager.Initialize();
177 /// Occurs whenever the application is resumed.
179 /// <since_tizen> 4 </since_tizen>
180 public event EventHandler Resumed;
183 /// Occurs whenever the application is paused.
185 /// <since_tizen> 4 </since_tizen>
186 public event EventHandler Paused;
189 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
191 /// <since_tizen> 3 </since_tizen>
192 public enum WindowMode
197 /// <since_tizen> 3 </since_tizen>
202 /// <since_tizen> 3 </since_tizen>
207 /// Enumeration for theme options of the NUIApplication.
210 [EditorBrowsable(EditorBrowsableState.Never)]
211 public enum ThemeOptions : int
214 /// No option specified.
216 [EditorBrowsable(EditorBrowsableState.Never)]
220 /// Enable platform theme.
221 /// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
223 [EditorBrowsable(EditorBrowsableState.Never)]
224 PlatformThemeEnabled = 1 << 0,
227 /// Sets the default value of View.ThemeChangeSensitive.
228 /// when this option is on, all views are made sensitive on theme changing by default.
230 [EditorBrowsable(EditorBrowsableState.Never)]
231 ThemeChangeSensitive = 1 << 1,
235 /// ResourceManager to handle multilingual.
237 /// <since_tizen> 4 </since_tizen>
238 public static System.Resources.ResourceManager MultilingualResourceManager
242 return resourceManager;
246 resourceManager = value;
251 /// Gets the window instance.
253 /// <since_tizen> 3 </since_tizen>
254 [Obsolete("Please do not use! This will be deprecated!")]
255 [EditorBrowsable(EditorBrowsableState.Never)]
260 return GetDefaultWindow();
265 /// Gets the Application Id.
267 /// <since_tizen> 6 </since_tizen>
268 [EditorBrowsable(EditorBrowsableState.Never)]
273 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
278 /// Gets the default window.
280 /// <returns>The default Window.</returns>
281 /// <since_tizen> 6 </since_tizen>
282 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
283 [EditorBrowsable(EditorBrowsableState.Never)]
284 public static Window GetDefaultWindow()
286 return Window.Instance;
289 internal Application ApplicationHandle
293 return ((NUICoreBackend)this.Backend).ApplicationHandle;
298 /// Register the assembly to XAML.
300 /// <since_tizen> 5 </since_tizen>
301 public static void RegisterAssembly(Assembly assembly)
303 XamlParser.s_assemblies.Add(assembly);
307 /// Runs the NUIApplication.
309 /// <param name="args">Arguments from commandline.</param>
310 /// <since_tizen> 4 </since_tizen>
311 public override void Run(string[] args)
313 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
314 Backend.AddEventHandler(EventType.Resumed, OnResume);
315 Backend.AddEventHandler(EventType.Paused, OnPause);
320 /// Exits the NUIApplication.
322 /// <since_tizen> 4 </since_tizen>
323 public override void Exit()
329 /// Ensures that the function passed in is called from the main loop when it is idle.
331 /// <param name="func">The function to call</param>
332 /// <returns>true if added successfully, false otherwise</returns>
333 /// <since_tizen> 4 </since_tizen>
334 public bool AddIdle(System.Delegate func)
336 return ((NUICoreBackend)this.Backend).AddIdle(func);
340 /// Sets the number of frames per render.
342 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
344 /// Suggest this is a power of two:
345 /// 1 - render each vsync frame.
346 /// 2 - render every other vsync frame.
347 /// 4 - render every fourth vsync frame.
348 /// 8 - render every eighth vsync frame. <br />
349 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
351 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
352 [EditorBrowsable(EditorBrowsableState.Never)]
353 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
355 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
359 /// Overrides this method if you want to handle behavior.
361 /// <since_tizen> 3 </since_tizen>
362 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
364 base.OnLocaleChanged(e);
368 /// Overrides this method if you want to handle behavior.
370 /// <since_tizen> 3 </since_tizen>
371 protected override void OnLowBattery(LowBatteryEventArgs e)
373 base.OnLowBattery(e);
377 /// Overrides this method if you want to handle behavior.
379 /// <since_tizen> 3 </since_tizen>
380 protected override void OnLowMemory(LowMemoryEventArgs e)
386 /// Overrides this method if you want to handle behavior.
388 /// <since_tizen> 3 </since_tizen>
389 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
391 base.OnRegionFormatChanged(e);
395 /// Overrides this method if you want to handle behavior.
397 /// <since_tizen> 3 </since_tizen>
398 protected override void OnTerminate()
404 /// Overrides this method if you want to handle behavior.
406 /// <since_tizen> 3 </since_tizen>
407 protected virtual void OnPause()
409 Paused?.Invoke(this, EventArgs.Empty);
413 /// Overrides this method if you want to handle behavior.
415 /// <since_tizen> 3 </since_tizen>
416 protected virtual void OnResume()
418 Resumed?.Invoke(this, EventArgs.Empty);
422 /// Overrides this method if you want to handle behavior.
424 /// <since_tizen> 3 </since_tizen>
425 protected virtual void OnPreCreate()
430 /// Overrides this method if you want to handle behavior.
432 /// <since_tizen> 3 </since_tizen>
433 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
437 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
438 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
440 base.OnAppControlReceived(e);
444 /// Overrides this method if you want to handle behavior.
446 /// <since_tizen> 3 </since_tizen>
447 protected override void OnCreate()
453 /// This is used to improve application launch performance.
455 [EditorBrowsable(EditorBrowsableState.Never)]
456 static public void Preload()
458 Interop.Application.PreInitialize();
459 ThemeManager.Preload();
464 /// This is used to improve application launch performance.
466 [EditorBrowsable(EditorBrowsableState.Never)]
467 public void SendLaunchRequest(AppControl appControl)
469 TransitionOptions?.SendLaunchRequest(appControl);
473 /// This is used to improve application launch performance.
475 [EditorBrowsable(EditorBrowsableState.Never)]
476 public TransitionOptions TransitionOptions { get; set; }
479 /// Check if it is loaded as dotnet-loader-nui.
481 static internal bool IsPreload { get; set; }
483 private void ApplyThemeOption(ThemeOptions option)
485 if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
487 ThemeManager.PlatformThemeEnabled = true;
490 if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
492 ThemeManager.ApplicationThemeChangeSensitive = true;
498 /// Graphics Backend Type.
500 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
501 [EditorBrowsable(EditorBrowsableState.Never)]
502 [Obsolete("Please do not use! This will be deprecated!")]
503 public class Graphics
506 /// Graphics Backend Type.
508 public enum BackendType
511 /// The GLES backend.
515 /// The Vulkan backend.
521 /// The backend used by the NUIApplication.
523 [EditorBrowsable(EditorBrowsableState.Never)]
524 internal static BackendType Backend = BackendType.Gles;
526 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
527 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";