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.
18 #define ExternalThemeEnabled
22 using System.ComponentModel;
23 using System.Diagnostics.CodeAnalysis;
24 using System.Threading;
25 using System.Reflection;
26 using Tizen.Applications;
27 using Tizen.Applications.CoreBackend;
34 /// Represents an application that have a UI screen. The NUIApplication class has a default stage.
36 /// <since_tizen> 3 </since_tizen>
37 public class NUIApplication : CoreApplication
40 /// The instance of ResourceManager.
42 private static System.Resources.ResourceManager resourceManager = null;
43 private static string currentLoadedXaml = null;
48 private bool borderEnabled = false;
49 private IBorderInterface borderInterface = null;
52 /// Xaml loaded delegate.
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 public delegate void XamlLoadedHandler(string xamlName);
57 static NUIApplication()
59 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
63 /// The default constructor.
65 /// <since_tizen> 3 </since_tizen>
66 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
67 public NUIApplication() : base(new NUICoreBackend())
72 /// The constructor with window size and position.
74 /// <param name="windowSize">The window size.</param>
75 /// <param name="windowPosition">The window position.</param>
76 /// <since_tizen> 5 </since_tizen>
77 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
78 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
85 /// The constructor with a stylesheet.
87 /// <param name="styleSheet">The styleSheet url.</param>
88 /// <since_tizen> 3 </since_tizen>
89 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
90 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
95 /// The constructor with a stylesheet, window size, and position.
97 /// <param name="styleSheet">The styleSheet URL.</param>
98 /// <param name="windowSize">The window size.</param>
99 /// <param name="windowPosition">The window position.</param>
100 /// <since_tizen> 5 </since_tizen>
101 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
102 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
103 [EditorBrowsable(EditorBrowsableState.Never)]
104 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
109 /// The constructor with a stylesheet and window mode.
111 /// <param name="styleSheet">The styleSheet url.</param>
112 /// <param name="windowMode">The windowMode.</param>
113 /// <since_tizen> 3 </since_tizen>
114 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
115 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
120 /// The constructor with a stylesheet, window mode, window size, and position.
122 /// <param name="styleSheet">The styleSheet URL.</param>
123 /// <param name="windowMode">The windowMode.</param>
124 /// <param name="windowSize">The window size.</param>
125 /// <param name="windowPosition">The window position.</param>
126 /// <since_tizen> 5 </since_tizen>
127 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
128 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
129 [EditorBrowsable(EditorBrowsableState.Never)]
130 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
135 /// Internal inhouse constructor with Graphics Backend Type
137 /// <param name="backend"></param>
138 /// <param name="windowMode"></param>
139 /// <param name="windowSize"></param>
140 /// <param name="windowPosition"></param>
141 /// <param name="styleSheet"></param>
142 /// InhouseAPI, this could be opened in NextTizen
143 [Obsolete("Do not use! This will be deprecated!")]
144 [EditorBrowsable(EditorBrowsableState.Never)]
145 public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
147 //windowMode and styleSheet will be added later. currently it's not working as expected.
148 Graphics.Backend = backend;
149 Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
153 /// The constructor with theme option.
155 /// <param name="option">The theme option.</param>
156 [EditorBrowsable(EditorBrowsableState.Never)]
157 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
158 public NUIApplication(ThemeOptions option) : base(new NUICoreBackend())
160 ApplyThemeOption(option);
164 /// The constructor with window size and position and theme option.
166 /// <param name="windowSize">The window size.</param>
167 /// <param name="windowPosition">The window position.</param>
168 /// <param name="option">The theme option.</param>
169 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
170 [EditorBrowsable(EditorBrowsableState.Never)]
171 public NUIApplication(Size2D windowSize, Position2D windowPosition, ThemeOptions option) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
173 ApplyThemeOption(option);
177 /// The constructor with a stylesheet, window mode and default window type.
178 /// It is the only way to create an IME window.
180 /// <param name="styleSheet">The styleSheet URL.</param>
181 /// <param name="windowMode">The windowMode.</param>
182 /// <param name="type">The default window type.</param>
183 /// <since_tizen> 9 </since_tizen>
184 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
185 public NUIApplication(string styleSheet, WindowMode windowMode, WindowType type) : base(new NUICoreBackend(styleSheet, windowMode, type))
187 ExternalThemeManager.Initialize();
191 /// The constructor with a stylesheet, size, position, boderInterface and window mode
193 /// <param name="styleSheet">The styleSheet URL.</param>
194 /// <param name="windowSize">The window size.</param>
195 /// <param name="windowPosition">The window position.</param>
196 /// <param name="borderInterface"><see cref="Tizen.NUI.IBorderInterface"/>If borderInterface is null, defaultBorder is enabled.</param>
197 /// <param name="windowMode">The windowMode.</param>
198 [EditorBrowsable(EditorBrowsableState.Never)]
199 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
200 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition, IBorderInterface borderInterface, WindowMode windowMode = WindowMode.Opaque) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
202 EnableBorder(borderInterface);
206 /// The constructor with theme option and borderInterface.
208 /// <param name="option">The theme option.</param>
209 /// <param name="borderInterface"><see cref="Tizen.NUI.IBorderInterface"/>If borderInterface is null, defaultBorder is enabled.</param>
210 [EditorBrowsable(EditorBrowsableState.Never)]
211 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
212 public NUIApplication(ThemeOptions option, IBorderInterface borderInterface) : base(new NUICoreBackend())
214 EnableBorder(borderInterface);
215 ApplyThemeOption(option);
219 /// The constructor with window size, position, theme option and borderInterface.
221 /// <param name="windowSize">The window size.</param>
222 /// <param name="windowPosition">The window position.</param>
223 /// <param name="option">The theme option.</param>
224 /// <param name="borderInterface"><see cref="Tizen.NUI.IBorderInterface"/>If borderInterface is null, defaultBorder is enabled.</param>
225 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
226 [EditorBrowsable(EditorBrowsableState.Never)]
227 public NUIApplication(Size2D windowSize, Position2D windowPosition, ThemeOptions option, IBorderInterface borderInterface) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
229 EnableBorder(borderInterface);
230 ApplyThemeOption(option);
234 /// The constructor with a stylesheet, window mode, coretask
237 /// There is the UI thread feature.
238 /// UI thread is an additional thread that an Application object creates. The thread is for UI events.
239 /// To enable the UI Thread, you have to pass CoreTask object using this contructor.
240 /// When the UI thread feature is enabled, The methods of CoreTask are emitted on the main thread,
241 /// and the NUIApplication's events are emitted on the UI thread.
242 /// If you want to handle windows or actors in cases like when the memory level of the device is low, you have to use the NUIApplication events, not the CoreTask methods.
243 /// The CoreTask is not for handling GUI.
244 /// Callbacks of the all events in NUIApplication except the CoreTask are emitted on the UI thread.
246 /// <param name="styleSheet">The styleSheet URL.</param>
247 /// <param name="windowMode">The windowMode.</param>
248 /// <param name="task">True If app creates a UI Thread</param>
249 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
250 [EditorBrowsable(EditorBrowsableState.Never)]
251 public NUIApplication(string styleSheet, WindowMode windowMode, CoreTask task) : base(new NUICoreBackend(styleSheet, windowMode), task)
256 /// The constructor with a stylesheet, window mode, window size, position, coretask
258 /// <param name="styleSheet">The styleSheet URL.</param>
259 /// <param name="windowMode">The windowMode.</param>
260 /// <param name="windowSize">The window size.</param>
261 /// <param name="windowPosition">The window position.</param>
262 /// <param name="task">True If app creates a UI Thread</param>
263 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
264 [EditorBrowsable(EditorBrowsableState.Never)]
265 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition, CoreTask task) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition), task)
270 /// Occurs whenever the application is resumed.
272 /// <since_tizen> 4 </since_tizen>
273 public event EventHandler Resumed;
276 /// Occurs whenever the application is paused.
278 /// <since_tizen> 4 </since_tizen>
279 public event EventHandler Paused;
282 /// Xaml loaded event.
284 [EditorBrowsable(EditorBrowsableState.Never)]
285 public static event XamlLoadedHandler XamlLoaded;
288 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
290 /// <since_tizen> 3 </since_tizen>
291 public enum WindowMode
296 /// <since_tizen> 3 </since_tizen>
301 /// <since_tizen> 3 </since_tizen>
306 /// Enumeration for theme options of the NUIApplication.
309 [EditorBrowsable(EditorBrowsableState.Never)]
310 public enum ThemeOptions : int
313 /// No option specified.
315 [EditorBrowsable(EditorBrowsableState.Never)]
319 /// Enable platform theme.
320 /// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
322 [EditorBrowsable(EditorBrowsableState.Never)]
323 PlatformThemeEnabled = 1 << 0,
326 /// Sets the default value of View.ThemeChangeSensitive.
327 /// when this option is on, all views are made sensitive on theme changing by default.
329 [EditorBrowsable(EditorBrowsableState.Never)]
330 ThemeChangeSensitive = 1 << 1,
334 /// Current loaded xaml's full name.
336 [EditorBrowsable(EditorBrowsableState.Never)]
337 public static string CurrentLoadedXaml
341 return currentLoadedXaml;
345 if (currentLoadedXaml != value)
347 currentLoadedXaml = value;
348 XamlLoaded?.Invoke(value);
354 /// ResourceManager to handle multilingual.
356 /// <since_tizen> 4 </since_tizen>
357 public static System.Resources.ResourceManager MultilingualResourceManager
361 return resourceManager;
365 resourceManager = value;
370 /// Gets the window instance.
372 /// <since_tizen> 3 </since_tizen>
373 [Obsolete("Do not use! This will be deprecated!")]
374 [EditorBrowsable(EditorBrowsableState.Never)]
379 return GetDefaultWindow();
384 /// Gets the Application Id.
386 /// <since_tizen> 6 </since_tizen>
387 [EditorBrowsable(EditorBrowsableState.Never)]
392 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
397 /// Gets the default window.
399 /// <returns>The default Window.</returns>
400 /// <since_tizen> 6 </since_tizen>
401 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
402 [EditorBrowsable(EditorBrowsableState.Never)]
403 public static Window GetDefaultWindow()
405 return Window.Instance;
408 internal Application ApplicationHandle
412 return ((NUICoreBackend)this.Backend).ApplicationHandle;
417 /// Register the assembly to XAML.
419 /// <since_tizen> 5 </since_tizen>
420 public static void RegisterAssembly(Assembly assembly)
422 XamlParser.s_assemblies.Add(assembly);
426 /// Runs the NUIApplication.
428 /// <param name="args">Arguments from commandline.</param>
429 /// <since_tizen> 4 </since_tizen>
430 public override void Run(string[] args)
432 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
433 Backend.AddEventHandler(EventType.Resumed, OnResume);
434 Backend.AddEventHandler(EventType.Paused, OnPause);
439 /// Exits the NUIApplication.
441 /// <since_tizen> 4 </since_tizen>
442 public override void Exit()
448 /// Ensures that the function passed in is called from the main loop when it is idle.
450 /// <param name="func">The function to call</param>
451 /// <returns>true if added successfully, false otherwise</returns>
452 /// <since_tizen> 4 </since_tizen>
453 public bool AddIdle(System.Delegate func)
455 return ((NUICoreBackend)this.Backend).AddIdle(func);
459 /// Sets the number of frames per render.
461 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
463 /// Suggest this is a power of two:
464 /// 1 - render each vsync frame.
465 /// 2 - render every other vsync frame.
466 /// 4 - render every fourth vsync frame.
467 /// 8 - render every eighth vsync frame. <br />
468 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
470 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
471 [EditorBrowsable(EditorBrowsableState.Never)]
472 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
474 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
478 /// Overrides this method if you want to handle behavior.
480 /// <since_tizen> 3 </since_tizen>
481 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
483 base.OnLocaleChanged(e);
487 /// Overrides this method if you want to handle behavior.
489 /// <since_tizen> 3 </since_tizen>
490 protected override void OnLowBattery(LowBatteryEventArgs e)
492 base.OnLowBattery(e);
496 /// Overrides this method if you want to handle behavior.
498 /// <since_tizen> 3 </since_tizen>
499 protected override void OnLowMemory(LowMemoryEventArgs e)
505 /// Overrides this method if you want to handle behavior.
507 /// <since_tizen> 3 </since_tizen>
508 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
510 base.OnRegionFormatChanged(e);
514 /// Overrides this method if you want to handle behavior.
516 /// <since_tizen> 3 </since_tizen>
517 protected override void OnTerminate()
523 /// Overrides this method if you want to handle behavior.
525 /// <since_tizen> 3 </since_tizen>
526 protected virtual void OnPause()
528 Paused?.Invoke(this, EventArgs.Empty);
532 /// Overrides this method if you want to handle behavior.
534 /// <since_tizen> 3 </since_tizen>
535 protected virtual void OnResume()
537 Resumed?.Invoke(this, EventArgs.Empty);
541 /// Overrides this method if you want to handle behavior.
543 /// <since_tizen> 3 </since_tizen>
544 protected virtual void OnPreCreate()
546 Tizen.Tracer.Begin("[NUI] OnPreCreate()");
550 GetDefaultWindow().EnableBorder(borderInterface, new Window.BorderCloseDelegate(Exit));
557 /// Overrides this method if you want to handle behavior.
559 /// <since_tizen> 3 </since_tizen>
560 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
564 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
565 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
567 base.OnAppControlReceived(e);
571 /// Overrides this method if you want to handle behavior.
573 /// <since_tizen> 3 </since_tizen>
574 protected override void OnCreate()
576 Tizen.Tracer.Begin("[NUI] OnCreate()");
584 /// This is used to improve application launch performance.
586 [EditorBrowsable(EditorBrowsableState.Never)]
587 static public void Preload()
589 Interop.Application.PreInitialize();
590 #if ExternalThemeEnabled
591 ThemeManager.Preload();
597 /// Check if it is loaded as dotnet-loader-nui.
599 static internal bool IsPreload { get; set; }
601 private void ApplyThemeOption(ThemeOptions option)
603 if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
605 ThemeManager.PlatformThemeEnabled = true;
608 if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
610 ThemeManager.ApplicationThemeChangeSensitive = true;
614 private void EnableBorder(IBorderInterface borderInterface)
616 borderEnabled = true;
617 this.borderInterface = borderInterface;
622 /// Graphics Backend Type.
624 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
625 [EditorBrowsable(EditorBrowsableState.Never)]
626 [Obsolete("Do not use! This will be deprecated!")]
627 public class Graphics
630 /// Graphics Backend Type.
632 public enum BackendType
635 /// The GLES backend.
639 /// The Vulkan backend.
645 /// The backend used by the NUIApplication.
647 [EditorBrowsable(EditorBrowsableState.Never)]
648 internal static BackendType Backend = BackendType.Gles;
650 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
651 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";