Skip to content

Commit

Permalink
fix: skia shadows not updating properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-Antoine-Soucy committed Aug 24, 2023
1 parent 9e0763c commit 26b5358
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class ShadowCollection : ObservableCollection<Shadow>
{
public bool HasInnerShadow() => this.Any(s => s.IsInner);

public string ToKey(double width, double height, Windows.UI.Color? contentBackground)
=> string.Create(CultureInfo.InvariantCulture, $"w{width},h{height}") +
public string ToKey(double width, double height, double surfaceWidth, double surfaceHeight, Windows.UI.Color? contentBackground)
=> string.Create(CultureInfo.InvariantCulture, $"w{width},h{height}, sw{surfaceWidth}, sh{surfaceHeight}") +
(contentBackground.HasValue ? $",cb{contentBackground.Value}:" : ":") +
string.Join("/", this.Select(x => x.ToKey()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ public partial class ShadowContainer
{
private static readonly ILogger _logger = typeof(ShadowContainer).Log();

private record ShadowInfos(double Width, double Height, bool IsInner, double BlurRadius, double Spread, double X, double Y, Color color)
private record ShadowInfos(double Width, double Height, double SurfacelWidth, double SurfaceHeight, bool IsInner, double BlurRadius, double Spread, double X, double Y, Color color)
{
public static readonly ShadowInfos Empty = new ShadowInfos(0, 0, false, 0, 0, 0, 0, new Color());
public static readonly ShadowInfos Empty = new ShadowInfos(0, 0, 0, 0, false, 0, 0, 0, 0, new Color());

public static ShadowInfos From(Shadow shadow, double width, double height)
public static ShadowInfos From(Shadow shadow, double width, double height, double surfaceHeight, double surfaceWidth)
{
return new ShadowInfos(
width,
height,
surfaceWidth,
surfaceHeight,
shadow.IsInner,
shadow.BlurRadius,
shadow.Spread,
Expand Down Expand Up @@ -82,10 +84,10 @@ public static SKShadow From(Shadow shadow, float width, float height, float corn
private float _currentPixelRatio;
private Color? _currentContentBackgroundColor;

private bool NeedsPaint(double width, double height, float pixelRatio, out bool pixelRatioChanged)
private bool NeedsPaint(double width, double height, double surfaceHeight, double surfaceWidth, float pixelRatio, out bool pixelRatioChanged)
{
var shadows = Shadows ?? new ShadowCollection();
var newShadowInfos = shadows.Select(s => ShadowInfos.From(s, width, height)).ToArray();
var newShadowInfos = shadows.Select(s => ShadowInfos.From(s, width, height, surfaceHeight, surfaceWidth)).ToArray();

pixelRatioChanged = false;

Expand Down Expand Up @@ -126,7 +128,7 @@ private void OnSurfacePainted(object? sender, SKPaintSurfaceEventArgs e)
double width = _currentContent.ActualWidth;
double height = _currentContent.ActualHeight;

if (!NeedsPaint(width, height, pixelRatio, out bool pixelRatioChanged))
if (!NeedsPaint(width, height, surfaceHeight, surfaceWidth, pixelRatio, out bool pixelRatioChanged))
{
return;
}
Expand Down Expand Up @@ -160,7 +162,7 @@ private void OnSurfacePainted(object? sender, SKPaintSurfaceEventArgs e)
_currentContentBackgroundColor = null;
}

string shadowsKey = shadows.ToKey(width, height, _currentContentBackgroundColor);
string shadowsKey = shadows.ToKey(width, height, surfaceWidth, surfaceHeight, _currentContentBackgroundColor);
if (Cache.TryGetValue(shadowsKey, out var shadowsImage))
{
if (pixelRatioChanged)
Expand Down

0 comments on commit 26b5358

Please sign in to comment.