Skip to content

Commit

Permalink
Bugfix: Windows IconColorTintEffect HeightRequest (#1271)
Browse files Browse the repository at this point in the history
* Set position by AnchorPoint and use offset for Height/WidthRequest

* Fix typo

* Load image immediately when available

* Update Comment

---------

Co-authored-by: Brandon Minnick <[email protected]>
  • Loading branch information
kperdlich and brminnick authored Jul 5, 2023
1 parent 0c17bc9 commit bb31c87
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,24 @@ void ApplyTintColor(FrameworkElement platformView, View element, Color? color)

void LoadAndApplyImageTintColor(View element, WImage image, Color color)
{
image.ImageOpened += OnImageOpened;
if (image.IsLoaded)
{
ApplyTintColor();
}
else
{
image.ImageOpened += OnImageOpened;
}

void OnImageOpened(object sender, RoutedEventArgs e)
{
image.ImageOpened -= OnImageOpened;

ApplyTintColor();
}

void ApplyTintColor()
{
if (image.ActualSize != Vector2.Zero)
{
ApplyImageTintColor(element, image, color);
Expand Down Expand Up @@ -151,18 +163,20 @@ void ApplyImageTintColor(View element, WImage image, Color color)

var width = (float)image.ActualWidth;
var height = (float)image.ActualHeight;
var anchorPoint = new Vector2((float)element.AnchorX, (float)element.AnchorY);

// Hide possible visible pixels from original image.
// Workaround since the tinted image is added as a child to the current image and it's not possible to hide a parent without hiding its children using Visibility.Collapsed.
image.Width = image.Height = 0;

// Workaround requires offset to re-center tinted image.
var offset = new Vector3(-width * .5f, -height * .5f, 0f);
// Requested size requires additional offset to re-center tinted image.
var requiresAdditionalCenterOffset = element.WidthRequest != -1 || element.HeightRequest != -1;
var offset = requiresAdditionalCenterOffset ? new Vector3(width * anchorPoint.X, height * anchorPoint.Y, 0f) : Vector3.Zero;

ApplyTintCompositionEffect(image, color, width, height, offset, uri);
ApplyTintCompositionEffect(image, color, width, height, offset, anchorPoint, uri);
}

void ApplyTintCompositionEffect(FrameworkElement platformView, Color color, float width, float height, Vector3 offset, Uri surfaceMaskUri)
void ApplyTintCompositionEffect(FrameworkElement platformView, Color color, float width, float height, Vector3 offset, Vector2 anchorPoint, Uri surfaceMaskUri)
{
var compositor = ElementCompositionPreview.GetElementVisual(platformView).Compositor;

Expand All @@ -179,6 +193,7 @@ void ApplyTintCompositionEffect(FrameworkElement platformView, Color color, floa
currentSpriteVisual.Brush = maskBrush;
currentSpriteVisual.Size = new Vector2(width, height);
currentSpriteVisual.Offset = offset;
currentSpriteVisual.AnchorPoint = anchorPoint;

ElementCompositionPreview.SetElementChildVisual(platformView, currentSpriteVisual);
}
Expand Down

0 comments on commit bb31c87

Please sign in to comment.