Fix frame end task race condition in ScrollPanelWidget

This commit is contained in:
Curtis Shmyr
2016-11-13 07:09:47 +00:00
committed by Paul Chote
parent 9d7413ab3d
commit 33e1a6b2dd
5 changed files with 60 additions and 41 deletions

View File

@@ -16,11 +16,11 @@ namespace OpenRA.Primitives
{ {
public interface IObservableCollection public interface IObservableCollection
{ {
event Action<object> OnAdd; event Action<IObservableCollection, object> OnAdd;
event Action<object> OnRemove; event Action<IObservableCollection, object> OnRemove;
event Action<int> OnRemoveAt; event Action<IObservableCollection, int> OnRemoveAt;
event Action<object, object> OnSet; event Action<IObservableCollection, object, object> OnSet;
event Action OnRefresh; event Action<IObservableCollection> OnRefresh;
IEnumerable ObservedItems { get; } IEnumerable ObservedItems { get; }
} }
} }

View File

@@ -18,15 +18,15 @@ namespace OpenRA.Primitives
{ {
public class ObservableCollection<T> : Collection<T>, IObservableCollection public class ObservableCollection<T> : Collection<T>, IObservableCollection
{ {
public event Action<object> OnAdd = k => { }; public event Action<IObservableCollection, object> OnAdd = (x, k) => { };
// TODO Workaround for https://github.com/OpenRA/OpenRA/issues/6101 // TODO Workaround for https://github.com/OpenRA/OpenRA/issues/6101
#pragma warning disable 67 #pragma warning disable 67
public event Action<object> OnRemove = k => { }; public event Action<IObservableCollection, object> OnRemove = (x, k) => { };
#pragma warning restore #pragma warning restore
public event Action<int> OnRemoveAt = i => { }; public event Action<IObservableCollection, int> OnRemoveAt = (x, i) => { };
public event Action<object, object> OnSet = (o, n) => { }; public event Action<IObservableCollection, object, object> OnSet = (x, o, n) => { };
public event Action OnRefresh = () => { }; public event Action<IObservableCollection> OnRefresh = x => { };
public ObservableCollection() { } public ObservableCollection() { }
public ObservableCollection(IList<T> list) : base(list) { } public ObservableCollection(IList<T> list) : base(list) { }
@@ -35,25 +35,25 @@ namespace OpenRA.Primitives
{ {
var old = this[index]; var old = this[index];
base.SetItem(index, item); base.SetItem(index, item);
OnSet(old, item); OnSet(this, old, item);
} }
protected override void InsertItem(int index, T item) protected override void InsertItem(int index, T item)
{ {
base.InsertItem(index, item); base.InsertItem(index, item);
OnAdd(item); OnAdd(this, item);
} }
protected override void ClearItems() protected override void ClearItems()
{ {
base.ClearItems(); base.ClearItems();
OnRefresh(); OnRefresh(this);
} }
protected override void RemoveItem(int index) protected override void RemoveItem(int index)
{ {
base.RemoveItem(index); base.RemoveItem(index);
OnRemoveAt(index); OnRemoveAt(this, index);
} }
public IEnumerable ObservedItems public IEnumerable ObservedItems

View File

@@ -33,19 +33,19 @@ namespace OpenRA.Primitives
{ {
protected IDictionary<TKey, TValue> innerDict; protected IDictionary<TKey, TValue> innerDict;
public event Action<object> OnAdd = k => { }; public event Action<IObservableCollection, object> OnAdd = (x, k) => { };
public event Action<object> OnRemove = k => { }; public event Action<IObservableCollection, object> OnRemove = (x, k) => { };
// TODO Workaround for https://github.com/OpenRA/OpenRA/issues/6101 // TODO Workaround for https://github.com/OpenRA/OpenRA/issues/6101
#pragma warning disable 67 #pragma warning disable 67
public event Action<int> OnRemoveAt = i => { }; public event Action<IObservableCollection, int> OnRemoveAt = (x, i) => { };
public event Action<object, object> OnSet = (o, n) => { }; public event Action<IObservableCollection, object, object> OnSet = (x, o, n) => { };
#pragma warning restore #pragma warning restore
public event Action OnRefresh = () => { }; public event Action<IObservableCollection> OnRefresh = x => { };
protected void FireOnRefresh() protected void FireOnRefresh()
{ {
OnRefresh(); OnRefresh(this);
} }
protected ObservableDictionary() { } protected ObservableDictionary() { }
@@ -58,14 +58,14 @@ namespace OpenRA.Primitives
public virtual void Add(TKey key, TValue value) public virtual void Add(TKey key, TValue value)
{ {
innerDict.Add(key, value); innerDict.Add(key, value);
OnAdd(key); OnAdd(this, key);
} }
public bool Remove(TKey key) public bool Remove(TKey key)
{ {
var found = innerDict.Remove(key); var found = innerDict.Remove(key);
if (found) if (found)
OnRemove(key); OnRemove(this, key);
return found; return found;
} }
@@ -91,7 +91,7 @@ namespace OpenRA.Primitives
public void Clear() public void Clear()
{ {
innerDict.Clear(); innerDict.Clear();
OnRefresh(); OnRefresh(this);
} }
public int Count public int Count

View File

@@ -19,19 +19,19 @@ namespace OpenRA.Primitives
{ {
protected IList<T> innerList; protected IList<T> innerList;
public event Action<object> OnAdd = k => { }; public event Action<IObservableCollection, object> OnAdd = (x, k) => { };
public event Action<object> OnRemove = k => { }; public event Action<IObservableCollection, object> OnRemove = (x, k) => { };
// TODO Workaround for https://github.com/OpenRA/OpenRA/issues/6101 // TODO Workaround for https://github.com/OpenRA/OpenRA/issues/6101
#pragma warning disable 67 #pragma warning disable 67
public event Action<int> OnRemoveAt = i => { }; public event Action<IObservableCollection, int> OnRemoveAt = (x, i) => { };
public event Action<object, object> OnSet = (o, n) => { }; public event Action<IObservableCollection, object, object> OnSet = (x, o, n) => { };
#pragma warning restore #pragma warning restore
public event Action OnRefresh = () => { }; public event Action<IObservableCollection> OnRefresh = x => { };
protected void FireOnRefresh() protected void FireOnRefresh()
{ {
OnRefresh(); OnRefresh(this);
} }
public ObservableList() public ObservableList()
@@ -42,14 +42,14 @@ namespace OpenRA.Primitives
public virtual void Add(T item) public virtual void Add(T item)
{ {
innerList.Add(item); innerList.Add(item);
OnAdd(item); OnAdd(this, item);
} }
public bool Remove(T item) public bool Remove(T item)
{ {
var found = innerList.Remove(item); var found = innerList.Remove(item);
if (found) if (found)
OnRemove(item); OnRemove(this, item);
return found; return found;
} }
@@ -57,13 +57,13 @@ namespace OpenRA.Primitives
public void Clear() public void Clear()
{ {
innerList.Clear(); innerList.Clear();
OnRefresh(); OnRefresh(this);
} }
public void Insert(int index, T item) public void Insert(int index, T item)
{ {
innerList.Insert(index, item); innerList.Insert(index, item);
OnRefresh(); OnRefresh(this);
} }
public int Count { get { return innerList.Count; } } public int Count { get { return innerList.Count; } }
@@ -73,7 +73,7 @@ namespace OpenRA.Primitives
public void RemoveAt(int index) public void RemoveAt(int index)
{ {
innerList.RemoveAt(index); innerList.RemoveAt(index);
OnRemoveAt(index); OnRemoveAt(this, index);
} }
public T this[int index] public T this[int index]
@@ -87,7 +87,7 @@ namespace OpenRA.Primitives
{ {
var oldValue = innerList[index]; var oldValue = innerList[index];
innerList[index] = value; innerList[index] = value;
OnSet(oldValue, value); OnSet(this, oldValue, value);
} }
} }

View File

@@ -374,9 +374,15 @@ namespace OpenRA.Mods.Common.Widgets
}); });
} }
void BindingAdd(object item) void BindingAdd(IObservableCollection col, object item)
{ {
Game.RunAfterTick(() => BindingAddImpl(item)); Game.RunAfterTick(() =>
{
if (collection != col)
return;
BindingAddImpl(item);
});
} }
void BindingAddImpl(object item) void BindingAddImpl(object item)
@@ -393,30 +399,40 @@ namespace OpenRA.Mods.Common.Widgets
ScrollToBottom(); ScrollToBottom();
} }
void BindingRemove(object item) void BindingRemove(IObservableCollection col, object item)
{ {
Game.RunAfterTick(() => Game.RunAfterTick(() =>
{ {
if (collection != col)
return;
var widget = Children.FirstOrDefault(w => widgetItemEquals(w, item)); var widget = Children.FirstOrDefault(w => widgetItemEquals(w, item));
if (widget != null) if (widget != null)
RemoveChild(widget); RemoveChild(widget);
}); });
} }
void BindingRemoveAt(int index) void BindingRemoveAt(IObservableCollection col, int index)
{ {
Game.RunAfterTick(() => Game.RunAfterTick(() =>
{ {
if (collection != col)
return;
if (index < 0 || index >= Children.Count) if (index < 0 || index >= Children.Count)
return; return;
RemoveChild(Children[index]); RemoveChild(Children[index]);
}); });
} }
void BindingSet(object oldItem, object newItem) void BindingSet(IObservableCollection col, object oldItem, object newItem)
{ {
Game.RunAfterTick(() => Game.RunAfterTick(() =>
{ {
if (collection != col)
return;
var newWidget = makeWidget(newItem); var newWidget = makeWidget(newItem);
newWidget.Parent = this; newWidget.Parent = this;
@@ -433,10 +449,13 @@ namespace OpenRA.Mods.Common.Widgets
}); });
} }
void BindingRefresh() void BindingRefresh(IObservableCollection col)
{ {
Game.RunAfterTick(() => Game.RunAfterTick(() =>
{ {
if (collection != col)
return;
RemoveChildren(); RemoveChildren();
foreach (var item in collection.ObservedItems) foreach (var item in collection.ObservedItems)
BindingAddImpl(item); BindingAddImpl(item);