Skip to content

Commit af82af8

Browse files
bijingtonTheCodeTravelerne0rrmatrix
authored
Make it possible to define when the IconTintBehavior gets applied (#2304)
Co-authored-by: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Co-authored-by: James Crutchley <ne0rmatrix@gmail.com> Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com>
1 parent 4c2ce41 commit af82af8

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ protected override void OnAttachedTo(View bindable, AndroidView platformView)
2020
base.OnAttachedTo(bindable, platformView);
2121
nativeView = platformView;
2222

23-
ApplyTintColor(nativeView, TintColor);
23+
if (ApplyOn is IconTintColorApplyOn.OnBehaviorAttachedTo)
24+
{
25+
ApplyTintColor(nativeView, TintColor);
26+
}
2427

28+
bindable.Loaded += OnBindableLoaded;
2529
bindable.PropertyChanged += OnElementPropertyChanged;
2630
PropertyChanged += OnTintedImagePropertyChanged;
2731
}
@@ -33,9 +37,20 @@ protected override void OnDetachedFrom(View bindable, AndroidView platformView)
3337

3438
ClearTintColor(platformView);
3539

40+
bindable.Loaded -= OnBindableLoaded;
3641
bindable.PropertyChanged -= OnElementPropertyChanged;
3742
PropertyChanged -= OnTintedImagePropertyChanged;
3843
}
44+
45+
void OnBindableLoaded(object? sender, EventArgs e)
46+
{
47+
if (ApplyOn is not IconTintColorApplyOn.OnViewLoaded)
48+
{
49+
return;
50+
}
51+
52+
ApplyTintColor(nativeView, TintColor);
53+
}
3954

4055
static void ApplyTintColor(AndroidView? nativeView, Color? tintColor)
4156
{

src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.macios.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ protected override void OnViewPropertyChanged(View sender, PropertyChangedEventA
3030
protected override void OnAttachedTo(View bindable, UIView platformView)
3131
{
3232
base.OnAttachedTo(bindable, platformView);
33+
34+
bindable.Loaded += OnBindableLoaded;
3335

34-
ApplyTintColor(platformView, bindable, TintColor);
36+
if (ApplyOn is IconTintColorApplyOn.OnBehaviorAttachedTo)
37+
{
38+
ApplyTintColor(platformView, bindable, TintColor);
39+
}
3540

3641
PropertyChanged += (s, e) =>
3742
{
@@ -46,9 +51,27 @@ protected override void OnAttachedTo(View bindable, UIView platformView)
4651
protected override void OnDetachedFrom(View bindable, UIView platformView)
4752
{
4853
base.OnDetachedFrom(bindable, platformView);
54+
55+
bindable.Loaded -= OnBindableLoaded;
4956

5057
ClearTintColor(platformView, bindable);
5158
}
59+
60+
void OnBindableLoaded(object? sender, EventArgs e)
61+
{
62+
if (ApplyOn is not IconTintColorApplyOn.OnViewLoaded)
63+
{
64+
return;
65+
}
66+
67+
if (sender is not View view
68+
|| view.Handler?.PlatformView is not UIView platformView)
69+
{
70+
return;
71+
}
72+
73+
ApplyTintColor(platformView, view, TintColor);
74+
}
5275

5376
static void ClearTintColor(UIView platformView, View element)
5477
{

src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.shared.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
11
namespace CommunityToolkit.Maui.Behaviors;
22

3+
/// <summary>
4+
/// When to apply the status bar color and style.
5+
/// </summary>
6+
public enum IconTintColorApplyOn
7+
{
8+
/// <summary>
9+
/// Apply color when the behavior has been attached to the view.
10+
/// </summary>
11+
OnBehaviorAttachedTo,
12+
13+
/// <summary>
14+
/// Apply color when the view has been loaded.
15+
/// </summary>
16+
OnViewLoaded
17+
}
18+
319
/// <summary>
420
/// A behavior that allows you to tint an icon with a specified <see cref="Color"/>.
521
/// </summary>
622
public partial class IconTintColorBehavior : BasePlatformBehavior<View>
723
{
24+
/// <summary>
25+
/// <see cref="BindableProperty"/> that manages the ApplyOn property.
26+
/// </summary>
27+
public static readonly BindableProperty ApplyOnProperty =
28+
BindableProperty.Create(nameof(ApplyOn), typeof(IconTintColorApplyOn), typeof(IconTintColorBehavior), IconTintColorApplyOn.OnBehaviorAttachedTo);
29+
830
/// <summary>
931
/// Attached Bindable Property for the <see cref="TintColor"/>.
1032
/// </summary>
1133
public static readonly BindableProperty TintColorProperty =
1234
BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(IconTintColorBehavior), default);
1335

36+
/// <summary>
37+
/// When the status bar color should be applied.
38+
/// </summary>
39+
public IconTintColorApplyOn ApplyOn
40+
{
41+
get => (IconTintColorApplyOn)GetValue(ApplyOnProperty);
42+
set => SetValue(ApplyOnProperty, value);
43+
}
44+
1445
/// <summary>
1546
/// Property that represents the <see cref="Color"/> that Icon will be tinted.
1647
/// </summary>

src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ protected override void OnAttachedTo(View bindable, FrameworkElement platformVie
2525
{
2626
base.OnAttachedTo(bindable, platformView);
2727

28-
ApplyTintColor(platformView, bindable, TintColor);
28+
if (ApplyOn is IconTintColorApplyOn.OnBehaviorAttachedTo)
29+
{
30+
ApplyTintColor(platformView, bindable, TintColor);
31+
}
2932

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

5256
bindable.PropertyChanged -= OnElementPropertyChanged;
57+
bindable.Loaded -= OnBindableLoaded;
5358
RemoveTintColor(platformView);
5459
}
60+
61+
void OnBindableLoaded(object? sender, EventArgs e)
62+
{
63+
if (ApplyOn is not IconTintColorApplyOn.OnViewLoaded)
64+
{
65+
return;
66+
}
67+
68+
if (sender is not View view
69+
|| view.Handler?.PlatformView is not FrameworkElement platformView)
70+
{
71+
return;
72+
}
73+
74+
ApplyTintColor(platformView, view, TintColor);
75+
}
5576

5677
static bool TryGetButtonImage(WButton button, [NotNullWhen(true)] out WImage? image)
5778
{

0 commit comments

Comments
 (0)