[NUI] set default theme for TV profile
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Application / NUIApplication.cs
index 90e3aab..72b016c 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * 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.
@@ -14,6 +14,9 @@
  * limitations under the License.
  *
  */
+#if !PROFILE_TV
+#define ExternalThemeEnabled
+#endif
 
 using System;
 using System.ComponentModel;
@@ -22,9 +25,7 @@ using System.Threading;
 using System.Reflection;
 using Tizen.Applications;
 using Tizen.Applications.CoreBackend;
-using Tizen.NUI.Binding;
 using Tizen.NUI.Xaml;
-using System.Collections.Generic;
 
 namespace Tizen.NUI
 {
@@ -39,28 +40,32 @@ namespace Tizen.NUI
         /// The instance of ResourceManager.
         /// </summary>
         private static System.Resources.ResourceManager resourceManager = null;
-        private Size2D _windowSize2D = null;
-        private Position2D _windowPosition2D = null;
-        private TransitionOptions transitionOptions;
-        private string appId = null;
-        internal static NUIApplication me;
+        private static string currentLoadedXaml = null;
 
-        private static bool isPreload = false;
+        /// <summary>
+        /// The border window
+        /// </summary>
+        private bool borderEnabled = false;
+        private IBorderInterface borderInterface = null;
 
-        // TODO Enable this after tizen-theme-manager is released.
-        // private readonly ThemeLoader themeLoader = new ThemeLoader();
+        /// <summary>
+        /// Xaml loaded delegate.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public delegate void XamlLoadedHandler(string xamlName);
+
+        static NUIApplication()
+        {
+            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
+        }
 
         /// <summary>
         /// The default constructor.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        [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())
         {
-            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
-            me = this;
-
-            // TODO Enable this after tizen-theme-manager is released.
-            // themeLoader.ThemeChanged += TizenThemeChanged;
         }
 
         /// <summary>
@@ -70,15 +75,10 @@ namespace Tizen.NUI
         /// <param name="windowPosition">The window position.</param>
         /// <since_tizen> 5 </since_tizen>
         /// 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("", WindowMode.Opaque, windowSize, windowPosition))
+        public NUIApplication(Size2D windowSize, Position2D windowPosition) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
         {
-            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
-            // TODO Enable this after tizen-theme-manager is released.
-            // themeLoader.ThemeChanged += TizenThemeChanged;
-            _windowSize2D = windowSize;
-            _windowPosition2D = windowPosition;
-            me = this;
         }
 
         /// <summary>
@@ -86,12 +86,9 @@ namespace Tizen.NUI
         /// </summary>
         /// <param name="styleSheet">The styleSheet url.</param>
         /// <since_tizen> 3 </since_tizen>
+        [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))
         {
-            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
-            me = this;
-            // TODO Enable this after tizen-theme-manager is released.
-            // themeLoader.ThemeChanged += TizenThemeChanged;
         }
 
         /// <summary>
@@ -102,15 +99,10 @@ namespace Tizen.NUI
         /// <param name="windowPosition">The window position.</param>
         /// <since_tizen> 5 </since_tizen>
         /// 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))
         {
-            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
-            // TODO Enable this after tizen-theme-manager is released.
-            // themeLoader.ThemeChanged += TizenThemeChanged;
-            _windowSize2D = windowSize;
-            _windowPosition2D = windowPosition;
-            me = this;
         }
 
         /// <summary>
@@ -119,12 +111,9 @@ namespace Tizen.NUI
         /// <param name="styleSheet">The styleSheet url.</param>
         /// <param name="windowMode">The windowMode.</param>
         /// <since_tizen> 3 </since_tizen>
+        [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))
         {
-            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
-            me = this;
-            // TODO Enable this after tizen-theme-manager is released.
-            // themeLoader.ThemeChanged += TizenThemeChanged;
         }
 
         /// <summary>
@@ -136,15 +125,10 @@ namespace Tizen.NUI
         /// <param name="windowPosition">The window position.</param>
         /// <since_tizen> 5 </since_tizen>
         /// 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))
         {
-            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
-            // TODO Enable this after tizen-theme-manager is released.
-            // themeLoader.ThemeChanged += TizenThemeChanged;
-            _windowSize2D = windowSize;
-            _windowPosition2D = windowPosition;
-            me = this;
         }
 
         /// <summary>
@@ -155,21 +139,131 @@ namespace Tizen.NUI
         /// <param name="windowSize"></param>
         /// <param name="windowPosition"></param>
         /// <param name="styleSheet"></param>
-        /// InhouseAPI, this could be opend in NextTizen
-        [Obsolete("Please do not use! This will be deprecated!")]
+        /// InhouseAPI, this could be opened in NextTizen
+        [Obsolete("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. currenlty it's not working as expected.
+            //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!");
+        }
 
-            if (windowSize != null) { _windowSize2D = windowSize; }
-            if (windowPosition != null) { _windowPosition2D = windowPosition; }
-            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
-            me = this;
-            // TODO Enable this after tizen-theme-manager is released.
-            // themeLoader.ThemeChanged += TizenThemeChanged;
+        /// <summary>
+        /// The constructor with theme option.
+        /// </summary>
+        /// <param name="option">The theme option.</param>
+        [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);
+        }
+
+        /// <summary>
+        /// The constructor with window size and position and theme option.
+        /// </summary>
+        /// <param name="windowSize">The window size.</param>
+        /// <param name="windowPosition">The window position.</param>
+        /// <param name="option">The theme option.</param>
+        [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);
+        }
+
+        /// <summary>
+        /// The constructor with a stylesheet, window mode and default window type.
+        /// It is the only way to create an IME window.
+        /// </summary>
+        /// <param name="styleSheet">The styleSheet URL.</param>
+        /// <param name="windowMode">The windowMode.</param>
+        /// <param name="type">The default window type.</param>
+        /// <since_tizen> 9 </since_tizen>
+        [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, WindowType type) : base(new NUICoreBackend(styleSheet, windowMode, type))
+        {
+            ExternalThemeManager.Initialize();
+        }
+
+        /// <summary>
+        /// The constructor with a stylesheet, size, position, boderInterface and window mode
+        /// </summary>
+        /// <param name="styleSheet">The styleSheet URL.</param>
+        /// <param name="windowSize">The window size.</param>
+        /// <param name="windowPosition">The window position.</param>
+        /// <param name="borderInterface"><see cref="Tizen.NUI.IBorderInterface"/>If borderInterface is null, defaultBorder is enabled.</param>
+        /// <param name="windowMode">The windowMode.</param>
+        [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(string styleSheet, Size2D windowSize, Position2D windowPosition, IBorderInterface borderInterface, WindowMode windowMode = WindowMode.Opaque) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition))
+        {
+            EnableBorder(borderInterface);
+        }
+
+        /// <summary>
+        /// The constructor with theme option and borderInterface.
+        /// </summary>
+        /// <param name="option">The theme option.</param>
+        /// <param name="borderInterface"><see cref="Tizen.NUI.IBorderInterface"/>If borderInterface is null, defaultBorder is enabled.</param>
+        [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, IBorderInterface borderInterface) : base(new NUICoreBackend())
+        {
+            EnableBorder(borderInterface);
+            ApplyThemeOption(option);
+        }
+
+        /// <summary>
+        /// The constructor with window size, position, theme option and borderInterface.
+        /// </summary>
+        /// <param name="windowSize">The window size.</param>
+        /// <param name="windowPosition">The window position.</param>
+        /// <param name="option">The theme option.</param>
+        /// <param name="borderInterface"><see cref="Tizen.NUI.IBorderInterface"/>If borderInterface is null, defaultBorder is enabled.</param>
+        [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, IBorderInterface borderInterface) : base(new NUICoreBackend("", NUIApplication.WindowMode.Opaque, windowSize, windowPosition))
+        {
+            EnableBorder(borderInterface);
+            ApplyThemeOption(option);
+        }
+
+        /// <summary>
+        /// The constructor with a stylesheet, window mode, coretask
+        /// </summary>
+        /// <note>
+        /// There is the UI thread feature.
+        /// UI thread is an additional thread that an Application object creates. The thread is for UI events.
+        /// To enable the UI Thread, you have to pass CoreTask object using this contructor.
+        /// When the UI thread feature is enabled, The methods of CoreTask are emitted on the main thread,
+        /// and the NUIApplication's events are emitted on the UI thread.
+        /// 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.
+        /// The CoreTask is not for handling GUI.
+        /// Callbacks of the all events in NUIApplication except the CoreTask are emitted on the UI thread.
+        /// </note>
+        /// <param name="styleSheet">The styleSheet URL.</param>
+        /// <param name="windowMode">The windowMode.</param>
+        /// <param name="task">True If app creates a UI Thread</param>
+        [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, CoreTask task) : base(new NUICoreBackend(styleSheet, windowMode), task)
+        {
+        }
+
+        /// <summary>
+        /// The constructor with a stylesheet, window mode, window size, position, coretask
+        /// </summary>
+        /// <param name="styleSheet">The styleSheet URL.</param>
+        /// <param name="windowMode">The windowMode.</param>
+        /// <param name="windowSize">The window size.</param>
+        /// <param name="windowPosition">The window position.</param>
+        /// <param name="task">True If app creates a UI Thread</param>
+        [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, CoreTask task) : base(new NUICoreBackend(styleSheet, windowMode, windowSize, windowPosition), task)
+        {
         }
 
         /// <summary>
@@ -185,6 +279,12 @@ namespace Tizen.NUI
         public event EventHandler Paused;
 
         /// <summary>
+        /// Xaml loaded event.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static event XamlLoadedHandler XamlLoaded;
+
+        /// <summary>
         /// Enumeration for deciding whether a NUI application window is opaque or transparent.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
@@ -203,6 +303,54 @@ namespace Tizen.NUI
         }
 
         /// <summary>
+        /// Enumeration for theme options of the NUIApplication.
+        /// </summary>
+        [Flags]
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public enum ThemeOptions : int
+        {
+            /// <summary>
+            /// No option specified.
+            /// </summary>
+            [EditorBrowsable(EditorBrowsableState.Never)]
+            None = 0,
+
+            /// <summary>
+            /// Enable platform theme.
+            /// When this option is on, all views in the NUIApplication is affected by platform theme (e.g. light/dark).
+            /// </summary>
+            [EditorBrowsable(EditorBrowsableState.Never)]
+            PlatformThemeEnabled = 1 << 0,
+
+            /// <summary>
+            /// Sets the default value of View.ThemeChangeSensitive.
+            /// when this option is on, all views are made sensitive on theme changing by default.
+            /// </summary>
+            [EditorBrowsable(EditorBrowsableState.Never)]
+            ThemeChangeSensitive = 1 << 1,
+        };
+
+        /// <summary>
+        /// Current loaded xaml's full name.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static string CurrentLoadedXaml
+        {
+            get
+            {
+                return currentLoadedXaml;
+            }
+            set
+            {
+                if (currentLoadedXaml != value)
+                {
+                    currentLoadedXaml = value;
+                    XamlLoaded?.Invoke(value);
+                }
+            }
+        }
+
+        /// <summary>
         /// ResourceManager to handle multilingual.
         /// </summary>
         /// <since_tizen> 4 </since_tizen>
@@ -222,7 +370,7 @@ namespace Tizen.NUI
         /// Gets the window instance.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
-        [Obsolete("Please do not use! This will be deprecated!")]
+        [Obsolete("Do not use! This will be deprecated!")]
         [EditorBrowsable(EditorBrowsableState.Never)]
         public Window Window
         {
@@ -241,7 +389,7 @@ namespace Tizen.NUI
         {
             get
             {
-                return appId;
+                return Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
             }
         }
 
@@ -395,6 +543,14 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         protected virtual void OnPreCreate()
         {
+            Tizen.Tracer.Begin("[NUI] OnPreCreate()");
+
+            if (borderEnabled)
+            {
+                GetDefaultWindow().EnableBorder(borderInterface, new Window.BorderCloseDelegate(Exit));
+            }
+
+            Tizen.Tracer.End();
         }
 
         /// <summary>
@@ -417,8 +573,11 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         protected override void OnCreate()
         {
+            Tizen.Tracer.Begin("[NUI] OnCreate()");
+
             base.OnCreate();
-            appId = Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
+
+            Tizen.Tracer.End();
         }
 
         /// <summary>
@@ -428,66 +587,34 @@ namespace Tizen.NUI
         static public void Preload()
         {
             Interop.Application.PreInitialize();
-            isPreload = true;
+#if ExternalThemeEnabled
+            ThemeManager.Preload();
+#endif
+            IsPreload = true;
         }
 
         /// <summary>
-        /// This is used to improve application launch performance.
+        /// Check if it is loaded as dotnet-loader-nui.
         /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public void SendLaunchRequest(AppControl appControl)
-        {
-            transitionOptions.SendLaunchRequest(appControl);
-        }
+        static internal bool IsPreload { get; set; }
 
-        /// <summary>
-        /// This is used to improve application launch performance.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public TransitionOptions TransitionOptions
+        private void ApplyThemeOption(ThemeOptions option)
         {
-            get
+            if ((option & ThemeOptions.PlatformThemeEnabled) != 0)
             {
-                return transitionOptions;
+                ThemeManager.PlatformThemeEnabled = true;
             }
-            set
+
+            if ((option & ThemeOptions.ThemeChangeSensitive) != 0)
             {
-                transitionOptions = value;
+                ThemeManager.ApplicationThemeChangeSensitive = true;
             }
         }
 
-        // TODO Enable this after tizen-theme-manager is released.
-        // private void TizenThemeChanged(object sender, ThemeEventArgs e)
-        // {
-        //     string prefix = "/theme/";
-
-        //     Dictionary<string, string> changedResources = new Dictionary<string, string>();
-        //     foreach (string key in ThemeManager.DefaultTheme.Resources.Keys)
-        //     {
-        //         // NOTE Need improve this code by checking HasKey
-        //         string newValue = null;
-        //         try
-        //         {
-        //             newValue = e.Theme.GetString(prefix + key);
-        //         }
-        //         catch { }
-        //         if (newValue != null)
-        //         {
-        //             changedResources[key] = newValue;
-        //         }
-        //     }
-        //     ThemeManager.UpdateCurrentThemeResources(changedResources);
-        // }
-
-        /// <summary>
-        /// Check if it is loaded as dotnet-loader-nui.
-        /// </summary>
-        static internal bool IsPreload
+        private void EnableBorder(IBorderInterface borderInterface)
         {
-            get
-            {
-                return isPreload;
-            }
+            borderEnabled = true;
+            this.borderInterface = borderInterface;
         }
     }
 
@@ -496,7 +623,7 @@ namespace Tizen.NUI
     /// </summary>
     [SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeStaticOrNotInheritable")]
     [EditorBrowsable(EditorBrowsableState.Never)]
-    [Obsolete("Please do not use! This will be deprecated!")]
+    [Obsolete("Do not use! This will be deprecated!")]
     public class Graphics
     {
         /// <summary>
@@ -523,5 +650,4 @@ namespace Tizen.NUI
         internal const string GlesCSharpBinder = NDalicPINVOKE.Lib;
         internal const string VulkanCSharpBinder = "libdali-csharp-binder-vk.so";
     }
-
 }