Skip to content

Commit

Permalink
fix: Fix AutoSuggestBox QueryIcon size
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jun 5, 2024
1 parent 3b918ce commit 7d9c63d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,40 @@

using static Private.Infrastructure.TestServices;
using static Microsoft.UI.Xaml.Controls.AutoSuggestionBoxTextChangeReason;
using SamplesApp.UITests;
using Uno.UI.RuntimeTests.Helpers;
using Windows.Foundation;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
{
[TestClass]
[RunsOnUIThread]
public class Given_AutoSuggestBox
{
[TestMethod]
[UnoWorkItem("https://github.com/unoplatform/uno/issues/15662")]
public async Task When_SymbolIcon_Verify_Size()
{
var SUT = new AutoSuggestBox()
{
QueryIcon = new SymbolIcon(Symbol.Home),
};

await UITestHelper.Load(SUT);

#if __SKIA__ || __WASM__
var tb = SUT.FindChildren<TextBlock>().Single(tb => tb.Text.Length == 1 && tb.Text[0] == (char)Symbol.Home);
#else
var tb = SUT.EnumerateAllChildren(c => c is TextBlock tb && tb.Text.Length == 1 && tb.Text[0] == (char)Symbol.Home).Single();
#endif

Assert.AreEqual(12, tb.FontSize);

var tbBounds = tb.GetAbsoluteBounds();

Assert.AreEqual(new Size(12, 12), new Size(tbBounds.Width, tbBounds.Height));
}

#if !WINAPPSDK // GetTemplateChild is protected in UWP while public in Uno.
[TestMethod]
public async Task When_Text_Changed_Should_Reflect_In_DataTemplate_TextBox()
Expand Down
9 changes: 7 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/AutoSuggestBox/AutoSuggestBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,13 @@ private void UpdateQueryButton()
return;
}

_queryButton.Content = QueryIcon;
_queryButton.Visibility = QueryIcon == null ? Visibility.Collapsed : Visibility.Visible;
var queryIcon = QueryIcon;
if (queryIcon is SymbolIcon symbolIcon)
{
symbolIcon.SetFontSize(0);
}
_queryButton.Content = queryIcon;
_queryButton.Visibility = queryIcon == null ? Visibility.Collapsed : Visibility.Visible;
}

private void UpdateTextBox()
Expand Down
20 changes: 18 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/Icons/SymbolIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.UI.Xaml.Controls;
/// </summary>
public sealed partial class SymbolIcon : IconElement
{
private const double DefaultFontSize = 20.0;
private double _fontSize = 20.0;

private readonly TextBlock _textBlock;

Expand Down Expand Up @@ -63,7 +63,12 @@ private void SynchronizeProperties()
_textBlock.TextAlignment = Microsoft.UI.Xaml.TextAlignment.Center;
_textBlock.HorizontalAlignment = HorizontalAlignment.Stretch;
_textBlock.VerticalAlignment = VerticalAlignment.Center;
_textBlock.FontSize = DefaultFontSize;

if (_fontSize > 0)
{
_textBlock.FontSize = _fontSize;
}

_textBlock.FontStyle = FontStyle.Normal;
_textBlock.FontFamily = GetSymbolFontFamily();
_textBlock.IsTextScaleFactorEnabled = false;
Expand All @@ -73,6 +78,17 @@ private void SynchronizeProperties()
_textBlock.Foreground = Foreground;
}

internal void SetFontSize(double fontSize)
{
_fontSize = fontSize;
if (fontSize == 0)
{
_textBlock.ClearValue(TextBlock.FontSizeProperty);
}

InvalidateMeasure();
}

private void SetSymbolText() => _textBlock.Text = new string((char)Symbol, 1);

private static FontFamily GetSymbolFontFamily() =>
Expand Down

0 comments on commit 7d9c63d

Please sign in to comment.