Skip to content

Commit

Permalink
fix: Match TextBox.VerticalContentAlignment with Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Apr 7, 2024
1 parent e74305b commit 5364b4b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.UI.RuntimeTests.Helpers;
using FluentAssertions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using static Private.Infrastructure.TestServices;
using Microsoft.UI.Xaml;
using Windows.UI;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MUXControlsTestApp.Utilities;
using System.Runtime.InteropServices;
using Uno.UI.Helpers;
using Uno.UI.RuntimeTests.Helpers;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI;

using static Private.Infrastructure.TestServices;

#if WINAPPSDK
using Uno.UI.Extensions;
Expand Down Expand Up @@ -853,5 +857,58 @@ public async Task When_GotFocus_BringIntoView()

Assert.AreEqual(0, SUT.VerticalOffset);
}

[TestMethod]
public async Task When_VerticalContentAlignment_Is_Changed()
{
// This test ensures that setting VerticalContentAlignment on the TextBox doesn't flow to:
// 1) PlaceholderTextContentPresenter.VerticalContentAlignment
// 2) ContentElement.VerticalContentAlignment
// 3) PlaceholderTextContentPresenter.VerticalAlignment
// 4) ContentElement.VerticalAlignment
// This matches the behavior observed on Windows
var xaml = """
<Grid>
<Grid.Resources>
<Style x:Key="TextBoxAlignmentTestStyle" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid x:Name="RootGrid">
<Grid VerticalAlignment="Stretch">
<ContentControl x:Name="PlaceholderTextContentPresenter" />
<ContentControl x:Name="ContentElement" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<TextBox Style="{StaticResource TextBoxAlignmentTestStyle}" />
</Grid>
""";
var grid = XamlHelper.LoadXaml<Grid>(xaml);
WindowHelper.WindowContent = grid;
await WindowHelper.WaitForLoaded(grid);
var textBox = (TextBox)grid.Children.Single();

Assert.AreEqual(VerticalAlignment.Center, textBox.VerticalContentAlignment);
textBox.VerticalContentAlignment = VerticalAlignment.Bottom;

#if WINDOWS_UWP
var getTemplateChild = typeof(Control).GetMethod("GetTemplateChild", BindingFlags.Instance | BindingFlags.NonPublic);
var placeHolder = (ContentControl)getTemplateChild.Invoke(textBox, new object[] { "PlaceholderTextContentPresenter" });
var contentElement = (ContentControl)getTemplateChild.Invoke(textBox, new object[] { "ContentElement" });
#else
var placeHolder = (ContentControl)textBox.GetTemplateChild("PlaceholderTextContentPresenter");
var contentElement = (ContentControl)textBox.GetTemplateChild("ContentElement");
#endif
Assert.AreEqual(VerticalAlignment.Top, placeHolder.VerticalContentAlignment);
Assert.AreEqual(VerticalAlignment.Top, contentElement.VerticalContentAlignment);
Assert.AreEqual(VerticalAlignment.Stretch, placeHolder.VerticalAlignment);
Assert.AreEqual(VerticalAlignment.Stretch, contentElement.VerticalAlignment);
}
}
}
20 changes: 0 additions & 20 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ private void InitializeProperties()
OnTextAlignmentChanged(TextAlignment);
OnTextWrappingChanged();
OnFocusStateChanged((FocusState)FocusStateProperty.GetMetadata(GetType()).DefaultValue, FocusState, initial: true);
OnVerticalContentAlignmentChanged(VerticalAlignment.Top, VerticalContentAlignment);
OnTextCharacterCasingChanged(CharacterCasing);
UpdateDescriptionVisibility(true);
var buttonRef = _deleteButton?.GetTarget();
Expand Down Expand Up @@ -1159,25 +1158,6 @@ public void OnTemplateRecycled()

public override string GetAccessibilityInnerText() => Text;

protected override void OnVerticalContentAlignmentChanged(VerticalAlignment oldVerticalContentAlignment, VerticalAlignment newVerticalContentAlignment)
{
base.OnVerticalContentAlignmentChanged(oldVerticalContentAlignment, newVerticalContentAlignment);

if (_contentElement != null)
{
_contentElement.VerticalContentAlignment = newVerticalContentAlignment;
}

if (_placeHolder != null)
{
_placeHolder.VerticalAlignment = newVerticalContentAlignment;
}

OnVerticalContentAlignmentChangedPartial(oldVerticalContentAlignment, newVerticalContentAlignment);
}

partial void OnVerticalContentAlignmentChangedPartial(VerticalAlignment oldVerticalContentAlignment, VerticalAlignment newVerticalContentAlignment);

public void Select(int start, int length)
{
if (start < 0)
Expand Down

0 comments on commit 5364b4b

Please sign in to comment.