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 System.Collections.Generic;
33 /// Represents an application that have a UI screen. The NUIApplication class has a default stage.
35 /// <since_tizen> 3 </since_tizen>
36 public class NUIApplication : CoreApplication
39 /// The instance of ResourceManager.
41 private static System.Resources.ResourceManager resourceManager = null;
42 private Size2D _windowSize2D = null;
43 private Position2D _windowPosition2D = null;
44 private TransitionOptions transitionOptions;
45 private string appId = null;
46 internal static NUIApplication me;
48 private static bool isPreLoad = false;
50 // TODO Enable this after tizen-theme-manager is released.
51 // private readonly ThemeLoader themeLoader = new ThemeLoader();
54 /// The default constructor.
56 /// <since_tizen> 3 </since_tizen>
57 public NUIApplication() : base(new NUICoreBackend())
59 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
62 // TODO Enable this after tizen-theme-manager is released.
63 // themeLoader.ThemeChanged += TizenThemeChanged;
67 /// The constructor with window size and position.
69 /// <param name="windowSize">The window size.</param>
70 /// <param name="windowPosition">The window position.</param>
71 /// <since_tizen> 5 </since_tizen>
72 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
73 [EditorBrowsable(EditorBrowsableState.Never)]
74 public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", WindowMode.Opaque, windowSize, windowPosition))
76 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
77 // TODO Enable this after tizen-theme-manager is released.
78 // themeLoader.ThemeChanged += TizenThemeChanged;
79 _windowSize2D = windowSize;
80 _windowPosition2D = windowPosition;
85 /// The constructor with a stylesheet.
87 /// <param name="styleSheet">The styleSheet url.</param>
88 /// <since_tizen> 3 </since_tizen>
89 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
91 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
93 // TODO Enable this after tizen-theme-manager is released.
94 // themeLoader.ThemeChanged += TizenThemeChanged;
98 /// The constructor with a stylesheet, window size, and position.
100 /// <param name="styleSheet">The styleSheet URL.</param>
101 /// <param name="windowSize">The window size.</param>
102 /// <param name="windowPosition">The window position.</param>
103 /// <since_tizen> 5 </since_tizen>
104 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
105 [EditorBrowsable(EditorBrowsableState.Never)]
106 public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
108 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
109 // TODO Enable this after tizen-theme-manager is released.
110 // themeLoader.ThemeChanged += TizenThemeChanged;
111 _windowSize2D = windowSize;
112 _windowPosition2D = windowPosition;
117 /// The constructor with a stylesheet and window mode.
119 /// <param name="styleSheet">The styleSheet url.</param>
120 /// <param name="windowMode">The windowMode.</param>
121 /// <since_tizen> 3 </since_tizen>
122 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
124 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
126 // TODO Enable this after tizen-theme-manager is released.
127 // themeLoader.ThemeChanged += TizenThemeChanged;
131 /// The constructor with a stylesheet, window mode, window size, and position.
133 /// <param name="styleSheet">The styleSheet URL.</param>
134 /// <param name="windowMode">The windowMode.</param>
135 /// <param name="windowSize">The window size.</param>
136 /// <param name="windowPosition">The window position.</param>
137 /// <since_tizen> 5 </since_tizen>
138 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
139 [EditorBrowsable(EditorBrowsableState.Never)]
140 public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
142 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
143 // TODO Enable this after tizen-theme-manager is released.
144 // themeLoader.ThemeChanged += TizenThemeChanged;
145 _windowSize2D = windowSize;
146 _windowPosition2D = windowPosition;
151 /// Internal inhouse constructor with Graphics Backend Type
153 /// <param name="backend"></param>
154 /// <param name="windowMode"></param>
155 /// <param name="windowSize"></param>
156 /// <param name="windowPosition"></param>
157 /// <param name="styleSheet"></param>
158 /// InhouseAPI, this could be opend in NextTizen
159 [Obsolete("Please do not use! This will be deprecated!")]
160 [EditorBrowsable(EditorBrowsableState.Never)]
161 public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
163 //windowMode and styleSheet will be added later. currenlty it's not working as expected.
164 Graphics.Backend = backend;
165 Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
167 if (windowSize != null) { _windowSize2D = windowSize; }
168 if (windowPosition != null) { _windowPosition2D = windowPosition; }
169 Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
171 // TODO Enable this after tizen-theme-manager is released.
172 // themeLoader.ThemeChanged += TizenThemeChanged;
176 /// Occurs whenever the application is resumed.
178 /// <since_tizen> 4 </since_tizen>
179 public event EventHandler Resumed;
182 /// Occurs whenever the application is paused.
184 /// <since_tizen> 4 </since_tizen>
185 public event EventHandler Paused;
188 /// Enumeration for deciding whether a NUI application window is opaque or transparent.
190 /// <since_tizen> 3 </since_tizen>
191 public enum WindowMode
196 /// <since_tizen> 3 </since_tizen>
201 /// <since_tizen> 3 </since_tizen>
206 /// ResourceManager to handle multilingual.
208 /// <since_tizen> 4 </since_tizen>
209 public static System.Resources.ResourceManager MultilingualResourceManager
213 return resourceManager;
217 resourceManager = value;
222 /// Gets the window instance.
224 /// <since_tizen> 3 </since_tizen>
225 [Obsolete("Please do not use! This will be deprecated!")]
226 [EditorBrowsable(EditorBrowsableState.Never)]
231 return GetDefaultWindow();
236 /// Gets the Application Id.
238 /// <since_tizen> 6 </since_tizen>
239 [EditorBrowsable(EditorBrowsableState.Never)]
249 /// Gets the default window.
251 /// <returns>The default Window.</returns>
252 /// <since_tizen> 6 </since_tizen>
253 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
254 [EditorBrowsable(EditorBrowsableState.Never)]
255 public static Window GetDefaultWindow()
257 return Window.Instance;
260 internal Application ApplicationHandle
264 return ((NUICoreBackend)this.Backend).ApplicationHandle;
269 /// Register the assembly to XAML.
271 /// <since_tizen> 5 </since_tizen>
272 public static void RegisterAssembly(Assembly assembly)
274 XamlParser.s_assemblies.Add(assembly);
278 /// Runs the NUIApplication.
280 /// <param name="args">Arguments from commandline.</param>
281 /// <since_tizen> 4 </since_tizen>
282 public override void Run(string[] args)
284 Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
285 Backend.AddEventHandler(EventType.Resumed, OnResume);
286 Backend.AddEventHandler(EventType.Paused, OnPause);
291 /// Exits the NUIApplication.
293 /// <since_tizen> 4 </since_tizen>
294 public override void Exit()
300 /// Ensures that the function passed in is called from the main loop when it is idle.
302 /// <param name="func">The function to call</param>
303 /// <returns>true if added successfully, false otherwise</returns>
304 /// <since_tizen> 4 </since_tizen>
305 public bool AddIdle(System.Delegate func)
307 return ((NUICoreBackend)this.Backend).AddIdle(func);
311 /// Sets the number of frames per render.
313 /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
315 /// Suggest this is a power of two:
316 /// 1 - render each vsync frame.
317 /// 2 - render every other vsync frame.
318 /// 4 - render every fourth vsync frame.
319 /// 8 - render every eighth vsync frame. <br />
320 /// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
322 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
323 [EditorBrowsable(EditorBrowsableState.Never)]
324 public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
326 Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
330 /// Overrides this method if you want to handle behavior.
332 /// <since_tizen> 3 </since_tizen>
333 protected override void OnLocaleChanged(LocaleChangedEventArgs e)
335 base.OnLocaleChanged(e);
339 /// Overrides this method if you want to handle behavior.
341 /// <since_tizen> 3 </since_tizen>
342 protected override void OnLowBattery(LowBatteryEventArgs e)
344 base.OnLowBattery(e);
348 /// Overrides this method if you want to handle behavior.
350 /// <since_tizen> 3 </since_tizen>
351 protected override void OnLowMemory(LowMemoryEventArgs e)
357 /// Overrides this method if you want to handle behavior.
359 /// <since_tizen> 3 </since_tizen>
360 protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
362 base.OnRegionFormatChanged(e);
366 /// Overrides this method if you want to handle behavior.
368 /// <since_tizen> 3 </since_tizen>
369 protected override void OnTerminate()
375 /// Overrides this method if you want to handle behavior.
377 /// <since_tizen> 3 </since_tizen>
378 protected virtual void OnPause()
380 Paused?.Invoke(this, EventArgs.Empty);
384 /// Overrides this method if you want to handle behavior.
386 /// <since_tizen> 3 </since_tizen>
387 protected virtual void OnResume()
389 Resumed?.Invoke(this, EventArgs.Empty);
393 /// Overrides this method if you want to handle behavior.
395 /// <since_tizen> 3 </since_tizen>
396 protected virtual void OnPreCreate()
401 /// Overrides this method if you want to handle behavior.
403 /// <since_tizen> 3 </since_tizen>
404 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
408 Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
409 Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
411 base.OnAppControlReceived(e);
415 /// Overrides this method if you want to handle behavior.
417 /// <since_tizen> 3 </since_tizen>
418 protected override void OnCreate()
421 appId = Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
425 /// This is used to improve application launch performance.
427 [EditorBrowsable(EditorBrowsableState.Never)]
428 static public void PreLoad()
430 Interop.Application.PreInitialize();
435 /// This is used to improve application launch performance.
437 [EditorBrowsable(EditorBrowsableState.Never)]
438 public void SendLaunchRequest(AppControl appControl)
440 transitionOptions.SendLaunchRequest(appControl);
444 /// This is used to improve application launch performance.
446 [EditorBrowsable(EditorBrowsableState.Never)]
447 public TransitionOptions TransitionOptions
451 return transitionOptions;
455 transitionOptions = value;
459 // TODO Enable this after tizen-theme-manager is released.
460 // private void TizenThemeChanged(object sender, ThemeEventArgs e)
462 // string prefix = "/theme/";
464 // Dictionary<string, string> changedResources = new Dictionary<string, string>();
465 // foreach (string key in ThemeManager.DefaultTheme.Resources.Keys)
467 // // NOTE Need improve this code by checking HasKey
468 // string newValue = null;
471 // newValue = e.Theme.GetString(prefix + key);
474 // if (newValue != null)
476 // changedResources[key] = newValue;
479 // ThemeManager.UpdateCurrentThemeResources(changedResources);
483 /// Check if it is loaded as dotnet-loader-nui.
485 static internal bool IsPreLoad
495 /// Graphics Backend Type.
497 [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
498 [EditorBrowsable(EditorBrowsableState.Never)]
499 [Obsolete("Please do not use! This will be deprecated!")]
500 public class Graphics
503 /// Graphics Backend Type.
505 public enum BackendType
508 /// The GLES backend.
512 /// The Vulkan backend.
518 /// The backend used by the NUIApplication.
520 [EditorBrowsable(EditorBrowsableState.Never)]
521 internal static BackendType Backend = BackendType.Gles;
523 internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
524 internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";