From 1b57d0b2a4758f62aac085b8a3a97007abcfabb3 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Sun, 1 Dec 2024 14:41:05 +0800 Subject: [PATCH 1/2] feat: add readonly property: LineCount. --- src/Avalonia.Controls/TextBox.cs | 14 +++ .../TextBoxTests.cs | 100 ++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 62070f68474..92a99531e92 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -823,6 +823,20 @@ public bool CanRedo private set => SetAndRaise(CanRedoProperty, ref _canRedo, value); } + /// + /// Number of lines in the TextBox. + /// + /// number of lines in the TextBox, or -1 if no layout information is available + /// + /// 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. + /// + public int LineCount + { + get => this._presenter?.TextLayout.TextLines.Count ?? -1; + } + /// /// Raised when content is being copied to the clipboard /// diff --git a/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs b/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs index d6b24b627f9..5bc4d125403 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs @@ -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() From 2cf3a920aa12e8400e914af5c4dd6ea09759069e Mon Sep 17 00:00:00 2001 From: Dong Bin Date: Wed, 22 Jan 2025 14:40:33 +0800 Subject: [PATCH 2/2] feat: change property to method. --- src/Avalonia.Controls/TextBox.cs | 6 +++--- tests/Avalonia.Controls.UnitTests/TextBoxTests.cs | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 92a99531e92..1fd3960eeef 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -824,7 +824,7 @@ public bool CanRedo } /// - /// Number of lines in the TextBox. + /// Get the number of lines in the TextBox. /// /// number of lines in the TextBox, or -1 if no layout information is available /// @@ -832,9 +832,9 @@ public bool CanRedo /// The value returned is the number of lines in the entire TextBox, regardless of how many are /// currently in view. /// - public int LineCount + public int GetLineCount() { - get => this._presenter?.TextLayout.TextLines.Count ?? -1; + return this._presenter?.TextLayout.TextLines.Count ?? -1; } /// diff --git a/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs b/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs index 5bc4d125403..8e4f3a8a288 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs @@ -1243,7 +1243,7 @@ public void LineCount_Is_Correct(string? text, int lineCount) target.ApplyTemplate(); target.Measure(Size.Infinity); - Assert.Equal(lineCount, target.LineCount); + Assert.Equal(lineCount, target.GetLineCount()); } } @@ -1251,7 +1251,7 @@ public void LineCount_Is_Correct(string? text, int lineCount) public void Unmeasured_TextBox_Has_Negative_LineCount() { var b = new TextBox(); - Assert.Equal(-1, b.LineCount); + Assert.Equal(-1, b.GetLineCount()); } [Fact] @@ -1278,11 +1278,11 @@ public void LineCount_Is_Correct_After_Text_Change() target.ApplyTemplate(); target.Measure(Size.Infinity); - Assert.Equal(1, target.LineCount); + Assert.Equal(1, target.GetLineCount()); target.Text = "Hello\r\nWorld"; - Assert.Equal(2, target.LineCount); + Assert.Equal(2, target.GetLineCount()); } } @@ -1311,7 +1311,7 @@ public void Visible_LineCount_DoesNot_Affect_LineCount() target.ApplyTemplate(); target.Measure(Size.Infinity); - Assert.Equal(4, target.LineCount); + Assert.Equal(4, target.GetLineCount()); } }