Use reference count in action manager.
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
namespace Cryville.Crtr.Browsing.Actions {
|
||||
public class ActionManager {
|
||||
readonly Dictionary<Type, List<IResourceAction>> _actions = new Dictionary<Type, List<IResourceAction>>();
|
||||
readonly Dictionary<IResourceAction, int> _refCounts = new Dictionary<IResourceAction, int>();
|
||||
|
||||
public event Action Changed;
|
||||
|
||||
@@ -23,6 +24,8 @@ namespace Cryville.Crtr.Browsing.Actions {
|
||||
int index = actions.BinarySearch(action, ActionPriorityComparer.Instance);
|
||||
if (index < 0) index = ~index;
|
||||
actions.Insert(index, action);
|
||||
if (_refCounts.ContainsKey(action)) _refCounts[action]++;
|
||||
else _refCounts[action] = 0;
|
||||
Changed?.Invoke();
|
||||
}
|
||||
public void Register(IResourceAction action) {
|
||||
@@ -35,6 +38,7 @@ namespace Cryville.Crtr.Browsing.Actions {
|
||||
public void Unregister(Type type, IResourceAction action) {
|
||||
List<IResourceAction> actions;
|
||||
if (!_actions.TryGetValue(type, out actions)) return;
|
||||
if (--_refCounts[action] > 0) return;
|
||||
actions.Remove(action);
|
||||
Changed?.Invoke();
|
||||
}
|
||||
|
Reference in New Issue
Block a user