Skip to content

Commit bb31c87

Browse files
Bugfix: Windows IconColorTintEffect HeightRequest (#1271)
* Set position by AnchorPoint and use offset for Height/WidthRequest * Fix typo * Load image immediately when available * Update Comment --------- Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
1 parent 0c17bc9 commit bb31c87

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,24 @@ void ApplyTintColor(FrameworkElement platformView, View element, Color? color)
118118

119119
void LoadAndApplyImageTintColor(View element, WImage image, Color color)
120120
{
121-
image.ImageOpened += OnImageOpened;
121+
if (image.IsLoaded)
122+
{
123+
ApplyTintColor();
124+
}
125+
else
126+
{
127+
image.ImageOpened += OnImageOpened;
128+
}
122129

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

134+
ApplyTintColor();
135+
}
136+
137+
void ApplyTintColor()
138+
{
127139
if (image.ActualSize != Vector2.Zero)
128140
{
129141
ApplyImageTintColor(element, image, color);
@@ -151,18 +163,20 @@ void ApplyImageTintColor(View element, WImage image, Color color)
151163

152164
var width = (float)image.ActualWidth;
153165
var height = (float)image.ActualHeight;
166+
var anchorPoint = new Vector2((float)element.AnchorX, (float)element.AnchorY);
154167

155168
// Hide possible visible pixels from original image.
156169
// 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.
157170
image.Width = image.Height = 0;
158171

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

162-
ApplyTintCompositionEffect(image, color, width, height, offset, uri);
176+
ApplyTintCompositionEffect(image, color, width, height, offset, anchorPoint, uri);
163177
}
164178

165-
void ApplyTintCompositionEffect(FrameworkElement platformView, Color color, float width, float height, Vector3 offset, Uri surfaceMaskUri)
179+
void ApplyTintCompositionEffect(FrameworkElement platformView, Color color, float width, float height, Vector3 offset, Vector2 anchorPoint, Uri surfaceMaskUri)
166180
{
167181
var compositor = ElementCompositionPreview.GetElementVisual(platformView).Compositor;
168182

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

183198
ElementCompositionPreview.SetElementChildVisual(platformView, currentSpriteVisual);
184199
}

0 commit comments

Comments
 (0)