Skip to content
Merged
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
33 changes: 18 additions & 15 deletions src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public partial class ListViewModel : PageViewModel, IDisposable

public string SearchText { get; private set; } = string.Empty;

public string InitialSearchText { get; private set; } = string.Empty;

public CommandItemViewModel EmptyContent { get; private set; }

private bool _isDynamic;
Expand Down Expand Up @@ -128,15 +130,15 @@ private void FetchItems()

try
{
IListItem[] newItems = _model.Unsafe!.GetItems();
var newItems = _model.Unsafe!.GetItems();

// Collect all the items into new viewmodels
Collection<ListItemViewModel> newViewModels = [];

// TODO we can probably further optimize this by also keeping a
// HashSet of every ExtensionObject we currently have, and only
// building new viewmodels for the ones we haven't already built.
foreach (IListItem? item in newItems)
foreach (var item in newItems)
{
ListItemViewModel viewModel = new(item, new(this));

Expand All @@ -147,8 +149,8 @@ private void FetchItems()
}
}

IEnumerable<ListItemViewModel> firstTwenty = newViewModels.Take(20);
foreach (ListItemViewModel? item in firstTwenty)
var firstTwenty = newViewModels.Take(20);
foreach (var item in firstTwenty)
{
item?.SafeInitializeProperties();
}
Expand Down Expand Up @@ -233,7 +235,7 @@ private void InitializeItemsTask(CancellationToken ct)
iterable = Items.ToArray();
}

foreach (ListItemViewModel item in iterable)
foreach (var item in iterable)
{
ct.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -266,8 +268,8 @@ private static int ScoreListItem(string query, CommandItemViewModel listItem)
return 1;
}

MatchResult nameMatch = StringMatcher.FuzzySearch(query, listItem.Title);
MatchResult descriptionMatch = StringMatcher.FuzzySearch(query, listItem.Subtitle);
var nameMatch = StringMatcher.FuzzySearch(query, listItem.Title);
var descriptionMatch = StringMatcher.FuzzySearch(query, listItem.Subtitle);
return new[] { nameMatch.Score, (descriptionMatch.Score - 4) / 2, 0 }.Max();
}

Expand All @@ -280,7 +282,7 @@ private struct ScoredListItemViewModel
// Similarly stolen from ListHelpers.FilterList
public static IEnumerable<ListItemViewModel> FilterList(IEnumerable<ListItemViewModel> items, string query)
{
IOrderedEnumerable<ScoredListItemViewModel> scores = items
var scores = items
.Where(i => !i.IsInErrorState)
.Select(li => new ScoredListItemViewModel() { ViewModel = li, Score = ScoreListItem(query, li) })
.Where(score => score.Score > 0)
Expand Down Expand Up @@ -359,7 +361,7 @@ public override void InitializeProperties()
{
base.InitializeProperties();

IListPage? model = _model.Unsafe;
var model = _model.Unsafe;
if (model == null)
{
return; // throw?
Expand All @@ -373,8 +375,9 @@ public override void InitializeProperties()
_modelPlaceholderText = model.PlaceholderText;
UpdateProperty(nameof(PlaceholderText));

SearchText = model.SearchText;
InitialSearchText = SearchText = model.SearchText;
UpdateProperty(nameof(SearchText));
UpdateProperty(nameof(InitialSearchText));

EmptyContent = new(new(model.EmptyContent), PageContext);
EmptyContent.SlowInitializeProperties();
Expand All @@ -385,7 +388,7 @@ public override void InitializeProperties()

public void LoadMoreIfNeeded()
{
IListPage? model = this._model.Unsafe;
var model = this._model.Unsafe;
if (model == null)
{
return;
Expand All @@ -412,7 +415,7 @@ protected override void FetchProperty(string propertyName)
{
base.FetchProperty(propertyName);

IListPage? model = this._model.Unsafe;
var model = this._model.Unsafe;
if (model == null)
{
return; // throw?
Expand Down Expand Up @@ -475,21 +478,21 @@ protected override void UnsafeCleanup()

lock (_listLock)
{
foreach (ListItemViewModel item in Items)
foreach (var item in Items)
{
item.SafeCleanup();
}

Items.Clear();
foreach (ListItemViewModel item in FilteredItems)
foreach (var item in FilteredItems)
{
item.SafeCleanup();
}

FilteredItems.Clear();
}

IListPage? model = _model.Unsafe;
var model = _model.Unsafe;
if (model != null)
{
model.ItemsChanged -= Model_ItemsChanged;
Expand Down
31 changes: 22 additions & 9 deletions src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,31 @@ private void FilterBox_TextChanged(object sender, TextChangedEventArgs e)
private void Page_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var property = e.PropertyName;
if (CurrentPageViewModel is ListViewModel list &&
property == nameof(ListViewModel.SearchText))

if (CurrentPageViewModel is ListViewModel list)
{
// Only if the text actually changed...
// (sometimes this triggers on a round-trip of the SearchText)
if (FilterBox.Text != list.SearchText)
if (property == nameof(ListViewModel.SearchText))
{
// ... Update our displayed text, and...
FilterBox.Text = list.SearchText;
// Only if the text actually changed...
// (sometimes this triggers on a round-trip of the SearchText)
if (FilterBox.Text != list.SearchText)
{
// ... Update our displayed text, and...
FilterBox.Text = list.SearchText;

// ... Move the cursor to the end of the input
FilterBox.Select(FilterBox.Text.Length, 0);
// ... Move the cursor to the end of the input
FilterBox.Select(FilterBox.Text.Length, 0);
}
}
else if (property == nameof(ListViewModel.InitialSearchText))
{
// GH #38712:
// The ListPage will notify us of the `InitialSearchText` when
// we first load the viewmodel. We can use that as an
// opportunity to immediately select the search text. That lets
// the user start typing a new search without manually
// selecting the old one.
SelectSearch();
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading