Skip to content

Commit

Permalink
Merge pull request unoplatform#15677 from Youssef1313/asb-query-fontsize
Browse files Browse the repository at this point in the history
fix: Fix AutoSuggestBox QueryIcon size
  • Loading branch information
Youssef1313 authored Jun 6, 2024
2 parents 37b9d69 + ed2f412 commit 59d9d05
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,55 @@

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

#if __IOS__
using UIKit;
#elif __MACOS__
using AppKit;
#endif

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
{
[TestClass]
[RunsOnUIThread]
public class Given_AutoSuggestBox
{
#if !WINAPPSDK
[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 = (TextBlock)SUT.EnumerateAllChildren().SingleOrDefault(c => c is TextBlock textBlock && textBlock.Text.Length == 1 && textBlock.Text[0] == (char)Symbol.Home);
#endif

Assert.AreEqual(12, tb.FontSize);

var tbBounds = tb.GetAbsoluteBounds();

#if __WASM__
Assert.AreEqual(new Size(13, 12), new Size(tbBounds.Width, tbBounds.Height));
#elif __ANDROID__
Assert.AreEqual(new Size(12, 14), new Size(tbBounds.Width, tbBounds.Height));
#else
// 12, 12 is the right behavior here.
Assert.AreEqual(new Size(12, 12), new Size(tbBounds.Width, tbBounds.Height));
#endif
}
#endif

#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 59d9d05

Please sign in to comment.