Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder results on continued query instead of retaining original order #220

Merged
merged 1 commit into from
Oct 18, 2023
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
3 changes: 2 additions & 1 deletion Pinpoint.Plugin.AppSearch/AppSearchPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class AppSearchPlugin : AbstractPlugin

public override PluginManifest Manifest { get; } = new("App Search")
{
Description = "Search for installed apps. Type an app name or an abbreviation thereof.\n\nExamples: \"visual studio code\", \"vsc\""
Description = "Search for installed apps. Type an app name or an abbreviation thereof.\n\nExamples: \"visual studio code\", \"vsc\"",
Priority = PluginPriority.High,
};

public override async Task<bool> Initialize()
Expand Down
48 changes: 34 additions & 14 deletions Pinpoint.Win/ViewModels/ObservableUniqueCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Pinpoint.Win.ViewModels
{
public class ObservableUniqueCollection<T> : ObservableCollection<T>
{
private readonly HashSet<T> _hashSet = new();
private readonly Dictionary<T, int> _items = new();

public void AddRange(IEnumerable<T> items)
{
Expand All @@ -25,46 +25,66 @@ public bool TryAdd(T item)

protected override void InsertItem(int index, T item)
{
if (_hashSet.Add(item))
if (_items.ContainsKey(item))
{
base.InsertItem(index, item);
return;
}

_items[item] = index;
base.InsertItem(index, item);
}

protected override void ClearItems()
{
base.ClearItems();
_hashSet.Clear();
_items.Clear();
}

protected override void RemoveItem(int index)
{
var item = this[index];
_hashSet.Remove(item);
_items.Remove(this[index]);
base.RemoveItem(index);
}

protected override void SetItem(int index, T item)
{
if (_hashSet.Add(item))
if (_items.ContainsKey(item))
{
var oldItem = this[index];
_hashSet.Remove(oldItem);
base.SetItem(index, item);
return;
}

_items.Remove(this[index]);
_items[item] = index;

base.SetItem(index, item);
}

public void RemoveWhere(Predicate<T> predicate)
public void RemoveWhere(Predicate<T> filter)
{
for (var i = Count - 1; i >= 0; i--)
{
if (!predicate(this[i]))
if (filter(this[i]))
{
continue;
RemoveItem(i);
}
}
}

RemoveItem(i);
public new bool Contains(T item)
{
return _items.ContainsKey(item);
}

public new bool Remove(T item) {
if (!_items.ContainsKey(item))
{
return false;
}

_items.Remove(item);
base.Remove(item);

return true;
}
}
}
11 changes: 8 additions & 3 deletions Pinpoint.Win/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ private void TxtQuery_TextChanged(object sender, System.Windows.Controls.TextCha

if (!Model.PreviousQuery.Equals(query))
{
_ = Dispatcher.Invoke(async () => await UpdateResults());
_ = Dispatcher.Invoke(UpdateResults);
}
}

Expand Down Expand Up @@ -495,18 +495,23 @@ private async Task UpdateResults()
results.Add(result);
}

Model.Results.RemoveWhere(r => !results.Contains(r));

var shortcutKey = 0;
foreach (var result in results)
{
if (Model.Results.Contains(result))
{
Model.Results.Remove(result);
}

// If one of first 9 results, set keyboard shortcut for result
if (Model.Results.TryAdd(result) && shortcutKey < 9)
{
result.Shortcut = "CTRL+" + ++shortcutKey;
}
}

Model.Results.RemoveWhere(r => !results.Contains(r));

if (Model.Results.Count > 0 && LstResults.SelectedIndex == -1)
{
LstResults.SelectedIndex = 0;
Expand Down