// ----------------------------------------------------------------------- // // Copyright 2013 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Styling { using System; using System.Reactive.Subjects; /// /// Provides an observable for a style. /// /// /// This class takes an activator and a value. The activator is an observable which produces /// a bool. When the activator produces true, this observable will produce . /// When the activator produces false (and before the activator returns a value) it will /// produce . /// internal class StyleBinding : IObservable, IObservableDescription { /// /// The subject that provides the observable implementation. /// private BehaviorSubject subject = new BehaviorSubject(PerspexProperty.UnsetValue); /// /// Initializes a new instance of the class. /// /// The activator. /// The activated value. /// The binding description. public StyleBinding( IObservable activator, object activatedValue, string description) { this.ActivatedValue = activatedValue; this.Description = description; activator.Subscribe( active => this.subject.OnNext(active ? this.ActivatedValue : PerspexProperty.UnsetValue), error => this.subject.OnError(error), () => this.subject.OnCompleted()); } /// /// Gets a description of the binding. /// public string Description { get; private set; } /// /// Gets the activated value. /// public object ActivatedValue { get; private set; } /// /// Notifies the provider that an observer is to receive notifications. /// /// The observer. /// IDisposable object used to unsubscribe from the observable sequence. public IDisposable Subscribe(IObserver observer) { Contract.Requires(observer != null); return this.subject.Subscribe(observer); } } }