-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Media; | ||
|
||
namespace EleCho.WpfSuite | ||
{ | ||
/// <summary> | ||
/// Auto clip content depends on the parent border | ||
/// </summary> | ||
public class BorderContentAdapter : Decorator | ||
{ | ||
static BorderContentAdapter() | ||
{ | ||
ClipToBoundsProperty.OverrideMetadata(typeof(BorderContentAdapter), new PropertyMetadata(true)); | ||
} | ||
|
||
private static Geometry? CalculateBorderContentClip(System.Windows.Controls.Border border, Size contentSize) | ||
{ | ||
if (contentSize.Width <= 0 || | ||
contentSize.Height <= 0) | ||
{ | ||
return null; | ||
} | ||
|
||
var rect = new Rect(0, 0, contentSize.Width, contentSize.Height); | ||
var radii = new Border.Radii(border.CornerRadius, border.BorderThickness, false); | ||
|
||
var contentGeometry = new StreamGeometry(); | ||
using StreamGeometryContext ctx = contentGeometry.Open(); | ||
Border.GenerateGeometry(ctx, rect, radii); | ||
|
||
contentGeometry.Freeze(); | ||
return contentGeometry; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override Geometry GetLayoutClip(Size layoutSlotSize) | ||
{ | ||
var renderSize = RenderSize; | ||
|
||
if (Parent is EleCho.WpfSuite.Border border && | ||
CalculateBorderContentClip(border, renderSize) is { } borderContentClip) | ||
{ | ||
return borderContentClip; | ||
} | ||
else if (Parent is System.Windows.Controls.Border nativeBorder && | ||
CalculateBorderContentClip(nativeBorder, renderSize) is { } nativeBorderContentClip) | ||
{ | ||
return nativeBorderContentClip; | ||
} | ||
|
||
return base.GetLayoutClip(layoutSlotSize); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters