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

Improve colum ruler rendering #282

Merged
merged 2 commits into from
Oct 20, 2022
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
5 changes: 4 additions & 1 deletion src/AvaloniaEdit/Rendering/ColumnRulerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public void Draw(TextView textView, DrawingContext drawingContext)
var start = new Point(markerXPos, 0);
var end = new Point(markerXPos, Math.Max(textView.DocumentHeight, textView.Bounds.Height));

drawingContext.DrawLine(_pen, start, end);
drawingContext.DrawLine(
_pen,
start.SnapToDevicePixels(textView),
end.SnapToDevicePixels(textView));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/AvaloniaEdit/Rendering/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
/// <seealso cref="TextEditorOptions.ShowColumnRulers"/>
/// </summary>
public static readonly StyledProperty<IPen> ColumnRulerPenProperty =
AvaloniaProperty.Register<TextView, IPen>("ColumnRulerBrush", CreateFrozenPen(new SolidColorBrush(Color.FromArgb(145, 128, 128, 128))));
AvaloniaProperty.Register<TextView, IPen>("ColumnRulerBrush", CreateFrozenPen(new SolidColorBrush(Color.FromArgb(90, 128, 128, 128))));

private static ImmutablePen CreateFrozenPen(IBrush brush)
{
Expand Down
24 changes: 24 additions & 0 deletions src/AvaloniaEdit/Utils/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,30 @@ public static Rect ToAvalonia(this System.Drawing.Rectangle rect)
}
#endregion

#region Snap to device pixels
public static Point SnapToDevicePixels(this Point p, IVisual targetVisual)
{
var root = targetVisual.GetVisualRoot();

// Get the root control and its scaling
var scaling = new Vector(root.RenderScaling, root.RenderScaling);

// Create a matrix to translate from control coordinates to device coordinates.
var m = targetVisual.TransformToVisual((Control)root) * Matrix.CreateScale(scaling);

// Translate the point to device coordinates.
var devicePoint = p.Transform(m.Value);

// Snap the coordinate to the midpoint between device pixels.
devicePoint = new Point(((int)devicePoint.X) + 0.5, ((int)devicePoint.Y) + 0.5);

// Translate the point back to control coordinates.
var inv = m.Value.Invert();
Point result = devicePoint.Transform(inv);
return result;
}
#endregion

public static IEnumerable<AvaloniaObject> VisualAncestorsAndSelf(this AvaloniaObject obj)
{
while (obj != null)
Expand Down