Skip to content

Commit a2b3880

Browse files
committed
rework SuggestionsRequested to support AdvancedCollectionView
1 parent 9195720 commit a2b3880

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/RichSuggestBox/RichSuggestBox.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@ public partial class RichSuggestBox : ItemsControl
5050
private Border _suggestionsContainer;
5151

5252
private int _suggestionChoice;
53-
private string _currentQuery;
54-
private string _currentPrefix;
5553
private bool _ignoreChange;
5654
private bool _popupOpenDown;
5755
private bool _textCompositionActive;
58-
private ITextRange _currentRange;
56+
private RichSuggestQuery _currentQuery;
5957
private CancellationTokenSource _suggestionRequestedCancellationSource;
6058

6159
/// <summary>
@@ -70,7 +68,6 @@ public RichSuggestBox()
7068

7169
DefaultStyleKey = typeof(RichSuggestBox);
7270

73-
RegisterPropertyChangedCallback(ItemsSourceProperty, ItemsSource_PropertyChanged);
7471
RegisterPropertyChangedCallback(CornerRadiusProperty, OnCornerRadiusChanged);
7572
RegisterPropertyChangedCallback(PopupCornerRadiusProperty, OnCornerRadiusChanged);
7673
LostFocus += OnLostFocus;
@@ -405,12 +402,6 @@ private void RichEditBox_SizeChanged(object sender, SizeChangedEventArgs e)
405402
this.UpdatePopupOffset();
406403
}
407404

408-
private void ItemsSource_PropertyChanged(DependencyObject sender, DependencyProperty dp)
409-
{
410-
_suggestionChoice = 0;
411-
ShowSuggestionsPopup(_suggestionsList?.Items?.Count > 0);
412-
}
413-
414405
private async void RichEditBox_Paste(object sender, TextControlPasteEventArgs e)
415406
{
416407
Paste?.Invoke(this, e);
@@ -501,12 +492,13 @@ private async Task RequestSuggestionsAsync(ITextRange range = null)
501492
{
502493
string prefix;
503494
string query;
495+
var currentQuery = _currentQuery;
504496
var queryFound = range == null
505497
? TryExtractQueryFromSelection(out prefix, out query, out range)
506498
: TryExtractQueryFromRange(range, out prefix, out query);
507499

508-
if (queryFound && prefix == _currentPrefix && query == _currentQuery &&
509-
range.EndPosition == _currentRange?.EndPosition && _suggestionPopup.IsOpen)
500+
if (queryFound && prefix == currentQuery?.Prefix && query == currentQuery?.QueryText &&
501+
range.EndPosition == currentQuery?.Range.EndPosition && _suggestionPopup.IsOpen)
510502
{
511503
return;
512504
}
@@ -516,9 +508,7 @@ private async Task RequestSuggestionsAsync(ITextRange range = null)
516508

517509
if (queryFound)
518510
{
519-
_currentPrefix = prefix;
520-
_currentQuery = query;
521-
_currentRange = range;
511+
_currentQuery = new RichSuggestQuery { Prefix = prefix, QueryText = query, Range = range };
522512

523513
if (SuggestionsRequested != null)
524514
{
@@ -532,7 +522,13 @@ private async Task RequestSuggestionsAsync(ITextRange range = null)
532522
}
533523
catch (OperationCanceledException)
534524
{
535-
// pass
525+
return;
526+
}
527+
528+
if (!eventArgs.Cancel)
529+
{
530+
_suggestionChoice = 0;
531+
ShowSuggestionsPopup(_suggestionsList?.Items?.Count > 0);
536532
}
537533
}
538534
}
@@ -544,10 +540,11 @@ private async Task RequestSuggestionsAsync(ITextRange range = null)
544540

545541
internal async Task CommitSuggestionAsync(object selectedItem)
546542
{
547-
var range = _currentRange?.GetClone();
543+
var currentQuery = _currentQuery;
544+
var range = currentQuery?.Range.GetClone();
548545
var id = Guid.NewGuid();
549-
var prefix = _currentPrefix;
550-
var query = _currentQuery;
546+
var prefix = currentQuery?.Prefix;
547+
var query = currentQuery?.QueryText;
551548

552549
// range has length of 0 at the end of the commit.
553550
// Checking length == 0 to avoid committing twice.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Windows.UI.Text;
2+
3+
namespace Microsoft.Toolkit.Uwp.UI.Controls
4+
{
5+
/// <summary>
6+
/// A structure for <see cref="RichSuggestBox"/> to keep track of the current query internally.
7+
/// </summary>
8+
internal class RichSuggestQuery
9+
{
10+
public string Prefix { get; set; }
11+
12+
public string QueryText { get; set; }
13+
14+
public ITextRange Range { get; set; }
15+
}
16+
}

Microsoft.Toolkit.Uwp.UI.Controls.Input/RichSuggestBox/SuggestionsRequestedEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
99
/// <summary>
1010
/// Provide data for <see cref="RichSuggestBox.SuggestionsRequested"/> event.
1111
/// </summary>
12-
public class SuggestionsRequestedEventArgs : DeferredEventArgs
12+
public class SuggestionsRequestedEventArgs : DeferredCancelEventArgs
1313
{
1414
/// <summary>
1515
/// Gets or sets the prefix character used for the query.

0 commit comments

Comments
 (0)