Skip to content

Commit

Permalink
chore(ShadowContainer): refactored shadows size disposable after review
Browse files Browse the repository at this point in the history
  • Loading branch information
roubachof committed Sep 25, 2023
1 parent 83e5693 commit b359de7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
<StackPanel Spacing="8">
<TextBlock Text="Complex Shadow Shapes" Style="{StaticResource TitleTextBlockStyle}" />

<!--
<!--
Should be the same as: https://developer.mozilla.org/fr/docs/Web/CSS/CSS_backgrounds_and_borders/Box-shadow_generator
element {
width: 300px; height: 100px; background-color: #7A67F8; position: relative;
Expand All @@ -323,26 +323,26 @@
<utu:ShadowContainer Margin="0,30" Background="{StaticResource UnoColor}">
<utu:ShadowContainer.Shadows>
<utu:ShadowCollection>
<utu:Shadow BlurRadius="10"
IsInner="True"
<utu:Shadow IsInner="True"
OffsetX="-20"
OffsetY="-10"
Opacity="1"
BlurRadius="10"
Spread="10"
Color="Black" />
<utu:Shadow BlurRadius="5"
IsInner="True"
Color="Black"
Opacity="1" />
<utu:Shadow IsInner="True"
OffsetX="5"
OffsetY="-5"
Opacity="1"
BlurRadius="5"
Spread="0"
Color="#94DB6D" />
<utu:Shadow BlurRadius="5"
OffsetX="6"
Color="#94DB6D"
Opacity="1" />
<utu:Shadow OffsetX="6"
OffsetY="-5"
Opacity="1"
BlurRadius="5"
Spread="0"
Color="#E88BAB" />
Color="#E88BAB"
Opacity="1" />
</utu:ShadowCollection>
</utu:ShadowContainer.Shadows>
<Button Width="300"
Expand Down
45 changes: 25 additions & 20 deletions src/Uno.Toolkit.Skia.WinUI/Controls/Shadows/ShadowContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public partial class ShadowContainer : ContentControl

private readonly SerialDisposable _eventSubscriptions = new();

private readonly SerialDisposable _shadowHostSubscriptions = new();

private Grid? _panel;
private Canvas? _canvas;

Expand Down Expand Up @@ -77,6 +75,8 @@ private void BindToPaintingProperties()
this.RegisterDisposablePropertyChangedCallback(ShadowsProperty, OnShadowsChanged),
this.RegisterDisposablePropertyChangedCallback(ContentProperty, OnContentChanged),

RegisterDisposableShadowHostSizeChangedCallback(OnShadowHostSizeChanged),

backgroundNestedDisposable,
shadowsNestedDisposable,
contentNestedDisposable,
Expand All @@ -90,7 +90,7 @@ private void BindToPaintingProperties()
// This method should not fire any of InvalidateXyz-methods directly,
// in order to avoid duplicated invalidate calls.
// Which is why the BindToXyz has been separated from the OnXyzChanged.

void OnBackgroundChanged(DependencyObject sender, DependencyProperty dp)
{
BindToBackgroundMemberProperties(Background);
Expand All @@ -110,6 +110,11 @@ void OnContentChanged(DependencyObject sender, DependencyProperty dp)

InvalidateShadows();
}
// When the skia canvas size changes, the whole canvas is cleared: we'll need to redraw the shadows.
void OnShadowHostSizeChanged(object sender, SizeChangedEventArgs args)
{
_isShadowHostDirty = true;
}

void OnShadowPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
Expand All @@ -121,6 +126,7 @@ void OnShadowPropertyChanged(object? sender, PropertyChangedEventArgs e)
}
InvalidateShadows();
}

void OnInnerPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
InvalidateShadows();
Expand All @@ -141,6 +147,21 @@ void OnContentSizeChanged(object sender, SizeChangedEventArgs e)
InvalidateShadows();
}

SerialDisposable RegisterDisposableShadowHostSizeChangedCallback(SizeChangedEventHandler sizeChanged)
{
if (_shadowHost == null)
{
return new SerialDisposable();
}

_shadowHost.SizeChanged += OnShadowHostSizeChanged;
return new SerialDisposable
{
Disposable = Disposable.Create(() => _shadowHost.SizeChanged -= sizeChanged),
};

}

void BindToBackgroundMemberProperties(Brush? background)
{
backgroundNestedDisposable.Disposable = background switch
Expand Down Expand Up @@ -257,25 +278,9 @@ protected override void OnApplyTemplate()

_shadowHost = skiaCanvas;
_canvas?.Children.Insert(0, _shadowHost!);

BindToShadowHost(_shadowHost);
}

private void BindToShadowHost(SKXamlCanvas skiaCanvas)
{
_shadowHostSubscriptions.Disposable?.Dispose();
skiaCanvas.SizeChanged += OnShadowHostSizeChanged;
_shadowHostSubscriptions.Disposable = new CompositeDisposable
{
Disposable.Create(() => skiaCanvas.SizeChanged -= OnShadowHostSizeChanged),
};

// When the skia canvas size changes, the whole canvas is cleared: we'll need to redraw the shadows.
void OnShadowHostSizeChanged(object sender, SizeChangedEventArgs args)
{
_isShadowHostDirty = true;
}
}


private void InvalidateCanvasLayout()
{
Expand Down

0 comments on commit b359de7

Please sign in to comment.