Skip to content

Commit

Permalink
Merge pull request #329 from icsharpcode/customizable-searchresult-re…
Browse files Browse the repository at this point in the history
…ndering

Fix #316: Expose MarkerPen and MarkerCornerRadius on SearchPanel.
  • Loading branch information
siegfriedpammer committed Dec 28, 2021
2 parents 47f61e9 + 1c83076 commit 9e4089d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
59 changes: 51 additions & 8 deletions ICSharpCode.AvalonEdit/Search/SearchPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,57 @@ public class SearchPanel : Control
set { SetValue(MarkerBrushProperty, value); }
}

private static void MarkerBrushChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is SearchPanel panel) {
panel.renderer.MarkerBrush = (Brush)e.NewValue;
}
}

/// <summary>
/// Dependency property for <see cref="MarkerPen"/>.
/// </summary>
public static readonly DependencyProperty MarkerPenProperty =
DependencyProperty.Register("MarkerPen", typeof(Pen), typeof(SearchPanel),
new PropertyMetadata(null, MarkerPenChangedCallback));

/// <summary>
/// Gets/sets the Pen used for marking search results in the TextView.
/// </summary>
public Pen MarkerPen {
get { return (Pen)GetValue(MarkerPenProperty); }
set { SetValue(MarkerPenProperty, value); }
}

private static void MarkerPenChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is SearchPanel panel) {
panel.renderer.MarkerPen = (Pen)e.NewValue;
}
}

/// <summary>
/// Dependency property for <see cref="MarkerCornerRadius"/>.
/// </summary>
public static readonly DependencyProperty MarkerCornerRadiusProperty =
DependencyProperty.Register("MarkerCornerRadius", typeof(double), typeof(SearchPanel),
new PropertyMetadata(3.0, MarkerCornerRadiusChangedCallback));

/// <summary>
/// Gets/sets the corner-radius used for marking search results in the TextView.
/// </summary>
public double MarkerCornerRadius {
get { return (double)GetValue(MarkerCornerRadiusProperty); }
set { SetValue(MarkerCornerRadiusProperty, value); }
}

private static void MarkerCornerRadiusChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is SearchPanel panel) {
panel.renderer.MarkerCornerRadius = (double)e.NewValue;
}
}

/// <summary>
/// Dependency property for <see cref="Localization"/>.
/// </summary>
Expand All @@ -136,14 +187,6 @@ public class SearchPanel : Control
}
#endregion

static void MarkerBrushChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SearchPanel panel = d as SearchPanel;
if (panel != null) {
panel.renderer.MarkerBrush = (Brush)e.NewValue;
}
}

static SearchPanel()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchPanel), new FrameworkPropertyMetadata(typeof(SearchPanel)));
Expand Down
27 changes: 13 additions & 14 deletions ICSharpCode.AvalonEdit/Search/SearchResultBackgroundRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,14 @@ class SearchResultBackgroundRenderer : IBackgroundRenderer

public SearchResultBackgroundRenderer()
{
markerBrush = Brushes.LightGreen;
markerPen = new Pen(markerBrush, 1);
MarkerBrush = Brushes.LightGreen;
MarkerPen = null;
MarkerCornerRadius = 3.0;
}

Brush markerBrush;
Pen markerPen;

public Brush MarkerBrush {
get { return markerBrush; }
set {
this.markerBrush = value;
markerPen = new Pen(markerBrush, 1);
}
}
public Brush MarkerBrush { get; set; }
public Pen MarkerPen { get; set; }
public double MarkerCornerRadius { get; set; }

public void Draw(TextView textView, DrawingContext drawingContext)
{
Expand All @@ -74,11 +68,16 @@ public void Draw(TextView textView, DrawingContext drawingContext)
int viewStart = visualLines.First().FirstDocumentLine.Offset;
int viewEnd = visualLines.Last().LastDocumentLine.EndOffset;

Brush markerBrush = MarkerBrush;
Pen markerPen = MarkerPen;
double markerCornerRadius = MarkerCornerRadius;
double markerPenThickness = markerPen != null ? markerPen.Thickness : 0;

foreach (SearchResult result in currentResults.FindOverlappingSegments(viewStart, viewEnd - viewStart)) {
BackgroundGeometryBuilder geoBuilder = new BackgroundGeometryBuilder();
geoBuilder.AlignToWholePixels = true;
geoBuilder.BorderThickness = markerPen != null ? markerPen.Thickness : 0;
geoBuilder.CornerRadius = 3;
geoBuilder.BorderThickness = markerPenThickness;
geoBuilder.CornerRadius = markerCornerRadius;
geoBuilder.AddSegment(textView, result);
Geometry geometry = geoBuilder.CreateGeometry();
if (geometry != null) {
Expand Down

0 comments on commit 9e4089d

Please sign in to comment.