Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Clip Border content based on StrokeShape property #7335

Merged
merged 5 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion src/Core/src/Platform/iOS/ContentView.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using CoreAnimation;
using CoreGraphics;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Platform;

namespace Microsoft.Maui.Platform
{
public class ContentView : MauiView
{
IBorderStroke? _clip;
CAShapeLayer? _childMaskLayer;
internal event EventHandler? LayoutSubviewsChanged;

public override CGSize SizeThatFits(CGSize size)
Expand All @@ -32,7 +36,12 @@ public override void LayoutSubviews()
CrossPlatformMeasure?.Invoke(bounds.Width, bounds.Height);
CrossPlatformArrange?.Invoke(bounds);

LayoutSubviewsChanged?.Invoke(this, EventArgs.Empty);
if (ChildMaskLayer != null)
ChildMaskLayer.Frame = bounds;

SetClip();

LayoutSubviewsChanged?.Invoke(this, EventArgs.Empty);
}

public override void SetNeedsLayout()
Expand All @@ -43,5 +52,68 @@ public override void SetNeedsLayout()

internal Func<double, double, Size>? CrossPlatformMeasure { get; set; }
internal Func<Rect, Size>? CrossPlatformArrange { get; set; }

internal IBorderStroke? Clip
{
get { return _clip; }
set
{
_clip = value;

SetClip();
}
}

CAShapeLayer? ChildMaskLayer
{
get => _childMaskLayer;
set
{
var layer = GetChildLayer();

if (layer != null && _childMaskLayer != null)
layer.Mask = null;

_childMaskLayer = value;

if (layer != null)
layer.Mask = value;
}
}

CALayer? GetChildLayer()
{
if (Subviews.Length == 0)
return null;

var child = Subviews[0];

if (child.Layer == null || child.Layer.Sublayers == null)
return null;

return child.Layer;
}

void SetClip()
{
if (Subviews.Length == 0)
return;

var mask = ChildMaskLayer;

if (mask == null && Clip == null)
return;

mask ??= ChildMaskLayer = new CAShapeLayer();

var frame = Frame;

var bounds = new RectF(0, 0, (float)frame.Width, (float)frame.Height);

IShape? clipShape = Clip?.Shape;
var path = clipShape?.PathForBounds(bounds);
var nativePath = path?.AsCGPath();
mask.Path = nativePath;
}
}
}
5 changes: 4 additions & 1 deletion src/Core/src/Platform/iOS/StrokeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ internal static void UpdateMauiCALayer(this UIView platformView, IBorderStroke?
mauiCALayer.SetBorderWidth(border?.StrokeThickness ?? 0);
mauiCALayer.SetBorderDash(border?.StrokeDashPattern, border?.StrokeDashOffset ?? 0);
mauiCALayer.SetBorderMiterLimit(border?.StrokeMiterLimit ?? 0);

if (border != null)
{
mauiCALayer.SetBorderLineJoin(border.StrokeLineJoin);
Expand All @@ -144,6 +144,9 @@ internal static void UpdateMauiCALayer(this UIView platformView, IBorderStroke?

mauiCALayer.SetBorderShape(border?.Shape);
}

if (platformView is ContentView contentView)
contentView.Clip = border;
}

internal static void UpdateMauiCALayer(this UIView view)
Expand Down