Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix ClearButtonVisibility and VerticalTextAlignment issues with invisible Entry #13769

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalidate the MauiTextBox attached properties when the size changed (loaded, change visibility etc)

}

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();
});
}
}
}