[NUI] Refactor ButtonExtension
authorJiyun Yang <ji.yang@samsung.com>
Fri, 17 Mar 2023 04:53:15 +0000 (13:53 +0900)
committerJaehyun Cho <jaehyun0cho@gmail.com>
Tue, 28 Mar 2023 07:45:27 +0000 (16:45 +0900)
* Rename some functions to better match the design intent.
* Extract Helper class to provide common methods used by multiple classes.

Signed-off-by: Jiyun Yang <ji.yang@samsung.com>
src/Tizen.NUI.Components/Controls/Button.Internal.cs
src/Tizen.NUI.Components/Controls/Button.cs
src/Tizen.NUI.Components/Controls/Extension/ButtonExtension.cs
src/Tizen.NUI.Components/Controls/Extension/LottieButtonExtension.cs
src/Tizen.NUI.Components/Controls/Extension/LottieSwitchExtension.cs
src/Tizen.NUI.Components/Controls/Extension/SwitchExtension.cs
src/Tizen.NUI.Components/Controls/Switch.cs
src/Tizen.NUI.Components/PreloadStyle/OverlayAnimationButtonStyle.cs

index 2b84723..c7369f5 100644 (file)
@@ -34,7 +34,7 @@ namespace Tizen.NUI.Components
         private EventHandler<StateChangedEventArgs> stateChangeHandler;
 
         private bool isPressed = false;
-        internal bool styleApplying = false;
+        internal int styleApplying = 0;
 
         /// <summary>
         /// Gets accessibility name.
@@ -193,7 +193,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected void UpdateState()
         {
-            if (styleApplying) return;
+            if (styleApplying > 0) return;
 
             ControlState sourceState = ControlState;
             ControlState targetState;
index bda6455..6601b2b 100755 (executable)
@@ -357,7 +357,7 @@ namespace Tizen.NUI.Components
                     overlayImage = CreateOverlayImage();
                     if (null != Extension)
                     {
-                        overlayImage = Extension.OnCreateOverlayImage(this, overlayImage);
+                        Extension.ProcessOverlayImage(this, ref overlayImage);
                     }
                     if (null != overlayImage)
                     {
@@ -1007,24 +1007,25 @@ namespace Tizen.NUI.Components
 
             base.ApplyStyle(viewStyle);
 
-            if (!styleApplying && viewStyle is ButtonStyle buttonStyle)
+            if (viewStyle is ButtonStyle buttonStyle)
             {
-                styleApplying = true;
-
-                if (buttonStyle.Overlay != null)
-                {
-                    OverlayImage?.ApplyStyle(buttonStyle.Overlay);
-                }
+                styleApplying++;
 
                 if ((Extension = buttonStyle.CreateExtension()) != null)
                 {
-                    buttonIcon.Unparent();
-                    buttonIcon = Extension.OnCreateIcon(this, buttonIcon);
+                    bool needRelayout = false;
+                    needRelayout |= Extension.ProcessIcon(this, ref buttonIcon);
+                    needRelayout |= Extension.ProcessText(this, ref buttonText);
 
-                    buttonText.Unparent();
-                    buttonText = Extension.OnCreateText(this, buttonText);
+                    if (needRelayout)
+                    {
+                        LayoutItems();
+                    }
+                }
 
-                    LayoutItems();
+                if (buttonStyle.Overlay != null)
+                {
+                    OverlayImage?.ApplyStyle(buttonStyle.Overlay);
                 }
 
                 if (buttonStyle.Text != null)
@@ -1037,7 +1038,8 @@ namespace Tizen.NUI.Components
                 {
                     buttonIcon.ApplyStyle(buttonStyle.Icon);
                 }
-                styleApplying = false;
+
+                styleApplying--;
             }
 
             UpdateState();
index b18c06b..cbeabf8 100755 (executable)
@@ -32,39 +32,39 @@ namespace Tizen.NUI.Components.Extension
         protected Touch TouchInfo { get; private set; }
 
         /// <summary>
-        /// Called immediately after the Button creates the text part.
+        /// Perform further processing of the button text.
         /// </summary>
-        /// <param name="button">The Button instance that the extension currently applied to.</param>
-        /// <param name="text">The created Button's text part.</param>
-        /// <return>The refined button text.</return>
+        /// <param name="button">The button instance that the extension currently applied to.</param>
+        /// <param name="text">The reference of the button text.</param>
+        /// <return>True if the given text is replaced.</return>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public virtual TextLabel OnCreateText(Button button, TextLabel text)
+        public virtual bool ProcessText(Button button, ref TextLabel text)
         {
-            return text;
+            return false;
         }
 
         /// <summary>
-        /// Called immediately after the Button creates the icon part.
+        /// Perform further processing of the button icon.
         /// </summary>
-        /// <param name="button">The Button instance that the extension currently applied to.</param>
-        /// <param name="icon">The created Button's icon part.</param>
-        /// <return>The refined button icon.</return>
+        /// <param name="button">The button instance that the extension currently applied to.</param>
+        /// <param name="icon">The reference of the button icon.</param>
+        /// <return>True if the given icon is replaced.</return>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public virtual ImageView OnCreateIcon(Button button, ImageView icon)
+        public virtual bool ProcessIcon(Button button, ref ImageView icon)
         {
-            return icon;
+            return false;
         }
 
         /// <summary>
-        /// Called immediately after the Button creates the overlay image part.
+        /// Perform further processing of the button overlay image.
         /// </summary>
-        /// <param name="button">The Button instance that the extension currently applied to.</param>
-        /// <param name="overlayImage">The created Button's overlayImage part.</param>
-        /// <return>The refined button overlayImage.</return>
+        /// <param name="button">The button instance that the extension currently applied to.</param>
+        /// <param name="overlayImage">The reference of the button overlay image.</param>
+        /// <return>True if the given overlayImage is replaced.</return>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public virtual ImageView OnCreateOverlayImage(Button button, ImageView overlayImage)
+        public virtual bool ProcessOverlayImage(Button button, ref ImageView overlayImage)
         {
-            return overlayImage;
+            return false;
         }
 
         /// <summary>
index d173d26..67a499b 100755 (executable)
@@ -29,57 +29,56 @@ namespace Tizen.NUI.Components.Extension
     [EditorBrowsable(EditorBrowsableState.Never)]
     public class LottieButtonExtension : ButtonExtension
     {
-        /// <summary>
-        /// A constructor that creates LottieButtonExtension with a specified Lottie resource URL
-        /// </summary>
+        /// <inheritdoc/>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public LottieButtonExtension() : base()
+        public override bool ProcessIcon(Button button, ref ImageView icon)
         {
-            LottieView = new LottieAnimationView();
-        }
+            if (button.Style is ILottieButtonStyle lottieStyle)
+            {
+                var lottieView = LottieExtensionHelper.CreateLottieView(lottieStyle);
+                var parent = icon.GetParent();
 
-        /// <summary>
-        /// The Lottie view that will be used as an icon part in a Button.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        protected LottieAnimationView LottieView { get; set; }
+                icon.Unparent();
+                icon.Dispose();
+                icon = lottieView;
+                parent?.Add(icon);
 
-        /// <inheritdoc/>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public override ImageView OnCreateIcon(Button button, ImageView icon)
-        {
-            InitializeLottieView(button, LottieView);
+                return true;
+            }
 
-            return LottieView;
+            return false;
         }
 
         /// <inheritdoc/>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnControlStateChanged(Button button, View.ControlStateChangedEventArgs args)
         {
-            UpdateLottieView(button, args.PreviousState, LottieView);
+            if (button.Style is ILottieButtonStyle lottieStyle && button.Icon is LottieAnimationView lottieView)
+            {
+                LottieExtensionHelper.UpdateLottieView(lottieView, lottieStyle, args.PreviousState, button.ControlState);
+            }
         }
+    }
 
-        internal static void InitializeLottieView(Button button, LottieAnimationView lottieView)
+    internal static class LottieExtensionHelper
+    {
+        internal static LottieAnimationView CreateLottieView(ILottieButtonStyle lottieStyle)
         {
-            if (button.Style as ILottieButtonStyle == null)
+            var lottieView = new LottieAnimationView()
             {
-                throw new Exception("LottieButtonExtension must be used within a ILottieButtonStyle or derived class.");
-            }
-
-            var lottieStyle = (ILottieButtonStyle)button.Style;
-            lottieView.URL = lottieStyle.LottieUrl;
-            lottieView.StopBehavior = LottieAnimationView.StopBehaviorType.MaximumFrame;
+                URL = lottieStyle.LottieUrl,
+                StopBehavior = LottieAnimationView.StopBehaviorType.MaximumFrame
+            };
             if (lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(ControlState.Normal, out var result))
             {
                 result.Show(lottieView, true);
             }
+            return lottieView;
         }
 
-        internal static void UpdateLottieView(Button button, ControlState previousState, LottieAnimationView lottieView)
+        internal static void UpdateLottieView(LottieAnimationView lottieView, ILottieButtonStyle lottieStyle, ControlState previousState, ControlState currentState)
         {
-            var lottieStyle = ((ILottieButtonStyle)button.Style);
-            if (lottieStyle != null && lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(button.ControlState, out var result))
+            if (lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(currentState, out var result))
             {
                 result.Show(lottieView, !previousState.Contains(ControlState.Pressed));
             }
index 246bde2..569e332 100755 (executable)
@@ -28,35 +28,34 @@ namespace Tizen.NUI.Components.Extension
     [EditorBrowsable(EditorBrowsableState.Never)]
     public class LottieSwitchExtension : SwitchExtension
     {
-        /// <summary>
-        /// A constructor that creates LottieButtonExtension with a specified Lottie resource URL
-        /// </summary>
+        /// <inheritdoc/>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public LottieSwitchExtension() : base()
+        public override bool ProcessIcon(Button button, ref ImageView icon)
         {
-            LottieView = new LottieAnimationView();
-        }
+            if (button.Style is ILottieButtonStyle lottieStyle)
+            {
+                var lottieView = LottieExtensionHelper.CreateLottieView(lottieStyle);
+                var parent = icon.GetParent();
 
-        /// <summary>
-        /// The Lottie view that will be used as an icon part in a Button.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        protected LottieAnimationView LottieView { get; set; }
+                icon.Unparent();
+                icon.Dispose();
+                icon = lottieView;
+                parent?.Add(icon);
 
-        /// <inheritdoc/>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public override ImageView OnCreateIcon(Button button, ImageView icon)
-        {
-            LottieButtonExtension.InitializeLottieView(button, LottieView);
+                return true;
+            }
 
-            return LottieView;
+            return false;
         }
 
         /// <inheritdoc/>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnControlStateChanged(Button button, View.ControlStateChangedEventArgs args)
         {
-            LottieButtonExtension.UpdateLottieView(button, args.PreviousState, LottieView);
+            if (button.Style is ILottieButtonStyle lottieStyle && button.Icon is LottieAnimationView lottieView)
+            {
+                LottieExtensionHelper.UpdateLottieView(lottieView, lottieStyle, args.PreviousState, button.ControlState);
+            }
         }
     }
 }
index b9aff15..52a669f 100755 (executable)
@@ -30,31 +30,19 @@ namespace Tizen.NUI.Components.Extension
         }
 
         /// <summary>
-        /// Called immediately after the Switch creates the track part.
+        /// Perform further processing of the switch thumb.
         /// </summary>
-        /// <param name="switchButton">The Switch instance that the extension currently applied to.</param>
-        /// <param name="track">The created Switch's track part.</param>
-        /// <return>The refined switch track.</return>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public virtual ImageView OnCreateTrack(Switch switchButton, ImageView track)
-        {
-            return track;
-        }
-
-        /// <summary>
-        /// Called immediately after the Switch creates the thumb part.
-        /// </summary>
-        /// <param name="switchButton">The Switch instance that the extension currently applied to.</param>
-        /// <param name="thumb">The created Switch's thumb part.</param>
-        /// <return>The refined switch thumb.</return>
+        /// <param name="switchButton">The switch instance that the extension currently applied to.</param>
+        /// <param name="thumb">The reference of the switch thumb.</param>
+        /// <return>True if the given thumb is replaced.</return>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public virtual ImageView OnCreateThumb(Switch switchButton, ImageView thumb)
+        public bool ProcessThumb(Switch switchButton, ref ImageView thumb)
         {
             if (switchButton.IsSelected)
             {
                 OnSelectedChanged(switchButton);
             }
-            return thumb;
+            return false;
         }
 
         /// <summary>
index 301f9d9..5caef32 100755 (executable)
@@ -115,23 +115,18 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void ApplyStyle(ViewStyle viewStyle)
         {
-            styleApplying = true;
+            styleApplying++;
 
             base.ApplyStyle(viewStyle);
 
             if (viewStyle is SwitchStyle switchStyle)
             {
-                if (Extension != null) Extension.OnDispose(this);
-
-                if ((Extension = switchStyle.CreateExtension()) != null && Extension is SwitchExtension extension)
+                if (Extension is SwitchExtension extension)
                 {
-                    Icon.Unparent();
-                    thumb.Unparent();
-                    TextLabel.Unparent();
-                    Icon = extension.OnCreateTrack(this, Icon);
-                    thumb = extension.OnCreateThumb(this, thumb);
-                    Icon.Add(thumb);
-                    LayoutItems();
+                    if (extension.ProcessThumb(this, ref thumb))
+                    {
+                        LayoutItems();
+                    }
 
                     Icon.Relayout -= OnTrackOrThumbRelayout;
                     Icon.Relayout += OnTrackOrThumbRelayout;
@@ -149,14 +144,8 @@ namespace Tizen.NUI.Components
                 {
                     Thumb.ApplyStyle(switchStyle.Thumb);
                 }
-
-                if (switchStyle.Text != null)
-                {
-                    TextLabel.ThemeChangeSensitive = false;
-                    TextLabel.ApplyStyle(switchStyle.Text);
-                }
             }
-            styleApplying = false;
+            styleApplying--;
 
             UpdateState();
         }
index ed5cc94..38c4137 100755 (executable)
@@ -93,11 +93,11 @@ namespace Tizen.NUI.Components
 
         /// <inheritdoc/>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public override ImageView OnCreateOverlayImage(Button button, ImageView overlayImage)
+        public override bool ProcessOverlayImage(Button button, ref ImageView overlayImage)
         {
             overlayImage.Hide();
 
-            return overlayImage;
+            return false;
         }
 
         /// <inheritdoc/>