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 // TODO Enable this after tizen-theme-manager is released.
42 // private readonly ThemeLoader themeLoader = new ThemeLoader();
44 static NUIApplication()
46 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
47 // TODO Enable this after tizen-theme-manager is released.
48 // themeLoader.ThemeChanged += TizenThemeChanged;
52 /// The default constructor.
54 /// <since_tizen> 3 </since_tizen>
55 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
56 public NUIApplication() : base(new NUICoreBackend())
61 /// The constructor with window size and position.
63 /// <param name="windowSize">The window size.</param>
64 /// <param name="windowPosition">The window position.</param>
65 /// <since_tizen> 5 </since_tizen>
66 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
67 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
68 [EditorBrowsable(EditorBrowsableState.Never)]
69 public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
74 /// The constructor with a stylesheet.
76 /// <param name="styleSheet">The styleSheet url.</param>
77 /// <since_tizen> 3 </since_tizen>
78 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
79 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
84 /// The constructor with a stylesheet, window size, and position.
86 /// <param name="styleSheet">The styleSheet URL.</param>
87 /// <param name="windowSize">The window size.</param>
88 /// <param name="windowPosition">The window position.</param>
89 /// <since_tizen> 5 </since_tizen>
90 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
91 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
92 [EditorBrowsable(EditorBrowsableState.Never)]
93 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
98 /// The constructor with a stylesheet and window mode.
100 /// <param name="styleSheet">The styleSheet url.</param>
101 /// <param name="windowMode">The windowMode.</param>
102 /// <since_tizen> 3 </since_tizen>
103 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
104 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
109 /// The constructor with a stylesheet, window mode, window size, and position.
111 /// <param name="styleSheet">The styleSheet URL.</param>
112 /// <param name="windowMode">The windowMode.</param>
113 /// <param name="windowSize">The window size.</param>
114 /// <param name="windowPosition">The window position.</param>
115 /// <since_tizen> 5 </since_tizen>
116 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
117 [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
118 [EditorBrowsable(EditorBrowsableState.Never)]
119 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
124 /// Internal inhouse constructor with Graphics Backend Type
126 /// <param name="backend"></param>
127 /// <param name="windowMode"></param>
128 /// <param name="windowSize"></param>
129 /// <param name="windowPosition"></param>
130 /// <param name="styleSheet"></param>
131 /// InhouseAPI, this could be opend in NextTizen
132 [Obsolete("Please do not use! This will be deprecated!")]
133 [EditorBrowsable(EditorBrowsableState.Never)]
134 public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
136 //windowMode and styleSheet will be added later. currenlty it's not working as expected.
137 Graphics.Backend = backend;
138 Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
142 /// Occurs whenever the application is resumed.
144 /// <since_tizen> 4 </since_tizen>
145 public event EventHandler Resumed;
148 /// Occurs whenever the application is paused.
150 /// <since_tizen> 4 </since_tizen>
151 public event EventHandler Paused;
154 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
156 /// <since_tizen> 3 </since_tizen>
157 public enum WindowMode
162 /// <since_tizen> 3 </since_tizen>
167 /// <since_tizen> 3 </since_tizen>
172 /// ResourceManager to handle multilingual.
174 /// <since_tizen> 4 </since_tizen>
175 public static System.Resources.ResourceManager MultilingualResourceManager
179 return resourceManager;
183 resourceManager = value;
188 /// Gets the window instance.
190 /// <since_tizen> 3 </since_tizen>
191 [Obsolete("Please do not use! This will be deprecated!")]
192 [EditorBrowsable(EditorBrowsableState.Never)]
197 return GetDefaultWindow();
202 /// Gets the Application Id.
204 /// <since_tizen> 6 </since_tizen>
205 [EditorBrowsable(EditorBrowsableState.Never)]
210 return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
215 /// Gets the default window.
217 /// <returns>The default Window.</returns>
218 /// <since_tizen> 6 </since_tizen>
219 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 public static Window GetDefaultWindow()
223 return Window.Instance;
226 internal Application ApplicationHandle
230 return ((NUICoreBackend)this.Backend).ApplicationHandle;
235 /// Register the assembly to XAML.
237 /// <since_tizen> 5 </since_tizen>
238 public static void RegisterAssembly(Assembly assembly)
240 XamlParser.s_assemblies.Add(assembly);
244 /// Runs the NUIApplication.
246 /// <param name="args">Arguments from commandline.</param>
247 /// <since_tizen> 4 </since_tizen>
248 public override void Run(string[] args)
250 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
251 Backend.AddEventHandler(EventType.Resumed, OnResume);
252 Backend.AddEventHandler(EventType.Paused, OnPause);
257 /// Exits the NUIApplication.
259 /// <since_tizen> 4 </since_tizen>
260 public override void Exit()
266 /// Ensures that the function passed in is called from the main loop when it is idle.
268 /// <param name="func">The function to call</param>
269 /// <returns>true if added successfully, false otherwise</returns>
270 /// <since_tizen> 4 </since_tizen>
271 public bool AddIdle(System.Delegate func)
273 return ((NUICoreBackend)this.Backend).AddIdle(func);
277 /// Sets the number of frames per render.
279 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
281 /// Suggest this is a power of two:
282 /// 1 - render each vsync frame.
283 /// 2 - render every other vsync frame.
284 /// 4 - render every fourth vsync frame.
285 /// 8 - render every eighth vsync frame. <br />
286 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
288 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
289 [EditorBrowsable(EditorBrowsableState.Never)]
290 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
292 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
296 /// Overrides this method if you want to handle behavior.
298 /// <since_tizen> 3 </since_tizen>
299 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
301 base.OnLocaleChanged(e);
305 /// Overrides this method if you want to handle behavior.
307 /// <since_tizen> 3 </since_tizen>
308 protected override void OnLowBattery(LowBatteryEventArgs e)
310 base.OnLowBattery(e);
314 /// Overrides this method if you want to handle behavior.
316 /// <since_tizen> 3 </since_tizen>
317 protected override void OnLowMemory(LowMemoryEventArgs e)
323 /// Overrides this method if you want to handle behavior.
325 /// <since_tizen> 3 </since_tizen>
326 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
328 base.OnRegionFormatChanged(e);
332 /// Overrides this method if you want to handle behavior.
334 /// <since_tizen> 3 </since_tizen>
335 protected override void OnTerminate()
341 /// Overrides this method if you want to handle behavior.
343 /// <since_tizen> 3 </since_tizen>
344 protected virtual void OnPause()
346 Paused?.Invoke(this, EventArgs.Empty);
350 /// Overrides this method if you want to handle behavior.
352 /// <since_tizen> 3 </since_tizen>
353 protected virtual void OnResume()
355 Resumed?.Invoke(this, EventArgs.Empty);
359 /// Overrides this method if you want to handle behavior.
361 /// <since_tizen> 3 </since_tizen>
362 protected virtual void OnPreCreate()
367 /// Overrides this method if you want to handle behavior.
369 /// <since_tizen> 3 </since_tizen>
370 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
374 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
375 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
377 base.OnAppControlReceived(e);
381 /// Overrides this method if you want to handle behavior.
383 /// <since_tizen> 3 </since_tizen>
384 protected override void OnCreate()
390 /// This is used to improve application launch performance.
392 [EditorBrowsable(EditorBrowsableState.Never)]
393 static public void Preload()
395 Interop.Application.PreInitialize();
400 /// This is used to improve application launch performance.
402 [EditorBrowsable(EditorBrowsableState.Never)]
403 public void SendLaunchRequest(AppControl appControl)
405 TransitionOptions?.SendLaunchRequest(appControl);
409 /// This is used to improve application launch performance.
411 [EditorBrowsable(EditorBrowsableState.Never)]
412 public TransitionOptions TransitionOptions { get; set; }
414 // TODO Enable this after tizen-theme-manager is released.
415 // private void TizenThemeChanged(object sender, ThemeEventArgs e)
417 // string prefix = "/theme/";
419 // Dictionary<string, string> changedResources = new Dictionary<string, string>();
420 // foreach (string key in ThemeManager.DefaultTheme.Resources.Keys)
422 // // NOTE Need improve this code by checking HasKey
423 // string newValue = null;
426 // newValue = e.Theme.GetString(prefix + key);
429 // if (newValue != null)
431 // changedResources[key] = newValue;
434 // ThemeManager.UpdateCurrentThemeResources(changedResources);
438 /// Check if it is loaded as dotnet-loader-nui.
440 static internal bool IsPreload { get; set; }
444 /// Graphics Backend Type.
446 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
447 [EditorBrowsable(EditorBrowsableState.Never)]
448 [Obsolete("Please do not use! This will be deprecated!")]
449 public class Graphics
452 /// Graphics Backend Type.
454 public enum BackendType
457 /// The GLES backend.
461 /// The Vulkan backend.
467 /// The backend used by the NUIApplication.
469 [EditorBrowsable(EditorBrowsableState.Never)]
470 internal static BackendType Backend = BackendType.Gles;
472 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
473 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";