Skip to content

Commit

Permalink
Merge pull request #12 from emako/master
Browse files Browse the repository at this point in the history
feat: support ClipToBounds for Border
  • Loading branch information
SlimeNull authored Aug 29, 2024
2 parents 0a96a96 + 8fcdcfe commit 43f2082
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
<ProjectReference Include="..\EleCho.WpfSuite\EleCho.WpfSuite.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controls\" />
</ItemGroup>

<ItemGroup>
<Page Update="Styles\PasswordBoxResources.xaml">
<SubType>Designer</SubType>
Expand Down
39 changes: 32 additions & 7 deletions EleCho.WpfSuite/Controls/Border.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public Geometry ContentClip

contentGeometry.Freeze();
return contentGeometry;

}
else
{
Expand All @@ -62,10 +61,36 @@ public Geometry ContentClip
}

/// <inheritdoc/>
protected override void OnRender(DrawingContext dc)
protected override Size ArrangeOverride(Size finalSize)
{
SetValue(ContentClipPropertyKey, CalculateContentClip());
base.OnRender(dc);

return base.ArrangeOverride(finalSize);
}

/// <inheritdoc/>
protected override Geometry GetLayoutClip(Size layoutSlotSize)
{
var borderThickness = BorderThickness;
var cornerRadius = CornerRadius;
var renderSize = RenderSize;

if (renderSize.Width > 0 && renderSize.Height > 0)
{
var rect = new Rect(0, 0, renderSize.Width, renderSize.Height);
var radii = new Radii(cornerRadius, borderThickness, true);

var layoutGeometry = new StreamGeometry();
using StreamGeometryContext ctx = layoutGeometry.Open();
GenerateGeometry(ctx, rect, radii);

layoutGeometry.Freeze();
return layoutGeometry;
}
else
{
return base.GetLayoutClip(layoutSlotSize);
}
}

/// <summary>
Expand Down Expand Up @@ -200,10 +225,10 @@ private struct Radii
{
internal Radii(CornerRadius radii, Thickness borders, bool outer)
{
double left = 0.5 * borders.Left;
double top = 0.5 * borders.Top;
double right = 0.5 * borders.Right;
double bottom = 0.5 * borders.Bottom;
double left = 0.5 * borders.Left;
double top = 0.5 * borders.Top;
double right = 0.5 * borders.Right;
double bottom = 0.5 * borders.Bottom;

if (outer)
{
Expand Down
9 changes: 8 additions & 1 deletion WpfTest/Tests/TempPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,14 @@

<RepeatButton Content="QWQ"/>


<ws:Border ClipToBounds="True"
CornerRadius="5 10 15 20"
BorderThickness="3"
BorderBrush="Red"
MaxWidth="200"
Name="testClip">
<Image Source="/Assets/TestImage2.jpg"/>
</ws:Border>

</ws:StackPanel>
</ws:ScrollViewer>
Expand Down

0 comments on commit 43f2082

Please sign in to comment.