private void PropertyChanged(object?sender, AvaloniaPropertyChangedEventArgs e)
        {
            if (e.Property == _property)
            {
                if (e is AvaloniaPropertyChangedEventArgs <T> typedArgs)
                {
                    var newValue = e.Sender.GetValue <T>(typedArgs.Property);

                    if (!_value.HasValue || !EqualityComparer <T> .Default.Equals(newValue, _value.Value))
                    {
                        _value = newValue;
                        PublishNext(_value);
                    }
                }
                else
                {
                    var newValue = e.Sender.GetValue(e.Property);

                    if (!Equals(newValue, _value))
                    {
                        _value = (T)newValue !;
                        PublishNext(_value);
                    }
                }
            }
        }
Beispiel #2
0
        public void Binding_To_Direct_Property_Logs_BindingError()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <string> >();
            var called = false;

            LogCallback checkLogMessage = (level, area, src, mt, pv) =>
            {
                if (level == LogEventLevel.Warning &&
                    area == LogArea.Binding &&
                    mt == "Error in binding to {Target}.{Property}: {Message}" &&
                    pv.Length == 3 &&
                    pv[0] is Class1 &&
                    object.ReferenceEquals(pv[1], Class1.FooProperty) &&
                    (string)pv[2] == "Binding Error Message")
                {
                    called = true;
                }
            };

            using (TestLogSink.Start(checkLogMessage))
            {
                target.Bind(Class1.FooProperty, source);
                source.OnNext("baz");
                source.OnNext(BindingValue <string> .BindingError(new InvalidOperationException("Binding Error Message")));
            }

            Assert.True(called);
        }
Beispiel #3
0
 // TODO -- what if Value() is used outside the context of a property?
 public void Used(BindingValue value)
 {
     if (_properties.Any())
     {
         LastProperty.Used(value);
     }
 }
        public void Bind_Logs_Binding_Error()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <double> >();
            var called = false;
            var expectedMessageTemplate = "Error in binding to {Target}.{Property}: {Message}";

            LogCallback checkLogMessage = (level, area, src, mt, pv) =>
            {
                if (level == LogEventLevel.Warning &&
                    area == LogArea.Binding &&
                    mt == expectedMessageTemplate)
                {
                    called = true;
                }
            };

            using (TestLogSink.Start(checkLogMessage))
            {
                target.Bind(Class1.QuxProperty, source);
                source.OnNext(6.7);
                source.OnNext(BindingValue <double> .BindingError(new InvalidOperationException("Foo")));

                Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
                Assert.True(called);
            }
        }
Beispiel #5
0
 /// <summary>
 /// called on profile reset
 /// </summary>
 internal void Reset()
 {
     _boundRotaryControl = null;
     _pulsesSinceBinding = 0d;
     _lastAngle          = 0d;
     _initialInputValue  = null;
 }
 public void OnNext(BindingValue <T> value)
 {
     if (value.HasValue)
     {
         _source.OnNext(value.Value);
     }
 }
Beispiel #7
0
 /// <summary>
 /// Called when a avalonia property changes on the object.
 /// </summary>
 /// <param name="property">The property whose value has changed.</param>
 /// <param name="oldValue">The old value of the property.</param>
 /// <param name="newValue">The new value of the property.</param>
 /// <param name="priority">The priority of the new value.</param>
 protected virtual void OnPropertyChanged <T>(
     AvaloniaProperty <T> property,
     Optional <T> oldValue,
     BindingValue <T> newValue,
     BindingPriority priority)
 {
 }
Beispiel #8
0
 void IObserver <BindingValue <T> > .OnNext(BindingValue <T> value)
 {
     if (value.HasValue && _isActive)
     {
         _binding.Subject.OnNext(value.Value);
     }
 }
Beispiel #9
0
 public void UsedValue(BindingValue value)
 {
     if (_models.Any())
     {
         currentReport.Used(value);
     }
 }
Beispiel #10
0
        /// <summary>
        /// Sets the value of a direct property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="value">The value.</param>
        private void SetDirectValueUnchecked <T>(DirectPropertyBase <T> property, BindingValue <T> value)
        {
            var p = AvaloniaPropertyRegistry.Instance.FindRegisteredDirect(this, property);

            if (p == null)
            {
                throw new ArgumentException($"Property '{property.Name} not registered on '{this.GetType()}");
            }

            LogIfError(property, value);

            switch (value.Type)
            {
            case BindingValueType.UnsetValue:
            case BindingValueType.BindingError:
                var fallback = value.HasValue ? value : value.WithValue(property.GetUnsetValue(GetType()));
                property.InvokeSetter(this, fallback);
                break;

            case BindingValueType.DataValidationError:
                property.InvokeSetter(this, value);
                break;

            case BindingValueType.Value:
            case BindingValueType.BindingErrorWithFallback:
            case BindingValueType.DataValidationErrorWithFallback:
                property.InvokeSetter(this, value);
                break;
            }

            if (p.IsDataValidationEnabled)
            {
                UpdateDataValidation(property, value);
            }
        }
Beispiel #11
0
        void IValueSink.ValueChanged <T>(
            StyledPropertyBase <T> property,
            BindingPriority priority,
            Optional <T> oldValue,
            BindingValue <T> newValue)
        {
            oldValue = oldValue.HasValue ? oldValue : GetInheritedOrDefault(property);
            newValue = newValue.HasValue ? newValue : newValue.WithValue(GetInheritedOrDefault(property));

            LogIfError(property, newValue);

            if (!EqualityComparer <T> .Default.Equals(oldValue.Value, newValue.Value))
            {
                RaisePropertyChanged(property, oldValue, newValue, priority);

                Logger.TryGet(LogEventLevel.Verbose)?.Log(
                    LogArea.Property,
                    this,
                    "{Property} changed from {$Old} to {$Value} with priority {Priority}",
                    property,
                    oldValue,
                    newValue,
                    (BindingPriority)priority);
            }
        }
 protected override void Initialize()
 {
     if (_target.TryGetTarget(out var target))
     {
         _value = (T)target.GetValue(_property) !;
         target.PropertyChanged += PropertyChanged;
     }
 }
Beispiel #13
0
        /// <summary>
        /// Raises the <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="property">The property that has changed.</param>
        /// <param name="oldValue">The old property value.</param>
        /// <param name="newValue">The new property value.</param>
        /// <param name="priority">The priority of the binding that produced the value.</param>
        protected internal void RaisePropertyChanged <T>(
            AvaloniaProperty <T> property,
            Optional <T> oldValue,
            BindingValue <T> newValue,
            BindingPriority priority = BindingPriority.LocalValue)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));

            VerifyAccess();

            property.Notifying?.Invoke(this, true);

            try
            {
                AvaloniaPropertyChangedEventArgs <T> e = null;
                var hasChanged = property.HasChangedSubscriptions;

                if (hasChanged || _propertyChanged != null)
                {
                    e = new AvaloniaPropertyChangedEventArgs <T>(
                        this,
                        property,
                        oldValue,
                        newValue,
                        priority);
                }

                OnPropertyChanged(property, oldValue, newValue, priority);

                if (hasChanged)
                {
                    property.NotifyChanged(e);
                }

                _propertyChanged?.Invoke(this, e);

                if (_inpcChanged != null)
                {
                    var inpce = new PropertyChangedEventArgs(property.Name);
                    _inpcChanged(this, inpce);
                }

                if (property.Inherits && _inheritanceChildren != null)
                {
                    foreach (var child in _inheritanceChildren)
                    {
                        child.InheritedPropertyChanged(
                            property,
                            oldValue,
                            newValue.ToOptional());
                    }
                }
            }
            finally
            {
                property.Notifying?.Invoke(this, false);
            }
        }
        private void EnsureTemplate()
        {
            if (_value.HasValue)
            {
                return;
            }

            _value = (T)_template.Build();
        }
Beispiel #15
0
        protected void SetValue(string device, string name, BindingValue value)
        {
            string key = device + "." + name;

            if (_values.ContainsKey(key))
            {
                _values[key].SetValue(value, false);
            }
        }
        private T GetValue()
        {
            if (_value.HasValue)
            {
                return(_value.Value);
            }

            _value = _valueFactory();
            return(_value.Value);
        }
Beispiel #17
0
        public override void ProcessNetworkData(string id, string value)
        {
            BindingValue bound = new BindingValue(value);

            _value.SetValue(bound, false);
            _receivedTrigger.FireTrigger(bound);
            ValueReceived?.Invoke(this, new Value {
                Text = value
            });
        }
        private static ValueEntity CreateValueEntity(BindingValue bindingValue)
        {
            //var val = ValueEntity.Create(bindingValue.Value, bindingValue.Key);
            var val = new ValueEntity(bindingValue.Value, bindingValue.Key);

            val.Properties = bindingValue.PropertyBag;
            val.IsSelected = val.Properties.Selected();

            return(val);
        }
 public override BindingValue Convert(BindingValue value, BindingValueUnit from, BindingValueUnit to)
 {
     if (from is DegreesUnit)
     {
         return new BindingValue(value.DoubleValue / 57.3d);
     }
     else
     {
         return new BindingValue(value.DoubleValue * 57.3d);
     }
 }
        public void DataValidationError_With_FallbackValue_Causes_Target_Update()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <string> >();

            target.Bind(Class1.FooProperty, source);
            source.OnNext("initial");
            source.OnNext(BindingValue <string> .DataValidationError(new InvalidOperationException("Foo"), "bar"));

            Assert.Equal("bar", target.GetValue(Class1.FooProperty));
        }
        public void Binding_Error_Reverts_To_Default_Value()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <string> >();

            target.Bind(Class1.FooProperty, source);
            source.OnNext("initial");
            source.OnNext(BindingValue <string> .BindingError(new InvalidOperationException("Foo")));

            Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
        }
Beispiel #22
0
 public override BindingValue Convert(BindingValue value, BindingValueUnit from, BindingValueUnit to)
 {
     if (from is DegreesUnit)
     {
         return(new BindingValue(value.DoubleValue / 57.3d));
     }
     else
     {
         return(new BindingValue(value.DoubleValue * 57.3d));
     }
 }
Beispiel #23
0
 public AvaloniaPropertyChangedEventArgs(
     IAvaloniaObject sender,
     AvaloniaProperty <T> property,
     Optional <T> oldValue,
     BindingValue <T> newValue,
     BindingPriority priority)
     : base(sender, priority)
 {
     Property = property;
     OldValue = oldValue;
     NewValue = newValue;
 }
        /// <inheritdoc/>
        internal override void InvokeSetter(IAvaloniaObject instance, BindingValue <TValue> value)
        {
            if (Setter == null)
            {
                throw new ArgumentException($"The property {Name} is readonly.");
            }

            if (value.HasValue)
            {
                Setter((TOwner)instance, value.Value);
            }
        }
Beispiel #25
0
        void IValueSink.ValueChanged <TValue>(
            StyledPropertyBase <TValue> property,
            BindingPriority priority,
            Optional <TValue> oldValue,
            BindingValue <TValue> newValue)
        {
            if (priority == BindingPriority.LocalValue)
            {
                _localValue = default;
            }

            UpdateEffectiveValue();
        }
Beispiel #26
0
        protected override void OnPropertyChanged <T>(
            AvaloniaProperty <T> property,
            Optional <T> oldValue,
            BindingValue <T> newValue,
            BindingPriority priority)
        {
            base.OnPropertyChanged(property, oldValue, newValue, priority);

            if (property == SelectedDateProperty)
            {
                DataValidationErrors.SetError(this, newValue.Error);
            }
        }
Beispiel #27
0
        protected override void OnPropertyChanged <T>(
            AvaloniaProperty <T> property,
            Optional <T> oldValue,
            BindingValue <T> newValue,
            BindingPriority priority)
        {
            base.OnPropertyChanged(property, oldValue, newValue, priority);

            if (property == ButtonSpinnerLocationProperty)
            {
                UpdatePseudoClasses(newValue.GetValueOrDefault <Location>());
            }
        }
        public void Binding_Non_Validated_Direct_Property_Does_Not_Call_UpdateDataValidation()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <int> >();

            target.Bind(Class1.NonValidatedDirectProperty, source);
            source.OnNext(6);
            source.OnNext(BindingValue <int> .BindingError(new Exception()));
            source.OnNext(BindingValue <int> .DataValidationError(new Exception()));
            source.OnNext(6);

            Assert.Empty(target.Notifications);
        }
Beispiel #29
0
        private void ProcessIndicators()
        {
            BindingValue rwrPower = _falconInterface.GetValue("Aux Threat Warning", "power indicator");

            IsOn = rwrPower.BoolValue;

            SetValue("Threat Warning Prime", "handoff indicator", _falconInterface.GetValue("Threat Warning Prime", "handoff indicator"));
            SetValue("Threat Warning Prime", "launch indicator", new BindingValue(_falconInterface.GetValue("Threat Warning Prime", "launch indicator").BoolValue ? _flash4Hz : false));

            BindingValue twpPriority = _falconInterface.GetValue("Threat Warning Prime", "prioirty mode indicator");

            if (twpPriority.BoolValue && HasHiddenPriorityContacts)
            {
                SetValue("Threat Warning Prime", "prioirty mode indicator", new BindingValue(_flash4Hz));
            }
            else
            {
                SetValue("Threat Warning Prime", "prioirty mode indicator", twpPriority);
            }
            SetValue("Threat Warning Prime", "open mode indicator", new BindingValue(rwrPower.BoolValue && !twpPriority.BoolValue));
            SetValue("Threat Warning Prime", "naval indicator", _falconInterface.GetValue("Threat Warning Prime", "naval indicator"));

            BindingValue twpUnknown = _falconInterface.GetValue("Threat Warning Prime", "unknown indicator");

            if (rwrPower.BoolValue && !twpUnknown.BoolValue && HasHiddenUnknownContacts)
            {
                SetValue("Threat Warning Prime", "unknown indicator", new BindingValue(_flash4Hz));
            }
            else
            {
                SetValue("Threat Warning Prime", "unknown indicator", twpUnknown);
            }
            SetValue("Threat Warning Prime", "unknown mode indicator", twpUnknown);

            SetValue("Threat Warning Prime", "target step indicator", _falconInterface.GetValue("Threat Warning Prime", "target step indicator"));

            BindingValue twaSearch = _falconInterface.GetValue("Aux Threat Warning", "search indicator");

            if (!twaSearch.BoolValue && HasSearchModeContacts)
            {
                SetValue("Aux Threat Warning", "search indicator", new BindingValue(_flash4Hz));
            }
            else
            {
                SetValue("Aux Threat Warning", "search indicator", twaSearch);
            }

            SetValue("Aux Threat Warning", "activity indicator", _falconInterface.GetValue("Aux Threat Warning", "activity indicator"));
            SetValue("Aux Threat Warning", "low altitude indicator", _falconInterface.GetValue("Aux Threat Warning", "low altitude indicator"));
            SetValue("Aux Threat Warning", "power indicator", _falconInterface.GetValue("Aux Threat Warning", "power indicator"));
        }
        public bool Value(string key, Action<BindingValue> callback)
        {
            if (!Has(key)) return false;

            var value = new BindingValue{
                RawKey = key,
                RawValue = Get(key),
                Source = Provenance
            };

            callback(value);

            return true;
        }
Beispiel #31
0
        public override BindingValue Convert(BindingValue value, BindingValueUnit from, BindingValueUnit to)
        {
            PressureUnit fromUnit = from as PressureUnit;
            PressureUnit toUnit = to as PressureUnit;

            if (fromUnit != null && toUnit != null)
            {
                double newValue = ((value.DoubleValue * fromUnit.MassConversionFactor) / toUnit.MassConversionFactor) / (fromUnit.AreaConversionFactor / toUnit.AreaConversionFactor);
                return new BindingValue(newValue);
            }
            else
            {
                return BindingValue.Empty;
            }
        }
Beispiel #32
0
        private void InputValue_Execute(object action, HeliosActionEventArgs e)
        {
            if (e.Value.IsEmptyValue)
            {
                return;
            }
            BindingValue outputValue = new BindingValue(Calibration.Interpolate(e.Value.DoubleValue));

            if (outputValue.IsIdenticalTo(_interpolated))
            {
                return;
            }
            _interpolated = outputValue;
            _outputValue?.SetValue(outputValue, false);
        }
Beispiel #33
0
        public override BindingValue Convert(BindingValue value, BindingValueUnit from, BindingValueUnit to)
        {
            PressureUnit fromUnit = from as PressureUnit;
            PressureUnit toUnit   = to as PressureUnit;

            if (fromUnit != null && toUnit != null)
            {
                double newValue = ((value.DoubleValue * fromUnit.MassConversionFactor) / toUnit.MassConversionFactor) / (fromUnit.AreaConversionFactor / toUnit.AreaConversionFactor);
                return(new BindingValue(newValue));
            }
            else
            {
                return(BindingValue.Empty);
            }
        }
Beispiel #34
0
        public override BindingValue Convert(BindingValue value, BindingValueUnit from, BindingValueUnit to)
        {
            TimeUnit fromUnit = from as TimeUnit;
            TimeUnit toUnit = to as TimeUnit;

            if (fromUnit != null && toUnit != null)
            {
                double newValue = (value.DoubleValue * fromUnit.ConversionFactor) / toUnit.ConversionFactor;
                return new BindingValue(newValue);
            }
            else
            {
                return BindingValue.Empty;
            }
        }
 public void UsedValue(BindingValue value)
 {
     if (_models.Any())
     {
         currentReport.Used(value);
     }
 }
 public void UsedValue(BindingValue value)
 {
 }
Beispiel #37
0
 protected void SetValue(string device, string name, BindingValue value)
 {
     string key = device + "." + name;
     if (_values.ContainsKey(key))
     {
         _values[key].SetValue(value, false);
     }
 }
 public void Used(BindingValue value)
 {
     _values.Add(value);
 }
        public override void PollValue(JoystickState state)
        {
            bool newValue = GetValue(state);

            if (_lastPollValue != newValue)
            {
                BindingValue bindValue = new BindingValue(newValue);
                _lastPollValue = newValue;

                _value.SetValue(bindValue, false);

                if (newValue)
                {
                    _press.FireTrigger(bindValue);
                }
                else
                {
                    _release.FireTrigger(bindValue);
                }
            }
        }
 /// <summary>
 /// Converts value from one unit to another.
 /// </summary>
 /// <param name="value">Value which will be converted.</param>
 /// <param name="from">Unit which this value is currently in.</param>
 /// <param name="to">Unit to use for the return value.</param>
 /// <returns></returns>
 public abstract BindingValue Convert(BindingValue value, BindingValueUnit from, BindingValueUnit to);
        public override void PollValue(JoystickState state)
        {
            POVDirection newValue = GetValue(state);

            if (_lastPollValue != newValue)
            {
                BindingValue bindValue = new BindingValue((double)newValue);
                switch (_lastPollValue)
                {
                    case POVDirection.Up:
                        _upExit.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.UpAndRight:
                        _upRightExit.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.Right:
                        _rightExit.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.DownAndRight:
                        _downRightEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.Down:
                        _downExit.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.DownAndLeft:
                        _downLeftExit.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.Left:
                        _leftEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.UpAndLeft:
                        _upLeftExit.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.Center:
                        _centerExit.FireTrigger(BindingValue.Empty);
                        break;
                }

                switch (newValue)
                {
                    case POVDirection.Up:
                        _upEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.UpAndRight:
                        _upRightEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.Right:
                        _rightEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.DownAndRight:
                        _downRightEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.Down:
                        _downEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.DownAndLeft:
                        _downLeftEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.Left:
                        _leftEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.UpAndLeft:
                        _upLeftEnter.FireTrigger(BindingValue.Empty);
                        break;
                    case POVDirection.Center:
                        _centerEnter.FireTrigger(BindingValue.Empty);
                        break;
                }

                _value.SetValue(bindValue, false);
                _lastPollValue = newValue;
            }
        }