Skip to content
Merged
Changes from all 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
27 changes: 19 additions & 8 deletions DesktopClock/OutlinedTextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public StrokePosition StrokePosition
private FormattedText _FormattedText;
private Geometry _TextGeometry;
private Pen _Pen;
private PathGeometry _clipGeometry;
private Geometry _clipGeometry;

public Brush Fill
{
Expand Down Expand Up @@ -206,8 +206,15 @@ protected override void OnRender(DrawingContext drawingContext)

drawingContext.DrawGeometry(Fill, null, _TextGeometry);

// With a solid background the outline is transparent, so skip it; its clip geometry is the most expensive part of redrawing the clock each second.
if (!HasVisibleStroke())
{
return;
}

if (StrokePosition == StrokePosition.Outside)
{
_clipGeometry ??= BuildOutsideClipGeometry();
drawingContext.PushClip(_clipGeometry);
}
else if (StrokePosition == StrokePosition.Inside)
Expand Down Expand Up @@ -325,14 +332,18 @@ private void EnsureGeometry()

EnsureFormattedText();
_TextGeometry = _FormattedText.BuildGeometry(new Point(0, 0));
_clipGeometry = null;
}

if (StrokePosition == StrokePosition.Outside)
{
// https://stackoverflow.com/questions/93650/apply-stroke-to-a-textblock-in-wpf/35262509#comment106994536_49636033.
//var boundsGeo = new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight));
var boundsGeo = new RectangleGeometry(new Rect(-(2 * StrokeThickness), -(2 * StrokeThickness), ActualWidth + (4 * StrokeThickness), ActualHeight + (4 * StrokeThickness)));
_clipGeometry = Geometry.Combine(boundsGeo, _TextGeometry, GeometryCombineMode.Exclude, null);
}
private bool HasVisibleStroke() =>
StrokeThickness > 0 && Stroke is not (null or SolidColorBrush { Color.A: 0 } or SolidColorBrush { Opacity: 0 });

private Geometry BuildOutsideClipGeometry()
{
// https://stackoverflow.com/questions/93650/apply-stroke-to-a-textblock-in-wpf/35262509#comment106994536_49636033.
//var boundsGeo = new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight));
var boundsGeo = new RectangleGeometry(new Rect(-(2 * StrokeThickness), -(2 * StrokeThickness), ActualWidth + (4 * StrokeThickness), ActualHeight + (4 * StrokeThickness)));
return Geometry.Combine(boundsGeo, _TextGeometry, GeometryCombineMode.Exclude, null);
}
}

Expand Down
Loading