Skip to content

Commit 9e75f8b

Browse files
committed
Allow vertical scroll positioning with pixel accuracy #190
icsharpcode/AvalonEdit#190 (commented out)
1 parent 38a0b1f commit 9e75f8b

File tree

1 file changed

+70
-50
lines changed

1 file changed

+70
-50
lines changed

src/AvaloniaEdit/TextEditor.cs

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,57 +1119,77 @@ public void ScrollToLine(int line)
11191119
/// </summary>
11201120
public void ScrollTo(int line, int column)
11211121
{
1122-
//const double MinimumScrollPercentage = 0.3;
1122+
//const double MinimumScrollFraction = 0.3;
1123+
//ScrollTo(line, column, VisualYPosition.LineMiddle,
1124+
// null != scrollViewer ? scrollViewer.ViewportHeight / 2 : 0.0, MinimumScrollFraction);
1125+
}
11231126

1124-
//TextView textView = textArea.TextView;
1125-
//TextDocument document = textView.Document;
1126-
//if (scrollViewer != null && document != null)
1127-
//{
1128-
// if (line < 1)
1129-
// line = 1;
1130-
// if (line > document.LineCount)
1131-
// line = document.LineCount;
1132-
1133-
// IScrollInfo scrollInfo = textView;
1134-
// if (!scrollInfo.CanHorizontallyScroll)
1135-
// {
1136-
// // Word wrap is enabled. Ensure that we have up-to-date info about line height so that we scroll
1137-
// // to the correct position.
1138-
// // This avoids that the user has to repeat the ScrollTo() call several times when there are very long lines.
1139-
// VisualLine vl = textView.GetOrConstructVisualLine(document.GetLineByNumber(line));
1140-
// double remainingHeight = scrollViewer.Viewport.Height / 2;
1141-
// while (remainingHeight > 0)
1142-
// {
1143-
// DocumentLine prevLine = vl.FirstDocumentLine.PreviousLine;
1144-
// if (prevLine == null)
1145-
// break;
1146-
// vl = textView.GetOrConstructVisualLine(prevLine);
1147-
// remainingHeight -= vl.Height;
1148-
// }
1149-
// }
1150-
1151-
// Point p = textArea.TextView.GetVisualPosition(new TextViewPosition(line, Math.Max(1, column)), VisualYPosition.LineMiddle);
1152-
// double verticalPos = p.Y - scrollViewer.ViewportHeight / 2;
1153-
// if (Math.Abs(verticalPos - scrollViewer.VerticalOffset) > MinimumScrollPercentage * scrollViewer.ViewportHeight)
1154-
// {
1155-
// scrollViewer.ScrollToVerticalOffset(Math.Max(0, verticalPos));
1156-
// }
1157-
// if (column > 0)
1158-
// {
1159-
// if (p.X > scrollViewer.ViewportWidth - Caret.MinimumDistanceToViewBorder * 2)
1160-
// {
1161-
// double horizontalPos = Math.Max(0, p.X - scrollViewer.ViewportWidth / 2);
1162-
// if (Math.Abs(horizontalPos - scrollViewer.HorizontalOffset) > MinimumScrollPercentage * scrollViewer.ViewportWidth)
1163-
// {
1164-
// scrollViewer.ScrollToHorizontalOffset(horizontalPos);
1165-
// }
1166-
// }
1167-
// else
1168-
// {
1169-
// scrollViewer.ScrollToHorizontalOffset(0);
1170-
// }
1171-
// }
1172-
//}
1127+
/// <summary>
1128+
/// Scrolls to the specified line/column.
1129+
/// This method requires that the TextEditor was already assigned a size (WPF layout must have run prior).
1130+
/// </summary>
1131+
/// <param name="line">Line to scroll to.</param>
1132+
/// <param name="column">Column to scroll to (important if wrapping is 'on', and for the horizontal scroll position).</param>
1133+
/// <param name="yPositionMode">The mode how to reference the Y position of the line.</param>
1134+
/// <param name="referencedVerticalViewPortOffset">Offset from the top of the viewport to where the referenced line/column should be positioned.</param>
1135+
/// <param name="minimumScrollFraction">The minimum vertical and/or horizontal scroll offset, expressed as fraction of the height or width of the viewport window, respectively.</param>
1136+
public void ScrollTo(int line, int column, VisualYPosition yPositionMode,
1137+
double referencedVerticalViewPortOffset, double minimumScrollFraction)
1138+
{
1139+
/*TextView textView = textArea.TextView;
1140+
TextDocument document = textView.Document;
1141+
if (scrollViewer != null && document != null)
1142+
{
1143+
if (line < 1)
1144+
line = 1;
1145+
if (line > document.LineCount)
1146+
line = document.LineCount;
1147+
1148+
IScrollInfo scrollInfo = textView;
1149+
if (!scrollInfo.CanHorizontallyScroll)
1150+
{
1151+
// Word wrap is enabled. Ensure that we have up-to-date info about line height so that we scroll
1152+
// to the correct position.
1153+
// This avoids that the user has to repeat the ScrollTo() call several times when there are very long lines.
1154+
VisualLine vl = textView.GetOrConstructVisualLine(document.GetLineByNumber(line));
1155+
double remainingHeight = referencedVerticalViewPortOffset;
1156+
1157+
while (remainingHeight > 0)
1158+
{
1159+
DocumentLine prevLine = vl.FirstDocumentLine.PreviousLine;
1160+
if (prevLine == null)
1161+
break;
1162+
vl = textView.GetOrConstructVisualLine(prevLine);
1163+
remainingHeight -= vl.Height;
1164+
}
1165+
}
1166+
1167+
Point p = textArea.TextView.GetVisualPosition(new TextViewPosition(line, Math.Max(1, column)),
1168+
yPositionMode);
1169+
double verticalPos = p.Y - referencedVerticalViewPortOffset;
1170+
if (Math.Abs(verticalPos - scrollViewer.VerticalOffset) >
1171+
minimumScrollFraction * scrollViewer.ViewportHeight)
1172+
{
1173+
scrollViewer.ScrollToVerticalOffset(Math.Max(0, verticalPos));
1174+
}
1175+
1176+
if (column > 0)
1177+
{
1178+
if (p.X > scrollViewer.ViewportWidth - Caret.MinimumDistanceToViewBorder * 2)
1179+
{
1180+
double horizontalPos = Math.Max(0, p.X - scrollViewer.ViewportWidth / 2);
1181+
if (Math.Abs(horizontalPos - scrollViewer.HorizontalOffset) >
1182+
minimumScrollFraction * scrollViewer.ViewportWidth)
1183+
{
1184+
scrollViewer.ScrollToHorizontalOffset(horizontalPos);
1185+
}
1186+
}
1187+
else
1188+
{
1189+
scrollViewer.ScrollToHorizontalOffset(0);
1190+
}
1191+
}
1192+
}*/
11731193
}
11741194
}
11751195
}

0 commit comments

Comments
 (0)