From 022decb830d726c0de9a57fab4eb8f977aab2e25 Mon Sep 17 00:00:00 2001 From: Jiyun Yang Date: Fri, 17 Mar 2023 13:53:15 +0900 Subject: [PATCH] [NUI] Refactor ButtonExtension * 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 --- .../Controls/Button.Internal.cs | 4 +- src/Tizen.NUI.Components/Controls/Button.cs | 30 ++++++------ .../Controls/Extension/ButtonExtension.cs | 36 +++++++------- .../Controls/Extension/LottieButtonExtension.cs | 57 +++++++++++----------- .../Controls/Extension/LottieSwitchExtension.cs | 35 +++++++------ .../Controls/Extension/SwitchExtension.cs | 24 +++------ src/Tizen.NUI.Components/Controls/Switch.cs | 25 +++------- .../PreloadStyle/OverlayAnimationButtonStyle.cs | 4 +- 8 files changed, 96 insertions(+), 119 deletions(-) diff --git a/src/Tizen.NUI.Components/Controls/Button.Internal.cs b/src/Tizen.NUI.Components/Controls/Button.Internal.cs index 2b84723..c7369f5 100644 --- a/src/Tizen.NUI.Components/Controls/Button.Internal.cs +++ b/src/Tizen.NUI.Components/Controls/Button.Internal.cs @@ -34,7 +34,7 @@ namespace Tizen.NUI.Components private EventHandler stateChangeHandler; private bool isPressed = false; - internal bool styleApplying = false; + internal int styleApplying = 0; /// /// 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; diff --git a/src/Tizen.NUI.Components/Controls/Button.cs b/src/Tizen.NUI.Components/Controls/Button.cs index bda6455..6601b2b 100755 --- a/src/Tizen.NUI.Components/Controls/Button.cs +++ b/src/Tizen.NUI.Components/Controls/Button.cs @@ -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(); diff --git a/src/Tizen.NUI.Components/Controls/Extension/ButtonExtension.cs b/src/Tizen.NUI.Components/Controls/Extension/ButtonExtension.cs index b18c06b..cbeabf8 100755 --- a/src/Tizen.NUI.Components/Controls/Extension/ButtonExtension.cs +++ b/src/Tizen.NUI.Components/Controls/Extension/ButtonExtension.cs @@ -32,39 +32,39 @@ namespace Tizen.NUI.Components.Extension protected Touch TouchInfo { get; private set; } /// - /// Called immediately after the Button creates the text part. + /// Perform further processing of the button text. /// - /// The Button instance that the extension currently applied to. - /// The created Button's text part. - /// The refined button text. + /// The button instance that the extension currently applied to. + /// The reference of the button text. + /// True if the given text is replaced. [EditorBrowsable(EditorBrowsableState.Never)] - public virtual TextLabel OnCreateText(Button button, TextLabel text) + public virtual bool ProcessText(Button button, ref TextLabel text) { - return text; + return false; } /// - /// Called immediately after the Button creates the icon part. + /// Perform further processing of the button icon. /// - /// The Button instance that the extension currently applied to. - /// The created Button's icon part. - /// The refined button icon. + /// The button instance that the extension currently applied to. + /// The reference of the button icon. + /// True if the given icon is replaced. [EditorBrowsable(EditorBrowsableState.Never)] - public virtual ImageView OnCreateIcon(Button button, ImageView icon) + public virtual bool ProcessIcon(Button button, ref ImageView icon) { - return icon; + return false; } /// - /// Called immediately after the Button creates the overlay image part. + /// Perform further processing of the button overlay image. /// - /// The Button instance that the extension currently applied to. - /// The created Button's overlayImage part. - /// The refined button overlayImage. + /// The button instance that the extension currently applied to. + /// The reference of the button overlay image. + /// True if the given overlayImage is replaced. [EditorBrowsable(EditorBrowsableState.Never)] - public virtual ImageView OnCreateOverlayImage(Button button, ImageView overlayImage) + public virtual bool ProcessOverlayImage(Button button, ref ImageView overlayImage) { - return overlayImage; + return false; } /// diff --git a/src/Tizen.NUI.Components/Controls/Extension/LottieButtonExtension.cs b/src/Tizen.NUI.Components/Controls/Extension/LottieButtonExtension.cs index d173d26..67a499b 100755 --- a/src/Tizen.NUI.Components/Controls/Extension/LottieButtonExtension.cs +++ b/src/Tizen.NUI.Components/Controls/Extension/LottieButtonExtension.cs @@ -29,57 +29,56 @@ namespace Tizen.NUI.Components.Extension [EditorBrowsable(EditorBrowsableState.Never)] public class LottieButtonExtension : ButtonExtension { - /// - /// A constructor that creates LottieButtonExtension with a specified Lottie resource URL - /// + /// [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(); - /// - /// The Lottie view that will be used as an icon part in a Button. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - protected LottieAnimationView LottieView { get; set; } + icon.Unparent(); + icon.Dispose(); + icon = lottieView; + parent?.Add(icon); - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override ImageView OnCreateIcon(Button button, ImageView icon) - { - InitializeLottieView(button, LottieView); + return true; + } - return LottieView; + return false; } /// [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)); } diff --git a/src/Tizen.NUI.Components/Controls/Extension/LottieSwitchExtension.cs b/src/Tizen.NUI.Components/Controls/Extension/LottieSwitchExtension.cs index 246bde2..569e332 100755 --- a/src/Tizen.NUI.Components/Controls/Extension/LottieSwitchExtension.cs +++ b/src/Tizen.NUI.Components/Controls/Extension/LottieSwitchExtension.cs @@ -28,35 +28,34 @@ namespace Tizen.NUI.Components.Extension [EditorBrowsable(EditorBrowsableState.Never)] public class LottieSwitchExtension : SwitchExtension { - /// - /// A constructor that creates LottieButtonExtension with a specified Lottie resource URL - /// + /// [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(); - /// - /// The Lottie view that will be used as an icon part in a Button. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - protected LottieAnimationView LottieView { get; set; } + icon.Unparent(); + icon.Dispose(); + icon = lottieView; + parent?.Add(icon); - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override ImageView OnCreateIcon(Button button, ImageView icon) - { - LottieButtonExtension.InitializeLottieView(button, LottieView); + return true; + } - return LottieView; + return false; } /// [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); + } } } } diff --git a/src/Tizen.NUI.Components/Controls/Extension/SwitchExtension.cs b/src/Tizen.NUI.Components/Controls/Extension/SwitchExtension.cs index b9aff15..52a669f 100755 --- a/src/Tizen.NUI.Components/Controls/Extension/SwitchExtension.cs +++ b/src/Tizen.NUI.Components/Controls/Extension/SwitchExtension.cs @@ -30,31 +30,19 @@ namespace Tizen.NUI.Components.Extension } /// - /// Called immediately after the Switch creates the track part. + /// Perform further processing of the switch thumb. /// - /// The Switch instance that the extension currently applied to. - /// The created Switch's track part. - /// The refined switch track. - [EditorBrowsable(EditorBrowsableState.Never)] - public virtual ImageView OnCreateTrack(Switch switchButton, ImageView track) - { - return track; - } - - /// - /// Called immediately after the Switch creates the thumb part. - /// - /// The Switch instance that the extension currently applied to. - /// The created Switch's thumb part. - /// The refined switch thumb. + /// The switch instance that the extension currently applied to. + /// The reference of the switch thumb. + /// True if the given thumb is replaced. [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; } /// diff --git a/src/Tizen.NUI.Components/Controls/Switch.cs b/src/Tizen.NUI.Components/Controls/Switch.cs index 301f9d9..5caef32 100755 --- a/src/Tizen.NUI.Components/Controls/Switch.cs +++ b/src/Tizen.NUI.Components/Controls/Switch.cs @@ -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(); } diff --git a/src/Tizen.NUI.Components/PreloadStyle/OverlayAnimationButtonStyle.cs b/src/Tizen.NUI.Components/PreloadStyle/OverlayAnimationButtonStyle.cs index ed5cc94..38c4137 100755 --- a/src/Tizen.NUI.Components/PreloadStyle/OverlayAnimationButtonStyle.cs +++ b/src/Tizen.NUI.Components/PreloadStyle/OverlayAnimationButtonStyle.cs @@ -93,11 +93,11 @@ namespace Tizen.NUI.Components /// [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; } /// -- 2.7.4