From 1ef6a7d904761cbd80b1b3816da948a55ee23064 Mon Sep 17 00:00:00 2001 From: Megan Yi Date: Wed, 31 Jul 2024 08:41:24 -0400 Subject: [PATCH 1/7] Set image source if not matching element source, allow tint to apply if image loaded --- .../IconTintColor/IconTintColorBehavior.windows.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs index 6e0bc6d35b..5d2196b748 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs @@ -124,6 +124,12 @@ void ApplyTintColor(FrameworkElement platformView, View element, Color? color) void LoadAndApplyImageTintColor(View element, WImage image, Color color) { + // Sometimes image source is not correct, in that case get the correct source from element + if (element is IImageElement { Source: FileImageSource fileImageSource }) + { + image.Source = new BitmapImage(new Uri($"ms-appx:///{fileImageSource.File}")); + } + if (element is IImageElement { Source: UriImageSource uriImageSource }) { image.Source = Path.GetExtension(uriImageSource.Uri.AbsolutePath) switch @@ -134,6 +140,10 @@ void LoadAndApplyImageTintColor(View element, WImage image, Color color) ApplyTintColor(); } + else if (image.IsLoaded) + { + ApplyTintColor(); + } else { image.ImageOpened += OnImageOpened; From 586f28d7d627a3d3ce30b5a8c40ccf1b34d95e76 Mon Sep 17 00:00:00 2001 From: Megan Yi Date: Wed, 31 Jul 2024 09:12:37 -0400 Subject: [PATCH 2/7] Remove check for image loaded which caused issue with ToggleImageSource button --- .../IconTintColor/IconTintColorBehavior.windows.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs index 5d2196b748..edc2de68f8 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs @@ -140,10 +140,6 @@ void LoadAndApplyImageTintColor(View element, WImage image, Color color) ApplyTintColor(); } - else if (image.IsLoaded) - { - ApplyTintColor(); - } else { image.ImageOpened += OnImageOpened; From 5857a4efbbe741d5d9438bb64775fea553cd2844 Mon Sep 17 00:00:00 2001 From: Megan Yi Date: Wed, 31 Jul 2024 10:46:15 -0400 Subject: [PATCH 3/7] Add check back for IsLoaded, edit check for if image is not ready to be tinted --- .../IconTintColorBehavior.windows.cs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs index edc2de68f8..c55035f719 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs @@ -55,7 +55,13 @@ protected override void OnDetachedFrom(View bindable, FrameworkElement platformV static bool TryGetButtonImage(WButton button, [NotNullWhen(true)] out WImage? image) { - image = button.Content as WImage; + image = button.Content switch + { + WImage windowsImage => windowsImage, + Microsoft.UI.Xaml.Controls.Panel panel => panel.Children?.OfType().FirstOrDefault(), + _ => null + }; + return image is not null; } @@ -124,13 +130,14 @@ void ApplyTintColor(FrameworkElement platformView, View element, Color? color) void LoadAndApplyImageTintColor(View element, WImage image, Color color) { - // Sometimes image source is not correct, in that case get the correct source from element - if (element is IImageElement { Source: FileImageSource fileImageSource }) + // In toggle source button, WImage source is not initially correct, + if (element is IImageElement { Source: FileImageSource fileImageSource } && + image.Source is BitmapImage bitmapImage && + Uri.Compare(new Uri($"{bitmapImage.UriSource.Scheme}:///{fileImageSource.File}"), bitmapImage.UriSource, UriComponents.Path, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) != 0) { - image.Source = new BitmapImage(new Uri($"ms-appx:///{fileImageSource.File}")); + image.ImageOpened += OnImageOpened; } - - if (element is IImageElement { Source: UriImageSource uriImageSource }) + else if (element is IImageElement { Source: UriImageSource uriImageSource }) { image.Source = Path.GetExtension(uriImageSource.Uri.AbsolutePath) switch { @@ -140,6 +147,10 @@ void LoadAndApplyImageTintColor(View element, WImage image, Color color) ApplyTintColor(); } + else if (image.IsLoaded) + { + ApplyTintColor(); + } else { image.ImageOpened += OnImageOpened; From 25b934d378cdeef66c34aadeeec36841d40c931e Mon Sep 17 00:00:00 2001 From: Megan Yi Date: Wed, 31 Jul 2024 10:47:40 -0400 Subject: [PATCH 4/7] Reset copying over TryGetButtonImage change --- .../IconTintColor/IconTintColorBehavior.windows.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs index c55035f719..4e9cebd9fd 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs @@ -55,13 +55,7 @@ protected override void OnDetachedFrom(View bindable, FrameworkElement platformV static bool TryGetButtonImage(WButton button, [NotNullWhen(true)] out WImage? image) { - image = button.Content switch - { - WImage windowsImage => windowsImage, - Microsoft.UI.Xaml.Controls.Panel panel => panel.Children?.OfType().FirstOrDefault(), - _ => null - }; - + image = button.Content as WImage; return image is not null; } @@ -130,7 +124,7 @@ void ApplyTintColor(FrameworkElement platformView, View element, Color? color) void LoadAndApplyImageTintColor(View element, WImage image, Color color) { - // In toggle source button, WImage source is not initially correct, + // Sometimes WImage source doesn't match View source so the image is not ready to be tinted, have to wait for next ImageOpened event if (element is IImageElement { Source: FileImageSource fileImageSource } && image.Source is BitmapImage bitmapImage && Uri.Compare(new Uri($"{bitmapImage.UriSource.Scheme}:///{fileImageSource.File}"), bitmapImage.UriSource, UriComponents.Path, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) != 0) From 934880c41ff32541a84dda8902746c96dfee5fea Mon Sep 17 00:00:00 2001 From: Megan Yi Date: Thu, 1 Aug 2024 11:17:18 -0400 Subject: [PATCH 5/7] Changed location of matching source check --- .../IconTintColorBehavior.windows.cs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs index 9d638750ca..f14d473769 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs @@ -130,14 +130,7 @@ void ApplyTintColor(FrameworkElement platformView, View element, Color? color) void LoadAndApplyImageTintColor(View element, WImage image, Color color) { - // Sometimes WImage source doesn't match View source so the image is not ready to be tinted, have to wait for next ImageOpened event - if (element is IImageElement { Source: FileImageSource fileImageSource } && - image.Source is BitmapImage bitmapImage && - Uri.Compare(new Uri($"{bitmapImage.UriSource.Scheme}:///{fileImageSource.File}"), bitmapImage.UriSource, UriComponents.Path, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) != 0) - { - image.ImageOpened += OnImageOpened; - } - else if (element is IImageElement { Source: UriImageSource uriImageSource }) + if (element is IImageElement { Source: UriImageSource uriImageSource }) { image.Source = Path.GetExtension(uriImageSource.Uri.AbsolutePath) switch { @@ -149,7 +142,17 @@ image.Source is BitmapImage bitmapImage && } else if (image.IsLoaded) { - ApplyTintColor(); + // Sometimes WImage source doesn't match View source so the image is not ready to be tinted, have to wait for next ImageOpened event + if (element is IImageElement { Source: FileImageSource fileImageSource } && + image.Source is BitmapImage bitmapImage && + Uri.Compare(new Uri($"{bitmapImage.UriSource.Scheme}:///{fileImageSource.File}"), bitmapImage.UriSource, UriComponents.Path, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) != 0) + { + image.ImageOpened += OnImageOpened; + } + else + { + ApplyTintColor(); + } } else { From 868faab9e7d1fff27b06ecd713697aa2a3e5a1d5 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Mon, 23 Sep 2024 04:28:06 -0700 Subject: [PATCH 6/7] Add `OnIconTintColorBehaviorPropertyChanged` --- .../IconTintColorBehavior.windows.cs | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs index f14d473769..f92f8fc2b7 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs @@ -27,21 +27,8 @@ protected override void OnAttachedTo(View bindable, FrameworkElement platformVie ApplyTintColor(platformView, bindable, TintColor); + PropertyChanged += OnIconTintColorBehaviorPropertyChanged; bindable.PropertyChanged += OnElementPropertyChanged; - this.PropertyChanged += (s, e) => - { - if (e.PropertyName == TintColorProperty.PropertyName) - { - if (currentColorBrush is not null && TintColor is not null) - { - currentColorBrush.Color = TintColor.ToWindowsColor(); - } - else - { - ApplyTintColor(platformView, bindable, TintColor); - } - } - }; } /// @@ -50,6 +37,8 @@ protected override void OnDetachedFrom(View bindable, FrameworkElement platformV base.OnDetachedFrom(bindable, platformView); bindable.PropertyChanged -= OnElementPropertyChanged; + PropertyChanged -= OnIconTintColorBehaviorPropertyChanged; + RemoveTintColor(platformView); } @@ -83,6 +72,30 @@ static bool TryGetSourceImageUri(WImage? imageControl, IImageElement? imageEleme return false; } + void OnIconTintColorBehaviorPropertyChanged(object? sender, PropertyChangedEventArgs e) + { + ArgumentNullException.ThrowIfNull(sender); + var iconTintColorBehavior = (IconTintColorBehavior)sender; + + if(iconTintColorBehavior.View is not IView bindable + || bindable.Handler?.PlatformView is not FrameworkElement platformView) + { + return; + } + + if (e.PropertyName == TintColorProperty.PropertyName) + { + if (currentColorBrush is not null && TintColor is not null) + { + currentColorBrush.Color = TintColor.ToWindowsColor(); + } + else + { + ApplyTintColor(platformView, bindable, TintColor); + } + } + } + void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName is not string propertyName @@ -99,7 +112,7 @@ void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs e) } } - void ApplyTintColor(FrameworkElement platformView, View element, Color? color) + void ApplyTintColor(FrameworkElement platformView, IView element, Color? color) { RemoveTintColor(platformView); @@ -128,7 +141,7 @@ void ApplyTintColor(FrameworkElement platformView, View element, Color? color) } } - void LoadAndApplyImageTintColor(View element, WImage image, Color color) + void LoadAndApplyImageTintColor(IView element, WImage image, Color color) { if (element is IImageElement { Source: UriImageSource uriImageSource }) { @@ -192,7 +205,7 @@ void OnImageSizeChanged(object sender, SizeChangedEventArgs e) } } - void ApplyImageTintColor(View element, WImage image, Color color) + void ApplyImageTintColor(IView element, WImage image, Color color) { if (!TryGetSourceImageUri(image, (IImageElement)element, out var uri)) { From 12fa1ae6c74be0749d91e0efa8f0d1f2f3b2e3d3 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Mon, 23 Sep 2024 04:44:37 -0700 Subject: [PATCH 7/7] Update IconTintColorBehavior.windows.cs --- .../IconTintColorBehavior.windows.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs index f92f8fc2b7..2fedd1b41e 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs @@ -38,7 +38,7 @@ protected override void OnDetachedFrom(View bindable, FrameworkElement platformV bindable.PropertyChanged -= OnElementPropertyChanged; PropertyChanged -= OnIconTintColorBehaviorPropertyChanged; - + RemoveTintColor(platformView); } @@ -77,7 +77,7 @@ void OnIconTintColorBehaviorPropertyChanged(object? sender, PropertyChangedEvent ArgumentNullException.ThrowIfNull(sender); var iconTintColorBehavior = (IconTintColorBehavior)sender; - if(iconTintColorBehavior.View is not IView bindable + if (iconTintColorBehavior.View is not IView bindable || bindable.Handler?.PlatformView is not FrameworkElement platformView) { return; @@ -99,7 +99,7 @@ void OnIconTintColorBehaviorPropertyChanged(object? sender, PropertyChangedEvent void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName is not string propertyName - || sender is not View bindable + || sender is not IView bindable || bindable.Handler?.PlatformView is not FrameworkElement platformView) { return; @@ -155,10 +155,11 @@ void LoadAndApplyImageTintColor(IView element, WImage image, Color color) } else if (image.IsLoaded) { - // Sometimes WImage source doesn't match View source so the image is not ready to be tinted, have to wait for next ImageOpened event - if (element is IImageElement { Source: FileImageSource fileImageSource } && - image.Source is BitmapImage bitmapImage && - Uri.Compare(new Uri($"{bitmapImage.UriSource.Scheme}:///{fileImageSource.File}"), bitmapImage.UriSource, UriComponents.Path, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) != 0) + // Sometimes WImage source doesn't match View source so the image is not ready to be tinted + // We must wait for next ImageOpened event + if (element is IImageElement { Source: FileImageSource fileImageSource } + && image.Source is BitmapImage bitmapImage + && Uri.Compare(new Uri($"{bitmapImage.UriSource.Scheme}:///{fileImageSource.File}"), bitmapImage.UriSource, UriComponents.Path, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) is not 0) { image.ImageOpened += OnImageOpened; }