Skip to content

Commit

Permalink
fix: Textblock can be constrained by maxlines on wasm and android
Browse files Browse the repository at this point in the history
  • Loading branch information
alextrepa committed Nov 10, 2020
1 parent edfd074 commit 0c33868
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,65 @@ void Test(string textControl, string borderControl)
}
}
}

[Test]
[AutoRetry]
public void When_MaxLines_Changed_On_Stack_Container()
{
Run("Uno.UI.Samples.Content.UITests.TextBlockControl.SimpleText_MaxLines_Multiple_Containers");

var stackTextBlockName = "stackTextBlock";
var maxLineSlider = _app.Marked("slider");

_app.WaitForElement(maxLineSlider);

var numberOfLines = 1;
maxLineSlider.SetDependencyPropertyValue("Value", $"{numberOfLines}");
var rectBefore = _app.GetRect(stackTextBlockName);
var lineHeight = rectBefore.Height;

numberOfLines = 3;
maxLineSlider.SetDependencyPropertyValue("Value", $"{numberOfLines}");

var rectAfter = _app.GetRect(stackTextBlockName);
var textBlockHeight = rectAfter.Height;

var actualNumberOfLines = (int)Math.Ceiling(textBlockHeight / lineHeight);
Assert.IsTrue(actualNumberOfLines == numberOfLines,
"Results \n" +
$"Expected Number of lines: {numberOfLines}. \n" +
$"Actual Number of lines:{actualNumberOfLines}. \n" +
$"Line height: {lineHeight}. \n");
}

[Test]
[AutoRetry]
public void When_MaxLines_Changed_On_Grid_Container()
{
Run("Uno.UI.Samples.Content.UITests.TextBlockControl.SimpleText_MaxLines_Multiple_Containers");

var gridTextBlockName = "gridTextBlock";
var maxLineSlider = _app.Marked("slider");

_app.WaitForElement(maxLineSlider);

var numberOfLines = 1;
maxLineSlider.SetDependencyPropertyValue("Value", $"{numberOfLines}");
var rectBefore = _app.GetRect(gridTextBlockName);
var lineHeight = rectBefore.Height;

numberOfLines = 2;
maxLineSlider.SetDependencyPropertyValue("Value", $"{numberOfLines}");

var rectAfter = _app.GetRect(gridTextBlockName);
var textBlockHeight = rectAfter.Height;

var actualNumberOfLines = (int)Math.Ceiling(textBlockHeight / lineHeight);
Assert.IsTrue(actualNumberOfLines == numberOfLines,
"Results \n" +
$"Expected Number of lines: {numberOfLines}. \n" +
$"Actual Number of lines:{actualNumberOfLines}. \n" +
$"Line height: {lineHeight}. \n");
}
}
}
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\SimpleText_MaxLines_Multiple_Containers.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\TextBlock_BrushColorChanging.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -4324,6 +4328,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\SimpleText_MaxLines_Different_Font_Size.xaml.cs">
<DependentUpon>SimpleText_MaxLines_Different_Font_Size.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\SimpleText_MaxLines_Multiple_Containers.xaml.cs">
<DependentUpon>SimpleText_MaxLines_Multiple_Containers.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\TextBlock_BrushColorChanging.xaml.cs">
<DependentUpon>TextBlock_BrushColorChanging.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<UserControl
x:Class="Uno.UI.Samples.Content.UITests.TextBlockControl.SimpleText_MaxLines_Multiple_Containers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.Samples.Content.UITests.TextBlockControl.TextBlockControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid Width="200">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="7*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Slider
Minimum="1"
Maximum="10"
x:Name="slider"
Grid.Column="0" />
<TextBlock
Text="{Binding Value, ElementName=slider}"
Grid.Column="1"
Foreground="Black" />
</Grid>

<Grid
Background="Cyan"
Grid.Row="1"
x:Name="grid1">
<TextBlock
x:Name="gridTextBlock"
Text="Grid container. This is a very very very very long WRAPPING (Wrap) text that *should* wrap because it goes out of the screen. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
FontSize="20"
TextWrapping="Wrap"
MaxLines="{Binding Value, ElementName=slider}"
Foreground="Black" />
</Grid>
<StackPanel
Background="Yellow"
Grid.Row="2"
x:Name="stack1">
<TextBlock
x:Name="stackTextBlock"
Text="Stack container. This is a very very very very long WRAPPING (Wrap) text that *should* wrap because it goes out of the screen. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
FontSize="20"
TextWrapping="Wrap"
MaxLines="{Binding Value, ElementName=slider}"
Foreground="Black" />
</StackPanel>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Uno.UI.Samples.Controls;
using Windows.UI.Xaml.Controls;


namespace Uno.UI.Samples.Content.UITests.TextBlockControl
{
[Sample]
public sealed partial class SimpleText_MaxLines_Multiple_Containers : UserControl
{
public SimpleText_MaxLines_Multiple_Containers()
{
this.InitializeComponent();
}
}
}
18 changes: 18 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ partial void OnTextChangedPartial()

// Invalidate _ellipsize
partial void OnTextTrimmingChangedPartial() => _ellipsize = null;
partial void OnMaxLinesChangedPartial() => _ellipsize = null;

// Invalidate _layoutAlignment
partial void OnTextAlignmentChangedPartial() => _layoutAlignment = null;
Expand All @@ -156,6 +157,7 @@ private void Update()
UpdateTextTrimming();
UpdateTextFormatted();
UpdatePaint();
UpdateMaxLines();
}

private void UpdateLayoutAlignment()
Expand Down Expand Up @@ -241,6 +243,22 @@ private void UpdateTextFormatted()
_textFormatted = GetTextFormatted();
}

/// <summary>
/// Sets ellipsize to truncateAt.END when a number of line is specified.
/// If ellipsize is another value, the text won't truncate.
/// </summary>
private void UpdateMaxLines()
{
if (_ellipsize != null)
{
return;
}

_ellipsize = this.IsLayoutConstrainedByMaxLines
? TruncateEnd
: null;
}

private Java.Lang.ICharSequence GetTextFormatted()
{
if (Text == string.Empty)
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ private void OnTextDecorationsChanged()
/// </summary>
private bool UseInlinesFastPath => _inlines == null;

/// <summary>
/// Returns if the TextBlock is constrained by a maximum number of lines.
/// </summary>
private bool IsLayoutConstrainedByMaxLines => MaxLines > 0;

/// <summary>
/// Gets the inlines which affect the typography of the TextBlock.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,19 @@ public TextBlock() : base("p")
OnTextAlignmentChangedPartial();
OnTextWrappingChangedPartial();
OnIsTextSelectionEnabledChangedPartial();
InitializeDefaultValues();

}

/// <summary>
/// Set default properties to vertical top.
/// In wasm, this behavior is closer to the default textblock property than stretch.
/// </summary>
private void InitializeDefaultValues()
{
this.SetValue(VerticalAlignmentProperty, VerticalAlignment.Top, DependencyPropertyValuePrecedences.DefaultValue);
}

private void ConditionalUpdate(ref bool condition, Action action)
{
if (condition)
Expand Down Expand Up @@ -115,6 +126,20 @@ protected override Size MeasureOverride(Size availableSize)
}
}

/// <summary>
/// When the control is constrained, it should only take it's desired size or
/// it will show all of it's content.
/// </summary>
/// <param name="finalSize"></param>
/// <returns></returns>
protected override Size ArrangeOverride(Size finalSize)
{
var arrangeSize = IsLayoutConstrainedByMaxLines
? DesiredSize
: finalSize;
return base.ArrangeOverride(arrangeSize);
}

private int GetCharacterIndexAtPoint(Point point) => throw new NotSupportedException();

partial void OnFontStyleChangedPartial() => _fontStyleChanged = true;
Expand Down

0 comments on commit 0c33868

Please sign in to comment.