From 10dcce7d8e6c8e332d557fd52b29dc97d8d6d50a Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com> Date: Tue, 4 Feb 2025 15:56:19 -0800 Subject: [PATCH] First commit --- .../IconTintColorBehavior.android.cs | 17 +++++++++- .../IconTintColorBehavior.macios.cs | 25 ++++++++++++++- .../IconTintColorBehavior.shared.cs | 31 +++++++++++++++++++ .../IconTintColorBehavior.windows.cs | 23 +++++++++++++- 4 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs index 633b2b05f8..2a6d76645f 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs @@ -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; bindable.PropertyChanged += OnElementPropertyChanged; PropertyChanged += OnTintedImagePropertyChanged; } @@ -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) { diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.macios.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.macios.cs index 295f517791..8baecb6d57 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.macios.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.macios.cs @@ -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) => { @@ -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) { diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.shared.cs index bef0c66c33..10a3da85f1 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.shared.cs @@ -1,16 +1,47 @@ namespace CommunityToolkit.Maui.Behaviors; +/// +/// When to apply the status bar color and style. +/// +public enum IconTintColorApplyOn +{ + /// + /// Apply color when the behavior has been attached to the view. + /// + OnBehaviorAttachedTo, + + /// + /// Apply color when the view has been loaded. + /// + OnViewLoaded +} + /// /// A behavior that allows you to tint an icon with a specified . /// public partial class IconTintColorBehavior : BasePlatformBehavior { + /// + /// that manages the ApplyOn property. + /// + public static readonly BindableProperty ApplyOnProperty = + BindableProperty.Create(nameof(ApplyOn), typeof(IconTintColorApplyOn), typeof(IconTintColorBehavior), IconTintColorApplyOn.OnBehaviorAttachedTo); + /// /// Attached Bindable Property for the . /// public static readonly BindableProperty TintColorProperty = BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(IconTintColorBehavior), default); + /// + /// When the status bar color should be applied. + /// + public IconTintColorApplyOn ApplyOn + { + get => (IconTintColorApplyOn)GetValue(ApplyOnProperty); + set => SetValue(ApplyOnProperty, value); + } + /// /// Property that represents the that Icon will be tinted. /// diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs index d29d27dedb..151364d0c9 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs @@ -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) => { @@ -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) {