Skip to content

Commit

Permalink
Ensure no duplicate results are shown
Browse files Browse the repository at this point in the history
  • Loading branch information
dkgv committed Oct 27, 2020
1 parent 3b09313 commit a708d89
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Pinpoint.Plugin.Everything/API/DefaultSearchConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class DefaultSearchConfig : ISearchConfig

public bool MatchCase { get; set; } = false;

public uint MaxResults { get; set; } = 16;
public uint MaxResults { get; set; } = 12;

public RequestFlag RequestFlag { get; set; } = RequestFlag.FileName | RequestFlag.Path | RequestFlag.Extension;

Expand Down
33 changes: 32 additions & 1 deletion Pinpoint.Plugin/AbstractQueryResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System;
using System.Drawing;

namespace Pinpoint.Plugin
{
Expand Down Expand Up @@ -28,5 +29,35 @@ protected AbstractQueryResult(string title, string subtitle = "")
/// Fired when result is selected (double-clicked or when "ENTER" is pressed) from list.
/// </summary>
public abstract void OnSelect();

protected bool Equals(AbstractQueryResult other)
{
return Title == other.Title && Subtitle == other.Subtitle && Equals(Instance, other.Instance);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((AbstractQueryResult) obj);
}

public override int GetHashCode()
{
return HashCode.Combine(Title, Subtitle, Instance);
}
}
}
2 changes: 1 addition & 1 deletion Pinpoint.Plugin/AppConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public static class AppConstants
public static readonly string SettingsFilePath = MainDirectory + "settings.json";
public static readonly string HotkeyIdentifier = "Show/Hide";

public const string Version = "0.0.1";
public const string Version = "0.0.2";
}
}
5 changes: 2 additions & 3 deletions Pinpoint.Win/Models/MainWindowModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Collections.ObjectModel;
using Pinpoint.Plugin;
using Pinpoint.Plugin;

namespace Pinpoint.Win.Models
{
internal class MainWindowModel : BaseWindowModel
{
private ThemeModel _theme = AppSettings.GetAsOrDefault("theme", ThemeModel.DarkTheme);

public ObservableCollection<AbstractQueryResult> Results { get; } = new ObservableCollection<AbstractQueryResult>();
public ObservableUniqueCollection<AbstractQueryResult> Results { get; } = new ObservableUniqueCollection<AbstractQueryResult>();

public ThemeModel Theme
{
Expand Down
62 changes: 62 additions & 0 deletions Pinpoint.Win/Models/ObservableUniqueCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Pinpoint.Win.Models
{
public class ObservableUniqueCollection<T> : ObservableCollection<T>
{
private readonly HashSet<T> _hashSet;

public ObservableUniqueCollection() : this(EqualityComparer<T>.Default)
{
}

public ObservableUniqueCollection(IEqualityComparer<T> equalityComparer) => _hashSet = new HashSet<T>(equalityComparer);

public void AddRange(IEnumerable<T> items)
{
foreach (var item in items)
{
InsertItem(Count, item);
}
}

public bool TryAdd(T item)
{
var previous = Count;
InsertItem(Count, item);
return Count > previous;
}

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

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

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

protected override void SetItem(int index, T item)
{
if (_hashSet.Add(item))
{
var oldItem = this[index];
_hashSet.Remove(oldItem);
base.SetItem(index, item);
}
}
}
}
5 changes: 4 additions & 1 deletion Pinpoint.Win/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
Margin="40 0 0 0"
x:Name="TxtQuery"
HorizontalAlignment="Left"
Watermark="Pinpoint Search"
Watermark="Pinpoint"
BorderThickness="0"
KeepWatermarkOnGotFocus="True"
FontSize="26"
Expand Down Expand Up @@ -171,6 +171,9 @@
<TextBlock Foreground="{Binding DataContext.Theme.TxtQueryForeground, ElementName=MainWindowName}"
Grid.Row="0"
Grid.Column="1"
MaxWidth="450"
HorizontalAlignment="Left"
TextTrimming="WordEllipsis"
Text="{Binding Title}"
FontSize="15" FontWeight="SemiBold"/>

Expand Down
7 changes: 4 additions & 3 deletions Pinpoint.Win/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,13 @@ private async Task UpdateResults()

await foreach(var result in _pluginEngine.Process(query, _cts.Token))
{
if (shortcutIndex < 9)
var didAdd = Model.Results.TryAdd(result);

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

Model.Results.Add(result);
}

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

0 comments on commit a708d89

Please sign in to comment.