Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix AutoSuggestBox QueryIcon size #15677

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading