Skip to content

Commit

Permalink
Merge pull request unoplatform#13810 from Youssef1313/issues/4502
Browse files Browse the repository at this point in the history
fix: Match TextBox.VerticalContentAlignment with Windows
  • Loading branch information
Youssef1313 authored Apr 18, 2024
2 parents e0fdfd1 + 5190bec commit a3c179e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 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 WINAPPSDK
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);
}
}
}
22 changes: 3 additions & 19 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,24 +1158,9 @@ 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);
// TODO: Remove as a breaking change for Uno 6
// Also, make OnVerticalContentAlignmentChanged private protected.
protected override void OnVerticalContentAlignmentChanged(VerticalAlignment oldVerticalContentAlignment, VerticalAlignment newVerticalContentAlignment) { }

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

0 comments on commit a3c179e

Please sign in to comment.