Skip to content
Open
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 @@ -395,9 +395,11 @@ void OnTokenCountChanged(TokenizingTextBox ttb, object? value = null)
maxTokensCounter.Text = $"{currentTokens}/{maxTokens}";
maxTokensCounter.Visibility = Visibility.Visible;

maxTokensCounter.Foreground = (currentTokens >= maxTokens)
? (SolidColorBrush)Application.Current.Resources["SystemFillColorCriticalBrush"]
: (SolidColorBrush)Application.Current.Resources["TextFillColorSecondaryBrush"];
if (maxTokensCounter.FindAscendant<TextBox>() is TextBox textBox)
{
// Keep the template's ThemeResource expressions active across local theme changes.
VisualStateManager.GoToState(textBox, currentTokens >= maxTokens ? TokenizingTextBox.PART_MaxReachedState : "Default", false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using CommunityToolkit.Tests;
using CommunityToolkit.WinUI.Controls;
using Windows.UI;

namespace TokenizingTextBoxTests;

Expand Down Expand Up @@ -158,6 +159,96 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
});
}

[TestCategory("Test_TokenizingTextBox_General")]
[DataTestMethod]
[DataRow(0)]
[DataRow(3)]
public async Task Test_TokenCounterForegroundFollowsLocalTheme(int initialTokens)
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(
@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:CommunityToolkit.WinUI.Controls"">
<StackPanel>
<controls:TokenizingTextBox x:Name=""tokenboxname"" MaximumTokens=""3""
PlaceholderText=""Tokens"" Width=""400"" RequestedTheme=""Light"" />
<TextBlock x:Name=""secondaryProbe"" Text=""Secondary""
Foreground=""{ThemeResource TextFillColorSecondaryBrush}"" RequestedTheme=""Light"" />
<TextBlock x:Name=""criticalProbe"" Text=""Critical""
Foreground=""{ThemeResource SystemFillColorCriticalBrush}"" RequestedTheme=""Light"" />
</StackPanel>
</Page>") as FrameworkElement;

Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
await LoadTestContentAsync(treeRoot);

var tokenBox = treeRoot.FindChild("tokenboxname") as TokenizingTextBox;
var secondaryProbe = treeRoot.FindChild("secondaryProbe") as TextBlock;
var criticalProbe = treeRoot.FindChild("criticalProbe") as TextBlock;

Assert.IsNotNull(tokenBox, "Could not find TokenizingTextBox in tree.");
Assert.IsNotNull(secondaryProbe, "Could not find the secondary theme resource probe.");
Assert.IsNotNull(criticalProbe, "Could not find the critical theme resource probe.");
Assert.AreEqual(ElementTheme.Light, secondaryProbe.ActualTheme);
Assert.AreEqual(ElementTheme.Light, criticalProbe.ActualTheme);

var lightSecondary = ((SolidColorBrush)secondaryProbe.Foreground).Color;
var lightCritical = ((SolidColorBrush)criticalProbe.Foreground).Color;

tokenBox.RequestedTheme = ElementTheme.Dark;
secondaryProbe.RequestedTheme = ElementTheme.Dark;
criticalProbe.RequestedTheme = ElementTheme.Dark;
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });

Assert.AreEqual(ElementTheme.Dark, secondaryProbe.ActualTheme);
Assert.AreEqual(ElementTheme.Dark, criticalProbe.ActualTheme);
var darkSecondary = ((SolidColorBrush)secondaryProbe.Foreground).Color;
var darkCritical = ((SolidColorBrush)criticalProbe.Foreground).Color;

Assert.AreNotEqual(lightSecondary, darkSecondary, "The secondary brush oracle must distinguish Light and Dark.");
Assert.AreNotEqual(lightCritical, darkCritical, "The critical brush oracle must distinguish Light and Dark.");

for (var i = 0; i < initialTokens; i++)
{
tokenBox.AddTokenItem($"Token {i}");
}

if (initialTokens == tokenBox.MaximumTokens)
{
await AssertCounterThemesAsync(criticalProbe, lightCritical, darkCritical, "3/3");
await tokenBox.ClearAsync();
}

await AssertCounterThemesAsync(secondaryProbe, lightSecondary, darkSecondary, "0/3");

async Task AssertCounterThemesAsync(TextBlock probe, Color lightColor, Color darkColor, string expectedText)
{
foreach (var theme in new[] { ElementTheme.Light, ElementTheme.Dark, ElementTheme.Light })
{
tokenBox.RequestedTheme = theme;
secondaryProbe.RequestedTheme = theme;
criticalProbe.RequestedTheme = theme;
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });

var counter = tokenBox.FindDescendant("PART_TokensCounter") as TextBlock;
Assert.IsNotNull(counter, "Could not find the native token counter.");
Assert.AreEqual(theme, tokenBox.ActualTheme);
Assert.AreEqual(theme, counter.ActualTheme);
Assert.AreEqual(theme, probe.ActualTheme);
Assert.AreEqual(Visibility.Visible, counter.Visibility);
Assert.AreEqual(expectedText, counter.Text);

var expectedColor = theme == ElementTheme.Light ? lightColor : darkColor;
Assert.AreEqual(expectedColor, ((SolidColorBrush)probe.Foreground).Color, "The element-local probe did not follow the requested theme.");
Assert.AreEqual(expectedColor, ((SolidColorBrush)counter.Foreground).Color, $"Counter {expectedText} did not follow the local {theme} theme.");
}
}
});
}

[TestCategory("Test_TokenizingTextBox_General")]
[TestMethod]
public async Task Test_SetInitialText()
Expand Down
Loading