From 7b371f8d2d7de95f2f920fec051a9ff78af7e848 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 1 Apr 2016 22:33:22 +0200 Subject: [PATCH] Give PriorityLevel a reference to its PriorityValue. And directly call the LevelValueChanged method on it rather than using callbacks. --- src/Perspex.Base/PriorityLevel.cs | 32 ++++--------- src/Perspex.Base/PriorityValue.cs | 80 ++++++++++++------------------- 2 files changed, 40 insertions(+), 72 deletions(-) diff --git a/src/Perspex.Base/PriorityLevel.cs b/src/Perspex.Base/PriorityLevel.cs index 0ca18e45bd..e0d096a835 100644 --- a/src/Perspex.Base/PriorityLevel.cs +++ b/src/Perspex.Base/PriorityLevel.cs @@ -31,34 +31,22 @@ namespace Perspex /// internal class PriorityLevel { - /// - /// Method called when current value changes. - /// - private readonly Action _changed; - - /// - /// The current direct value. - /// + private PriorityValue _owner; private object _directValue; - - /// - /// The index of the next . - /// private int _nextIndex; /// /// Initializes a new instance of the class. /// + /// The owner. /// The priority. - /// The precedence mode. - /// A method to be called when the current value changes. public PriorityLevel( - int priority, - Action changed) + PriorityValue owner, + int priority) { - Contract.Requires(changed != null); + Contract.Requires(owner != null); - _changed = changed; + _owner = owner; Priority = priority; Value = _directValue = PerspexProperty.UnsetValue; ActiveBindingIndex = -1; @@ -83,7 +71,7 @@ namespace Perspex set { Value = _directValue = value; - _changed(this); + _owner.LevelValueChanged(this); } } @@ -141,7 +129,7 @@ namespace Perspex { Value = entry.Value; ActiveBindingIndex = entry.Index; - _changed(this); + _owner.LevelValueChanged(this); } else { @@ -175,14 +163,14 @@ namespace Perspex { Value = binding.Value; ActiveBindingIndex = binding.Index; - _changed(this); + _owner.LevelValueChanged(this); return; } } Value = DirectValue; ActiveBindingIndex = -1; - _changed(this); + _owner.LevelValueChanged(this); } } } diff --git a/src/Perspex.Base/PriorityValue.cs b/src/Perspex.Base/PriorityValue.cs index 5a1ae7a4e0..50dbc5c0f7 100644 --- a/src/Perspex.Base/PriorityValue.cs +++ b/src/Perspex.Base/PriorityValue.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reactive.Subjects; using System.Text; using Perspex.Logging; using Perspex.Utilities; @@ -21,34 +20,15 @@ namespace Perspex /// priority binding that doesn't return . Where there /// are multiple bindings registered with the same priority, the most recently added binding /// has a higher priority. Each time the value changes, the - /// method on the + /// method on the /// owner object is fired with the old and new values. /// internal class PriorityValue { - /// - /// The owner of the object. - /// private readonly IPriorityValueOwner _owner; - - /// - /// The value type. - /// private readonly Type _valueType; - - /// - /// The currently registered bindings organised by priority. - /// private readonly Dictionary _levels = new Dictionary(); - - /// - /// The current value. - /// private object _value; - - /// - /// The function used to validate the value, if any. - /// private readonly Func _validate; /// @@ -169,6 +149,34 @@ namespace Perspex return b.ToString(); } + /// + /// Called when the value for a priority level changes. + /// + /// The priority level of the changed entry. + public void LevelValueChanged(PriorityLevel level) + { + if (level.Priority <= ValuePriority) + { + if (level.Value != PerspexProperty.UnsetValue) + { + UpdateValue(level.Value, level.Priority); + } + else + { + foreach (var i in _levels.Values.OrderBy(x => x.Priority)) + { + if (i.Value != PerspexProperty.UnsetValue) + { + UpdateValue(i.Value, i.Priority); + return; + } + } + + UpdateValue(PerspexProperty.UnsetValue, int.MaxValue); + } + } + } + /// /// Causes a revalidation of the value. /// @@ -197,7 +205,7 @@ namespace Perspex if (!_levels.TryGetValue(priority, out result)) { - result = new PriorityLevel(priority, ValueChanged); + result = new PriorityLevel(this, priority); _levels.Add(priority, result); } @@ -238,33 +246,5 @@ namespace Perspex value.GetType()); } } - - /// - /// Called when the value for a priority level changes. - /// - /// The priority level of the changed entry. - private void ValueChanged(PriorityLevel level) - { - if (level.Priority <= ValuePriority) - { - if (level.Value != PerspexProperty.UnsetValue) - { - UpdateValue(level.Value, level.Priority); - } - else - { - foreach (var i in _levels.Values.OrderBy(x => x.Priority)) - { - if (i.Value != PerspexProperty.UnsetValue) - { - UpdateValue(i.Value, i.Priority); - return; - } - } - - UpdateValue(PerspexProperty.UnsetValue, int.MaxValue); - } - } - } } }