Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to define when the IconTintBehavior gets applied #2304

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ protected override void OnAttachedTo(View bindable, AndroidView platformView)
base.OnAttachedTo(bindable, platformView);
nativeView = platformView;

ApplyTintColor(nativeView, TintColor);
if (ApplyOn is IconTintColorApplyOn.OnBehaviorAttachedTo)
{
ApplyTintColor(nativeView, TintColor);
}

bindable.Loaded += OnBindableLoaded;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we apply symmetry here and tidy up in Unloaded?

bindable.PropertyChanged += OnElementPropertyChanged;
PropertyChanged += OnTintedImagePropertyChanged;
}
Expand All @@ -33,9 +37,20 @@ protected override void OnDetachedFrom(View bindable, AndroidView platformView)

ClearTintColor(platformView);

bindable.Loaded -= OnBindableLoaded;
bindable.PropertyChanged -= OnElementPropertyChanged;
PropertyChanged -= OnTintedImagePropertyChanged;
}

void OnBindableLoaded(object? sender, EventArgs e)
{
if (ApplyOn is not IconTintColorApplyOn.OnViewLoaded)
{
return;
}

ApplyTintColor(nativeView, TintColor);
}

static void ApplyTintColor(AndroidView? nativeView, Color? tintColor)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ protected override void OnViewPropertyChanged(View sender, PropertyChangedEventA
protected override void OnAttachedTo(View bindable, UIView platformView)
{
base.OnAttachedTo(bindable, platformView);

bindable.Loaded += OnBindableLoaded;

ApplyTintColor(platformView, bindable, TintColor);
if (ApplyOn is IconTintColorApplyOn.OnBehaviorAttachedTo)
{
ApplyTintColor(platformView, bindable, TintColor);
}

PropertyChanged += (s, e) =>
{
Expand All @@ -46,9 +51,27 @@ protected override void OnAttachedTo(View bindable, UIView platformView)
protected override void OnDetachedFrom(View bindable, UIView platformView)
{
base.OnDetachedFrom(bindable, platformView);

bindable.Loaded -= OnBindableLoaded;

ClearTintColor(platformView, bindable);
}

void OnBindableLoaded(object? sender, EventArgs e)
{
if (ApplyOn is not IconTintColorApplyOn.OnViewLoaded)
{
return;
}

if (sender is not View view
|| view.Handler?.PlatformView is not UIView platformView)
{
return;
}

ApplyTintColor(platformView, view, TintColor);
}

static void ClearTintColor(UIView platformView, View element)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
namespace CommunityToolkit.Maui.Behaviors;

/// <summary>
/// When to apply the status bar color and style.
/// </summary>
public enum IconTintColorApplyOn
{
/// <summary>
/// Apply color when the behavior has been attached to the view.
/// </summary>
OnBehaviorAttachedTo,

/// <summary>
/// Apply color when the view has been loaded.
/// </summary>
OnViewLoaded
}

/// <summary>
/// A behavior that allows you to tint an icon with a specified <see cref="Color"/>.
/// </summary>
public partial class IconTintColorBehavior : BasePlatformBehavior<View>
{
/// <summary>
/// <see cref="BindableProperty"/> that manages the ApplyOn property.
/// </summary>
public static readonly BindableProperty ApplyOnProperty =
BindableProperty.Create(nameof(ApplyOn), typeof(IconTintColorApplyOn), typeof(IconTintColorBehavior), IconTintColorApplyOn.OnBehaviorAttachedTo);

/// <summary>
/// Attached Bindable Property for the <see cref="TintColor"/>.
/// </summary>
public static readonly BindableProperty TintColorProperty =
BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(IconTintColorBehavior), default);

/// <summary>
/// When the status bar color should be applied.
/// </summary>
public IconTintColorApplyOn ApplyOn
{
get => (IconTintColorApplyOn)GetValue(ApplyOnProperty);
set => SetValue(ApplyOnProperty, value);
}

/// <summary>
/// Property that represents the <see cref="Color"/> that Icon will be tinted.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ protected override void OnAttachedTo(View bindable, FrameworkElement platformVie
{
base.OnAttachedTo(bindable, platformView);

ApplyTintColor(platformView, bindable, TintColor);
if (ApplyOn is IconTintColorApplyOn.OnBehaviorAttachedTo)
{
ApplyTintColor(platformView, bindable, TintColor);
}

bindable.Loaded += OnBindableLoaded;
bindable.PropertyChanged += OnElementPropertyChanged;
this.PropertyChanged += (s, e) =>
{
Expand All @@ -50,8 +54,25 @@ protected override void OnDetachedFrom(View bindable, FrameworkElement platformV
base.OnDetachedFrom(bindable, platformView);

bindable.PropertyChanged -= OnElementPropertyChanged;
bindable.Loaded -= OnBindableLoaded;
RemoveTintColor(platformView);
}

void OnBindableLoaded(object? sender, EventArgs e)
{
if (ApplyOn is not IconTintColorApplyOn.OnViewLoaded)
{
return;
}

if (sender is not View view
|| view.Handler?.PlatformView is not FrameworkElement platformView)
{
return;
}

ApplyTintColor(platformView, view, TintColor);
}

static bool TryGetButtonImage(WButton button, [NotNullWhen(true)] out WImage? image)
{
Expand Down
Loading