* 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>
private EventHandler<StateChangedEventArgs> stateChangeHandler;
private bool isPressed = false;
- internal bool styleApplying = false;
+ internal int styleApplying = 0;
/// <summary>
/// Gets accessibility name.
[EditorBrowsable(EditorBrowsableState.Never)]
protected void UpdateState()
{
- if (styleApplying) return;
+ if (styleApplying > 0) return;
ControlState sourceState = ControlState;
ControlState targetState;
overlayImage = CreateOverlayImage();
if (null != Extension)
{
- overlayImage = Extension.OnCreateOverlayImage(this, overlayImage);
+ Extension.ProcessOverlayImage(this, ref overlayImage);
}
if (null != overlayImage)
{
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)
{
buttonIcon.ApplyStyle(buttonStyle.Icon);
}
- styleApplying = false;
+
+ styleApplying--;
}
UpdateState();
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>
[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));
}
[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);
+ }
}
}
}
}
/// <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>
[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;
{
Thumb.ApplyStyle(switchStyle.Thumb);
}
-
- if (switchStyle.Text != null)
- {
- TextLabel.ThemeChangeSensitive = false;
- TextLabel.ApplyStyle(switchStyle.Text);
- }
}
- styleApplying = false;
+ styleApplying--;
UpdateState();
}
/// <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/>