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

Fix #316: Expose MarkerPen and MarkerCornerRadius on SearchPanel. #329

Merged
merged 1 commit into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
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
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 Brush MarkerBrush {
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 Localization Localization {
}
#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 @@ public KnownLayer Layer {

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