Browse Source
* Add failing test for preserving run formatting during selection * Fix selection styling to preserve original text formatting in SelectableTextBlock * fix: apply old logic when `_textRuns` is nullpull/20137/head
committed by
GitHub
2 changed files with 104 additions and 5 deletions
@ -0,0 +1,59 @@ |
|||||
|
using System.Linq; |
||||
|
using Avalonia.Controls.Documents; |
||||
|
using Avalonia.Media; |
||||
|
using Avalonia.Media.TextFormatting; |
||||
|
using Avalonia.UnitTests; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Controls.UnitTests |
||||
|
{ |
||||
|
public class SelectableTextBlockTests : ScopedTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void SelectionForeground_Should_Not_Reset_Run_Typeface_And_Style() |
||||
|
{ |
||||
|
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) |
||||
|
{ |
||||
|
var target = new SelectableTextBlock |
||||
|
{ |
||||
|
SelectionForegroundBrush = Brushes.Red |
||||
|
}; |
||||
|
|
||||
|
var run = new Run("Hello") |
||||
|
{ |
||||
|
FontWeight = FontWeight.Bold, |
||||
|
FontStyle = FontStyle.Italic, |
||||
|
FontSize = 20 |
||||
|
}; |
||||
|
|
||||
|
target.Inlines.Add(run); |
||||
|
|
||||
|
target.Measure(Size.Infinity); |
||||
|
|
||||
|
target.SelectionStart = 0; |
||||
|
target.SelectionEnd = run.Text.Length; |
||||
|
|
||||
|
target.Measure(Size.Infinity); |
||||
|
|
||||
|
var textLayout = target.TextLayout; |
||||
|
Assert.NotNull(textLayout); |
||||
|
|
||||
|
var textRuns = textLayout.TextLines |
||||
|
.SelectMany(l => l.TextRuns) |
||||
|
.OfType<ShapedTextRun>() |
||||
|
.ToList(); |
||||
|
|
||||
|
Assert.NotEmpty(textRuns); |
||||
|
|
||||
|
var selectedRun = textRuns[0]; |
||||
|
var props = selectedRun.Properties; |
||||
|
|
||||
|
Assert.Equal(FontWeight.Bold, props.Typeface.Weight); |
||||
|
Assert.Equal(FontStyle.Italic, props.Typeface.Style); |
||||
|
|
||||
|
Assert.Same(target.SelectionForegroundBrush, props.ForegroundBrush); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue