-
Notifications
You must be signed in to change notification settings - Fork 402
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
Fix issue with tint not applying to loaded images #2077
Open
myix765
wants to merge
10
commits into
CommunityToolkit:main
Choose a base branch
from
myix765:tint-navigate-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1ef6a7d
Set image source if not matching element source, allow tint to apply …
myix765 586f28d
Remove check for image loaded which caused issue with ToggleImageSour…
myix765 5857a4e
Add check back for IsLoaded, edit check for if image is not ready to …
myix765 25b934d
Reset copying over TryGetButtonImage change
myix765 70dc76d
Merge branch 'main' into tint-navigate-bug
myix765 934880c
Changed location of matching source check
myix765 b25318f
Merge branch 'main' into tint-navigate-bug
myix765 45570cf
Merge branch 'main' into tint-navigate-bug
bijington 868faab
Add `OnIconTintColorBehaviorPropertyChanged`
brminnick 12fa1ae
Update IconTintColorBehavior.windows.cs
brminnick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// <inheritdoc/> | ||
|
@@ -50,6 +37,8 @@ protected override void OnDetachedFrom(View bindable, FrameworkElement platformV | |
base.OnDetachedFrom(bindable, platformView); | ||
|
||
bindable.PropertyChanged -= OnElementPropertyChanged; | ||
PropertyChanged -= OnIconTintColorBehaviorPropertyChanged; | ||
|
||
RemoveTintColor(platformView); | ||
} | ||
|
||
|
@@ -83,10 +72,34 @@ 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 | ||
|| sender is not View bindable | ||
|| sender is not IView bindable | ||
|| bindable.Handler?.PlatformView is not FrameworkElement platformView) | ||
{ | ||
return; | ||
|
@@ -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 }) | ||
{ | ||
|
@@ -140,6 +153,21 @@ void LoadAndApplyImageTintColor(View element, WImage image, Color color) | |
|
||
ApplyTintColor(); | ||
} | ||
else if (image.IsLoaded) | ||
{ | ||
// 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; | ||
} | ||
else | ||
{ | ||
ApplyTintColor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please update the else
{
ApplyTimeColor();
} I cannot reach this branch using the Sample App and am unable to test it and verify it works. |
||
} | ||
} | ||
else | ||
{ | ||
image.ImageOpened += OnImageOpened; | ||
|
@@ -178,7 +206,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)) | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please update the
IconTintColorBehaviorPage
in the sample app to include a use-case that hits this specific line of code where this specificif
statement resolves totrue
?I cannot reach this branch using the Sample App and am unable to test it and verify it works.