Skip to content

Commit

Permalink
[Windows] Optimize WrapperView (#24281)
Browse files Browse the repository at this point in the history
* Optimize WrapperView

* CS

* Bugfix

* Bugfix (2)

* Remove SuppressFinalize
  • Loading branch information
MartyIX authored Aug 20, 2024
1 parent 87ddfcc commit 639fcb0
Showing 1 changed file with 66 additions and 23 deletions.
89 changes: 66 additions & 23 deletions src/Core/src/Platform/Windows/WrapperView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,17 @@ namespace Microsoft.Maui.Platform
{
public partial class WrapperView : Grid, IDisposable
{
readonly Canvas _shadowCanvas;
Canvas? _shadowCanvas;
SpriteVisual? _shadowVisual;
DropShadow? _dropShadow;
UI.Xaml.Shapes.Rectangle? _shadowHost;
Rectangle? _shadowHost;
WSize _shadowHostSize;
Path? _borderPath;

FrameworkElement? _child;

public WrapperView()
{
_shadowCanvas = new Canvas();
_borderPath = new Path();

Children.Add(_shadowCanvas);
Children.Add(_borderPath);
}

long _visibilityDependencyPropertyCallbackToken;
Expand All @@ -44,15 +39,17 @@ public FrameworkElement? Child
get { return _child; }
internal set
{
if (_child != null)
if (_child is not null)
{
_child.SizeChanged -= OnChildSizeChanged;
_child.UnregisterPropertyChangedCallback(VisibilityProperty, _visibilityDependencyPropertyCallbackToken);
Children.Remove(_child);
}

if (value == null)
if (value is null)
{
return;
}

_child = value;
_child.SizeChanged += OnChildSizeChanged;
Expand All @@ -76,19 +73,25 @@ public void Dispose()

void UpdateClip()
{
if (Child == null)
if (Child is null)
{
return;
}

var clipGeometry = Clip;

if (clipGeometry == null)
if (clipGeometry is null)
{
return;
}

double width = Child.ActualWidth;
double height = Child.ActualHeight;

if (height <= 0 && width <= 0)
{
return;
}

var visual = ElementCompositionPreview.GetElementVisual(Child);
var compositor = visual.Compositor;
Expand Down Expand Up @@ -118,27 +121,41 @@ void DisposeBorder()

void UpdateBorder()
{
if (Border == null)
if (Border is null)
{
return;
}

if (_borderPath is null)
{
_borderPath = new();

int index = _shadowCanvas is not null ? 1 : 0;
Children.Insert(index, _borderPath);
}

IShape? borderShape = Border.Shape;
_borderPath?.UpdateBorderShape(borderShape, ActualWidth, ActualHeight);

_borderPath?.UpdateStroke(Border.Stroke);
_borderPath?.UpdateStrokeThickness(Border.StrokeThickness);
_borderPath?.UpdateStrokeDashPattern(Border.StrokeDashPattern);
_borderPath?.UpdateBorderDashOffset(Border.StrokeDashOffset);
_borderPath?.UpdateStrokeMiterLimit(Border.StrokeMiterLimit);
_borderPath?.UpdateStrokeLineCap(Border.StrokeLineCap);
_borderPath?.UpdateStrokeLineJoin(Border.StrokeLineJoin);
_borderPath.UpdateBorderShape(borderShape, ActualWidth, ActualHeight);

_borderPath.UpdateStroke(Border.Stroke);
_borderPath.UpdateStrokeThickness(Border.StrokeThickness);
_borderPath.UpdateStrokeDashPattern(Border.StrokeDashPattern);
_borderPath.UpdateBorderDashOffset(Border.StrokeDashOffset);
_borderPath.UpdateStrokeMiterLimit(Border.StrokeMiterLimit);
_borderPath.UpdateStrokeLineCap(Border.StrokeLineCap);
_borderPath.UpdateStrokeLineJoin(Border.StrokeLineJoin);
}

partial void ShadowChanged()
{
if (HasShadow)
{
UpdateShadowAsync().FireAndForget(IPlatformApplication.Current?.Services?.CreateLogger(nameof(WrapperView)));
}
else
{
CreateShadowAsync().FireAndForget(IPlatformApplication.Current?.Services?.CreateLogger(nameof(WrapperView)));
}
}

void OnChildSizeChanged(object sender, SizeChangedEventArgs e)
Expand All @@ -153,7 +170,7 @@ void OnChildSizeChanged(object sender, SizeChangedEventArgs e)
void OnChildVisibilityChanged(DependencyObject sender, DependencyProperty dp)
{
// OnChildSizeChanged does not fire for Visibility changes to child
if (sender is FrameworkElement child && _shadowCanvas.Children.Count > 0)
if (sender is FrameworkElement child && _shadowCanvas?.Children.Count > 0)
{
var shadowHost = _shadowCanvas.Children[0];
shadowHost.Visibility = child.Visibility;
Expand All @@ -162,14 +179,20 @@ void OnChildVisibilityChanged(DependencyObject sender, DependencyProperty dp)

void DisposeShadow()
{
if (_shadowCanvas == null)
if (_shadowCanvas is null)
{
return;
}

if (_shadowHost is not null)
{
ElementCompositionPreview.SetElementChildVisual(_shadowHost, null);
}

if (_shadowCanvas.Children.Count > 0)
{
_shadowCanvas.Children.RemoveAt(0);
}

if (_shadowVisual != null)
{
Expand All @@ -187,16 +210,28 @@ void DisposeShadow()
async Task CreateShadowAsync()
{
if (Child == null || Shadow == null || Shadow.Paint == null)
{
return;
}

var visual = ElementCompositionPreview.GetElementVisual(Child);

if (Clip != null && visual.Clip == null)
{
return;
}

double width = _shadowHostSize.Width;
double height = _shadowHostSize.Height;

if (_shadowCanvas is null)
{
_shadowCanvas = new();

// Shadow canvas must be the first child. The order of children (i.e. shadow canvas and border path) matters.
Children.Insert(0, _shadowCanvas);
}

var ttv = Child.TransformToVisual(_shadowCanvas);
global::Windows.Foundation.Point offset = ttv.TransformPoint(new global::Windows.Foundation.Point(0, 0));

Expand Down Expand Up @@ -229,7 +264,9 @@ async Task CreateShadowAsync()
async Task UpdateShadowAsync()
{
if (_dropShadow != null)
{
await SetShadowPropertiesAsync(_dropShadow, Shadow);
}

UpdateShadowSize();
}
Expand All @@ -241,12 +278,16 @@ void UpdateShadowSize()
float width = (float)_shadowHostSize.Width;

if (width <= 0)
{
width = (float)frameworkElement.ActualWidth;
}

float height = (float)_shadowHostSize.Height;

if (height <= 0)
{
height = (float)frameworkElement.ActualHeight;
}

if (_shadowVisual is not null)
{
Expand Down Expand Up @@ -283,7 +324,9 @@ async Task SetShadowPropertiesAsync(DropShadow dropShadow, IShadow? mauiShadow)
dropShadow.Opacity = opacity;

if (shadowColor != null)
{
dropShadow.Color = shadowColor.ToWindowsColor();
}

dropShadow.Offset = new Vector3((float)offset.X, (float)offset.Y, 0);
dropShadow.Mask = await Child.GetAlphaMaskAsync();
Expand Down

0 comments on commit 639fcb0

Please sign in to comment.