diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index aa436f5f4e..4e777b36ed 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -68,7 +68,7 @@ namespace Avalonia.Animation /// /// The value fill mode for this animation. /// - public FillMode FillMode { get; set; } + public FillMode FillMode { get; set; } /// /// Easing function to be used. @@ -80,10 +80,10 @@ namespace Avalonia.Animation this.CollectionChanged += delegate { _isChildrenChanged = true; }; } - private void InterpretKeyframes() + private IList InterpretKeyframes(Animatable control) { - var handlerList = new List<(Type, AvaloniaProperty)>(); - var kfList = new List(); + var handlerList = new List<(Type type, AvaloniaProperty property)>(); + var animatorKeyFrames = new List(); foreach (var keyframe in this) { @@ -99,41 +99,38 @@ namespace Avalonia.Animation if (!handlerList.Contains((handler, setter.Property))) handlerList.Add((handler, setter.Property)); - var newKF = new AnimatorKeyFrame() + var cue = keyframe.Cue; + + if (keyframe.TimingMode == KeyFrameTimingMode.TimeSpan) { - Handler = handler, - Property = setter.Property, - Cue = keyframe.Cue, - KeyTime = keyframe.KeyTime, - timeSpanSet = keyframe.timeSpanSet, - cueSet = keyframe.cueSet, - Value = setter.Value - }; - - kfList.Add(newKF); + cue = new Cue(keyframe.KeyTime.Ticks / Duration.Ticks); + } + + var newKF = new AnimatorKeyFrame(handler, cue); + + _subscription.Add(newKF.BindSetter(setter, control)); + + animatorKeyFrames.Add(newKF); } } - var newAnimatorInstances = new List<(Type handler, AvaloniaProperty prop, IAnimator inst)>(); + var newAnimatorInstances = new List(); - foreach (var handler in handlerList) + foreach (var (handlerType, property) in handlerList) { - var newInstance = (IAnimator)Activator.CreateInstance(handler.Item1); - newInstance.Property = handler.Item2; - newAnimatorInstances.Add((handler.Item1, handler.Item2, newInstance)); + var newInstance = (IAnimator)Activator.CreateInstance(handlerType); + newInstance.Property = property; + newAnimatorInstances.Add(newInstance); } - foreach (var kf in kfList) + foreach (var keyframe in animatorKeyFrames) { - var parent = newAnimatorInstances.Where(p => p.handler == kf.Handler && - p.prop == kf.Property) - .First(); - parent.inst.Add(kf); + var animator = newAnimatorInstances.First(a => a.GetType() == keyframe.AnimatorType && + a.Property == keyframe.Property); + animator.Add(keyframe); } - foreach(var instance in newAnimatorInstances) - _animators.Add(instance.inst); - + return newAnimatorInstances; } /// @@ -150,17 +147,11 @@ namespace Avalonia.Animation /// public IDisposable Apply(Animatable control, IObservable matchObs) { - if (_isChildrenChanged) - { - InterpretKeyframes(); - _isChildrenChanged = false; - } - - foreach (IAnimator keyframes in _animators) + foreach (IAnimator animator in InterpretKeyframes(control)) { - _subscription.Add(keyframes.Apply(this, control, matchObs)); + _subscription.Add(animator.Apply(this, control, matchObs)); } return this; } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/AnimatorKeyFrame.cs b/src/Avalonia.Animation/AnimatorKeyFrame.cs index 02457cb9aa..bd9c7a0184 100644 --- a/src/Avalonia.Animation/AnimatorKeyFrame.cs +++ b/src/Avalonia.Animation/AnimatorKeyFrame.cs @@ -4,6 +4,8 @@ using System.Text; using System.ComponentModel; using Avalonia.Metadata; using Avalonia.Collections; +using Avalonia.Data; +using Avalonia.Reactive; namespace Avalonia.Animation { @@ -11,13 +13,63 @@ namespace Avalonia.Animation /// Defines a KeyFrame that is used for /// objects. /// - public class AnimatorKeyFrame + public class AnimatorKeyFrame : AvaloniaObject { - public Type Handler; - public Cue Cue; - public TimeSpan KeyTime; - internal bool timeSpanSet, cueSet; - public AvaloniaProperty Property; - public object Value; + public static readonly DirectProperty ValueProperty = + AvaloniaProperty.RegisterDirect(nameof(Value), k => k._value, (k, v) => k._value = v); + + public AnimatorKeyFrame() + { + + } + + public AnimatorKeyFrame(Type animatorType, Cue cue) + { + AnimatorType = animatorType; + Cue = cue; + } + + public Type AnimatorType { get; } + public Cue Cue { get; } + public AvaloniaProperty Property { get; private set; } + + private object _value; + + public object Value + { + get => _value; + set => SetAndRaise(ValueProperty, ref _value, value); + } + + public IDisposable BindSetter(IAnimationSetter setter, Animatable targetControl) + { + Property = setter.Property; + var value = setter.Value; + + if (value is IBinding binding) + { + return this.Bind(ValueProperty, binding, targetControl); + } + else + { + return this.Bind(ValueProperty, ObservableEx.SingleValue(value).ToBinding(), targetControl); + } + } + + public T GetTypedValue() + { + var typeConv = TypeDescriptor.GetConverter(typeof(T)); + + if (Value == null) + { + throw new ArgumentNullException($"KeyFrame value can't be null."); + } + if (!typeConv.CanConvertTo(Value.GetType())) + { + throw new InvalidCastException($"KeyFrame value doesnt match property type."); + } + + return (T)typeConv.ConvertTo(Value, typeof(T)); + } } } diff --git a/src/Avalonia.Animation/AnimatorStateMachine`1.cs b/src/Avalonia.Animation/AnimatorStateMachine`1.cs index e37b0e592a..1a51b897c0 100644 --- a/src/Avalonia.Animation/AnimatorStateMachine`1.cs +++ b/src/Avalonia.Animation/AnimatorStateMachine`1.cs @@ -51,9 +51,9 @@ namespace Avalonia.Animation Disposed } - public void Initialize(Animation animation, Animatable control, Animator keyframes) + public void Initialize(Animation animation, Animatable control, Animator animator) { - _parent = keyframes; + _parent = animator; _targetAnimation = animation; _targetControl = control; _neutralValue = (T)_targetControl.GetValue(_parent.Property); @@ -123,121 +123,133 @@ namespace Avalonia.Animation double _tempDuration = 0d, _easedTime; - checkstate: - switch (_currentState) + bool handled = false; + + while (!handled) { - case KeyFramesStates.DoDelay: + switch (_currentState) + { + case KeyFramesStates.DoDelay: - if (_fillMode == FillMode.Backward - || _fillMode == FillMode.Both) - { - if (_currentIteration == 0) + if (_fillMode == FillMode.Backward + || _fillMode == FillMode.Both) { - _targetObserver.OnNext(_firstKFValue); + if (_currentIteration == 0) + { + _targetObserver.OnNext(_firstKFValue); + } + else + { + _targetObserver.OnNext(_lastInterpValue); + } + } + + if (_delayFrameCount > _delayTotalFrameCount) + { + _currentState = KeyFramesStates.DoRun; } else { - _targetObserver.OnNext(_lastInterpValue); + handled = true; + _delayFrameCount++; } - } - - if (_delayFrameCount > _delayTotalFrameCount) - { - _currentState = KeyFramesStates.DoRun; - goto checkstate; - } - _delayFrameCount++; - break; - - case KeyFramesStates.DoRun: - - if (_isReversed) - _currentState = KeyFramesStates.RunBackwards; - else - _currentState = KeyFramesStates.RunForwards; - - goto checkstate; - - case KeyFramesStates.RunForwards: - - if (_durationFrameCount > _durationTotalFrameCount) - { - _currentState = KeyFramesStates.RunComplete; - goto checkstate; - } + break; - _tempDuration = (double)_durationFrameCount / _durationTotalFrameCount; - _currentState = KeyFramesStates.RunApplyValue; + case KeyFramesStates.DoRun: - goto checkstate; + if (_isReversed) + _currentState = KeyFramesStates.RunBackwards; + else + _currentState = KeyFramesStates.RunForwards; - case KeyFramesStates.RunBackwards: + break; - if (_durationFrameCount > _durationTotalFrameCount) - { - _currentState = KeyFramesStates.RunComplete; - goto checkstate; - } + case KeyFramesStates.RunForwards: - _tempDuration = (double)(_durationTotalFrameCount - _durationFrameCount) / _durationTotalFrameCount; - _currentState = KeyFramesStates.RunApplyValue; + if (_durationFrameCount > _durationTotalFrameCount) + { + _currentState = KeyFramesStates.RunComplete; + } + else + { + _tempDuration = (double)_durationFrameCount / _durationTotalFrameCount; + _currentState = KeyFramesStates.RunApplyValue; - goto checkstate; + } + break; - case KeyFramesStates.RunApplyValue: + case KeyFramesStates.RunBackwards: - _easedTime = _targetAnimation.Easing.Ease(_tempDuration); + if (_durationFrameCount > _durationTotalFrameCount) + { + _currentState = KeyFramesStates.RunComplete; + } + else + { + _tempDuration = (double)(_durationTotalFrameCount - _durationFrameCount) / _durationTotalFrameCount; + _currentState = KeyFramesStates.RunApplyValue; + } + break; - _durationFrameCount++; - _lastInterpValue = Interpolator(_easedTime, _neutralValue); - _targetObserver.OnNext(_lastInterpValue); - _currentState = KeyFramesStates.DoRun; + case KeyFramesStates.RunApplyValue: - break; + _easedTime = _targetAnimation.Easing.Ease(_tempDuration); - case KeyFramesStates.RunComplete: + _durationFrameCount++; + _lastInterpValue = Interpolator(_easedTime, _neutralValue); + _targetObserver.OnNext(_lastInterpValue); + _currentState = KeyFramesStates.DoRun; + handled = true; + break; - if (_checkLoopAndRepeat) - { - _delayFrameCount = 0; - _durationFrameCount = 0; + case KeyFramesStates.RunComplete: - if (_isLooping) - { - _currentState = KeyFramesStates.DoRun; - } - else if (_isRepeating) + if (_checkLoopAndRepeat) { - if (_currentIteration >= _repeatCount) + _delayFrameCount = 0; + _durationFrameCount = 0; + + if (_isLooping) { - _currentState = KeyFramesStates.Stop; + _currentState = KeyFramesStates.DoRun; } - else + else if (_isRepeating) { - _currentState = KeyFramesStates.DoRun; + if (_currentIteration >= _repeatCount) + { + _currentState = KeyFramesStates.Stop; + } + else + { + _currentState = KeyFramesStates.DoRun; + } + _currentIteration++; } - _currentIteration++; - } - if (_animationDirection == PlaybackDirection.Alternate - || _animationDirection == PlaybackDirection.AlternateReverse) - _isReversed = !_isReversed; + if (_animationDirection == PlaybackDirection.Alternate + || _animationDirection == PlaybackDirection.AlternateReverse) + _isReversed = !_isReversed; - break; - } + break; + } - _currentState = KeyFramesStates.Stop; - goto checkstate; + _currentState = KeyFramesStates.Stop; + break; - case KeyFramesStates.Stop: + case KeyFramesStates.Stop: - if (_fillMode == FillMode.Forward - || _fillMode == FillMode.Both) - { - _targetControl.SetValue(_parent.Property, _lastInterpValue, BindingPriority.LocalValue); - } - _targetObserver.OnCompleted(); - break; + if (_fillMode == FillMode.Forward + || _fillMode == FillMode.Both) + { + _targetControl.SetValue(_parent.Property, _lastInterpValue, BindingPriority.LocalValue); + } + _targetObserver.OnCompleted(); + handled = true; + break; + default: + handled = true; + break; + } } } @@ -253,4 +265,4 @@ namespace Avalonia.Animation _currentState = KeyFramesStates.Disposed; } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index 6d4ae7d8e2..a1eef87e1e 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -19,7 +19,7 @@ namespace Avalonia.Animation /// /// List of type-converted keyframes. /// - private Dictionary _convertedKeyframes = new Dictionary(); + private readonly SortedList _convertedKeyframes = new SortedList(); private bool _isVerfifiedAndConverted; @@ -38,12 +38,11 @@ namespace Avalonia.Animation public virtual IDisposable Apply(Animation animation, Animatable control, IObservable obsMatch) { if (!_isVerfifiedAndConverted) - VerifyConvertKeyFrames(animation, typeof(T)); + VerifyConvertKeyFrames(); return obsMatch - .Where(p => p == true) // Ignore triggers when global timers are paused. - .Where(p => Timing.GetGlobalPlayState() != PlayState.Pause) + .Where(p => p && Timing.GetGlobalPlayState() != PlayState.Pause) .Subscribe(_ => { var timerObs = RunKeyFrames(animation, control); @@ -60,8 +59,8 @@ namespace Avalonia.Animation /// The time parameter, relative to the total animation time protected (double IntraKFTime, KeyFramePair KFPair) GetKFPairAndIntraKFTime(double t) { - KeyValuePair firstCue, lastCue; - int kvCount = _convertedKeyframes.Count(); + KeyValuePair firstCue, lastCue; + int kvCount = _convertedKeyframes.Count; if (kvCount > 2) { if (DoubleUtils.AboutEqual(t, 0.0) || t < 0.0) @@ -76,8 +75,8 @@ namespace Avalonia.Animation } else { - firstCue = _convertedKeyframes.Where(j => j.Key <= t).Last(); - lastCue = _convertedKeyframes.Where(j => j.Key >= t).First(); + firstCue = _convertedKeyframes.Last(j => j.Key <= t); + lastCue = _convertedKeyframes.First(j => j.Key >= t); } } else @@ -89,7 +88,9 @@ namespace Avalonia.Animation double t0 = firstCue.Key; double t1 = lastCue.Key; var intraframeTime = (t - t0) / (t1 - t0); - return (intraframeTime, new KeyFramePair(firstCue, lastCue)); + var firstFrameData = (firstCue.Value.frame.GetTypedValue(), firstCue.Value.isNeutral); + var lastFrameData = (lastCue.Value.frame.GetTypedValue(), lastCue.Value.isNeutral); + return (intraframeTime, new KeyFramePair(firstFrameData, lastFrameData)); } @@ -98,17 +99,14 @@ namespace Avalonia.Animation /// private IDisposable RunKeyFrames(Animation animation, Animatable control) { - var _kfStateMach = new AnimatorStateMachine(); - _kfStateMach.Initialize(animation, control, this); + var stateMachine = new AnimatorStateMachine(); + stateMachine.Initialize(animation, control, this); Timing.AnimationStateTimer - .TakeWhile(_ => !_kfStateMach._unsubscribe) - .Subscribe(p => - { - _kfStateMach.Step(p, DoInterpolation); - }); + .TakeWhile(_ => !stateMachine._unsubscribe) + .Subscribe(p => stateMachine.Step(p, DoInterpolation)); - return control.Bind(Property, _kfStateMach, BindingPriority.Animation); + return control.Bind(Property, stateMachine, BindingPriority.Animation); } /// @@ -119,39 +117,19 @@ namespace Avalonia.Animation /// /// Verifies and converts keyframe values according to this class's target type. /// - private void VerifyConvertKeyFrames(Animation animation, Type type) + private void VerifyConvertKeyFrames() { - var typeConv = TypeDescriptor.GetConverter(type); - - foreach (AnimatorKeyFrame k in this) + foreach (AnimatorKeyFrame keyframe in this) { - if (k.Value == null) - { - throw new ArgumentNullException($"KeyFrame value can't be null."); - } - if (!typeConv.CanConvertTo(k.Value.GetType())) - { - throw new InvalidCastException($"KeyFrame value doesnt match property type."); - } - - T convertedValue = (T)typeConv.ConvertTo(k.Value, type); - - Cue _normalizedCue = k.Cue; - - if (k.timeSpanSet) - { - _normalizedCue = new Cue(k.KeyTime.Ticks / animation.Duration.Ticks); - } - - _convertedKeyframes.Add(_normalizedCue.CueValue, (convertedValue, false)); + _convertedKeyframes.Add(keyframe.Cue.CueValue, (keyframe, false)); } - SortKeyFrameCues(_convertedKeyframes); + AddNeutralKeyFramesIfNeeded(); _isVerfifiedAndConverted = true; } - private void SortKeyFrameCues(Dictionary convertedValues) + private void AddNeutralKeyFramesIfNeeded() { bool hasStartKey, hasEndKey; hasStartKey = hasEndKey = false; @@ -170,23 +148,20 @@ namespace Avalonia.Animation } if (!hasStartKey || !hasEndKey) - AddNeutralKeyFrames(hasStartKey, hasEndKey, _convertedKeyframes); - - _convertedKeyframes = _convertedKeyframes.OrderBy(p => p.Key) - .ToDictionary((k) => k.Key, (v) => v.Value); + AddNeutralKeyFrames(hasStartKey, hasEndKey); } - private void AddNeutralKeyFrames(bool hasStartKey, bool hasEndKey, Dictionary convertedKeyframes) + private void AddNeutralKeyFrames(bool hasStartKey, bool hasEndKey) { if (!hasStartKey) { - convertedKeyframes.Add(0.0d, (default(T), true)); + _convertedKeyframes.Add(0.0d, (new AnimatorKeyFrame { Value = default(T) }, true)); } if (!hasEndKey) { - convertedKeyframes.Add(1.0d, (default(T), true)); + _convertedKeyframes.Add(1.0d, (new AnimatorKeyFrame { Value = default(T) }, true)); } } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/Cue.cs b/src/Avalonia.Animation/Cue.cs index fe36b13495..5a95c108e3 100644 --- a/src/Avalonia.Animation/Cue.cs +++ b/src/Avalonia.Animation/Cue.cs @@ -10,7 +10,7 @@ namespace Avalonia.Animation /// A Cue object for . /// [TypeConverter(typeof(CueTypeConverter))] - public struct Cue : IEquatable, IEquatable + public readonly struct Cue : IEquatable, IEquatable { /// /// The normalized percent value, ranging from 0.0 to 1.0 diff --git a/src/Avalonia.Animation/DoubleAnimator.cs b/src/Avalonia.Animation/DoubleAnimator.cs index 5b994377f1..154f37360c 100644 --- a/src/Avalonia.Animation/DoubleAnimator.cs +++ b/src/Avalonia.Animation/DoubleAnimator.cs @@ -24,15 +24,15 @@ namespace Avalonia.Animation var firstKF = pair.KFPair.FirstKeyFrame; var secondKF = pair.KFPair.SecondKeyFrame; - if (firstKF.Value.isNeutral) + if (firstKF.isNeutral) y0 = neutralValue; else - y0 = firstKF.Value.TargetValue; + y0 = firstKF.TargetValue; - if (secondKF.Value.isNeutral) + if (secondKF.isNeutral) y1 = neutralValue; else - y1 = secondKF.Value.TargetValue; + y1 = secondKF.TargetValue; // Do linear parametric interpolation return y0 + (pair.IntraKFTime) * (y1 - y0); diff --git a/src/Avalonia.Animation/IAnimationSetter.cs b/src/Avalonia.Animation/IAnimationSetter.cs index f2a94c9ed6..2d22377286 100644 --- a/src/Avalonia.Animation/IAnimationSetter.cs +++ b/src/Avalonia.Animation/IAnimationSetter.cs @@ -5,4 +5,4 @@ namespace Avalonia.Animation AvaloniaProperty Property { get; set; } object Value { get; set; } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/KeyFrame.cs b/src/Avalonia.Animation/KeyFrame.cs index 46be119c36..ea04aa0aab 100644 --- a/src/Avalonia.Animation/KeyFrame.cs +++ b/src/Avalonia.Animation/KeyFrame.cs @@ -7,6 +7,11 @@ using Avalonia.Collections; namespace Avalonia.Animation { + internal enum KeyFrameTimingMode + { + TimeSpan = 1, + Cue + } /// /// Stores data regarding a specific key @@ -14,7 +19,6 @@ namespace Avalonia.Animation /// public class KeyFrame : AvaloniaList { - internal bool timeSpanSet, cueSet; private TimeSpan _ktimeSpan; private Cue _kCue; @@ -30,6 +34,8 @@ namespace Avalonia.Animation { } + internal KeyFrameTimingMode TimingMode { get; private set; } + /// /// Gets or sets the key time of this . /// @@ -42,11 +48,11 @@ namespace Avalonia.Animation } set { - if (cueSet) + if (TimingMode == KeyFrameTimingMode.Cue) { throw new InvalidOperationException($"You can only set either {nameof(KeyTime)} or {nameof(Cue)}."); } - timeSpanSet = true; + TimingMode = KeyFrameTimingMode.TimeSpan; _ktimeSpan = value; } } @@ -63,11 +69,11 @@ namespace Avalonia.Animation } set { - if (timeSpanSet) + if (TimingMode == KeyFrameTimingMode.TimeSpan) { throw new InvalidOperationException($"You can only set either {nameof(KeyTime)} or {nameof(Cue)}."); } - cueSet = true; + TimingMode = KeyFrameTimingMode.Cue; _kCue = value; } } diff --git a/src/Avalonia.Animation/KeyFramePair`1.cs b/src/Avalonia.Animation/KeyFramePair`1.cs index c192479a1d..408b13e0d8 100644 --- a/src/Avalonia.Animation/KeyFramePair`1.cs +++ b/src/Avalonia.Animation/KeyFramePair`1.cs @@ -22,7 +22,7 @@ namespace Avalonia.Animation /// /// /// - public KeyFramePair(KeyValuePair FirstKeyFrame, KeyValuePair LastKeyFrame) : this() + public KeyFramePair((T TargetValue, bool isNeutral) FirstKeyFrame, (T TargetValue, bool isNeutral) LastKeyFrame) : this() { this.FirstKeyFrame = FirstKeyFrame; this.SecondKeyFrame = LastKeyFrame; @@ -31,11 +31,11 @@ namespace Avalonia.Animation /// /// First object. /// - public KeyValuePair FirstKeyFrame { get; private set; } + public (T TargetValue, bool isNeutral) FirstKeyFrame { get; } /// /// Second object. /// - public KeyValuePair SecondKeyFrame { get; private set; } + public (T TargetValue, bool isNeutral) SecondKeyFrame { get; } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Base/AvaloniaObject.cs b/src/Avalonia.Base/AvaloniaObject.cs index 761c0618da..35e189e6a4 100644 --- a/src/Avalonia.Base/AvaloniaObject.cs +++ b/src/Avalonia.Base/AvaloniaObject.cs @@ -22,7 +22,7 @@ namespace Avalonia /// /// This class is analogous to DependencyObject in WPF. /// - public class AvaloniaObject : IAvaloniaObject, IAvaloniaObjectDebug, INotifyPropertyChanged, IPriorityValueOwner + public class AvaloniaObject : IAvaloniaObject, IAvaloniaObjectDebug, INotifyPropertyChanged { /// /// The parent object that inherited values are inherited from. @@ -45,21 +45,8 @@ namespace Avalonia /// private EventHandler _propertyChanged; - private DeferredSetter _directDeferredSetter; private ValueStore _values; - - /// - /// Delayed setter helper for direct properties. Used to fix #855. - /// - private DeferredSetter DirectPropertyDeferredSetter - { - get - { - return _directDeferredSetter ?? - (_directDeferredSetter = new DeferredSetter()); - } - } - + private ValueStore Values => _values ?? (_values = new ValueStore(this)); /// /// Initializes a new instance of the class. @@ -225,7 +212,7 @@ namespace Avalonia } else if (_values != null) { - var result = _values.GetValue(property); + var result = Values.GetValue(property); if (result == AvaloniaProperty.UnsetValue) { @@ -376,12 +363,7 @@ namespace Avalonia description, priority); - if (_values == null) - { - _values = new ValueStore(this); - } - - return _values.AddBinding(property, source, priority); + return Values.AddBinding(property, source, priority); } } @@ -414,9 +396,8 @@ namespace Avalonia VerifyAccess(); _values?.Revalidate(property); } - - /// - void IPriorityValueOwner.Changed(AvaloniaProperty property, int priority, object oldValue, object newValue) + + internal void PriorityValueChanged(AvaloniaProperty property, int priority, object oldValue, object newValue) { oldValue = (oldValue == AvaloniaProperty.UnsetValue) ? GetDefaultValue(property) : @@ -439,9 +420,8 @@ namespace Avalonia (BindingPriority)priority); } } - - /// - void IPriorityValueOwner.BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification) + + internal void BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification) { UpdateDataValidation(property, notification); } @@ -456,7 +436,7 @@ namespace Avalonia /// Gets all priority values set on the object. /// /// A collection of property/value tuples. - internal IDictionary GetSetValues() => _values?.GetSetValues(); + internal IDictionary GetSetValues() => Values?.GetSetValues(); /// /// Forces revalidation of properties when a property value changes. @@ -566,12 +546,12 @@ namespace Avalonia T value) { Contract.Requires(setterCallback != null); - return DirectPropertyDeferredSetter.SetAndNotify( + return Values.Setter.SetAndNotify( property, ref field, - (object val, ref T backing, Action notify) => + (object update, ref T backing, Action notify) => { - setterCallback((T)val, ref backing, notify); + setterCallback((T)update, ref backing, notify); return true; }, value); @@ -737,13 +717,8 @@ namespace Avalonia originalValue?.GetType().FullName ?? "(null)")); } - if (_values == null) - { - _values = new ValueStore(this); - } - LogPropertySet(property, value, priority); - _values.AddValue(property, value, (int)priority); + Values.AddValue(property, value, (int)priority); } /// diff --git a/src/Avalonia.Base/Diagnostics/AvaloniaObjectExtensions.cs b/src/Avalonia.Base/Diagnostics/AvaloniaObjectExtensions.cs index f44f9043f0..7afbcabd2a 100644 --- a/src/Avalonia.Base/Diagnostics/AvaloniaObjectExtensions.cs +++ b/src/Avalonia.Base/Diagnostics/AvaloniaObjectExtensions.cs @@ -23,15 +23,24 @@ namespace Avalonia.Diagnostics { var set = o.GetSetValues(); - PriorityValue value; - - if (set.TryGetValue(property, out value)) + if (set.TryGetValue(property, out var obj)) { - return new AvaloniaPropertyValue( - property, - o.GetValue(property), - (BindingPriority)value.ValuePriority, - value.GetDiagnostic()); + if (obj is PriorityValue value) + { + return new AvaloniaPropertyValue( + property, + o.GetValue(property), + (BindingPriority)value.ValuePriority, + value.GetDiagnostic()); + } + else + { + return new AvaloniaPropertyValue( + property, + obj, + BindingPriority.LocalValue, + "Local value"); + } } else { diff --git a/src/Avalonia.Base/IPriorityValueOwner.cs b/src/Avalonia.Base/IPriorityValueOwner.cs index 5f63f6ef91..8cbf212381 100644 --- a/src/Avalonia.Base/IPriorityValueOwner.cs +++ b/src/Avalonia.Base/IPriorityValueOwner.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using Avalonia.Data; +using Avalonia.Utilities; namespace Avalonia { @@ -31,5 +32,7 @@ namespace Avalonia /// Ensures that the current thread is the UI thread. /// void VerifyAccess(); + + DeferredSetter Setter { get; } } } diff --git a/src/Avalonia.Base/PriorityValue.cs b/src/Avalonia.Base/PriorityValue.cs index c474f9098e..03094e2236 100644 --- a/src/Avalonia.Base/PriorityValue.cs +++ b/src/Avalonia.Base/PriorityValue.cs @@ -21,7 +21,7 @@ namespace Avalonia /// 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 @@ -30,7 +30,6 @@ namespace Avalonia private readonly SingleOrDictionary _levels = new SingleOrDictionary(); private readonly Func _validate; - private static readonly DeferredSetter delayedSetter = new DeferredSetter(); private (object value, int priority) _value; /// @@ -243,12 +242,18 @@ namespace Avalonia /// The priority level that the value came from. private void UpdateValue(object value, int priority) { - delayedSetter.SetAndNotify(this, + Owner.Setter.SetAndNotify(Property, ref _value, UpdateCore, (value, priority)); } + private bool UpdateCore( + object update, + ref (object value, int priority) backing, + Action notify) + => UpdateCore(((object, int))update, ref backing, notify); + private bool UpdateCore( (object value, int priority) update, ref (object value, int priority) backing, diff --git a/src/Avalonia.Base/Utilities/DeferredSetter.cs b/src/Avalonia.Base/Utilities/DeferredSetter.cs index fdfa160134..ae6f599005 100644 --- a/src/Avalonia.Base/Utilities/DeferredSetter.cs +++ b/src/Avalonia.Base/Utilities/DeferredSetter.cs @@ -8,11 +8,10 @@ namespace Avalonia.Utilities { /// /// A utility class to enable deferring assignment until after property-changed notifications are sent. + /// Used to fix #855. /// - /// The type of the object that represents the property. /// The type of value with which to track the delayed assignment. - class DeferredSetter - where TProperty: class + class DeferredSetter { private struct NotifyDisposable : IDisposable { @@ -37,29 +36,44 @@ namespace Avalonia.Utilities { public bool Notifying { get; set; } - private Queue pendingValues; + private SingleOrQueue pendingValues; - public Queue PendingValues + public SingleOrQueue PendingValues { get { - return pendingValues ?? (pendingValues = new Queue()); + return pendingValues ?? (pendingValues = new SingleOrQueue()); } } } - private readonly ConditionalWeakTable setRecords = new ConditionalWeakTable(); + private Dictionary _setRecords; + private Dictionary SetRecords + => _setRecords ?? (_setRecords = new Dictionary()); + + private SettingStatus GetOrCreateStatus(AvaloniaProperty property) + { + if (!SetRecords.TryGetValue(property, out var status)) + { + status = new SettingStatus(); + SetRecords.Add(property, status); + } + + return status; + } /// /// Mark the property as currently notifying. /// /// The property to mark as notifying. /// Returns a disposable that when disposed, marks the property as done notifying. - private NotifyDisposable MarkNotifying(TProperty property) + private NotifyDisposable MarkNotifying(AvaloniaProperty property) { Contract.Requires(!IsNotifying(property)); - - return new NotifyDisposable(setRecords.GetOrCreateValue(property)); + + SettingStatus status = GetOrCreateStatus(property); + + return new NotifyDisposable(status); } /// @@ -67,19 +81,19 @@ namespace Avalonia.Utilities /// /// The property. /// If the property is currently notifying listeners. - private bool IsNotifying(TProperty property) - => setRecords.TryGetValue(property, out var value) && value.Notifying; + private bool IsNotifying(AvaloniaProperty property) + => SetRecords.TryGetValue(property, out var value) && value.Notifying; /// /// Add a pending assignment for the property. /// /// The property. /// The value to assign. - private void AddPendingSet(TProperty property, TSetRecord value) + private void AddPendingSet(AvaloniaProperty property, TSetRecord value) { Contract.Requires(IsNotifying(property)); - setRecords.GetOrCreateValue(property).PendingValues.Enqueue(value); + GetOrCreateStatus(property).PendingValues.Enqueue(value); } /// @@ -87,9 +101,9 @@ namespace Avalonia.Utilities /// /// The property to check. /// If the property has any pending assignments. - private bool HasPendingSet(TProperty property) + private bool HasPendingSet(AvaloniaProperty property) { - return setRecords.TryGetValue(property, out var status) && status.PendingValues.Count != 0; + return SetRecords.TryGetValue(property, out var status) && !status.PendingValues.Empty; } /// @@ -97,9 +111,9 @@ namespace Avalonia.Utilities /// /// The property to check. /// The first pending assignment for the property. - private TSetRecord GetFirstPendingSet(TProperty property) + private TSetRecord GetFirstPendingSet(AvaloniaProperty property) { - return setRecords.GetOrCreateValue(property).PendingValues.Dequeue(); + return GetOrCreateStatus(property).PendingValues.Dequeue(); } public delegate bool SetterDelegate(TSetRecord record, ref TValue backing, Action notifyCallback); @@ -115,7 +129,7 @@ namespace Avalonia.Utilities /// /// The value to try to set. public bool SetAndNotify( - TProperty property, + AvaloniaProperty property, ref TValue backing, SetterDelegate setterCallback, TSetRecord value) @@ -144,6 +158,7 @@ namespace Avalonia.Utilities } }); } + return updated; } else if(!object.Equals(value, backing)) diff --git a/src/Avalonia.Base/Utilities/SingleOrQueue.cs b/src/Avalonia.Base/Utilities/SingleOrQueue.cs new file mode 100644 index 0000000000..4a66b72a56 --- /dev/null +++ b/src/Avalonia.Base/Utilities/SingleOrQueue.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Avalonia.Utilities +{ + /// + /// FIFO Queue optimized for holding zero or one items. + /// + /// The type of items held in the queue. + public class SingleOrQueue + { + private T _head; + private Queue _tail; + + private Queue Tail => _tail ?? (_tail = new Queue()); + + private bool HasTail => _tail != null; + + public bool Empty { get; private set; } = true; + + public void Enqueue(T value) + { + if (Empty) + { + _head = value; + } + else + { + Tail.Enqueue(value); + } + + Empty = false; + } + + public T Dequeue() + { + if (Empty) + { + throw new InvalidOperationException("Cannot dequeue from an empty queue!"); + } + + var result = _head; + + if (HasTail && Tail.Count != 0) + { + _head = Tail.Dequeue(); + } + else + { + _head = default; + Empty = true; + } + + return result; + } + } +} diff --git a/src/Avalonia.Base/ValueStore.cs b/src/Avalonia.Base/ValueStore.cs index 8283edab80..ab80e74923 100644 --- a/src/Avalonia.Base/ValueStore.cs +++ b/src/Avalonia.Base/ValueStore.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Avalonia.Data; +using Avalonia.Utilities; namespace Avalonia { @@ -91,15 +92,15 @@ namespace Avalonia public void BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification) { - ((IPriorityValueOwner)_owner).BindingNotificationReceived(property, notification); + _owner.BindingNotificationReceived(property, notification); } public void Changed(AvaloniaProperty property, int priority, object oldValue, object newValue) { - ((IPriorityValueOwner)_owner).Changed(property, priority, oldValue, newValue); + _owner.PriorityValueChanged(property, priority, oldValue, newValue); } - public IDictionary GetSetValues() => throw new NotImplementedException(); + public IDictionary GetSetValues() => _values; public object GetValue(AvaloniaProperty property) { @@ -115,7 +116,7 @@ namespace Avalonia public bool IsAnimating(AvaloniaProperty property) { - return _values.TryGetValue(property, out var value) ? (value as PriorityValue)?.IsAnimating ?? false : false; + return _values.TryGetValue(property, out var value) && value is PriorityValue priority && priority.IsAnimating; } public bool IsSet(AvaloniaProperty property) @@ -148,13 +149,11 @@ namespace Avalonia validate2 = v => validate(_owner, v); } - PriorityValue result = new PriorityValue( + return new PriorityValue( this, property, property.PropertyType, validate2); - - return result; } private object Validate(AvaloniaProperty property, object value) @@ -168,5 +167,16 @@ namespace Avalonia return value; } + + private DeferredSetter _defferedSetter; + + public DeferredSetter Setter + { + get + { + return _defferedSetter ?? + (_defferedSetter = new DeferredSetter()); + } + } } } diff --git a/src/Avalonia.Controls/ProgressBar.cs b/src/Avalonia.Controls/ProgressBar.cs index 5e5a460368..b7db352c74 100644 --- a/src/Avalonia.Controls/ProgressBar.cs +++ b/src/Avalonia.Controls/ProgressBar.cs @@ -21,18 +21,27 @@ namespace Avalonia.Controls public static readonly StyledProperty OrientationProperty = AvaloniaProperty.Register(nameof(Orientation), Orientation.Horizontal); + private static readonly DirectProperty IndeterminateStartingOffsetProperty = + AvaloniaProperty.RegisterDirect( + nameof(IndeterminateStartingOffset), + p => p.IndeterminateStartingOffset, + (p, o) => p.IndeterminateStartingOffset = o); + + private static readonly DirectProperty IndeterminateEndingOffsetProperty = + AvaloniaProperty.RegisterDirect( + nameof(IndeterminateEndingOffset), + p => p.IndeterminateEndingOffset, + (p, o) => p.IndeterminateEndingOffset = o); + private Border _indicator; - private IndeterminateAnimation _indeterminateAnimation; static ProgressBar() { PseudoClass(OrientationProperty, o => o == Avalonia.Controls.Orientation.Vertical, ":vertical"); PseudoClass(OrientationProperty, o => o == Avalonia.Controls.Orientation.Horizontal, ":horizontal"); + PseudoClass(IsIndeterminateProperty, ":indeterminate"); ValueProperty.Changed.AddClassHandler(x => x.ValueChanged); - - IsIndeterminateProperty.Changed.AddClassHandler( - (p, e) => { if (p._indicator != null) p.UpdateIsIndeterminate((bool)e.NewValue); }); } public bool IsIndeterminate @@ -46,6 +55,19 @@ namespace Avalonia.Controls get => GetValue(OrientationProperty); set => SetValue(OrientationProperty, value); } + private double _indeterminateStartingOffset; + private double IndeterminateStartingOffset + { + get => _indeterminateStartingOffset; + set => SetAndRaise(IndeterminateStartingOffsetProperty, ref _indeterminateStartingOffset, value); + } + + private double _indeterminateEndingOffset; + private double IndeterminateEndingOffset + { + get => _indeterminateEndingOffset; + set => SetAndRaise(IndeterminateEndingOffsetProperty, ref _indeterminateEndingOffset, value); + } /// protected override Size ArrangeOverride(Size finalSize) @@ -60,7 +82,6 @@ namespace Avalonia.Controls _indicator = e.NameScope.Get("PART_Indicator"); UpdateIndicator(Bounds.Size); - UpdateIsIndeterminate(IsIndeterminate); } private void UpdateIndicator(Size bounds) @@ -70,9 +91,20 @@ namespace Avalonia.Controls if (IsIndeterminate) { if (Orientation == Orientation.Horizontal) - _indicator.Width = bounds.Width / 5.0; + { + var width = bounds.Width / 5.0; + IndeterminateStartingOffset = -width; + _indicator.Width = width; + IndeterminateEndingOffset = bounds.Width; + + } else - _indicator.Height = bounds.Height / 5.0; + { + var height = bounds.Height / 5.0; + IndeterminateStartingOffset = -bounds.Height; + _indicator.Height = height; + IndeterminateEndingOffset = height; + } } else { @@ -86,53 +118,9 @@ namespace Avalonia.Controls } } - private void UpdateIsIndeterminate(bool isIndeterminate) - { - if (isIndeterminate) - { - if (_indeterminateAnimation == null || _indeterminateAnimation.Disposed) - _indeterminateAnimation = IndeterminateAnimation.StartAnimation(this); - } - else - _indeterminateAnimation?.Dispose(); - } - private void ValueChanged(AvaloniaPropertyChangedEventArgs e) { UpdateIndicator(Bounds.Size); } - - // TODO: Implement Indeterminate Progress animation - // in xaml (most ideal) or if it's not possible - // then on this class. - private class IndeterminateAnimation : IDisposable - { - private WeakReference _progressBar; - - private bool _disposed; - - public bool Disposed => _disposed; - - private IndeterminateAnimation(ProgressBar progressBar) - { - _progressBar = new WeakReference(progressBar); - - } - - public static IndeterminateAnimation StartAnimation(ProgressBar progressBar) - { - return new IndeterminateAnimation(progressBar); - } - - private Rect GetAnimationRect(TimeSpan time) - { - return Rect.Empty; - } - - public void Dispose() - { - _disposed = true; - } - } } } diff --git a/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs b/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs index 555a0b2354..ce8ad36c17 100644 --- a/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs +++ b/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs @@ -31,6 +31,7 @@ namespace Avalonia.Diagnostics.ViewModels } }; + SelectedTab = 0; root.GetObservable(TopLevel.PointerOverElementProperty) .Subscribe(x => PointerOverElement = x?.GetType().Name); } diff --git a/src/Avalonia.Styling/Styling/Setter.cs b/src/Avalonia.Styling/Styling/Setter.cs index 31b685f6b1..c75bae4db8 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -126,7 +126,7 @@ namespace Avalonia.Styling if (source != null) { - var cloned = Clone(source, style, activator); + var cloned = Clone(source, source.Mode == BindingMode.Default ? Property.GetMetadata(control.GetType()).DefaultBindingMode : source.Mode, style, activator); return BindingOperations.Apply(control, Property, cloned, null); } } @@ -134,13 +134,13 @@ namespace Avalonia.Styling return Disposable.Empty; } - private InstancedBinding Clone(InstancedBinding sourceInstance, IStyle style, IObservable activator) + private InstancedBinding Clone(InstancedBinding sourceInstance, BindingMode mode, IStyle style, IObservable activator) { if (activator != null) { var description = style?.ToString(); - switch (sourceInstance.Mode) + switch (mode) { case BindingMode.OneTime: if (sourceInstance.Observable != null) diff --git a/src/Avalonia.Themes.Default/ProgressBar.xaml b/src/Avalonia.Themes.Default/ProgressBar.xaml index c9c898562c..c4cbfed350 100644 --- a/src/Avalonia.Themes.Default/ProgressBar.xaml +++ b/src/Avalonia.Themes.Default/ProgressBar.xaml @@ -7,14 +7,9 @@ - - - - + @@ -35,4 +30,36 @@ - \ No newline at end of file + + + diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/BindingExtension.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/BindingExtension.cs index c3229d814c..789f7675be 100644 --- a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/BindingExtension.cs +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/BindingExtension.cs @@ -87,19 +87,28 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions if (string.IsNullOrWhiteSpace(path) || path == ".") { result.Path = string.Empty; + return result; } - else if (path.StartsWith("#")) + else if (path.StartsWith("!")) + { + int pathStart = 0; + for (; pathStart < path.Length && path[pathStart] == '!'; ++pathStart); + result.Path = path.Substring(0, pathStart); + path = path.Substring(pathStart); + } + + if (path.StartsWith("#")) { var dot = path.IndexOf('.'); if (dot != -1) { - result.Path = path.Substring(dot + 1); + result.Path += path.Substring(dot + 1); result.ElementName = path.Substring(1, dot - 1); } else { - result.Path = string.Empty; + result.Path += string.Empty; result.ElementName = path.Substring(1); } } @@ -114,12 +123,12 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions string relativeSourceMode; if (dot != -1) { - result.Path = path.Substring(dot + 1); + result.Path += path.Substring(dot + 1); relativeSourceMode = path.Substring(1, dot - 1); } else { - result.Path = string.Empty; + result.Path += string.Empty; relativeSourceMode = path.Substring(1); } @@ -170,7 +179,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions } else { - result.Path = path; + result.Path += path; } return result; @@ -229,4 +238,4 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions public RelativeSource RelativeSource { get; set; } } -} \ No newline at end of file +} diff --git a/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs b/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs index 3933a6ff19..cdb5fc3416 100644 --- a/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs +++ b/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs @@ -7,7 +7,6 @@ using System.Linq; using System.Reflection; using Avalonia.Controls; using Avalonia.Data; -using Avalonia.Markup.Data; using Avalonia.Markup.Xaml.Templates; using Avalonia.Media; using Avalonia.Metadata; @@ -33,6 +32,7 @@ namespace Avalonia.Markup.Xaml.Context private static readonly IEnumerable ForcedAssemblies = new[] { typeof(AvaloniaObject).GetTypeInfo().Assembly, + typeof(Animation.Animation).GetTypeInfo().Assembly, typeof(Control).GetTypeInfo().Assembly, typeof(Style).GetTypeInfo().Assembly, typeof(DataTemplate).GetTypeInfo().Assembly, @@ -146,4 +146,4 @@ namespace Avalonia.Markup.Xaml.Context return null; } } -} \ No newline at end of file +} diff --git a/src/Markup/Avalonia.Markup/Data/Binding.cs b/src/Markup/Avalonia.Markup/Data/Binding.cs index e5f7ea1742..867999dc91 100644 --- a/src/Markup/Avalonia.Markup/Data/Binding.cs +++ b/src/Markup/Avalonia.Markup/Data/Binding.cs @@ -132,7 +132,10 @@ namespace Avalonia.Data } else if (RelativeSource.Mode == RelativeSourceMode.TemplatedParent) { - observer = CreateTemplatedParentObserver(target, Path, enableDataValidation); + observer = CreateTemplatedParentObserver( + (target as IStyledElement) ?? (anchor as IStyledElement), + Path, + enableDataValidation); } else if (RelativeSource.Mode == RelativeSourceMode.FindAncestor) { diff --git a/src/OSX/Avalonia.MonoMac/SystemDialogsImpl.cs b/src/OSX/Avalonia.MonoMac/SystemDialogsImpl.cs index 04db728a2b..16a0b25ef1 100644 --- a/src/OSX/Avalonia.MonoMac/SystemDialogsImpl.cs +++ b/src/OSX/Avalonia.MonoMac/SystemDialogsImpl.cs @@ -25,9 +25,9 @@ namespace Avalonia.MonoMac else { if (panel is NSOpenPanel openPanel) - tcs.SetResult(openPanel.Urls.Select(url => url.AbsoluteString).ToArray()); + tcs.SetResult(openPanel.Urls.Select(url => url.AbsoluteString.Replace("file://", "")).ToArray()); else - tcs.SetResult(new[] { panel.Url.AbsoluteString }); + tcs.SetResult(new[] { panel.Url.AbsoluteString.Replace("file://", "") }); } panel.OrderOut(panel); keyWindow?.MakeKeyAndOrderFront(keyWindow); diff --git a/tests/Avalonia.Base.UnitTests/PriorityValueTests.cs b/tests/Avalonia.Base.UnitTests/PriorityValueTests.cs index d6650aa104..2f1b7862a7 100644 --- a/tests/Avalonia.Base.UnitTests/PriorityValueTests.cs +++ b/tests/Avalonia.Base.UnitTests/PriorityValueTests.cs @@ -1,11 +1,12 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Utilities; +using Moq; using System; using System.Linq; using System.Reactive.Linq; using System.Reactive.Subjects; -using Moq; using Xunit; namespace Avalonia.Base.UnitTests @@ -21,7 +22,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Initial_Value_Should_Be_UnsetValue() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); Assert.Same(AvaloniaProperty.UnsetValue, target.Value); } @@ -29,7 +30,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void First_Binding_Sets_Value() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); target.Add(Single("foo"), 0); @@ -39,7 +40,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Changing_Binding_Should_Set_Value() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var subject = new BehaviorSubject("foo"); target.Add(subject, 0); @@ -51,7 +52,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Setting_Direct_Value_Should_Override_Binding() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); target.Add(Single("foo"), 0); target.SetValue("bar", 0); @@ -62,7 +63,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Binding_Firing_Should_Override_Direct_Value() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var source = new BehaviorSubject("initial"); target.Add(source, 0); @@ -76,7 +77,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Earlier_Binding_Firing_Should_Not_Override_Later() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var nonActive = new BehaviorSubject("na"); var source = new BehaviorSubject("initial"); @@ -92,7 +93,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Binding_Completing_Should_Revert_To_Direct_Value() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var source = new BehaviorSubject("initial"); target.Add(source, 0); @@ -108,7 +109,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Binding_With_Lower_Priority_Has_Precedence() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); target.Add(Single("foo"), 1); target.Add(Single("bar"), 0); @@ -120,7 +121,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Later_Binding_With_Same_Priority_Should_Take_Precedence() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); target.Add(Single("foo"), 1); target.Add(Single("bar"), 0); @@ -133,7 +134,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Changing_Binding_With_Lower_Priority_Should_Set_Not_Value() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var subject = new BehaviorSubject("bar"); target.Add(Single("foo"), 0); @@ -146,7 +147,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void UnsetValue_Should_Fall_Back_To_Next_Binding() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var subject = new BehaviorSubject("bar"); target.Add(subject, 0); @@ -162,7 +163,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Adding_Value_Should_Call_OnNext() { - var owner = new Mock(); + var owner = GetMockOwner(); var target = new PriorityValue(owner.Object, TestProperty, typeof(string)); target.Add(Single("foo"), 0); @@ -173,7 +174,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Changing_Value_Should_Call_OnNext() { - var owner = new Mock(); + var owner = GetMockOwner(); var target = new PriorityValue(owner.Object, TestProperty, typeof(string)); var subject = new BehaviorSubject("foo"); @@ -186,7 +187,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Disposing_A_Binding_Should_Revert_To_Next_Value() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); target.Add(Single("foo"), 0); var disposable = target.Add(Single("bar"), 0); @@ -199,7 +200,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Disposing_A_Binding_Should_Remove_BindingEntry() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); target.Add(Single("foo"), 0); var disposable = target.Add(Single("bar"), 0); @@ -212,7 +213,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Completing_A_Binding_Should_Revert_To_Previous_Binding() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var source = new BehaviorSubject("bar"); target.Add(Single("foo"), 0); @@ -226,7 +227,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Completing_A_Binding_Should_Revert_To_Lower_Priority() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var source = new BehaviorSubject("bar"); target.Add(Single("foo"), 1); @@ -240,7 +241,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Completing_A_Binding_Should_Remove_BindingEntry() { - var target = new PriorityValue(null, TestProperty, typeof(string)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(string)); var subject = new BehaviorSubject("bar"); target.Add(Single("foo"), 0); @@ -254,7 +255,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Direct_Value_Should_Be_Coerced() { - var target = new PriorityValue(null, TestProperty, typeof(int), x => Math.Min((int)x, 10)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(int), x => Math.Min((int)x, 10)); target.SetValue(5, 0); Assert.Equal(5, target.Value); @@ -265,7 +266,7 @@ namespace Avalonia.Base.UnitTests [Fact] public void Bound_Value_Should_Be_Coerced() { - var target = new PriorityValue(null, TestProperty, typeof(int), x => Math.Min((int)x, 10)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(int), x => Math.Min((int)x, 10)); var source = new Subject(); target.Add(source, 0); @@ -279,7 +280,7 @@ namespace Avalonia.Base.UnitTests public void Revalidate_Should_ReCoerce_Value() { var max = 10; - var target = new PriorityValue(null, TestProperty, typeof(int), x => Math.Min((int)x, max)); + var target = new PriorityValue(GetMockOwner().Object, TestProperty, typeof(int), x => Math.Min((int)x, max)); var source = new Subject(); target.Add(source, 0); @@ -302,5 +303,12 @@ namespace Avalonia.Base.UnitTests { return Observable.Never().StartWith(value); } + + private static Mock GetMockOwner() + { + var owner = new Mock(); + owner.SetupGet(o => o.Setter).Returns(new DeferredSetter()); + return owner; + } } } diff --git a/tests/Avalonia.Base.UnitTests/Utilities/SingleOrQueueTests.cs b/tests/Avalonia.Base.UnitTests/Utilities/SingleOrQueueTests.cs new file mode 100644 index 0000000000..f1ab265bc9 --- /dev/null +++ b/tests/Avalonia.Base.UnitTests/Utilities/SingleOrQueueTests.cs @@ -0,0 +1,50 @@ +using Avalonia.Utilities; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace Avalonia.Base.UnitTests.Utilities +{ + public class SingleOrQueueTests + { + [Fact] + public void New_SingleOrQueue_Is_Empty() + { + Assert.True(new SingleOrQueue().Empty); + } + + [Fact] + public void Dequeue_Throws_When_Empty() + { + var queue = new SingleOrQueue(); + + Assert.Throws(() => queue.Dequeue()); + } + + [Fact] + public void Enqueue_Adds_Element() + { + var queue = new SingleOrQueue(); + + queue.Enqueue(1); + + Assert.False(queue.Empty); + + Assert.Equal(1, queue.Dequeue()); + } + + [Fact] + public void Multiple_Elements_Dequeued_In_Correct_Order() + { + var queue = new SingleOrQueue(); + + queue.Enqueue(1); + queue.Enqueue(2); + queue.Enqueue(3); + Assert.Equal(1, queue.Dequeue()); + Assert.Equal(2, queue.Dequeue()); + Assert.Equal(3, queue.Dequeue()); + } + } +} diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests_RelativeSource.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests_RelativeSource.cs index 095aae7742..c6fe79bc0c 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests_RelativeSource.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests_RelativeSource.cs @@ -297,7 +297,60 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml Assert.Equal("title", button.Content); } } + + [Fact] + public void Shorthand_Binding_With_Negation_Works() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + +