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 allow scroll below document #254

Merged
merged 4 commits into from
Sep 19, 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
4 changes: 2 additions & 2 deletions src/AvaloniaEdit/Editing/Caret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,14 @@ public Rect CalculateCaretRectangle()
/// <summary>
/// Minimum distance of the caret to the view border.
/// </summary>
internal const double MinimumDistanceToViewBorder = 30;
internal const double MinimumDistanceToViewBorder = 0;

/// <summary>
/// Scrolls the text view so that the caret is visible.
/// </summary>
public void BringCaretToView()
{
BringCaretToView(MinimumDistanceToViewBorder);
BringCaretToView(0);
}

public void BringCaretToView(double border)
Expand Down
4 changes: 2 additions & 2 deletions src/AvaloniaEdit/Editing/LineNumberMargin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
{
ExtendSelection(currentSeg);
}
TextArea.Caret.BringCaretToView(5.0);
TextArea.Caret.BringCaretToView(0);
}
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ protected override void OnPointerMoved(PointerEventArgs e)
if (currentSeg == SimpleSegment.Invalid)
return;
ExtendSelection(currentSeg);
TextArea.Caret.BringCaretToView(5.0);
TextArea.Caret.BringCaretToView(0);
}
base.OnPointerMoved(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/AvaloniaEdit/Editing/SelectionMouseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ private void ExtendSelectionToMouse(PointerEventArgs e)
TextArea.Caret.Offset = newWord.Offset < _startWord.Offset ? newWord.Offset : Math.Max(newWord.EndOffset, _startWord.EndOffset);
}
}
TextArea.Caret.BringCaretToView(5.0);
TextArea.Caret.BringCaretToView(0);
}
#endregion

Expand Down
44 changes: 22 additions & 22 deletions src/AvaloniaEdit/Rendering/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ public TextDocument Document
set => SetValue(DocumentProperty, value);
}

internal double FontSize => GetValue(TextBlock.FontSizeProperty);
internal double FontSize
{
get => GetValue(TemplatedControl.FontSizeProperty);
set => SetValue(TemplatedControl.FontSizeProperty, value);
}

internal FontFamily FontFamily
{
get => GetValue(TemplatedControl.FontFamilyProperty);
set => SetValue(TemplatedControl.FontFamilyProperty, value);
}

/// <summary>
/// Occurs when the document property has changed.
Expand Down Expand Up @@ -872,8 +882,6 @@ protected override Size MeasureOverride(Size availableSize)
foreach (var layer in Layers) {
layer.Measure(availableSize);
}

//InvalidateVisual(); // = InvalidateArrange+InvalidateRender

MeasureInlineObjects();

Expand Down Expand Up @@ -904,32 +912,29 @@ protected override Size MeasureOverride(Size availableSize)
maxWidth += AdditionalHorizontalScrollAmount;
var heightTreeHeight = DocumentHeight;
var options = Options;
double desiredHeight = Math.Min(availableSize.Height, heightTreeHeight);
double extraHeightToAllowScrollBelowDocument = 0;
if (options.AllowScrollBelowDocument)
{
if (!double.IsInfinity(_scrollViewport.Height))
{
// HACK: we need to keep at least Caret.MinimumDistanceToViewBorder visible so that we don't scroll back up when the user types after
// scrolling to the very bottom.
var minVisibleDocumentHeight = Math.Max(DefaultLineHeight, Caret.MinimumDistanceToViewBorder);
// scrollViewportBottom: bottom of scroll view port, but clamped so that at least minVisibleDocumentHeight of the document stays visible.
var scrollViewportBottom = Math.Min(heightTreeHeight - minVisibleDocumentHeight, _scrollOffset.Y) + _scrollViewport.Height;
var minVisibleDocumentHeight = DefaultLineHeight;
// increase the extend height to allow scrolling below the document
heightTreeHeight = Math.Max(heightTreeHeight, scrollViewportBottom);
extraHeightToAllowScrollBelowDocument = desiredHeight - minVisibleDocumentHeight;
}
}

TextLayer.SetVisualLines(_visibleVisualLines);

SetScrollData(availableSize,
new Size(maxWidth, heightTreeHeight),
new Size(maxWidth, heightTreeHeight + extraHeightToAllowScrollBelowDocument),
_scrollOffset);

// Size of control (scorll viewport) might be changed during ArrageOverride. We only need document size for now.
_documentSize = new Size(maxWidth, heightTreeHeight);

VisualLinesChanged?.Invoke(this, EventArgs.Empty);

return new Size(Math.Min(availableSize.Width, maxWidth), Math.Min(availableSize.Height, heightTreeHeight));
return new Size(Math.Min(availableSize.Width, maxWidth), desiredHeight);
}

/// <summary>
Expand Down Expand Up @@ -1152,13 +1157,13 @@ protected override Size ArrangeOverride(Size finalSize)
// validate scroll position
var newScrollOffsetX = _scrollOffset.X;
var newScrollOffsetY = _scrollOffset.Y;
if (_scrollOffset.X + finalSize.Width > _documentSize.Width)
if (_scrollOffset.X + finalSize.Width > _scrollExtent.Width)
{
newScrollOffsetX = Math.Max(0, _documentSize.Width - finalSize.Width);
newScrollOffsetX = Math.Max(0, _scrollExtent.Width - finalSize.Width);
}
if (_scrollOffset.Y + finalSize.Height > _documentSize.Height)
if (_scrollOffset.Y + finalSize.Height > _scrollExtent.Height)
{
newScrollOffsetY = Math.Max(0, _documentSize.Height - finalSize.Height);
newScrollOffsetY = Math.Max(0, _scrollExtent.Height - finalSize.Height);
}

// Apply final view port and offset
Expand Down Expand Up @@ -1317,11 +1322,6 @@ internal void ArrangeTextLayer(IList<VisualLineDrawingVisual> visuals)
/// </summary>
private Size _scrollExtent;

/// <summary>
/// Size of the document, in pixels.
/// </summary>
private Size _documentSize;

/// <summary>
/// Offset of the scroll position.
/// </summary>
Expand Down Expand Up @@ -2052,7 +2052,7 @@ bool ILogicalScrollable.CanVerticallyScroll
}
}

bool ILogicalScrollable.IsLogicalScrollEnabled => true;
bool ILogicalScrollable.IsLogicalScrollEnabled => true;

Size ILogicalScrollable.ScrollSize => new Size(10, 50);

Expand Down
17 changes: 17 additions & 0 deletions src/AvaloniaEdit/TextEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ static TextEditor()
IsModifiedProperty.Changed.Subscribe(OnIsModifiedChanged);
ShowLineNumbersProperty.Changed.Subscribe(OnShowLineNumbersChanged);
LineNumbersForegroundProperty.Changed.Subscribe(OnLineNumbersForegroundChanged);
FontFamilyProperty.Changed.Subscribe(OnFontFamilyPropertyChanged);
FontSizeProperty.Changed.Subscribe(OnFontSizePropertyChanged);
}

/// <summary>
Expand Down Expand Up @@ -562,6 +564,21 @@ private static void OnLineNumbersForegroundChanged(AvaloniaPropertyChangedEventA

lineNumberMargin?.SetValue(ForegroundProperty, e.NewValue);
}

private static void OnFontFamilyPropertyChanged(AvaloniaPropertyChangedEventArgs e)
{
var editor = e.Sender as TextEditor;

editor?.TextArea.TextView.SetValue(FontFamilyProperty, e.NewValue);
}

private static void OnFontSizePropertyChanged(AvaloniaPropertyChangedEventArgs e)
{
var editor = e.Sender as TextEditor;

editor?.TextArea.TextView.SetValue(FontSizeProperty, e.NewValue);
}

#endregion

#region TextBoxBase-like methods
Expand Down