2 * Copyright (c) 2019 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;
25 using Tizen.NUI.Binding;
27 using Tizen.Applications.ThemeManager;
28 using System.Collections.Generic;
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 Size2D _windowSize2D = null;
44 private Position2D _windowPosition2D = null;
45 private TransitionOptions transitionOptions;
46 private string appId = null;
47 internal static NUIApplication me;
49 private static bool isPreLoad = false;
50 private readonly ThemeLoader themeLoader = new ThemeLoader();
53 /// The default constructor.
55 /// <since_tizen> 3 </since_tizen>
56 public NUIApplication() : base(new NUICoreBackend())
58 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
60 themeLoader.ThemeChanged += TizenThemeChanged;
64 /// The constructor with window size and position.
66 /// <param name="windowSize">The window size.</param>
67 /// <param name="windowPosition">The window position.</param>
68 /// <since_tizen> 5 </since_tizen>
69 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
70 [EditorBrowsable(EditorBrowsableState.Never)]
71 public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", WindowMode.Opaque, windowSize, windowPosition))
73 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
74 themeLoader.ThemeChanged += TizenThemeChanged;
75 _windowSize2D = windowSize;
76 _windowPosition2D = windowPosition;
81 /// The constructor with a stylesheet.
83 /// <param name="styleSheet">The styleSheet url.</param>
84 /// <since_tizen> 3 </since_tizen>
85 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
87 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
89 themeLoader.ThemeChanged += TizenThemeChanged;
93 /// The constructor with a stylesheet, window size, and position.
95 /// <param name="styleSheet">The styleSheet URL.</param>
96 /// <param name="windowSize">The window size.</param>
97 /// <param name="windowPosition">The window position.</param>
98 /// <since_tizen> 5 </since_tizen>
99 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
100 [EditorBrowsable(EditorBrowsableState.Never)]
101 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
103 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
104 themeLoader.ThemeChanged += TizenThemeChanged;
105 _windowSize2D = windowSize;
106 _windowPosition2D = windowPosition;
111 /// The constructor with a stylesheet and window mode.
113 /// <param name="styleSheet">The styleSheet url.</param>
114 /// <param name="windowMode">The windowMode.</param>
115 /// <since_tizen> 3 </since_tizen>
116 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
118 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
120 themeLoader.ThemeChanged += TizenThemeChanged;
124 /// The constructor with a stylesheet, window mode, window size, and position.
126 /// <param name="styleSheet">The styleSheet URL.</param>
127 /// <param name="windowMode">The windowMode.</param>
128 /// <param name="windowSize">The window size.</param>
129 /// <param name="windowPosition">The window position.</param>
130 /// <since_tizen> 5 </since_tizen>
131 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
132 [EditorBrowsable(EditorBrowsableState.Never)]
133 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
135 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
136 themeLoader.ThemeChanged += TizenThemeChanged;
137 _windowSize2D = windowSize;
138 _windowPosition2D = windowPosition;
143 /// Internal inhouse constructor with Graphics Backend Type
145 /// <param name="backend"></param>
146 /// <param name="windowMode"></param>
147 /// <param name="windowSize"></param>
148 /// <param name="windowPosition"></param>
149 /// <param name="styleSheet"></param>
150 /// InhouseAPI, this could be opend in NextTizen
151 [Obsolete("Please do not use! This will be deprecated!")]
152 [EditorBrowsable(EditorBrowsableState.Never)]
153 public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
155 //windowMode and styleSheet will be added later. currenlty it's not working as expected.
156 Graphics.Backend = backend;
157 Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
159 if (windowSize != null) { _windowSize2D = windowSize; }
160 if (windowPosition != null) { _windowPosition2D = windowPosition; }
161 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
163 themeLoader.ThemeChanged += TizenThemeChanged;
167 /// Occurs whenever the application is resumed.
169 /// <since_tizen> 4 </since_tizen>
170 public event EventHandler Resumed;
173 /// Occurs whenever the application is paused.
175 /// <since_tizen> 4 </since_tizen>
176 public event EventHandler Paused;
179 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
181 /// <since_tizen> 3 </since_tizen>
182 public enum WindowMode
187 /// <since_tizen> 3 </since_tizen>
192 /// <since_tizen> 3 </since_tizen>
197 /// ResourceManager to handle multilingual.
199 /// <since_tizen> 4 </since_tizen>
200 public static System.Resources.ResourceManager MultilingualResourceManager
204 return resourceManager;
208 resourceManager = value;
213 /// Gets the window instance.
215 /// <since_tizen> 3 </since_tizen>
216 [Obsolete("Please do not use! This will be deprecated!")]
217 [EditorBrowsable(EditorBrowsableState.Never)]
222 return GetDefaultWindow();
227 /// Gets the Application Id.
229 /// <since_tizen> 6 </since_tizen>
230 [EditorBrowsable(EditorBrowsableState.Never)]
240 /// Gets the default window.
242 /// <returns>The default Window.</returns>
243 /// <since_tizen> 6 </since_tizen>
244 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
245 [EditorBrowsable(EditorBrowsableState.Never)]
246 public static Window GetDefaultWindow()
248 return Window.Instance;
251 internal Application ApplicationHandle
255 return ((NUICoreBackend)this.Backend).ApplicationHandle;
260 /// Register the assembly to XAML.
262 /// <since_tizen> 5 </since_tizen>
263 public static void RegisterAssembly(Assembly assembly)
265 XamlParser.s_assemblies.Add(assembly);
269 /// Runs the NUIApplication.
271 /// <param name="args">Arguments from commandline.</param>
272 /// <since_tizen> 4 </since_tizen>
273 public override void Run(string[] args)
275 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
276 Backend.AddEventHandler(EventType.Resumed, OnResume);
277 Backend.AddEventHandler(EventType.Paused, OnPause);
282 /// Exits the NUIApplication.
284 /// <since_tizen> 4 </since_tizen>
285 public override void Exit()
291 /// Ensures that the function passed in is called from the main loop when it is idle.
293 /// <param name="func">The function to call</param>
294 /// <returns>true if added successfully, false otherwise</returns>
295 /// <since_tizen> 4 </since_tizen>
296 public bool AddIdle(System.Delegate func)
298 return ((NUICoreBackend)this.Backend).AddIdle(func);
302 /// Sets the number of frames per render.
304 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
306 /// Suggest this is a power of two:
307 /// 1 - render each vsync frame.
308 /// 2 - render every other vsync frame.
309 /// 4 - render every fourth vsync frame.
310 /// 8 - render every eighth vsync frame. <br />
311 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
313 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
314 [EditorBrowsable(EditorBrowsableState.Never)]
315 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
317 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
321 /// Overrides this method if you want to handle behavior.
323 /// <since_tizen> 3 </since_tizen>
324 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
326 base.OnLocaleChanged(e);
330 /// Overrides this method if you want to handle behavior.
332 /// <since_tizen> 3 </since_tizen>
333 protected override void OnLowBattery(LowBatteryEventArgs e)
335 base.OnLowBattery(e);
339 /// Overrides this method if you want to handle behavior.
341 /// <since_tizen> 3 </since_tizen>
342 protected override void OnLowMemory(LowMemoryEventArgs e)
348 /// Overrides this method if you want to handle behavior.
350 /// <since_tizen> 3 </since_tizen>
351 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
353 base.OnRegionFormatChanged(e);
357 /// Overrides this method if you want to handle behavior.
359 /// <since_tizen> 3 </since_tizen>
360 protected override void OnTerminate()
366 /// Overrides this method if you want to handle behavior.
368 /// <since_tizen> 3 </since_tizen>
369 protected virtual void OnPause()
371 Paused?.Invoke(this, EventArgs.Empty);
375 /// Overrides this method if you want to handle behavior.
377 /// <since_tizen> 3 </since_tizen>
378 protected virtual void OnResume()
380 Resumed?.Invoke(this, EventArgs.Empty);
384 /// Overrides this method if you want to handle behavior.
386 /// <since_tizen> 3 </since_tizen>
387 protected virtual void OnPreCreate()
392 /// Overrides this method if you want to handle behavior.
394 /// <since_tizen> 3 </since_tizen>
395 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
399 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
400 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
402 base.OnAppControlReceived(e);
406 /// Overrides this method if you want to handle behavior.
408 /// <since_tizen> 3 </since_tizen>
409 protected override void OnCreate()
412 appId = Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
416 /// This is used to improve application launch performance.
418 [EditorBrowsable(EditorBrowsableState.Never)]
419 static public void PreLoad()
421 Interop.Application.PreInitialize();
426 /// This is used to improve application launch performance.
428 [EditorBrowsable(EditorBrowsableState.Never)]
429 public void SendLaunchRequest(AppControl appControl)
431 transitionOptions.SendLaunchRequest(appControl);
435 /// This is used to improve application launch performance.
437 [EditorBrowsable(EditorBrowsableState.Never)]
438 public TransitionOptions TransitionOptions
442 return transitionOptions;
446 transitionOptions = value;
449 private void TizenThemeChanged(object sender, ThemeEventArgs e)
451 string prefix = "/theme/";
453 Dictionary<string, string> changedResources = new Dictionary<string, string>();
454 foreach (string key in ThemeManager.DefaultTheme.Resources.Keys)
456 // NOTE Need improve this code by checking HasKey
457 string newValue = null;
460 newValue = e.Theme.GetString(prefix + key);
463 if (newValue != null)
465 changedResources[key] = newValue;
468 ThemeManager.UpdateCurrentThemeResources(changedResources);
472 /// Check if it is loaded as dotnet-loader-nui.
474 static internal bool IsPreLoad
484 /// Graphics Backend Type.
486 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
487 [EditorBrowsable(EditorBrowsableState.Never)]
488 [Obsolete("Please do not use! This will be deprecated!")]
489 public class Graphics
492 /// Graphics Backend Type.
494 public enum BackendType
497 /// The GLES backend.
501 /// The Vulkan backend.
507 /// The backend used by the NUIApplication.
509 [EditorBrowsable(EditorBrowsableState.Never)]
510 internal static BackendType Backend = BackendType.Gles;
512 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
513 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";