diff --git a/components/RichSuggestBox/src/RichSuggestBox.Suggestion.cs b/components/RichSuggestBox/src/RichSuggestBox.Suggestion.cs index 53c56b1cd..1abe0f705 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 053303611..a32d13968 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); diff --git a/components/RichSuggestBox/tests/Test_RichSuggestBox.cs b/components/RichSuggestBox/tests/Test_RichSuggestBox.cs index f7b3e699e..1f828e857 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;