Skip to content

Commit

Permalink
Fixed crash when removing keys from key combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
OronDF343 committed Jan 9, 2016
1 parent d12b869 commit ac8eda5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void RemoveItem(object[] context)
if (context[0] == null || context[1] == null) return;
var set = context[0] as ObservableHashSet<Key>;
var set2 = context[0] as ObservableCollection<string>;
foreach (var item in ((IList)context[1]).OfType<Key>().ToList()) set?.Remove(item);
foreach (var item in ((IList)context[1]).OfType<Key>().ToList()) set?.RemoveSlow(item);
foreach (var item in ((IList)context[1]).OfType<string>().ToList()) set2?.Remove(item);
}
}
Expand Down
19 changes: 18 additions & 1 deletion FuwaTea.Lib/Collections/ObservableHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;

Expand Down Expand Up @@ -144,11 +145,27 @@ public void CopyTo(T[] array, int arrayIndex)
_hashSet.CopyTo(array, arrayIndex);
}

/// <summary>
/// Has the correct CollectionChanged event. Good for when the event handler is slow but the <see cref="ObservableHashSet{T}"/> contains few items.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool RemoveSlow(T item)
{
CheckLock();
var index = _hashSet.ToList().IndexOf(item);
if (index < 0) return false;
_hashSet.Remove(item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
OnPropertyChanged(nameof(Count));
return true;
}

public bool Remove(T item)
{
CheckLock();
if (!_hashSet.Remove(item)) return false;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
OnPropertyChanged(nameof(Count));
return true;
}
Expand Down

0 comments on commit ac8eda5

Please sign in to comment.