Skip to content

Commit

Permalink
Fix ClearButtonVisibility and VerticalTextAlignment issues with invis…
Browse files Browse the repository at this point in the history
…ible Entry on Windows (#13769)
  • Loading branch information
jsuarezruiz committed Mar 9, 2023
1 parent 569b057 commit 9fa5f5f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Core/src/Handlers/Entry/EntryHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ protected override void ConnectHandler(TextBox platformView)
{
platformView.KeyUp += OnPlatformKeyUp;
platformView.TextChanged += OnPlatformTextChanged;
platformView.Loaded += OnPlatformLoaded;
platformView.SizeChanged += OnPlatformViewSizeChanged;
}

protected override void DisconnectHandler(TextBox platformView)
{
platformView.Loaded -= OnPlatformLoaded;
platformView.SizeChanged -= OnPlatformViewSizeChanged;
platformView.KeyUp -= OnPlatformKeyUp;
platformView.TextChanged -= OnPlatformTextChanged;

Expand Down Expand Up @@ -139,8 +139,8 @@ void OnPlatformSelectionChanged(object sender, RoutedEventArgs e)
if (VirtualView.SelectionLength != selectedTextLength)
VirtualView.SelectionLength = selectedTextLength;
}

void OnPlatformLoaded(object sender, RoutedEventArgs e) =>
void OnPlatformViewSizeChanged(object sender, SizeChangedEventArgs e) =>
MauiTextBox.InvalidateAttachedProperties(PlatformView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Automation.Provider;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
Expand All @@ -16,6 +18,56 @@ namespace Microsoft.Maui.DeviceTests
{
public partial class EntryHandlerTests
{
[Theory(DisplayName = "VerticalTextAlignment Works Correctly with hidden Entry")]
[InlineData(TextAlignment.Start)]
[InlineData(TextAlignment.Center)]
[InlineData(TextAlignment.End)]
public async Task VerticalTextAlignmentWorksCorrectlyWithHiddenEntry(TextAlignment textAlignment)
{
var layout = new LayoutStub();

var entry = new EntryStub
{
Text = "Test",
Visibility = Visibility.Collapsed,
VerticalTextAlignment = textAlignment,
};

var button = new ButtonStub
{
Text = "Change TextAlignment"
};

layout.Add(entry);
layout.Add(button);

var clicked = false;

button.Clicked += delegate
{
entry.Visibility = Visibility.Visible;
clicked = true;
};

await PerformClick(button);

Assert.True(clicked);

var platformAlignment = GetNativeVerticalTextAlignment(textAlignment);

// Attach for windows because it uses control templates
var values = await GetValueAsync(entry, (handler) =>
handler.PlatformView.AttachAndRun(() =>
new
{
ViewValue = entry.VerticalTextAlignment,
PlatformViewValue = GetNativeVerticalTextAlignment(handler)
}));

Assert.Equal(textAlignment, values.ViewValue);
Assert.Equal(platformAlignment, values.PlatformViewValue);
}

[Theory(DisplayName = "MaxLength Works Correctly")]
[InlineData("123")]
[InlineData("Hello")]
Expand Down Expand Up @@ -121,5 +173,19 @@ int GetNativeCursorPosition(EntryHandler entryHandler) =>

int GetNativeSelectionLength(EntryHandler entryHandler) =>
GetNativeEntry(entryHandler).SelectionLength;

Button GetNativeButton(ButtonHandler buttonHandler) =>
buttonHandler.PlatformView;

Task PerformClick(IButton button)
{
return InvokeOnMainThreadAsync(() =>
{
var platformButton = GetNativeButton(CreateHandler<ButtonHandler>(button));
var ap = new ButtonAutomationPeer(platformButton);
var ip = ap.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
ip?.Invoke();
});
}
}
}

0 comments on commit 9fa5f5f

Please sign in to comment.