diff --git a/components/TokenizingTextBox/src/TokenizingTextBoxItem.AutoSuggestBox.cs b/components/TokenizingTextBox/src/TokenizingTextBoxItem.AutoSuggestBox.cs index 305d91cc..dfedfc4d 100644 --- a/components/TokenizingTextBox/src/TokenizingTextBoxItem.AutoSuggestBox.cs +++ b/components/TokenizingTextBox/src/TokenizingTextBoxItem.AutoSuggestBox.cs @@ -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() is TextBox textBox) + { + // Keep the template's ThemeResource expressions active across local theme changes. + VisualStateManager.GoToState(textBox, currentTokens >= maxTokens ? TokenizingTextBox.PART_MaxReachedState : "Default", false); + } } } diff --git a/components/TokenizingTextBox/tests/Test_TokenizingTextBox_General.cs b/components/TokenizingTextBox/tests/Test_TokenizingTextBox_General.cs index 3848bedd..69987080 100644 --- a/components/TokenizingTextBox/tests/Test_TokenizingTextBox_General.cs +++ b/components/TokenizingTextBox/tests/Test_TokenizingTextBox_General.cs @@ -4,6 +4,7 @@ using CommunityToolkit.Tests; using CommunityToolkit.WinUI.Controls; +using Windows.UI; namespace TokenizingTextBoxTests; @@ -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( +@" + + + + + +") 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()