[NUI] AppBar code refactoring (#2861)
authorJaehyun Cho <jaehyun0cho@gmail.com>
Mon, 12 Apr 2021 02:45:35 +0000 (11:45 +0900)
committerhuiyueun <35286162+huiyueun@users.noreply.github.com>
Tue, 20 Apr 2021 06:13:00 +0000 (15:13 +0900)
To enhance usability, some APIs are added and removed as follows.

Title and Actions are added to set these properties with constructor.

SetTitle(), AddActions() and RemoveActions() are removed because the
above Title and Actions cover theses methods.

Comment's indentation is also fixed.

Co-authored-by: Jaehyun Cho <jae_hyun.cho@samsung.com>
src/Tizen.NUI.Components/Controls/Navigation/AppBar.cs
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/AppBarSample.cs

index be404eb..bacb4d2 100755 (executable)
@@ -14,8 +14,9 @@
  * limitations under the License.
  *
  */
-using System;
+
 using System.ComponentModel;
+using System.Collections.Generic;
 using Tizen.NUI.BaseComponents;
 
 namespace Tizen.NUI.Components
@@ -29,14 +30,17 @@ namespace Tizen.NUI.Components
     {
         private bool autoNavigationContent = true;
 
+        private string title = null;
+
+        private View navigationContent = null;
+        private View titleContent = null;
+        private View actionContent = null;
+        private IEnumerable<View> actionContentViews = null;
+
         private View defaultNavigationContent = null;
         private View defaultTitleContent = null;
         private View defaultActionContent = null;
 
-        private View appBarNavigation = null;
-        private View appBarTitle = null;
-        private View appBarAction = null;
-
         private AppBarStyle appBarStyle => ViewStyle as AppBarStyle;
 
         private bool styleApplied = false;
@@ -84,18 +88,18 @@ namespace Tizen.NUI.Components
 
             if (type == DisposeTypes.Explicit)
             {
-                if (appBarNavigation == defaultNavigationContent)
+                if (navigationContent == defaultNavigationContent)
                 {
-                    if (appBarNavigation != null)
+                    if (navigationContent != null)
                     {
-                        Utility.Dispose(appBarNavigation);
+                        Utility.Dispose(navigationContent);
                     }
                 }
                 else
                 {
-                    if (appBarNavigation != null)
+                    if (navigationContent != null)
                     {
-                        Utility.Dispose(appBarNavigation);
+                        Utility.Dispose(navigationContent);
                     }
 
                     if (defaultNavigationContent != null)
@@ -104,18 +108,18 @@ namespace Tizen.NUI.Components
                     }
                 }
 
-                if (appBarTitle == defaultTitleContent)
+                if (titleContent == defaultTitleContent)
                 {
-                    if (appBarTitle != null)
+                    if (titleContent != null)
                     {
-                        Utility.Dispose(appBarTitle);
+                        Utility.Dispose(titleContent);
                     }
                 }
                 else
                 {
-                    if (appBarTitle != null)
+                    if (titleContent != null)
                     {
-                        Utility.Dispose(appBarTitle);
+                        Utility.Dispose(titleContent);
                     }
 
                     if (defaultTitleContent != null)
@@ -124,18 +128,18 @@ namespace Tizen.NUI.Components
                     }
                 }
 
-                if (appBarAction == defaultActionContent)
+                if (actionContent == defaultActionContent)
                 {
-                    if (appBarAction != null)
+                    if (actionContent != null)
                     {
-                        Utility.Dispose(appBarAction);
+                        Utility.Dispose(actionContent);
                     }
                 }
                 else
                 {
-                    if (appBarAction != null)
+                    if (actionContent != null)
                     {
-                        Utility.Dispose(appBarAction);
+                        Utility.Dispose(actionContent);
                     }
 
                     if (defaultActionContent != null)
@@ -158,22 +162,22 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return appBarNavigation;
+                return navigationContent;
             }
             set
             {
-                if (appBarNavigation == value)
+                if (navigationContent == value)
                 {
                     return;
                 }
 
-                if (appBarNavigation != null)
+                if (navigationContent != null)
                 {
-                    Remove(appBarNavigation);
+                    Remove(navigationContent);
                 }
 
-                appBarNavigation = value;
-                if (appBarNavigation == null)
+                navigationContent = value;
+                if (navigationContent == null)
                 {
                     return;
                 }
@@ -183,66 +187,175 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
+        /// Title text of AppBar.
+        /// SetTitle sets title text to the default title content.
+        /// If TitleContent is not TextLabel, then Title does not set title text of the TitleContent.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public string Title
+        {
+            get
+            {
+                return title;
+            }
+            set
+            {
+                if (title == value)
+                {
+                    return;
+                }
+
+                title = value;
+
+                if (TitleContent is TextLabel textLabel)
+                {
+                    textLabel.Text = title;
+                }
+            }
+        }
+
+        /// <summary>
         /// Title content of AppBar. TitleContent is added to Children automatically.
-        /// If TitleContent is set by user, then SetText does not set title text of the replaced TitleContent.
+        /// If TitleContent is not TextLabel, then Title does not set title text of the TitleContent.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public View TitleContent
         {
             get
             {
-                return appBarTitle;
+                return titleContent;
             }
             set
             {
-                if (appBarTitle == value)
+                if (titleContent == value)
                 {
                     return;
                 }
 
-                if (appBarTitle != null)
+                if (titleContent != null)
                 {
-                    Remove(appBarTitle);
+                    Remove(titleContent);
                 }
 
-                appBarTitle = value;
-                if (appBarTitle == null)
+                titleContent = value;
+                if (titleContent == null)
                 {
                     return;
                 }
 
+                if (titleContent is TextLabel textLabel)
+                {
+                    textLabel.Text = Title;
+                }
+
                 ResetContent();
             }
         }
 
         /// <summary>
+        /// Action views of AppBar.
+        /// Action views are added to ActionContent of AppBar.
+        /// The Action and ActionButton styles of AppBarStyle are applied to actions only by setting Actions.
+        /// If you do not want to apply Action and ActionButton styles to action views, then please use ActionContent.Add() instead.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public IEnumerable<View> Actions
+        {
+            get
+            {
+                return actionContentViews;
+            }
+            set
+            {
+                if (ActionContent == null)
+                {
+                    actionContentViews = value;
+                    return;
+                }
+
+                if (actionContentViews != null)
+                {
+                    foreach (var oldAction in actionContentViews)
+                    {
+                        if (ActionContent.Children?.Contains(oldAction) == true)
+                        {
+                            ActionContent.Children.Remove(oldAction);
+                        }
+                    }
+                }
+
+                actionContentViews = value;
+
+                if (actionContentViews == null)
+                {
+                    return;
+                }
+
+                foreach (var action in actionContentViews)
+                {
+                    // Apply Action and ActionButton styles.
+                    if ((action is Button) && (appBarStyle?.ActionButton != null))
+                    {
+                        action.ApplyStyle(appBarStyle.ActionButton);
+                    }
+                    else if (appBarStyle?.ActionView != null)
+                    {
+                        action.ApplyStyle(appBarStyle.ActionView);
+                    }
+
+                    ActionContent.Add(action);
+                }
+            }
+        }
+
+        /// <summary>
         /// Action content of AppBar. ActionContent is added to Children automatically.
-        /// Action content can contain action views and action buttons by AddActions.
-        /// If ActionContent is set by user, then AddActions does not add actions to the replaced ActionContent.
-        /// The Action and ActionButton styles of AppBarStyle are applied to actions only by AddActions.
-        /// If ActionContent is set by user, then RemoveActions does not remove actions from the replaced ActionContent.
+        /// Action content contains action views and action buttons by Actions.
+        /// The Action and ActionButton styles of AppBarStyle are applied to actions only by setting Actions.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public View ActionContent
         {
             get
             {
-                return appBarAction;
+                return actionContent;
             }
             set
             {
-                if (appBarAction == value)
+                if (actionContent == value)
                 {
                     return;
                 }
 
-                if (appBarAction != null)
+                var oldActionContent = actionContent;
+                actionContent = value;
+
+                // Add views first before remove previous action content
+                // not to cause Garbage Collector collects views.
+                if ((actionContent != null) && (Actions != null))
                 {
-                    Remove(appBarAction);
+                    foreach (var action in Actions)
+                    {
+                        // Apply Action and ActionButton styles.
+                        if ((action is Button) && (appBarStyle?.ActionButton != null))
+                        {
+                            action.ApplyStyle(appBarStyle.ActionButton);
+                        }
+                        else if (appBarStyle?.ActionView != null)
+                        {
+                            action.ApplyStyle(appBarStyle.ActionView);
+                        }
+
+                        actionContent.Add(action);
+                    }
                 }
 
-                appBarAction = value;
-                if (appBarAction == null)
+                if (oldActionContent != null)
+                {
+                    Remove(oldActionContent);
+                }
+
+                if (actionContent == null)
                 {
                     return;
                 }
@@ -277,7 +390,10 @@ namespace Tizen.NUI.Components
 
                 if (autoNavigationContent == true)
                 {
-                    NavigationContent = DefaultNavigationContent;
+                    if (NavigationContent == null)
+                    {
+                        NavigationContent = DefaultNavigationContent;
+                    }
                 }
                 else if (NavigationContent == DefaultNavigationContent)
                 {
@@ -297,7 +413,7 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                //TODO: Do not set default navigation content if there is no previous page.
+                // TODO: Do not set default navigation content if there is no previous page.
                 if (defaultNavigationContent == null)
                 {
                     defaultNavigationContent = CreateDefaultNavigationContent();
@@ -356,125 +472,65 @@ namespace Tizen.NUI.Components
 
             base.ApplyStyle(viewStyle);
 
-            //Apply Back Button style.
-            if ((appBarStyle?.BackButton != null) && (DefaultNavigationContent is Button))
+            // Apply Back Button style.
+            if ((appBarStyle?.BackButton != null) && (DefaultNavigationContent is Button button))
             {
-                ((Button)DefaultNavigationContent)?.ApplyStyle(appBarStyle.BackButton);
+                button.ApplyStyle(appBarStyle.BackButton);
             }
 
-            //Apply Title style.
-            if ((appBarStyle?.TitleTextLabel != null) && (DefaultTitleContent is TextLabel))
+            // Apply Title style.
+            if ((appBarStyle?.TitleTextLabel != null) && (DefaultTitleContent is TextLabel textLabel))
             {
-                ((TextLabel)DefaultTitleContent)?.ApplyStyle(appBarStyle.TitleTextLabel);
+                textLabel.ApplyStyle(appBarStyle.TitleTextLabel);
             }
 
-            //Apply ActionCellPadding style.
-            if (DefaultActionContent?.Layout is LinearLayout)
+            // Apply ActionCellPadding style.
+            if (DefaultActionContent?.Layout is LinearLayout linearLayout)
             {
-                ((LinearLayout)DefaultActionContent?.Layout).CellPadding = new Size2D(appBarStyle?.ActionCellPadding?.Width ?? 0, appBarStyle?.ActionCellPadding?.Height ?? 0);
+                linearLayout.CellPadding = new Size2D(appBarStyle?.ActionCellPadding?.Width ?? 0, appBarStyle?.ActionCellPadding?.Height ?? 0);
             }
 
-            //Apply Action and ActionButton styles.
-            if (DefaultActionContent?.ChildCount > 0)
+            // Apply Action and ActionButton styles.
+            if (DefaultActionContent?.Children != null)
             {
                 foreach (var action in DefaultActionContent?.Children)
                 {
                     if ((action is Button) && (appBarStyle?.ActionButton != null))
                     {
-                        ((Button)action)?.ApplyStyle(appBarStyle.ActionButton);
+                        action.ApplyStyle(appBarStyle.ActionButton);
                     }
                     else if (appBarStyle?.ActionView != null)
                     {
-                        action?.ApplyStyle(appBarStyle.ActionView);
+                        action.ApplyStyle(appBarStyle.ActionView);
                     }
                 }
             }
 
             styleApplied = true;
 
-            //Calculate children's positions based on padding sizes.
+            // Calculate children's positions based on padding sizes.
             CalculatePosition();
         }
 
         /// <summary>
-        /// Sets title text of AppBar.
-        /// SetTitle sets title text to the default title content.
-        /// Therefore, if TitleContent is set by user, then SetTitle does not set title text of the replaced TitleContent.
-        /// </summary>
-        /// <param name="title">Title text of AppBar.</param>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public void SetTitle(string title)
-        {
-            if (DefaultTitleContent is TextLabel)
-            {
-                ((TextLabel)DefaultTitleContent).Text = title;
-            }
-        }
-
-        /// <summary>
-        /// Adds actions to ActionContent of AppBar.
-        /// The Action and ActionButton styles of AppBarStyle are applied to the added actions.
-        /// AddActions adds action views to the default action content.
-        /// Therefore, if ActionContent is set by user, then AddActions does not add actions to the replaced ActionContent.
-        /// </summary>
-        /// <param name="actions">Actions to be added to ActionContent of AppBar.</param>
-        /// <exception cref="ArgumentNullException">Thrown when the argument actions is null.</exception>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public void AddActions(params View[] actions)
-        {
-            if (actions == null)
-            {
-                throw new ArgumentNullException(nameof(actions), "actions should not be null.");
-            }
-
-            foreach (var action in actions)
-            {
-                //Apply Action and ActionButton styles.
-                if ((action is Button) && (appBarStyle?.ActionButton != null))
-                {
-                    ((Button)action)?.ApplyStyle(appBarStyle.ActionButton);
-                }
-                else if (appBarStyle?.ActionView != null)
-                {
-                    action?.ApplyStyle(appBarStyle.ActionView);
-                }
-
-                DefaultActionContent.Add(action);
-            }
-        }
-
-        /// <summary>
-        /// Removes actions from ActionContent of AppBar.
-        /// RemoveActions removes action views from the default action content.
-        /// Therefore, if ActionContent is set by user, then RemoveActions does not remove actions from the replaced ActionContent.
+        /// Gets AppBar style.
         /// </summary>
-        /// <param name="actions">Actions to be removed from ActionContent of AppBar.</param>
-        /// <exception cref="ArgumentNullException">Thrown when the argument actions is null.</exception>
+        /// <returns>The default AppBar style.</returns>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public void RemoveActions(params View[] actions)
+        protected override ViewStyle CreateViewStyle()
         {
-            if (actions == null)
-            {
-                throw new ArgumentNullException(nameof(actions), "actions should not be null.");
-            }
-
-            foreach (var action in actions)
-            {
-                DefaultActionContent.Remove(action);
-            }
+            return new AppBarStyle();
         }
 
         private void Initialize()
         {
-            //Navigation, Title and Action are located horizontally.
-            var linearLayout = new LinearLayout()
+            // Navigation, Title and Action are located horizontally.
+            Layout = new LinearLayout()
             {
                 LinearOrientation = LinearLayout.Orientation.Horizontal,
                 LinearAlignment = LinearLayout.Alignment.CenterVertical,
             };
 
-            Layout = linearLayout;
-
             WidthSpecification = LayoutParamPolicies.MatchParent;
 
             if (AutoNavigationContent == true)
@@ -493,7 +549,7 @@ namespace Tizen.NUI.Components
 
             backButton.Clicked += (object sender, ClickedEventArgs args) =>
             {
-                //The page of app bar is popped when default back button is clicked.
+                // The page of app bar is popped when default back button is clicked.
                 var page = GetParent() as Page;
                 if (page != null)
                 {
@@ -519,13 +575,13 @@ namespace Tizen.NUI.Components
 
         private View CreateDefaultActionContent()
         {
-            return new Control()
+            return new View()
             {
                 Layout = new LinearLayout()
                 {
                     LinearOrientation = LinearLayout.Orientation.Horizontal,
 
-                    //Apply ActionCellPadding style.
+                    // Apply ActionCellPadding style.
                     CellPadding = new Size2D(appBarStyle?.ActionCellPadding?.Width ?? 0, appBarStyle?.ActionCellPadding?.Height ?? 0),
                 },
                 Weight = 0.0f,
@@ -534,39 +590,39 @@ namespace Tizen.NUI.Components
 
         private void ResetContent()
         {
-            //To keep the order of NavigationContent, TitleContent and ActionContent,
-            //the existing contents are removed and added again.
-            if ((appBarNavigation != null) && Children.Contains(appBarNavigation))
+            // To keep the order of NavigationContent, TitleContent and ActionContent,
+            // the existing contents are removed and added again.
+            if ((navigationContent != null) && Children.Contains(navigationContent))
             {
-                Remove(appBarNavigation);
+                Remove(navigationContent);
             }
 
-            if ((appBarTitle != null) && Children.Contains(appBarTitle))
+            if ((titleContent != null) && Children.Contains(titleContent))
             {
-                Remove(appBarTitle);
+                Remove(titleContent);
             }
 
-            if ((appBarAction != null) && Children.Contains(appBarAction))
+            if ((actionContent != null) && Children.Contains(actionContent))
             {
-                Remove(appBarAction);
+                Remove(actionContent);
             }
 
-            if (appBarNavigation != null)
+            if (navigationContent != null)
             {
-                Add(appBarNavigation);
+                Add(navigationContent);
             }
 
-            if (appBarTitle != null)
+            if (titleContent != null)
             {
-                Add(appBarTitle);
+                Add(titleContent);
             }
 
-            if (appBarAction != null)
+            if (actionContent != null)
             {
-                Add(appBarAction);
+                Add(actionContent);
             }
 
-            //Calculate children's positions based on padding sizes.
+            // Calculate children's positions based on padding sizes.
             CalculatePosition();
         }
 
@@ -577,7 +633,7 @@ namespace Tizen.NUI.Components
                 return;
             }
 
-            //Apply NavigationPadding style.
+            // Apply NavigationPadding style.
             if ((NavigationContent != null) && (appBarStyle?.NavigationPadding != null))
             {
                 if (NavigationContent.Margin.NotEqualTo(appBarStyle.NavigationPadding))
@@ -586,7 +642,7 @@ namespace Tizen.NUI.Components
                 }
             }
 
-            //Apply ActionPadding style.
+            // Apply ActionPadding style.
             if ((ActionContent != null) && (appBarStyle?.ActionPadding != null))
             {
                 if (ActionContent.Margin.NotEqualTo(appBarStyle.ActionPadding))
@@ -595,15 +651,5 @@ namespace Tizen.NUI.Components
                 }
             }
         }
-
-        /// <summary>
-        /// Gets AppBar style.
-        /// </summary>
-        /// <returns>The default AppBar style.</returns>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        protected override ViewStyle CreateViewStyle()
-        {
-            return new AppBarStyle();
-        }
     }
-}
+}
\ No newline at end of file
index 4e85182..db583a2 100755 (executable)
@@ -1,4 +1,6 @@
-using Tizen.NUI.BaseComponents;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using Tizen.NUI.BaseComponents;
 using Tizen.NUI.Components;
 
 namespace Tizen.NUI.Samples
@@ -28,10 +30,10 @@ namespace Tizen.NUI.Samples
 
             firstAppBar = new AppBar()
             {
-                AutoNavigationContent = false
+                AutoNavigationContent = false,
+                Title = "First Page",
+                Actions = new View[] { firstActionButton },
             };
-            firstAppBar.SetTitle("First Page");
-            firstAppBar.AddActions(firstActionButton);
 
             firstButton = new Button()
             {
@@ -64,9 +66,11 @@ namespace Tizen.NUI.Samples
                 NUIApplication.GetDefaultWindow().GetDefaultNavigator().Pop();
             };
 
-            secondAppBar = new AppBar();
-            secondAppBar.SetTitle("Second Page");
-            secondAppBar.AddActions(secondActionButton);
+            secondAppBar = new AppBar()
+            {
+                Title = "Second Page",
+                Actions = new View[] { secondActionButton },
+            };
 
             secondButton = new Button()
             {