Skip to content

Commit

Permalink
Modified the code changes and included UI test
Browse files Browse the repository at this point in the history
  • Loading branch information
NirmalKumarYuvaraj committed Nov 20, 2024
1 parent 6fea74e commit 181f706
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24977.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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.Issue24977"
Title="Issue 24977">

<Grid RowDefinitions="50, 50, *, 50" Margin="30">
<Entry Text="Content before" AutomationId="EntryBefore" FontSize="Large" ReturnType="Next" BackgroundColor="Aquamarine" Grid.Row="0" />
<Label x:Name="CursorHeightTracker" Text="0" AutomationId="CursorHeightTracker" FontSize="Large" BackgroundColor="Aquamarine" Grid.Row="1" />
<Editor x:Name="editor" Text="Hello World!" AutomationId="IssueEditor" FontSize="Large" BackgroundColor="Orange" Grid.Row="2" VerticalTextAlignment="Center" TextChanged="Editor_TextChanged" />
<Button Text="Erase" Clicked="Button_Clicked" Grid.Row="3"/>
</Grid>

</ContentPage>
71 changes: 71 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24977.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Microsoft.Maui.Platform;

namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 24977, "Keyboard Scrolling in editors with Center or End VerticalTextAlignment is off", PlatformAffected.iOS)]
public partial class Issue24977 : ContentPage
{
public Issue24977()
{
InitializeComponent();
}

private void Button_Clicked(object sender, EventArgs e)
{
editor.Text = string.Empty;
}

private void Editor_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is Editor editor)
{
AddCursorHeightToLabel(editor);
}
}

void AddCursorHeightToLabel (Editor editor)
{
#if IOS
var textInput = editor.Handler.PlatformView as UIKit.UITextView;
var selectedTextRange = textInput?.SelectedTextRange;
var localCursor = selectedTextRange is not null ? textInput?.GetCaretRectForPosition(selectedTextRange.Start) : null;

if (localCursor is CoreGraphics.CGRect local && textInput is not null)
{
var container = GetContainerView(textInput);
var cursorInContainer = container.ConvertRectFromView(local, textInput);
var cursorInWindow = container.ConvertRectToView(cursorInContainer, null);

CursorHeightTracker.Text = cursorInWindow.Y.ToString();

}

}

UIKit.UIView GetContainerView(UIKit.UIView startingPoint)
{
var rootView = FindResponder<Microsoft.Maui.Platform.ContainerViewController>(startingPoint)?.View;

if (rootView is not null)
{
return rootView;
}

return null;
}

T FindResponder<T>(UIKit.UIView view) where T : UIKit.UIResponder
{
var nextResponder = view as UIKit.UIResponder;
while (nextResponder is not null)
{
nextResponder = nextResponder.NextResponder;

if (nextResponder is T responder)
return responder;
}
return null;
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#if IOS
using System.Drawing;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using OpenQA.Selenium.Appium.Interactions;
using OpenQA.Selenium.Appium.MultiTouch;
using OpenQA.Selenium.Interactions;
using UITest.Appium;
using UITest.Core;
using System.Text;
using OpenQA.Selenium;

namespace Microsoft.Maui.TestCases.Tests.Issues;
public class Issue24977 : _IssuesUITest
{
public Issue24977(TestDevice device) : base(device) { }

public override string Issue => "Keyboard Scrolling in editors with Center or End VerticalTextAlignment is off";

[Test]
[Category(UITestCategories.Editor)]
public void KeepEditorCursorAboveKeyboardWithVerticalAlignmentAsCenter()
{
var app = App as AppiumApp;
if (app is null)
{
return;
}

var editorRect = app.WaitForElement("IssueEditor").GetRect();
app.Click("IssueEditor");

var sb = new StringBuilder();
for (int i = 1; i <= 15; i++)
{
sb.Append($"\n{i}");
}

app.EnterText("IssueEditor", sb.ToString());

var keyboardLocation = KeyboardScrolling.FindiOSKeyboardLocation(app.Driver);

var cursorLabel = app.WaitForElement("CursorHeightTracker").GetText();

var cursorHeight1 = Convert.ToDouble(cursorLabel);

if (keyboardLocation is Point keyboardPoint)
{
Assert.That(cursorHeight1 < keyboardPoint.Y);
}
else
{
Assert.Fail("keyboardLocation is null");
}
}
}
#endif
22 changes: 6 additions & 16 deletions src/Core/src/Platform/iOS/MauiTextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,17 @@ void OnChanged(object? sender, EventArgs e)

void ShouldCenterVertically()
{
var contentHeight = ContentSize.Height;
var contentHeight = ContentSize.Height - TextContainerInset.Top - TextContainerInset.Bottom;
var availableSpace = Bounds.Height - contentHeight * ZoomScale;
if (availableSpace <= 0)
return;
ContentOffset = VerticalTextAlignment switch

TextContainerInset = VerticalTextAlignment switch
{
Maui.TextAlignment.Center => new CGPoint(0, -Math.Max(1, availableSpace / 2)),
Maui.TextAlignment.End => new CGPoint(0, -Math.Max(1, availableSpace)),
_ => ContentOffset,
Maui.TextAlignment.Center => new UIEdgeInsets((nfloat)Math.Max(1, availableSpace / 2), TextContainerInset.Left, TextContainerInset.Bottom, TextContainerInset.Right),
Maui.TextAlignment.End => new UIEdgeInsets((nfloat)Math.Max(1, availableSpace), TextContainerInset.Left, TextContainerInset.Bottom, TextContainerInset.Right),
_ => TextContainerInset,
};

// Scroll the content to the cursor position if it is hidden by the keyboard
if (KeyboardAutoManagerScroll.IsKeyboardShowing)
    {
       var cursorRect = KeyboardAutoManagerScroll.FindCursorPosition();
       if (cursorRect.HasValue && cursorRect.Value.Bottom > KeyboardAutoManagerScroll.KeyboardFrame.Top)
       {
         var offset = cursorRect.Value.Bottom - KeyboardAutoManagerScroll.KeyboardFrame.Top;
         ContentOffset = new CGPoint(ContentOffset.X, ContentOffset.Y + offset);
       }
     }
}

void UpdatePlaceholderFont(UIFont? value)
Expand Down

0 comments on commit 181f706

Please sign in to comment.