From 3158c9e1f1e217c4c4617ab660bfa08e6a8ebe70 Mon Sep 17 00:00:00 2001 From: azchohfi <527713+azchohfi@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:03:16 +0000 Subject: [PATCH 1/2] Add RichSuggestBox unload popup regression test Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- .../tests/Test_RichSuggestBox.cs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/components/RichSuggestBox/tests/Test_RichSuggestBox.cs b/components/RichSuggestBox/tests/Test_RichSuggestBox.cs index f7b3e699..1f828e85 100644 --- a/components/RichSuggestBox/tests/Test_RichSuggestBox.cs +++ b/components/RichSuggestBox/tests/Test_RichSuggestBox.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using CommunityToolkit.Common.Deferred; using CommunityToolkit.Tests; using CommunityToolkit.WinUI.Controls; @@ -312,6 +313,62 @@ await App.DispatcherQueue.EnqueueAsync(async () => }); } + [TestCategory(nameof(RichSuggestBox))] + [TestMethod] + [DataRow(true, DisplayName = "Popup open, request pending")] + [DataRow(false, DisplayName = "Popup closed, request pending")] + public async Task Test_RichSuggestBox_UnloadClosesSuggestionPopup(bool openPopupBeforeUnload) + { + await App.DispatcherQueue.EnqueueAsync(async () => + { + var rsb = new RichSuggestBox() + { + Prefixes = "@", + ItemsSource = new List { "Ada", "Alan", "Grace", "Linus" }, + }; + + var deferRequests = !openPopupBeforeUnload; + EventDeferral? pendingDeferral = null; + rsb.SuggestionRequested += (sender, args) => + { + if (deferRequests) + { + pendingDeferral = args.GetDeferral(); + } + }; + + await LoadTestContentAsync(rsb); + var popup = (rsb.FindDescendant("SuggestionsPopup") as Popup)!; + Assert.IsNotNull(popup, "Suggestion popup template part was not found."); + + var selection = rsb.TextDocument!.Selection; + if (openPopupBeforeUnload) + { + selection.TypeText("@"); + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + Assert.IsTrue(popup.IsOpen, "Suggestion popup did not open for the initial query."); + + deferRequests = true; + selection.TypeText("A"); + } + else + { + selection.TypeText("@"); + } + + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + Assert.IsNotNull(pendingDeferral, "Suggestion request was not left pending."); + Assert.AreEqual(openPopupBeforeUnload, popup.IsOpen, "Unexpected popup state while the request is pending."); + + await UnloadTestContentAsync(rsb); + Assert.IsFalse(popup.IsOpen, "Suggestion popup is still open after the control was unloaded."); + + pendingDeferral!.Complete(); + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + Assert.IsFalse(popup.IsOpen, "Suggestion popup reopened after a pending request completed on an unloaded control."); + }); + } + private static void AssertToken(RichSuggestBox rsb, RichSuggestToken token) { var document = rsb.TextDocument; From d7dfbc92021c1feed1d35719ba186d61f56d444d Mon Sep 17 00:00:00 2001 From: azchohfi <527713+azchohfi@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:05:30 +0000 Subject: [PATCH 2/2] Close RichSuggestBox suggestion popup on unload Cancel the pending suggestion request and close the popup when the control unloads, and do not reopen the popup from a request that was superseded or cancelled while awaiting SuggestionRequested. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- .../RichSuggestBox/src/RichSuggestBox.Suggestion.cs | 3 ++- components/RichSuggestBox/src/RichSuggestBox.cs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/components/RichSuggestBox/src/RichSuggestBox.Suggestion.cs b/components/RichSuggestBox/src/RichSuggestBox.Suggestion.cs index 53c56b1c..1abe0f70 100644 --- a/components/RichSuggestBox/src/RichSuggestBox.Suggestion.cs +++ b/components/RichSuggestBox/src/RichSuggestBox.Suggestion.cs @@ -60,7 +60,8 @@ private async Task RequestSuggestionsAsync(ITextRange? range = null) } } - if (!eventArgs.Cancel) + // The request may have been superseded or the control unloaded while awaiting. + if (!eventArgs.Cancel && !cancellationToken.IsCancellationRequested) { _suggestionChoice = 0; ShowSuggestionsPopup(_suggestionsList?.Items?.Count > 0); diff --git a/components/RichSuggestBox/src/RichSuggestBox.cs b/components/RichSuggestBox/src/RichSuggestBox.cs index 05330361..a32d1396 100644 --- a/components/RichSuggestBox/src/RichSuggestBox.cs +++ b/components/RichSuggestBox/src/RichSuggestBox.cs @@ -64,6 +64,7 @@ public RichSuggestBox() RegisterPropertyChangedCallback(PopupCornerRadiusProperty, OnCornerRadiusChanged); LostFocus += OnLostFocus; Loaded += OnLoaded; + Unloaded += OnUnloaded; } /// @@ -267,6 +268,17 @@ private void OnLoaded(object sender, RoutedEventArgs e) _scrollViewer = _richEditBox?.FindDescendant()!; } + private void OnUnloaded(object sender, RoutedEventArgs e) + { + var tokenSource = _currentQuery?.CancellationTokenSource; + if (!(tokenSource?.IsCancellationRequested ?? true)) + { + tokenSource.Cancel(); + } + + ShowSuggestionsPopup(false); + } + private void OnLostFocus(object sender, RoutedEventArgs e) { ShowSuggestionsPopup(false);