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

[TextBox] Add readonly property: LineCount. #17656

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/Avalonia.Controls/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,20 @@ public bool CanRedo
private set => SetAndRaise(CanRedoProperty, ref _canRedo, value);
}

/// <summary>
/// Number of lines in the TextBox.
/// </summary>
/// <value>number of lines in the TextBox, or -1 if no layout information is available</value>
/// <remarks>
/// If Wrap == true, changing the width of the TextBox may change this value.
/// The value returned is the number of lines in the entire TextBox, regardless of how many are
/// currently in view.
/// </remarks>
public int LineCount
{
get => this._presenter?.TextLayout.TextLines.Count ?? -1;
}

/// <summary>
/// Raised when content is being copied to the clipboard
/// </summary>
Expand Down
100 changes: 100 additions & 0 deletions tests/Avalonia.Controls.UnitTests/TextBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,106 @@ public void MinLines_Sets_ScrollViewer_MinHeight_With_TextPresenter_Margin(int m
Assert.Equal((minLines * target.LineHeight) + textPresenterMargin.Top + textPresenterMargin.Bottom, scrollViewer.MinHeight);
}
}

[Theory]
[InlineData(null, 1)]
[InlineData("", 1)]
[InlineData("Hello", 1)]
[InlineData("Hello\r\nWorld", 2)]
public void LineCount_Is_Correct(string? text, int lineCount)
{
using (UnitTestApplication.Start(Services))
{
var target = new TextBox
{
Template = CreateTemplate(),
Text = text,
AcceptsReturn = true
};

var impl = CreateMockTopLevelImpl();
var topLevel = new TestTopLevel(impl.Object)
{
Template = CreateTopLevelTemplate()
};
topLevel.Content = target;
topLevel.ApplyTemplate();
topLevel.LayoutManager.ExecuteInitialLayoutPass();

target.ApplyTemplate();
target.Measure(Size.Infinity);

Assert.Equal(lineCount, target.LineCount);
}
}

[Fact]
public void Unmeasured_TextBox_Has_Negative_LineCount()
{
var b = new TextBox();
Assert.Equal(-1, b.LineCount);
}

[Fact]
public void LineCount_Is_Correct_After_Text_Change()
{
using (UnitTestApplication.Start(Services))
{
var target = new TextBox
{
Template = CreateTemplate(),
Text = "Hello",
AcceptsReturn = true
};

var impl = CreateMockTopLevelImpl();
var topLevel = new TestTopLevel(impl.Object)
{
Template = CreateTopLevelTemplate()
};
topLevel.Content = target;
topLevel.ApplyTemplate();
topLevel.LayoutManager.ExecuteInitialLayoutPass();

target.ApplyTemplate();
target.Measure(Size.Infinity);

Assert.Equal(1, target.LineCount);

target.Text = "Hello\r\nWorld";

Assert.Equal(2, target.LineCount);
}
}

[Fact]
public void Visible_LineCount_DoesNot_Affect_LineCount()
{
using (UnitTestApplication.Start(Services))
{
var target = new TextBox
{
Template = CreateTemplate(),
Text = "Hello\r\nWorld\r\nHello\r\nAvalonia",
AcceptsReturn = true,
MaxLines = 2,
};

var impl = CreateMockTopLevelImpl();
var topLevel = new TestTopLevel(impl.Object)
{
Template = CreateTopLevelTemplate()
};
topLevel.Content = target;
topLevel.ApplyTemplate();
topLevel.LayoutManager.ExecuteInitialLayoutPass();

target.ApplyTemplate();
target.Measure(Size.Infinity);

Assert.Equal(4, target.LineCount);
}
}

[Fact]
public void CanUndo_CanRedo_Is_False_When_Initialized()
Expand Down