Skip to content
Draft
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
3 changes: 2 additions & 1 deletion components/RichSuggestBox/src/RichSuggestBox.Suggestion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions components/RichSuggestBox/src/RichSuggestBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public RichSuggestBox()
RegisterPropertyChangedCallback(PopupCornerRadiusProperty, OnCornerRadiusChanged);
LostFocus += OnLostFocus;
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}

/// <summary>
Expand Down Expand Up @@ -267,6 +268,17 @@ private void OnLoaded(object sender, RoutedEventArgs e)
_scrollViewer = _richEditBox?.FindDescendant<ScrollViewer>()!;
}

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);
Expand Down
57 changes: 57 additions & 0 deletions components/RichSuggestBox/tests/Test_RichSuggestBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<string> { "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;
Expand Down