Skip to content

Commit

Permalink
iOS fix dotnet#21837 & a Ui Test
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo committed May 14, 2024
1 parent a1c9b93 commit eefd4b0
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue21837"
Title="Issue21837">

<VerticalStackLayout HorizontalOptions="Start" Spacing="10">
<Label AutomationId="resultLabel" Text="{Binding TapCount, StringFormat='Number of recognized taps: {0}'}" FontSize="30" FontAttributes="Bold" />

<Label Text="Single line, progressively limited width:" FontSize="20" FontAttributes="Bold" />

<Label AutomationId="label1" HorizontalOptions="Start" MaxLines="1" BackgroundColor="LightCyan" WidthRequest="600">
<Label.FormattedText>
<FormattedString>
<Span Text="Only our WidthRequests differ. Otherwise we're the same. Please tap us!">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" />
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Label AutomationId="label2" HorizontalOptions="Start" MaxLines="1" BackgroundColor="LightCyan" WidthRequest="500">
<Label.FormattedText>
<FormattedString>
<Span Text="Only our WidthRequests differ. Otherwise we're the same. Please tap us!">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" />
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Label AutomationId="label3" HorizontalOptions="Start" MaxLines="1" LineBreakMode="TailTruncation" BackgroundColor="LightCyan" >
<Label.FormattedText>
<FormattedString>
<Span Text="Only our WidthRequests differ. Otherwise we're the same. Please tap us!">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" />
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Label AutomationId="label4" HorizontalOptions="Start" MaxLines="1" BackgroundColor="LightCyan" WidthRequest="300">
<Label.FormattedText>
<FormattedString>
<Span Text="Only our WidthRequests differ. Otherwise we're the same. Please tap us!">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" />
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>

<Label Text="Limited width, progressively limited lines:" FontSize="20" FontAttributes="Bold" />

<Label AutomationId="label5" HorizontalOptions="Start" MaxLines="7" BackgroundColor="LightGoldenrodYellow" WidthRequest="100">
<Label.FormattedText>
<FormattedString>
<Span Text="Only our MaxLines differ. Otherwise we're the same. Please tap us!">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" />
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Label AutomationId="label6" HorizontalOptions="Start" MaxLines="6" BackgroundColor="LightGoldenrodYellow" WidthRequest="100">
<Label.FormattedText>
<FormattedString>
<Span Text="Only our MaxLines differ. Otherwise we're the same. Please tap us!">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" />
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Label AutomationId="label7" HorizontalOptions="Start" MaxLines="5" BackgroundColor="LightGoldenrodYellow" WidthRequest="100">
<Label.FormattedText>
<FormattedString>
<Span Text="Only our MaxLines differ. Otherwise we're the same. Please tap us!">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" />
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 21837, "Span's TapGestureRecognizer not working if text is truncated", PlatformAffected.All)]

public partial class Issue21837 : ContentPage
{
private int _tapCount;
public int TapCount
{
get => _tapCount;
set
{
_tapCount = value;
OnPropertyChanged();
}
}

public Command TapCommand { get; set; }

public Issue21837()
{
InitializeComponent();
TapCommand = new Command(() => ++TapCount);
BindingContext = this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ internal static void RecalculateSpanPositions(this UILabel control, Label elemen
var yaxis = startRect.Top;
var lineHeights = new List<double>();

while ((endRect.Bottom - yaxis) > 0.001)
while (Math.Max(endRect.Bottom, finalSize.Bottom) - yaxis > 0.001)
{
double lineHeight;
if (yaxis == startRect.Top) // First Line
Expand All @@ -197,8 +197,14 @@ internal static void RecalculateSpanPositions(this UILabel control, Label elemen
}
else // Bottom Line
{
lineHeight = endRect.Bottom - endRect.Top;
lineHeight = Math.Max(endRect.Bottom - endRect.Top, finalSize.Bottom - finalSize.Top);
}

if (lineHeight == 0)
{
break;
}

lineHeights.Add(lineHeight);
yaxis += (float)lineHeight;
}
Expand All @@ -211,7 +217,7 @@ internal static void RecalculateSpanPositions(this UILabel control, Label elemen
}
else
{
((ISpatialElement)span).Region = Region.FromLines(lineHeights.ToArray(), finalSize.Width, startRect.X, endRect.X, startRect.Top).Inflate(5);
((ISpatialElement)span).Region = Region.FromLines(lineHeights.ToArray(), finalSize.Width, startRect.X, Math.Max(endRect.X, finalSize.Width - startRect.X), startRect.Top).Inflate(5);
}

// Update current location
Expand Down
29 changes: 29 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue21837.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue21837 : _IssuesUITest
{
public Issue21837(TestDevice device) : base(device)
{
}

public override string Issue => "Span's TapGestureRecognizer not working if text is truncated";

[Test]
public void TapsShouldBeCorrectlyRecognized()
{
int numberOfTappableLabels = 7;

_ = App.WaitForElement("label1");

for (int i = 0; i < numberOfTappableLabels; i++)
App.Click($"label{i + 1}");

var resultText = App.FindElement("resultLabel").GetText();
Assert.AreEqual(resultText, $"Number of recognized taps: {numberOfTappableLabels}");
}
}
}

0 comments on commit eefd4b0

Please sign in to comment.