/*
* Copyright (c) 2021 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Reflection;
using Tizen.Applications;
using Tizen.Applications.CoreBackend;
using Tizen.NUI.Xaml;
namespace Tizen.NUI
{
///
/// Represents an application that have a UI screen. The NUIApplication class has a default stage.
///
/// 3
public class NUIApplication : CoreApplication
{
///
/// The instance of ResourceManager.
///
private static System.Resources.ResourceManager resourceManager = null;
private static string currentLoadedXaml = null;
///
/// Xaml loaded delegate.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void XamlLoadedHandler(string xamlName);
static NUIApplication()
{
Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
}
///
/// The default constructor.
///
/// 3
[SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
public NUIApplication() : base(new NUICoreBackend())
{
}
///
/// The constructor with window size and position.
///
/// The window size.
/// The window position.
/// 5
/// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
[SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
[EditorBrowsable(EditorBrowsableState.Never)]
public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
{
}
///
/// The constructor with a stylesheet.
///
/// The styleSheet url.
/// 3
[SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
{
}
///
/// The constructor with a stylesheet, window size, and position.
///
/// The styleSheet URL.
/// The window size.
/// The window position.
/// 5
/// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
[SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
[EditorBrowsable(EditorBrowsableState.Never)]
public NUIApplication(string styleSheet, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, WindowMode.Opaque, windowSize, windowPosition))
{
}
///
/// The constructor with a stylesheet and window mode.
///
/// The styleSheet url.
/// The windowMode.
/// 3
[SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
{
}
///
/// The constructor with a stylesheet, window mode, window size, and position.
///
/// The styleSheet URL.
/// The windowMode.
/// The window size.
/// The window position.
/// 5
/// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
[SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
[EditorBrowsable(EditorBrowsableState.Never)]
public NUIApplication(string styleSheet, WindowMode windowMode, Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
{
}
///
/// Internal inhouse constructor with Graphics Backend Type
///
///
///
///
///
///
/// InhouseAPI, this could be opened in NextTizen
[Obsolete("Please do not use! This will be deprecated!")]
[EditorBrowsable(EditorBrowsableState.Never)]
public NUIApplication(Graphics.BackendType backend, WindowMode windowMode = WindowMode.Opaque, Size2D windowSize = null, Position2D windowPosition = null, string styleSheet = "") : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
{
//windowMode and styleSheet will be added later. currently it's not working as expected.
Graphics.Backend = backend;
Tizen.Log.Error("NUI", "Plaese DO NOT set graphical backend type with this constructor! This will give no effect!");
}
///
/// The constructor with theme option.
///
/// The theme option.
[EditorBrowsable(EditorBrowsableState.Never)]
[SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
public NUIApplication(ThemeOptions option) : base(new NUICoreBackend())
{
ApplyThemeOption(option);
}
///
/// The constructor with window size and position and theme option.
///
/// The window size.
/// The window position.
/// The theme option.
[SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "NUICoreBackend is disposed in the base class when the application is terminated")]
[EditorBrowsable(EditorBrowsableState.Never)]
public NUIApplication(Size2D windowSize, Position2D windowPosition, ThemeOptions option) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
{
ApplyThemeOption(option);
}
///
/// The constructor with a stylesheet, window mode and default window type.
/// It is the only way to create an IME window.
///
/// The styleSheet URL.
/// The windowMode.
/// The default window type.
/// 9
public NUIApplication(string styleSheet, WindowMode windowMode, WindowType type) : base(new NUICoreBackend(styleSheet, windowMode, type))
{
ExternalThemeManager.Initialize();
}
///
/// Occurs whenever the application is resumed.
///
/// 4
public event EventHandler Resumed;
///
/// Occurs whenever the application is paused.
///
/// 4
public event EventHandler Paused;
///
/// Xaml loaded event.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public static event XamlLoadedHandler XamlLoaded;
///
/// Enumeration for deciding whether a NUI application window is opaque or transparent.
///
/// 3
public enum WindowMode
{
///
/// Opaque
///
/// 3
Opaque = 0,
///
/// Transparent
///
/// 3
Transparent = 1
}
///
/// Enumeration for theme options of the NUIApplication.
///
[Flags]
[EditorBrowsable(EditorBrowsableState.Never)]
public enum ThemeOptions : int
{
///
/// No option specified.
///
[EditorBrowsable(EditorBrowsableState.Never)]
None = 0,
///
/// Enable platform theme.
/// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
///
[EditorBrowsable(EditorBrowsableState.Never)]
PlatformThemeEnabled = 1 << 0,
///
/// Sets the default value of View.ThemeChangeSensitive.
/// when this option is on, all views are made sensitive on theme changing by default.
///
[EditorBrowsable(EditorBrowsableState.Never)]
ThemeChangeSensitive = 1 << 1,
};
///
/// Current loaded xaml's full name.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public static string CurrentLoadedXaml
{
get
{
return currentLoadedXaml;
}
set
{
if (currentLoadedXaml != value)
{
currentLoadedXaml = value;
XamlLoaded?.Invoke(value);
}
}
}
///
/// ResourceManager to handle multilingual.
///
/// 4
public static System.Resources.ResourceManager MultilingualResourceManager
{
get
{
return resourceManager;
}
set
{
resourceManager = value;
}
}
///
/// Gets the window instance.
///
/// 3
[Obsolete("Please do not use! This will be deprecated!")]
[EditorBrowsable(EditorBrowsableState.Never)]
public Window Window
{
get
{
return GetDefaultWindow();
}
}
///
/// Gets the Application Id.
///
/// 6
[EditorBrowsable(EditorBrowsableState.Never)]
public string AppId
{
get
{
return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
}
}
///
/// Gets the default window.
///
/// The default Window.
/// 6
// This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
[EditorBrowsable(EditorBrowsableState.Never)]
public static Window GetDefaultWindow()
{
return Window.Instance;
}
internal Application ApplicationHandle
{
get
{
return ((NUICoreBackend)this.Backend).ApplicationHandle;
}
}
///
/// Register the assembly to XAML.
///
/// 5
public static void RegisterAssembly(Assembly assembly)
{
XamlParser.s_assemblies.Add(assembly);
}
///
/// Runs the NUIApplication.
///
/// Arguments from commandline.
/// 4
public override void Run(string[] args)
{
Backend.AddEventHandler(EventType.PreCreated, OnPreCreate);
Backend.AddEventHandler(EventType.Resumed, OnResume);
Backend.AddEventHandler(EventType.Paused, OnPause);
base.Run(args);
}
///
/// Exits the NUIApplication.
///
/// 4
public override void Exit()
{
base.Exit();
}
///
/// Ensures that the function passed in is called from the main loop when it is idle.
///
/// The function to call
/// true if added successfully, false otherwise
/// 4
public bool AddIdle(System.Delegate func)
{
return ((NUICoreBackend)this.Backend).AddIdle(func);
}
///
/// Sets the number of frames per render.
///
/// The number of vsyncs between successive renders.
///
/// Suggest this is a power of two:
/// 1 - render each vsync frame.
/// 2 - render every other vsync frame.
/// 4 - render every fourth vsync frame.
/// 8 - render every eighth vsync frame.
/// For example, if an application runs on 60 FPS and SetRenderRefreshRate(2) is called, the frames per second will be changed to 30.
///
/// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public static void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
{
Adaptor.Instance.SetRenderRefreshRate(numberOfVSyncsPerRender);
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected override void OnLocaleChanged(LocaleChangedEventArgs e)
{
base.OnLocaleChanged(e);
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected override void OnLowBattery(LowBatteryEventArgs e)
{
base.OnLowBattery(e);
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected override void OnLowMemory(LowMemoryEventArgs e)
{
base.OnLowMemory(e);
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
{
base.OnRegionFormatChanged(e);
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected override void OnTerminate()
{
base.OnTerminate();
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected virtual void OnPause()
{
Paused?.Invoke(this, EventArgs.Empty);
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected virtual void OnResume()
{
Resumed?.Invoke(this, EventArgs.Empty);
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected virtual void OnPreCreate()
{
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
{
if (e != null)
{
Log.Info("NUI", "OnAppControlReceived() is called! ApplicationId=" + e.ReceivedAppControl.ApplicationId);
Log.Info("NUI", "CallerApplicationId=" + e.ReceivedAppControl.CallerApplicationId + " IsReplyRequest=" + e.ReceivedAppControl.IsReplyRequest);
}
base.OnAppControlReceived(e);
}
///
/// Overrides this method if you want to handle behavior.
///
/// 3
protected override void OnCreate()
{
base.OnCreate();
}
///
/// This is used to improve application launch performance.
///
[EditorBrowsable(EditorBrowsableState.Never)]
static public void Preload()
{
Interop.Application.PreInitialize();
#if ExternalThemeEnabled
ThemeManager.Preload();
#endif
IsPreload = true;
}
///
/// This is used to improve application launch performance.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendLaunchRequest(AppControl appControl)
{
TransitionOptions?.SendLaunchRequest(appControl);
}
///
/// This is used to improve application launch performance.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public TransitionOptions TransitionOptions { get; set; }
///
/// Check if it is loaded as dotnet-loader-nui.
///
static internal bool IsPreload { get; set; }
private void ApplyThemeOption(ThemeOptions option)
{
if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
{
ThemeManager.PlatformThemeEnabled = true;
}
if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
{
ThemeManager.ApplicationThemeChangeSensitive = true;
}
}
}
///
/// Graphics Backend Type.
///
[SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Please do not use! This will be deprecated!")]
public class Graphics
{
///
/// Graphics Backend Type.
///
public enum BackendType
{
///
/// The GLES backend.
///
Gles,
///
/// The Vulkan backend.
///
Vulkan
}
///
/// The backend used by the NUIApplication.
///
[EditorBrowsable(EditorBrowsableState.Never)]
internal static BackendType Backend = BackendType.Gles;
internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
}
}