From 5364b4b12ec52bc7358ac9aa91f0535e35f75167 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Mon, 25 Sep 2023 21:32:53 +0300 Subject: [PATCH 1/3] fix: Match TextBox.VerticalContentAlignment with Windows --- .../Windows_UI_Xaml_Controls/Given_TextBox.cs | 71 +++++++++++++++++-- .../UI/Xaml/Controls/TextBox/TextBox.cs | 20 ------ 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs index 3d01e867b66a..7090201dce37 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs @@ -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; @@ -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 = """ + + + + + + + + """; + var grid = XamlHelper.LoadXaml(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); + } } } diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs index 19043dfd4bb6..55e417189afe 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs @@ -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(); @@ -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) From 90b111889b1d0671043740e62491756716646679 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Sun, 7 Apr 2024 14:04:27 +0200 Subject: [PATCH 2/3] chore: Adjust --- .../Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs index 7090201dce37..21d0f41568a2 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs @@ -897,7 +897,7 @@ public async Task When_VerticalContentAlignment_Is_Changed() Assert.AreEqual(VerticalAlignment.Center, textBox.VerticalContentAlignment); textBox.VerticalContentAlignment = VerticalAlignment.Bottom; -#if WINDOWS_UWP +#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" }); From 5190becf406fe2912b9ef4e27587498bc184abe2 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Tue, 9 Apr 2024 15:00:22 +0200 Subject: [PATCH 3/3] chore: Adjust --- src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs index 55e417189afe..aba68f462d43 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs @@ -1158,6 +1158,10 @@ public void OnTemplateRecycled() public override string GetAccessibilityInnerText() => Text; + // 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) { if (start < 0)