From 30a756fb8305a4997bc2533260d5cda8c8d44d24 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 17 Jan 2016 22:23:33 +0100 Subject: [PATCH] Refactored binding somewhat. - Simplified PerpsexObject interface to a simplified single interface, IPerspexObject - Moved GetObservable etc to extension method --- samples/TestApplicationShared/GalleryStyle.cs | 2 +- .../Perspex.Markup.Xaml/Data/Binding.cs | 49 +----- .../Perspex.Markup.Xaml/Data/IXamlBinding.cs | 4 +- .../Perspex.Markup.Xaml/Data/MultiBinding.cs | 6 +- src/Perspex.Animation/Animate.cs | 4 +- src/Perspex.Base/IObservablePropertyBag.cs | 95 ------------ .../{IPropertyBag.cs => IPerspexObject.cs} | 60 +++++--- src/Perspex.Base/Perspex.Base.csproj | 3 +- src/Perspex.Base/PerspexObject.cs | 93 +----------- src/Perspex.Base/PerspexObjectExtensions.cs | 140 +++++++++++++++++- .../Presenters/ScrollContentPresenter.cs | 4 +- .../Presenters/TextPresenter.cs | 8 +- src/Perspex.Controls/Primitives/AccessText.cs | 2 +- src/Perspex.Controls/Primitives/ScrollBar.cs | 8 +- .../Primitives/TemplatedControl.cs | 2 +- src/Perspex.Controls/Primitives/Track.cs | 2 +- src/Perspex.Controls/RadioButton.cs | 2 +- src/Perspex.Controls/ScrollViewer.cs | 10 +- src/Perspex.Controls/TextBlock.cs | 8 +- src/Perspex.Controls/TextBox.cs | 4 +- src/Perspex.Controls/TopLevel.cs | 4 +- src/Perspex.Diagnostics/DevTools.cs | 2 +- .../Views/ControlDetailsView.cs | 2 +- .../Views/LogicalTreeView.cs | 2 +- .../Views/VisualTreeView.cs | 2 +- src/Perspex.SceneGraph/Animation/CrossFade.cs | 4 +- .../Media/MatrixTransform.cs | 2 +- .../Media/RotateTransform.cs | 2 +- .../Media/TranslateTransform.cs | 4 +- src/Perspex.Styling/Styling/IStyleable.cs | 2 +- .../Styling/ITemplatedControl.cs | 9 +- src/Perspex.Styling/Styling/Selectors.cs | 2 +- .../Data/BindingTests.cs | 124 +++++++--------- .../Data/BindingTests_TemplatedParent.cs | 40 ++--- .../Data/MultiBindingTests.cs | 5 +- .../SelectorTests_Child.cs | 53 +------ .../SelectorTests_Descendent.cs | 52 +------ .../TestControlBase.cs | 53 +------ .../TestTemplatedControl.cs | 57 +------ 39 files changed, 337 insertions(+), 590 deletions(-) delete mode 100644 src/Perspex.Base/IObservablePropertyBag.cs rename src/Perspex.Base/{IPropertyBag.cs => IPerspexObject.cs} (57%) diff --git a/samples/TestApplicationShared/GalleryStyle.cs b/samples/TestApplicationShared/GalleryStyle.cs index b29e22d2ec..38b29deab9 100644 --- a/samples/TestApplicationShared/GalleryStyle.cs +++ b/samples/TestApplicationShared/GalleryStyle.cs @@ -23,7 +23,7 @@ namespace TestApplication { Setters = new[] { - new Setter (TemplatedControl.TemplateProperty, new FuncControlTemplate (TabControlTemplate)) + new Setter (TemplatedControl.TemplateProperty, new FuncControlTemplate(TabControlTemplate)) } }, diff --git a/src/Markup/Perspex.Markup.Xaml/Data/Binding.cs b/src/Markup/Perspex.Markup.Xaml/Data/Binding.cs index 00ed242855..f7546b3db8 100644 --- a/src/Markup/Perspex.Markup.Xaml/Data/Binding.cs +++ b/src/Markup/Perspex.Markup.Xaml/Data/Binding.cs @@ -55,7 +55,7 @@ namespace Perspex.Markup.Xaml.Data /// /// The target instance. /// The target property. - public void Bind(IObservablePropertyBag instance, PerspexProperty property) + public void Bind(IPerspexObject instance, PerspexProperty property) { Contract.Requires(instance != null); Contract.Requires(property != null); @@ -67,7 +67,8 @@ namespace Perspex.Markup.Xaml.Data if (subject != null) { - Bind(instance, property, subject); + var mode = Mode == BindingMode.Default ? property.DefaultBindingMode : Mode; + instance.Bind(property, subject, mode, Priority); } } @@ -81,7 +82,7 @@ namespace Perspex.Markup.Xaml.Data /// /// An . public ISubject CreateSubject( - IObservablePropertyBag target, + IPerspexObject target, Type targetType, bool targetIsDataContext = false) { @@ -125,42 +126,6 @@ namespace Perspex.Markup.Xaml.Data ConverterParameter); } - /// - /// Applies a binding subject to a property on an instance. - /// - /// The target instance. - /// The target property. - /// The binding subject. - internal void Bind(IObservablePropertyBag target, PerspexProperty property, ISubject subject) - { - Contract.Requires(target != null); - Contract.Requires(property != null); - Contract.Requires(subject != null); - - var mode = Mode == BindingMode.Default ? - property.DefaultBindingMode : Mode; - - switch (mode) - { - case BindingMode.Default: - case BindingMode.OneWay: - target.Bind(property, subject, Priority); - break; - case BindingMode.TwoWay: - target.BindTwoWay(property, subject, Priority); - break; - case BindingMode.OneTime: - target.GetObservable(Control.DataContextProperty).Subscribe(dataContext => - { - subject.Take(1).Subscribe(x => target.SetValue(property, x, Priority)); - }); - break; - case BindingMode.OneWayToSource: - target.GetObservable(property).Subscribe(subject); - break; - } - } - private static PathInfo ParsePath(string path) { var result = new PathInfo(); @@ -209,7 +174,7 @@ namespace Perspex.Markup.Xaml.Data } private ExpressionObserver CreateDataContextSubject( - IObservablePropertyBag target, + IPerspexObject target, string path, bool targetIsDataContext) { @@ -231,7 +196,7 @@ namespace Perspex.Markup.Xaml.Data { return new ExpressionObserver( target.GetObservable(Visual.VisualParentProperty) - .OfType() + .OfType() .Select(x => x.GetObservable(Control.DataContextProperty)) .Switch(), path); @@ -239,7 +204,7 @@ namespace Perspex.Markup.Xaml.Data } private ExpressionObserver CreateTemplatedParentSubject( - IObservablePropertyBag target, + IPerspexObject target, string path) { Contract.Requires(target != null); diff --git a/src/Markup/Perspex.Markup.Xaml/Data/IXamlBinding.cs b/src/Markup/Perspex.Markup.Xaml/Data/IXamlBinding.cs index e72fb5eccc..144302a9a7 100644 --- a/src/Markup/Perspex.Markup.Xaml/Data/IXamlBinding.cs +++ b/src/Markup/Perspex.Markup.Xaml/Data/IXamlBinding.cs @@ -16,7 +16,7 @@ namespace Perspex.Markup.Xaml.Data /// /// The target instance. /// The target property. - void Bind(IObservablePropertyBag instance, PerspexProperty property); + void Bind(IPerspexObject instance, PerspexProperty property); /// /// Creates a subject that can be used to get and set the value of the binding. @@ -28,7 +28,7 @@ namespace Perspex.Markup.Xaml.Data /// /// An . ISubject CreateSubject( - IObservablePropertyBag target, + IPerspexObject target, Type targetType, bool targetIsDataContext = false); } diff --git a/src/Markup/Perspex.Markup.Xaml/Data/MultiBinding.cs b/src/Markup/Perspex.Markup.Xaml/Data/MultiBinding.cs index 4ea1feb9a2..41f67a0467 100644 --- a/src/Markup/Perspex.Markup.Xaml/Data/MultiBinding.cs +++ b/src/Markup/Perspex.Markup.Xaml/Data/MultiBinding.cs @@ -48,7 +48,7 @@ namespace Perspex.Markup.Xaml.Data /// /// The target instance. /// The target property. - public void Bind(IObservablePropertyBag instance, PerspexProperty property) + public void Bind(IPerspexObject instance, PerspexProperty property) { var subject = CreateSubject(instance, property.PropertyType); @@ -68,7 +68,7 @@ namespace Perspex.Markup.Xaml.Data /// /// An . public ISubject CreateSubject( - IObservablePropertyBag target, + IPerspexObject target, Type targetType, bool targetIsDataContext = false) { @@ -91,7 +91,7 @@ namespace Perspex.Markup.Xaml.Data /// The target instance. /// The target property. /// The binding subject. - internal void Bind(IObservablePropertyBag target, PerspexProperty property, ISubject subject) + internal void Bind(IPerspexObject target, PerspexProperty property, ISubject subject) { var mode = Mode == BindingMode.Default ? property.DefaultBindingMode : Mode; diff --git a/src/Perspex.Animation/Animate.cs b/src/Perspex.Animation/Animate.cs index 85db3e71b7..d076c23953 100644 --- a/src/Perspex.Animation/Animate.cs +++ b/src/Perspex.Animation/Animate.cs @@ -96,7 +96,7 @@ namespace Perspex.Animation /// The duration of the animation. /// An that can be used to track or stop the animation. public static Animation Property( - IObservablePropertyBag target, + IPerspexObject target, PerspexProperty property, object start, object finish, @@ -119,7 +119,7 @@ namespace Perspex.Animation /// The duration of the animation. /// An that can be used to track or stop the animation. public static Animation Property( - IObservablePropertyBag target, + IPerspexObject target, PerspexProperty property, T start, T finish, diff --git a/src/Perspex.Base/IObservablePropertyBag.cs b/src/Perspex.Base/IObservablePropertyBag.cs deleted file mode 100644 index 451cb63a89..0000000000 --- a/src/Perspex.Base/IObservablePropertyBag.cs +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) The Perspex Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - -using System; -using System.Reactive.Subjects; - -namespace Perspex -{ - /// - /// Interface for getting/setting bindings on an object. - /// - public interface IObservablePropertyBag : IPropertyBag - { - /// - /// Binds a to an observable. - /// - /// The property. - /// The observable. - /// The priority of the binding. - /// - /// A disposable which can be used to terminate the binding. - /// - IDisposable Bind( - PerspexProperty property, - IObservable source, - BindingPriority priority = BindingPriority.LocalValue); - - /// - /// Binds a to an observable. - /// - /// The type of the property. - /// The property. - /// The observable. - /// The priority of the binding. - /// - /// A disposable which can be used to terminate the binding. - /// - IDisposable Bind( - PerspexProperty property, - IObservable source, - BindingPriority priority = BindingPriority.LocalValue); - - /// - /// Initiates a two-way binding between s. - /// - /// The property on this object. - /// The source object. - /// The property on the source object. - /// The priority of the binding. - /// - /// A disposable which can be used to terminate the binding. - /// - /// - /// The binding is first carried out from to this. - /// - IDisposable BindTwoWay( - PerspexProperty property, - PerspexObject source, - PerspexProperty sourceProperty, - BindingPriority priority = BindingPriority.LocalValue); - - /// - /// Initiates a two-way binding between a and an - /// . - /// - /// The property on this object. - /// The subject to bind to. - /// The priority of the binding. - /// - /// A disposable which can be used to terminate the binding. - /// - /// - /// The binding is first carried out from to this. - /// - IDisposable BindTwoWay( - PerspexProperty property, - ISubject source, - BindingPriority priority = BindingPriority.LocalValue); - - /// - /// Gets an observable for a . - /// - /// The property. - /// An observable. - IObservable GetObservable(PerspexProperty property); - - /// - /// Gets an observable for a . - /// - /// The type of the property. - /// The property. - /// An observable. - IObservable GetObservable(PerspexProperty property); - } -} \ No newline at end of file diff --git a/src/Perspex.Base/IPropertyBag.cs b/src/Perspex.Base/IPerspexObject.cs similarity index 57% rename from src/Perspex.Base/IPropertyBag.cs rename to src/Perspex.Base/IPerspexObject.cs index fe29c349ef..47ccd7b9ff 100644 --- a/src/Perspex.Base/IPropertyBag.cs +++ b/src/Perspex.Base/IPerspexObject.cs @@ -1,23 +1,19 @@ // Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using System; + namespace Perspex { /// /// Interface for getting/setting values on an object. /// - public interface IPropertyBag + public interface IPerspexObject { /// - /// Gets the object that inherited values are inherited from. + /// Raised when a value changes on this object. /// - IPropertyBag InheritanceParent { get; } - - /// - /// Clears a 's local value. - /// - /// The property. - void ClearValue(PerspexProperty property); + event EventHandler PropertyChanged; /// /// Gets a value. @@ -34,13 +30,6 @@ namespace Perspex /// The value. T GetValue(PerspexProperty property); - /// - /// Checks whether a is registered on this object. - /// - /// The property. - /// True if the property is registered, otherwise false. - bool IsRegistered(PerspexProperty property); - /// /// Checks whether a is set on this object. /// @@ -54,7 +43,10 @@ namespace Perspex /// The property. /// The value. /// The priority of the value. - void SetValue(PerspexProperty property, object value, BindingPriority priority = BindingPriority.LocalValue); + void SetValue( + PerspexProperty property, + object value, + BindingPriority priority = BindingPriority.LocalValue); /// /// Sets a value. @@ -63,6 +55,38 @@ namespace Perspex /// The property. /// The value. /// The priority of the value. - void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue); + void SetValue( + PerspexProperty property, + T value, + BindingPriority priority = BindingPriority.LocalValue); + + /// + /// Binds a to an observable. + /// + /// The property. + /// The observable. + /// The priority of the binding. + /// + /// A disposable which can be used to terminate the binding. + /// + IDisposable Bind( + PerspexProperty property, + IObservable source, + BindingPriority priority = BindingPriority.LocalValue); + + /// + /// Binds a to an observable. + /// + /// The type of the property. + /// The property. + /// The observable. + /// The priority of the binding. + /// + /// A disposable which can be used to terminate the binding. + /// + IDisposable Bind( + PerspexProperty property, + IObservable source, + BindingPriority priority = BindingPriority.LocalValue); } } \ No newline at end of file diff --git a/src/Perspex.Base/Perspex.Base.csproj b/src/Perspex.Base/Perspex.Base.csproj index b0f6d928ff..b08bce7e2f 100644 --- a/src/Perspex.Base/Perspex.Base.csproj +++ b/src/Perspex.Base/Perspex.Base.csproj @@ -45,8 +45,7 @@ - - + diff --git a/src/Perspex.Base/PerspexObject.cs b/src/Perspex.Base/PerspexObject.cs index b95758a7e7..eeb67a2482 100644 --- a/src/Perspex.Base/PerspexObject.cs +++ b/src/Perspex.Base/PerspexObject.cs @@ -23,7 +23,7 @@ namespace Perspex /// /// This class is analogous to DependencyObject in WPF. /// - public class PerspexObject : IObservablePropertyBag, INotifyPropertyChanged + public class PerspexObject : IPerspexObject, INotifyPropertyChanged { /// /// The parent object that inherited values are inherited from. @@ -89,11 +89,6 @@ namespace Perspex remove { _inpcChanged -= value; } } - /// - /// Gets the object that inherited values are inherited from. - /// - IPropertyBag IPropertyBag.InheritanceParent => InheritanceParent; - /// /// Gets or sets the parent object that inherited values /// are inherited from. @@ -188,7 +183,7 @@ namespace Perspex SetValue(binding.Property, sourceBinding.Source.GetValue(sourceBinding.Property), binding.Priority); break; case BindingMode.OneWayToSource: - sourceBinding.Source.Bind(sourceBinding.Property, GetObservable(binding.Property), binding.Priority); + sourceBinding.Source.Bind(sourceBinding.Property, this.GetObservable(binding.Property), binding.Priority); break; case BindingMode.TwoWay: BindTwoWay(binding.Property, sourceBinding.Source, sourceBinding.Property); @@ -223,81 +218,6 @@ namespace Perspex SetValue(property, PerspexProperty.UnsetValue); } - /// - /// Gets an observable for a . - /// - /// The property. - /// An observable. - public IObservable GetObservable(PerspexProperty property) - { - Contract.Requires(property != null); - - return new PerspexObservable( - observer => - { - EventHandler handler = (s, e) => - { - if (e.Property == property) - { - observer.OnNext(e.NewValue); - } - }; - - observer.OnNext(GetValue(property)); - - PropertyChanged += handler; - - return Disposable.Create(() => - { - PropertyChanged -= handler; - }); - }, - GetDescription(property)); - } - - /// - /// Gets an observable for a . - /// - /// The property type. - /// The property. - /// An observable. - public IObservable GetObservable(PerspexProperty property) - { - Contract.Requires(property != null); - - return GetObservable((PerspexProperty)property).Cast(); - } - - /// - /// Gets an observable for a . - /// - /// The type of the property. - /// The property. - /// An observable which when subscribed pushes the old and new values of the - /// property each time it is changed. - public IObservable> GetObservableWithHistory(PerspexProperty property) - { - return new PerspexObservable>( - observer => - { - EventHandler handler = (s, e) => - { - if (e.Property == property) - { - observer.OnNext(Tuple.Create((T)e.OldValue, (T)e.NewValue)); - } - }; - - PropertyChanged += handler; - - return Disposable.Create(() => - { - PropertyChanged -= handler; - }); - }, - GetDescription(property)); - } - /// /// Gets a value. /// @@ -589,7 +509,7 @@ namespace Perspex return new CompositeDisposable( Bind(property, source.GetObservable(sourceProperty)), - source.Bind(sourceProperty, GetObservable(property))); + source.Bind(sourceProperty, this.GetObservable(property))); } /// @@ -619,7 +539,7 @@ namespace Perspex return new CompositeDisposable( Bind(property, source), - GetObservable(property).Subscribe(source)); + this.GetObservable(property).Subscribe(source)); } /// @@ -638,11 +558,6 @@ namespace Perspex } /// - bool IPropertyBag.IsRegistered(PerspexProperty property) - { - return PerspexPropertyRegistry.Instance.IsRegistered(this, property); - } - /// /// Gets all priority values set on the object. /// diff --git a/src/Perspex.Base/PerspexObjectExtensions.cs b/src/Perspex.Base/PerspexObjectExtensions.cs index 7ba3fd33d4..52c8042ac8 100644 --- a/src/Perspex.Base/PerspexObjectExtensions.cs +++ b/src/Perspex.Base/PerspexObjectExtensions.cs @@ -2,8 +2,10 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using System.Linq; +using System.Reactive.Disposables; using System.Reactive.Linq; +using System.Reactive.Subjects; +using Perspex.Reactive; namespace Perspex { @@ -12,6 +14,131 @@ namespace Perspex /// public static class PerspexObjectExtensions { + /// + /// Gets an observable for a . + /// + /// The object. + /// The property. + /// An observable. + public static IObservable GetObservable(this IPerspexObject o, PerspexProperty property) + { + Contract.Requires(o != null); + Contract.Requires(property != null); + + return new PerspexObservable( + observer => + { + EventHandler handler = (s, e) => + { + if (e.Property == property) + { + observer.OnNext(e.NewValue); + } + }; + + observer.OnNext(o.GetValue(property)); + + o.PropertyChanged += handler; + + return Disposable.Create(() => + { + o.PropertyChanged -= handler; + }); + }, + GetDescription(o, property)); + } + + /// + /// Gets an observable for a . + /// + /// The object. + /// The property type. + /// The property. + /// An observable. + public static IObservable GetObservable(this IPerspexObject o, PerspexProperty property) + { + Contract.Requires(o != null); + Contract.Requires(property != null); + + return o.GetObservable((PerspexProperty)property).Cast(); + } + + /// + /// Gets an observable for a . + /// + /// The object. + /// The type of the property. + /// The property. + /// + /// An observable which when subscribed pushes the old and new values of the property each + /// time it is changed. + /// + public static IObservable> GetObservableWithHistory( + this IPerspexObject o, + PerspexProperty property) + { + Contract.Requires(o != null); + Contract.Requires(property != null); + + return new PerspexObservable>( + observer => + { + EventHandler handler = (s, e) => + { + if (e.Property == property) + { + observer.OnNext(Tuple.Create((T)e.OldValue, (T)e.NewValue)); + } + }; + + o.PropertyChanged += handler; + + return Disposable.Create(() => + { + o.PropertyChanged -= handler; + }); + }, + GetDescription(o, property)); + } + + /// + /// Binds a property to a subject according to a . + /// + /// The object. + /// The property to bind. + /// The binding source. + /// The binding mode. + /// The binding priority. + /// + public static IDisposable Bind( + this IPerspexObject o, + PerspexProperty property, + ISubject source, + BindingMode mode, + BindingPriority priority = BindingPriority.LocalValue) + { + Contract.Requires(o != null); + Contract.Requires(property != null); + Contract.Requires(source != null); + + switch (mode) + { + case BindingMode.Default: + case BindingMode.OneWay: + return o.Bind(property, source, priority); + case BindingMode.TwoWay: + return new CompositeDisposable( + o.Bind(property, source, priority), + o.GetObservable(property).Subscribe(source)); + case BindingMode.OneTime: + return source.Take(1).Subscribe(x => o.SetValue(property, x, priority)); + case BindingMode.OneWayToSource: + return o.GetObservable(property).Subscribe(source); + default: + throw new ArgumentException("Invalid binding mode."); + } + } + /// /// Subscribes to a property changed notifications for changes that originate from a /// . @@ -52,6 +179,17 @@ namespace Perspex return observable.Subscribe(e => SubscribeAdapter(e, handler)); } + /// + /// Gets a description of a property that van be used in observables. + /// + /// The object. + /// The property + /// The description. + private static string GetDescription(IPerspexObject o, PerspexProperty property) + { + return $"{o.GetType().Name}.{property.Name}"; + } + /// /// Observer method for . diff --git a/src/Perspex.Controls/Presenters/ScrollContentPresenter.cs b/src/Perspex.Controls/Presenters/ScrollContentPresenter.cs index 1ea8b3ef41..d8cc99105f 100644 --- a/src/Perspex.Controls/Presenters/ScrollContentPresenter.cs +++ b/src/Perspex.Controls/Presenters/ScrollContentPresenter.cs @@ -61,7 +61,7 @@ namespace Perspex.Controls.Presenters { AddHandler(RequestBringIntoViewEvent, BringIntoViewRequested); - GetObservable(ChildProperty).Subscribe(ChildChanged); + this.GetObservable(ChildProperty).Subscribe(ChildChanged); } /// @@ -236,7 +236,7 @@ namespace Perspex.Controls.Presenters { scrollable.InvalidateScroll = () => UpdateFromScrollable(scrollable); _scrollableSubscription = new CompositeDisposable( - GetObservable(OffsetProperty).Skip(1).Subscribe(x => scrollable.Offset = x), + this.GetObservable(OffsetProperty).Skip(1).Subscribe(x => scrollable.Offset = x), Disposable.Create(() => scrollable.InvalidateScroll = null)); UpdateFromScrollable(scrollable); } diff --git a/src/Perspex.Controls/Presenters/TextPresenter.cs b/src/Perspex.Controls/Presenters/TextPresenter.cs index 8c25fc5a4f..c923340bd3 100644 --- a/src/Perspex.Controls/Presenters/TextPresenter.cs +++ b/src/Perspex.Controls/Presenters/TextPresenter.cs @@ -33,15 +33,15 @@ namespace Perspex.Controls.Presenters _caretTimer.Interval = TimeSpan.FromMilliseconds(500); _caretTimer.Tick += CaretTimerTick; - _canScrollHorizontally = GetObservable(TextWrappingProperty) + _canScrollHorizontally = this.GetObservable(TextWrappingProperty) .Select(x => x == TextWrapping.NoWrap); Observable.Merge( - GetObservable(SelectionStartProperty), - GetObservable(SelectionEndProperty)) + this.GetObservable(SelectionStartProperty), + this.GetObservable(SelectionEndProperty)) .Subscribe(_ => InvalidateFormattedText()); - GetObservable(CaretIndexProperty) + this.GetObservable(CaretIndexProperty) .Subscribe(CaretIndexChanged); } diff --git a/src/Perspex.Controls/Primitives/AccessText.cs b/src/Perspex.Controls/Primitives/AccessText.cs index 9ad2640534..38dab95803 100644 --- a/src/Perspex.Controls/Primitives/AccessText.cs +++ b/src/Perspex.Controls/Primitives/AccessText.cs @@ -37,7 +37,7 @@ namespace Perspex.Controls.Primitives /// public AccessText() { - GetObservable(TextProperty).Subscribe(TextChanged); + this.GetObservable(TextProperty).Subscribe(TextChanged); } /// diff --git a/src/Perspex.Controls/Primitives/ScrollBar.cs b/src/Perspex.Controls/Primitives/ScrollBar.cs index 06961d569f..fc40ad3d18 100644 --- a/src/Perspex.Controls/Primitives/ScrollBar.cs +++ b/src/Perspex.Controls/Primitives/ScrollBar.cs @@ -36,10 +36,10 @@ namespace Perspex.Controls.Primitives public ScrollBar() { var isVisible = Observable.Merge( - GetObservable(MinimumProperty).Select(_ => Unit.Default), - GetObservable(MaximumProperty).Select(_ => Unit.Default), - GetObservable(ViewportSizeProperty).Select(_ => Unit.Default), - GetObservable(VisibilityProperty).Select(_ => Unit.Default)) + this.GetObservable(MinimumProperty).Select(_ => Unit.Default), + this.GetObservable(MaximumProperty).Select(_ => Unit.Default), + this.GetObservable(ViewportSizeProperty).Select(_ => Unit.Default), + this.GetObservable(VisibilityProperty).Select(_ => Unit.Default)) .Select(_ => CalculateIsVisible()); Bind(IsVisibleProperty, isVisible, BindingPriority.Style); } diff --git a/src/Perspex.Controls/Primitives/TemplatedControl.cs b/src/Perspex.Controls/Primitives/TemplatedControl.cs index b6ce37fa88..8ec6cece63 100644 --- a/src/Perspex.Controls/Primitives/TemplatedControl.cs +++ b/src/Perspex.Controls/Primitives/TemplatedControl.cs @@ -231,7 +231,7 @@ namespace Perspex.Controls.Primitives // If the binding is a template binding, then complete when the Template changes. if (source.Priority == BindingPriority.TemplatedParent) { - var templateChanged = GetObservable(TemplateProperty).Skip(1); + var templateChanged = this.GetObservable(TemplateProperty).Skip(1); result.SourceObservable = result.Source.GetObservable(result.Property) .TakeUntil(templateChanged); diff --git a/src/Perspex.Controls/Primitives/Track.cs b/src/Perspex.Controls/Primitives/Track.cs index 4e343b65b3..6fe4f3811b 100644 --- a/src/Perspex.Controls/Primitives/Track.cs +++ b/src/Perspex.Controls/Primitives/Track.cs @@ -38,7 +38,7 @@ namespace Perspex.Controls.Primitives public Track() { - GetObservableWithHistory(ThumbProperty).Subscribe(val => + this.GetObservableWithHistory(ThumbProperty).Subscribe(val => { if (val.Item1 != null) { diff --git a/src/Perspex.Controls/RadioButton.cs b/src/Perspex.Controls/RadioButton.cs index 9f74de4940..2c254d67ba 100644 --- a/src/Perspex.Controls/RadioButton.cs +++ b/src/Perspex.Controls/RadioButton.cs @@ -12,7 +12,7 @@ namespace Perspex.Controls { public RadioButton() { - GetObservable(IsCheckedProperty).Subscribe(IsCheckedChanged); + this.GetObservable(IsCheckedProperty).Subscribe(IsCheckedChanged); } protected override void Toggle() diff --git a/src/Perspex.Controls/ScrollViewer.cs b/src/Perspex.Controls/ScrollViewer.cs index 6391918325..bdd770617b 100644 --- a/src/Perspex.Controls/ScrollViewer.cs +++ b/src/Perspex.Controls/ScrollViewer.cs @@ -135,8 +135,8 @@ namespace Perspex.Controls public ScrollViewer() { var extentAndViewport = Observable.CombineLatest( - GetObservable(ExtentProperty), - GetObservable(ViewportProperty)) + this.GetObservable(ExtentProperty), + this.GetObservable(ViewportProperty)) .Select(x => new { Extent = x[0], Viewport = x[1] }); Bind( @@ -155,15 +155,15 @@ namespace Perspex.Controls VerticalScrollBarMaximumProperty, extentAndViewport.Select(x => Max(x.Extent.Height - x.Viewport.Height, 0))); - GetObservable(OffsetProperty).Subscribe(x => + this.GetObservable(OffsetProperty).Subscribe(x => { SetValue(HorizontalScrollBarValueProperty, x.X); SetValue(VerticalScrollBarValueProperty, x.Y); }); var scrollBarOffset = Observable.CombineLatest( - GetObservable(HorizontalScrollBarValueProperty), - GetObservable(VerticalScrollBarValueProperty)) + this.GetObservable(HorizontalScrollBarValueProperty), + this.GetObservable(VerticalScrollBarValueProperty)) .Select(x => new Vector(x[0], x[1])) .Subscribe(x => Offset = x); } diff --git a/src/Perspex.Controls/TextBlock.cs b/src/Perspex.Controls/TextBlock.cs index 65ee4b5f70..1c5bde93ea 100644 --- a/src/Perspex.Controls/TextBlock.cs +++ b/src/Perspex.Controls/TextBlock.cs @@ -106,10 +106,10 @@ namespace Perspex.Controls public TextBlock() { Observable.Merge( - GetObservable(TextProperty).Select(_ => Unit.Default), - GetObservable(TextAlignmentProperty).Select(_ => Unit.Default), - GetObservable(FontSizeProperty).Select(_ => Unit.Default), - GetObservable(FontStyleProperty).Select(_ => Unit.Default)) + this.GetObservable(TextProperty).Select(_ => Unit.Default), + this.GetObservable(TextAlignmentProperty).Select(_ => Unit.Default), + this.GetObservable(FontSizeProperty).Select(_ => Unit.Default), + this.GetObservable(FontStyleProperty).Select(_ => Unit.Default)) .Subscribe(_ => { InvalidateFormattedText(); diff --git a/src/Perspex.Controls/TextBox.cs b/src/Perspex.Controls/TextBox.cs index 0bfc8d2eeb..3df18911fc 100644 --- a/src/Perspex.Controls/TextBox.cs +++ b/src/Perspex.Controls/TextBox.cs @@ -73,7 +73,7 @@ namespace Perspex.Controls public TextBox() { - var canScrollHorizontally = GetObservable(AcceptsReturnProperty) + var canScrollHorizontally = this.GetObservable(AcceptsReturnProperty) .Select(x => !x); Bind( @@ -81,7 +81,7 @@ namespace Perspex.Controls canScrollHorizontally, BindingPriority.Style); - var horizontalScrollBarVisibility = GetObservable(AcceptsReturnProperty) + var horizontalScrollBarVisibility = this.GetObservable(AcceptsReturnProperty) .Select(x => x ? ScrollBarVisibility.Auto : ScrollBarVisibility.Hidden); Bind( diff --git a/src/Perspex.Controls/TopLevel.cs b/src/Perspex.Controls/TopLevel.cs index 4dceab17be..3b4c8f2d66 100644 --- a/src/Perspex.Controls/TopLevel.cs +++ b/src/Perspex.Controls/TopLevel.cs @@ -115,8 +115,8 @@ namespace Perspex.Controls _accessKeyHandler?.SetOwner(this); styler?.ApplyStyles(this); - GetObservable(ClientSizeProperty).Skip(1).Subscribe(x => PlatformImpl.ClientSize = x); - GetObservable(PointerOverElementProperty) + this.GetObservable(ClientSizeProperty).Skip(1).Subscribe(x => PlatformImpl.ClientSize = x); + this.GetObservable(PointerOverElementProperty) .Select( x => (x as InputElement)?.GetObservable(CursorProperty) ?? Observable.Empty()) .Switch().Subscribe(cursor => PlatformImpl.SetCursor(cursor?.PlatformCursor)); diff --git a/src/Perspex.Diagnostics/DevTools.cs b/src/Perspex.Diagnostics/DevTools.cs index f5c7e3fb1f..a9364d2566 100644 --- a/src/Perspex.Diagnostics/DevTools.cs +++ b/src/Perspex.Diagnostics/DevTools.cs @@ -21,7 +21,7 @@ namespace Perspex.Diagnostics public DevTools() { _viewModel = new DevToolsViewModel(); - GetObservable(RootProperty).Subscribe(x => _viewModel.Root = x); + this.GetObservable(RootProperty).Subscribe(x => _viewModel.Root = x); InitializeComponent(); } diff --git a/src/Perspex.Diagnostics/Views/ControlDetailsView.cs b/src/Perspex.Diagnostics/Views/ControlDetailsView.cs index 199ece2122..fdbe7fd54f 100644 --- a/src/Perspex.Diagnostics/Views/ControlDetailsView.cs +++ b/src/Perspex.Diagnostics/Views/ControlDetailsView.cs @@ -19,7 +19,7 @@ namespace Perspex.Diagnostics.Views public ControlDetailsView() { InitializeComponent(); - GetObservable(DataContextProperty) + this.GetObservable(DataContextProperty) .Subscribe(x => ViewModel = (ControlDetailsViewModel)x); } diff --git a/src/Perspex.Diagnostics/Views/LogicalTreeView.cs b/src/Perspex.Diagnostics/Views/LogicalTreeView.cs index 21038ad50f..c29c1ddff1 100644 --- a/src/Perspex.Diagnostics/Views/LogicalTreeView.cs +++ b/src/Perspex.Diagnostics/Views/LogicalTreeView.cs @@ -20,7 +20,7 @@ namespace Perspex.Diagnostics.Views public LogicalTreeView() { InitializeComponent(); - GetObservable(DataContextProperty) + this.GetObservable(DataContextProperty) .Subscribe(x => ViewModel = (LogicalTreeViewModel)x); } diff --git a/src/Perspex.Diagnostics/Views/VisualTreeView.cs b/src/Perspex.Diagnostics/Views/VisualTreeView.cs index 4b187d00a0..d8d6ca03ce 100644 --- a/src/Perspex.Diagnostics/Views/VisualTreeView.cs +++ b/src/Perspex.Diagnostics/Views/VisualTreeView.cs @@ -21,7 +21,7 @@ namespace Perspex.Diagnostics.Views public VisualTreeView() { InitializeComponent(); - GetObservable(DataContextProperty) + this.GetObservable(DataContextProperty) .Subscribe(x => ViewModel = (VisualTreeViewModel)x); } diff --git a/src/Perspex.SceneGraph/Animation/CrossFade.cs b/src/Perspex.SceneGraph/Animation/CrossFade.cs index 4f3486c06a..d035a9c83d 100644 --- a/src/Perspex.SceneGraph/Animation/CrossFade.cs +++ b/src/Perspex.SceneGraph/Animation/CrossFade.cs @@ -58,7 +58,7 @@ namespace Perspex.Animation if (from != null) { tasks.Add(Animate.Property( - (IObservablePropertyBag)from, + (IPerspexObject)from, Visual.OpacityProperty, from.Opacity, 0, @@ -72,7 +72,7 @@ namespace Perspex.Animation to.IsVisible = true; tasks.Add(Animate.Property( - (IObservablePropertyBag)to, + (IPerspexObject)to, Visual.OpacityProperty, 0, 1, diff --git a/src/Perspex.SceneGraph/Media/MatrixTransform.cs b/src/Perspex.SceneGraph/Media/MatrixTransform.cs index 96477cec14..ea6e5c668a 100644 --- a/src/Perspex.SceneGraph/Media/MatrixTransform.cs +++ b/src/Perspex.SceneGraph/Media/MatrixTransform.cs @@ -21,7 +21,7 @@ namespace Perspex.Media /// public MatrixTransform() { - GetObservable(MatrixProperty).Subscribe(_ => RaiseChanged()); + this.GetObservable(MatrixProperty).Subscribe(_ => RaiseChanged()); } /// diff --git a/src/Perspex.SceneGraph/Media/RotateTransform.cs b/src/Perspex.SceneGraph/Media/RotateTransform.cs index d314ca28c1..b2beb9f284 100644 --- a/src/Perspex.SceneGraph/Media/RotateTransform.cs +++ b/src/Perspex.SceneGraph/Media/RotateTransform.cs @@ -21,7 +21,7 @@ namespace Perspex.Media /// public RotateTransform() { - GetObservable(AngleProperty).Subscribe(_ => RaiseChanged()); + this.GetObservable(AngleProperty).Subscribe(_ => RaiseChanged()); } /// diff --git a/src/Perspex.SceneGraph/Media/TranslateTransform.cs b/src/Perspex.SceneGraph/Media/TranslateTransform.cs index 03d60ab25b..72f1e6010f 100644 --- a/src/Perspex.SceneGraph/Media/TranslateTransform.cs +++ b/src/Perspex.SceneGraph/Media/TranslateTransform.cs @@ -27,8 +27,8 @@ namespace Perspex.Media /// public TranslateTransform() { - GetObservable(XProperty).Subscribe(_ => RaiseChanged()); - GetObservable(YProperty).Subscribe(_ => RaiseChanged()); + this.GetObservable(XProperty).Subscribe(_ => RaiseChanged()); + this.GetObservable(YProperty).Subscribe(_ => RaiseChanged()); } /// diff --git a/src/Perspex.Styling/Styling/IStyleable.cs b/src/Perspex.Styling/Styling/IStyleable.cs index e5a3f9db81..36b092ff1d 100644 --- a/src/Perspex.Styling/Styling/IStyleable.cs +++ b/src/Perspex.Styling/Styling/IStyleable.cs @@ -10,7 +10,7 @@ namespace Perspex.Styling /// /// Interface for styleable elements. /// - public interface IStyleable : IObservablePropertyBag, INamed + public interface IStyleable : IPerspexObject, INamed { /// /// Raised when the control's style should be removed. diff --git a/src/Perspex.Styling/Styling/ITemplatedControl.cs b/src/Perspex.Styling/Styling/ITemplatedControl.cs index a31d834e5a..27e328613b 100644 --- a/src/Perspex.Styling/Styling/ITemplatedControl.cs +++ b/src/Perspex.Styling/Styling/ITemplatedControl.cs @@ -5,14 +5,7 @@ using System; namespace Perspex.Styling { - public interface ITemplatedControl + public interface ITemplatedControl : IPerspexObject { - /// - /// Gets an observable for a . - /// - /// - /// The property to get the observable for. - /// The observable. - IObservable GetObservable(PerspexProperty property); } } diff --git a/src/Perspex.Styling/Styling/Selectors.cs b/src/Perspex.Styling/Styling/Selectors.cs index 7ed066f293..3c88cac0a6 100644 --- a/src/Perspex.Styling/Styling/Selectors.cs +++ b/src/Perspex.Styling/Styling/Selectors.cs @@ -240,7 +240,7 @@ namespace Perspex.Styling private static SelectorMatch MatchPropertyEquals(IStyleable x, PerspexProperty property, object value) { - if (!x.IsRegistered(property)) + if (!PerspexPropertyRegistry.Instance.IsRegistered(x, property)) { return SelectorMatch.False; } diff --git a/tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests.cs b/tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests.cs index 95675fef4b..2cd71d4ac9 100644 --- a/tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests.cs +++ b/tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests.cs @@ -1,14 +1,11 @@ // Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. -using System; -using System.Globalization; -using System.Reactive.Linq; -using System.Reactive.Subjects; using Moq; using Perspex.Controls; using Perspex.Markup.Data; using Perspex.Markup.Xaml.Data; +using ReactiveUI; using Xunit; namespace Perspex.Markup.Xaml.UnitTests.Data @@ -18,101 +15,101 @@ namespace Perspex.Markup.Xaml.UnitTests.Data [Fact] public void OneWay_Binding_Should_Be_Set_Up() { - var target = CreateTarget(); + var source = new Source { Foo = "foo" }; + var target = new TextBlock { DataContext = source }; var binding = new Binding { Path = "Foo", Mode = BindingMode.OneWay, }; - binding.Bind(target.Object, TextBox.TextProperty); + binding.Bind(target, TextBox.TextProperty); - target.Verify(x => x.Bind( - TextBox.TextProperty, - It.IsAny>(), - BindingPriority.LocalValue)); + Assert.Equal("foo", target.Text); + source.Foo = "bar"; + Assert.Equal("bar", target.Text); + target.Text = "baz"; + Assert.Equal("bar", source.Foo); } [Fact] public void TwoWay_Binding_Should_Be_Set_Up() { - var target = CreateTarget(); + var source = new Source { Foo = "foo" }; + var target = new TextBlock { DataContext = source }; var binding = new Binding { Path = "Foo", Mode = BindingMode.TwoWay, }; - binding.Bind(target.Object, TextBox.TextProperty); + binding.Bind(target, TextBox.TextProperty); - target.Verify(x => x.BindTwoWay( - TextBox.TextProperty, - It.IsAny>(), - BindingPriority.LocalValue)); + Assert.Equal("foo", target.Text); + source.Foo = "bar"; + Assert.Equal("bar", target.Text); + target.Text = "baz"; + Assert.Equal("baz", source.Foo); } [Fact] public void OneTime_Binding_Should_Be_Set_Up() { - var dataContext = new BehaviorSubject(null); - var expression = new BehaviorSubject(null); - var target = CreateTarget(dataContext: dataContext); + var source = new Source { Foo = "foo" }; + var target = new TextBlock { DataContext = source }; var binding = new Binding { Path = "Foo", Mode = BindingMode.OneTime, }; - binding.Bind(target.Object, TextBox.TextProperty, expression); + binding.Bind(target, TextBox.TextProperty); - target.Verify(x => x.SetValue( - (PerspexProperty)TextBox.TextProperty, - null, - BindingPriority.LocalValue)); - target.ResetCalls(); - - expression.OnNext("foo"); - dataContext.OnNext(1); - - target.Verify(x => x.SetValue( - (PerspexProperty)TextBox.TextProperty, - "foo", - BindingPriority.LocalValue)); + Assert.Equal("foo", target.Text); + source.Foo = "bar"; + Assert.Equal("foo", target.Text); + target.Text = "baz"; + Assert.Equal("bar", source.Foo); } [Fact] public void OneWayToSource_Binding_Should_Be_Set_Up() { - var textObservable = new Mock>(); - var expression = new Mock>(); - var target = CreateTarget(text: textObservable.Object); + var source = new Source { Foo = "foo" }; + var target = new TextBlock { DataContext = source, Text = "bar" }; var binding = new Binding { Path = "Foo", Mode = BindingMode.OneWayToSource, }; - binding.Bind(target.Object, TextBox.TextProperty, expression.Object); + binding.Bind(target, TextBox.TextProperty); - textObservable.Verify(x => x.Subscribe(expression.Object)); + Assert.Equal("bar", source.Foo); + target.Text = "baz"; + Assert.Equal("baz", source.Foo); + source.Foo = "quz"; + Assert.Equal("baz", target.Text); } [Fact] public void Default_BindingMode_Should_Be_Used() { - var target = CreateTarget(null); + // Default for TextBox.Text is two-way. + var source = new Source { Foo = "foo" }; + var target = new TextBlock { DataContext = source }; var binding = new Binding { Path = "Foo", }; - binding.Bind(target.Object, TextBox.TextProperty); + binding.Bind(target, TextBox.TextProperty); - // Default for TextBox.Text is two-way. - target.Verify(x => x.BindTwoWay( - TextBox.TextProperty, - It.IsAny>(), - BindingPriority.LocalValue)); + Assert.Equal("foo", target.Text); + source.Foo = "bar"; + Assert.Equal("bar", target.Text); + target.Text = "baz"; + Assert.Equal("baz", source.Foo); } [Fact] @@ -164,13 +161,13 @@ namespace Perspex.Markup.Xaml.UnitTests.Data [Fact] public void Should_Use_DefaultValueConverter_When_No_Converter_Specified() { - var target = CreateTarget(null); + var target = new TextBlock(); ; var binding = new Binding { Path = "Foo", }; - var result = binding.CreateSubject(target.Object, TextBox.TextProperty.PropertyType); + var result = binding.CreateSubject(target, TextBox.TextProperty.PropertyType); Assert.IsType(((ExpressionSubject)result).Converter); } @@ -178,7 +175,7 @@ namespace Perspex.Markup.Xaml.UnitTests.Data [Fact] public void Should_Use_Supplied_Converter() { - var target = CreateTarget(null); + var target = new TextBlock(); var converter = new Mock(); var binding = new Binding { @@ -186,7 +183,7 @@ namespace Perspex.Markup.Xaml.UnitTests.Data Path = "Foo", }; - var result = binding.CreateSubject(target.Object, TextBox.TextProperty.PropertyType); + var result = binding.CreateSubject(target, TextBox.TextProperty.PropertyType); Assert.Same(converter.Object, ((ExpressionSubject)result).Converter); } @@ -194,7 +191,7 @@ namespace Perspex.Markup.Xaml.UnitTests.Data [Fact] public void Should_Pass_ConverterParameter_To_Supplied_Converter() { - var target = CreateTarget(); + var target = new TextBlock(); var converter = new Mock(); var binding = new Binding { @@ -203,7 +200,7 @@ namespace Perspex.Markup.Xaml.UnitTests.Data Path = "Bar", }; - var result = binding.CreateSubject(target.Object, TextBox.TextProperty.PropertyType); + var result = binding.CreateSubject(target, TextBox.TextProperty.PropertyType); Assert.Same("foo", ((ExpressionSubject)result).ConverterParameter); } @@ -261,24 +258,15 @@ namespace Perspex.Markup.Xaml.UnitTests.Data Assert.Equal(2, vm.Bar); } - private Mock CreateTarget(object dataContext) - { - return CreateTarget(dataContext: Observable.Never().StartWith(dataContext)); - } - - private Mock CreateTarget( - IObservable dataContext = null, - IObservable text = null) + public class Source : ReactiveObject { - var result = new Mock(); - - dataContext = dataContext ?? Observable.Never().StartWith((object)null); - text = text ?? Observable.Never().StartWith((string)null); + private string _foo; - result.Setup(x => x.GetObservable(Control.DataContextProperty)).Returns(dataContext); - result.Setup(x => x.GetObservable((PerspexProperty)Control.DataContextProperty)).Returns(dataContext); - result.Setup(x => x.GetObservable((PerspexProperty)TextBox.TextProperty)).Returns(text); - return result; + public string Foo + { + get { return _foo; } + set { this.RaiseAndSetIfChanged(ref _foo, value); } + } } private class OldDataContextViewModel @@ -297,7 +285,7 @@ namespace Perspex.Markup.Xaml.UnitTests.Data public OldDataContextTest() { - Bind(BarProperty, GetObservable(FooProperty)); + Bind(BarProperty, this.GetObservable(FooProperty)); } } } diff --git a/tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests_TemplatedParent.cs b/tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests_TemplatedParent.cs index af0b492e49..75804b0748 100644 --- a/tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests_TemplatedParent.cs +++ b/tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests_TemplatedParent.cs @@ -48,46 +48,26 @@ namespace Perspex.Markup.Xaml.UnitTests.Data binding.Bind(target.Object, TextBox.TextProperty); - target.Verify(x => x.BindTwoWay( + target.Verify(x => x.Bind( TextBox.TextProperty, It.IsAny>(), BindingPriority.TemplatedParent)); } - [Fact] - public void OneWayToSource_Binding_Should_Be_Set_Up() - { - var textObservable = new Mock>(); - var expression = new Mock>(); - var target = CreateTarget(text: textObservable.Object); - var binding = new Binding - { - Path = "Foo", - Mode = BindingMode.OneWayToSource, - }; - - binding.Bind(target.Object, TextBox.TextProperty, expression.Object); - - textObservable.Verify(x => x.Subscribe(expression.Object)); - } - - private Mock CreateTarget(ITemplatedControl templatedParent) + private Mock CreateTarget(ITemplatedControl templatedParent) { - return CreateTarget(templatedParent: Observable.Never().StartWith(templatedParent)); + return CreateTarget(templatedParent: templatedParent); } - private Mock CreateTarget( - IObservable templatedParent = null, - IObservable text = null) + private Mock CreateTarget( + ITemplatedControl templatedParent = null, + string text = null) { - var result = new Mock(); - - templatedParent = templatedParent ?? Observable.Never().StartWith((ITemplatedControl)null); - text = text ?? Observable.Never().StartWith((string)null); + var result = new Mock(); - result.Setup(x => x.GetObservable(Control.TemplatedParentProperty)).Returns(templatedParent); - result.Setup(x => x.GetObservable((PerspexProperty)Control.TemplatedParentProperty)).Returns(templatedParent); - result.Setup(x => x.GetObservable((PerspexProperty)TextBox.TextProperty)).Returns(text); + result.Setup(x => x.GetValue(Control.TemplatedParentProperty)).Returns(templatedParent); + result.Setup(x => x.GetValue((PerspexProperty)Control.TemplatedParentProperty)).Returns(templatedParent); + result.Setup(x => x.GetValue((PerspexProperty)TextBox.TextProperty)).Returns(text); return result; } } diff --git a/tests/Perspex.Markup.Xaml.UnitTests/Data/MultiBindingTests.cs b/tests/Perspex.Markup.Xaml.UnitTests/Data/MultiBindingTests.cs index 23db6d4c39..9583c85525 100644 --- a/tests/Perspex.Markup.Xaml.UnitTests/Data/MultiBindingTests.cs +++ b/tests/Perspex.Markup.Xaml.UnitTests/Data/MultiBindingTests.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reactive.Linq; -using System.Reactive.Subjects; using Moq; using Perspex.Controls; using Perspex.Markup.Xaml.Data; @@ -31,10 +30,8 @@ namespace Perspex.Markup.Xaml.UnitTests.Data } }; - var target = new Mock(); + var target = new Mock(); target.Setup(x => x.GetValue(Control.DataContextProperty)).Returns(source); - target.Setup(x => x.GetObservable(Control.DataContextProperty)).Returns( - Observable.Never().StartWith(source)); var subject = binding.CreateSubject(target.Object, typeof(string)); var result = await subject.Take(1); diff --git a/tests/Perspex.Styling.UnitTests/SelectorTests_Child.cs b/tests/Perspex.Styling.UnitTests/SelectorTests_Child.cs index 8a579223b3..f9e0f1090e 100644 --- a/tests/Perspex.Styling.UnitTests/SelectorTests_Child.cs +++ b/tests/Perspex.Styling.UnitTests/SelectorTests_Child.cs @@ -6,11 +6,8 @@ using System.Collections.Generic; using System.Linq; using System.Reactive; using System.Reactive.Linq; -using System.Reactive.Subjects; -using System.Threading.Tasks; using Perspex.Collections; using Perspex.Controls; -using Perspex.Styling; using Xunit; namespace Perspex.Styling.UnitTests @@ -80,6 +77,8 @@ namespace Perspex.Styling.UnitTests Classes = new Classes(); } + public event EventHandler PropertyChanged; + public Classes Classes { get; } public string Name { get; set; } @@ -96,42 +95,29 @@ namespace Perspex.Styling.UnitTests IObservable IStyleable.StyleDetach { get; } - public IPropertyBag InheritanceParent - { - get - { - throw new NotImplementedException(); - } - } - IPerspexReadOnlyList IStyleable.Classes => Classes; - public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority) - { - throw new NotImplementedException(); - } - - public void SetValue(PerspexProperty property, object value, BindingPriority priority) + public object GetValue(PerspexProperty property) { throw new NotImplementedException(); } - public IObservable GetObservable(PerspexProperty property) + public T GetValue(PerspexProperty property) { throw new NotImplementedException(); } - public bool IsRegistered(PerspexProperty property) + public void SetValue(PerspexProperty property, object value, BindingPriority priority) { throw new NotImplementedException(); } - public void ClearValue(PerspexProperty property) + public void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue) { throw new NotImplementedException(); } - public object GetValue(PerspexProperty property) + public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority) { throw new NotImplementedException(); } @@ -145,31 +131,6 @@ namespace Perspex.Styling.UnitTests { throw new NotImplementedException(); } - - public IObservable GetObservable(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public T GetValue(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue) - { - throw new NotImplementedException(); - } - - public IDisposable BindTwoWay(PerspexProperty property, PerspexObject source, PerspexProperty sourceProperty, BindingPriority priority = BindingPriority.LocalValue) - { - throw new NotImplementedException(); - } - - public IDisposable BindTwoWay(PerspexProperty property, ISubject source, BindingPriority priority = BindingPriority.LocalValue) - { - throw new NotImplementedException(); - } } public class TestLogical1 : TestLogical diff --git a/tests/Perspex.Styling.UnitTests/SelectorTests_Descendent.cs b/tests/Perspex.Styling.UnitTests/SelectorTests_Descendent.cs index f7ce2beaa9..e5477ebee3 100644 --- a/tests/Perspex.Styling.UnitTests/SelectorTests_Descendent.cs +++ b/tests/Perspex.Styling.UnitTests/SelectorTests_Descendent.cs @@ -5,11 +5,9 @@ using System; using System.Linq; using System.Reactive; using System.Reactive.Linq; -using System.Reactive.Subjects; using System.Threading.Tasks; using Perspex.Collections; using Perspex.Controls; -using Perspex.Styling; using Xunit; namespace Perspex.Styling.UnitTests @@ -110,6 +108,8 @@ namespace Perspex.Styling.UnitTests Classes = new Classes(); } + public event EventHandler PropertyChanged; + public Classes Classes { get; } public string Name { get; set; } @@ -124,44 +124,31 @@ namespace Perspex.Styling.UnitTests public ITemplatedControl TemplatedParent { get; } - public IPropertyBag InheritanceParent - { - get - { - throw new NotImplementedException(); - } - } - IPerspexReadOnlyList IStyleable.Classes => Classes; IObservable IStyleable.StyleDetach { get; } - public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority) - { - throw new NotImplementedException(); - } - - public void SetValue(PerspexProperty property, object value, BindingPriority priority) + public object GetValue(PerspexProperty property) { throw new NotImplementedException(); } - public IObservable GetObservable(PerspexProperty property) + public T GetValue(PerspexProperty property) { throw new NotImplementedException(); } - public bool IsRegistered(PerspexProperty property) + public void SetValue(PerspexProperty property, object value, BindingPriority priority) { throw new NotImplementedException(); } - public void ClearValue(PerspexProperty property) + public void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue) { throw new NotImplementedException(); } - public object GetValue(PerspexProperty property) + public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority) { throw new NotImplementedException(); } @@ -175,31 +162,6 @@ namespace Perspex.Styling.UnitTests { throw new NotImplementedException(); } - - public IObservable GetObservable(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public T GetValue(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue) - { - throw new NotImplementedException(); - } - - public IDisposable BindTwoWay(PerspexProperty property, PerspexObject source, PerspexProperty sourceProperty, BindingPriority priority = BindingPriority.LocalValue) - { - throw new NotImplementedException(); - } - - public IDisposable BindTwoWay(PerspexProperty property, ISubject source, BindingPriority priority = BindingPriority.LocalValue) - { - throw new NotImplementedException(); - } } public class TestLogical1 : TestLogical diff --git a/tests/Perspex.Styling.UnitTests/TestControlBase.cs b/tests/Perspex.Styling.UnitTests/TestControlBase.cs index 82ab942a09..6743d2e468 100644 --- a/tests/Perspex.Styling.UnitTests/TestControlBase.cs +++ b/tests/Perspex.Styling.UnitTests/TestControlBase.cs @@ -3,7 +3,6 @@ using System; using System.Reactive; -using System.Reactive.Subjects; using Perspex.Collections; using Perspex.Controls; @@ -17,6 +16,8 @@ namespace Perspex.Styling.UnitTests SubscribeCheckObservable = new TestObservable(); } + public event EventHandler PropertyChanged; + public string Name { get; set; } public virtual Classes Classes { get; set; } @@ -31,79 +32,41 @@ namespace Perspex.Styling.UnitTests set; } - public IPropertyBag InheritanceParent - { - get - { - throw new NotImplementedException(); - } - } - IPerspexReadOnlyList IStyleable.Classes => Classes; IObservable IStyleable.StyleDetach { get; } - public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority) - { - throw new NotImplementedException(); - } - - public void SetValue(PerspexProperty property, object value, BindingPriority priority) - { - throw new NotImplementedException(); - } - - public IObservable GetObservable(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public bool IsRegistered(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public void ClearValue(PerspexProperty property) - { - throw new NotImplementedException(); - } - public object GetValue(PerspexProperty property) { throw new NotImplementedException(); } - public bool IsSet(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority = BindingPriority.LocalValue) + public T GetValue(PerspexProperty property) { throw new NotImplementedException(); } - public IObservable GetObservable(PerspexProperty property) + public void SetValue(PerspexProperty property, object value, BindingPriority priority) { throw new NotImplementedException(); } - public T GetValue(PerspexProperty property) + public void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue) { throw new NotImplementedException(); } - public void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue) + public bool IsSet(PerspexProperty property) { throw new NotImplementedException(); } - public IDisposable BindTwoWay(PerspexProperty property, PerspexObject source, PerspexProperty sourceProperty, BindingPriority priority = BindingPriority.LocalValue) + public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority) { throw new NotImplementedException(); } - public IDisposable BindTwoWay(PerspexProperty property, ISubject source, BindingPriority priority = BindingPriority.LocalValue) + public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority = BindingPriority.LocalValue) { throw new NotImplementedException(); } diff --git a/tests/Perspex.Styling.UnitTests/TestTemplatedControl.cs b/tests/Perspex.Styling.UnitTests/TestTemplatedControl.cs index 1348b1d662..f84a7d7ad6 100644 --- a/tests/Perspex.Styling.UnitTests/TestTemplatedControl.cs +++ b/tests/Perspex.Styling.UnitTests/TestTemplatedControl.cs @@ -2,9 +2,7 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using System.Collections.Generic; using System.Reactive; -using System.Reactive.Subjects; using Perspex.Collections; using Perspex.Controls; @@ -12,6 +10,8 @@ namespace Perspex.Styling.UnitTests { public abstract class TestTemplatedControl : ITemplatedControl, IStyleable { + public event EventHandler PropertyChanged; + public abstract Classes Classes { get; @@ -32,29 +32,16 @@ namespace Perspex.Styling.UnitTests get; } - public abstract IEnumerable VisualChildren - { - get; - } - - public IPropertyBag InheritanceParent - { - get - { - throw new NotImplementedException(); - } - } - IPerspexReadOnlyList IStyleable.Classes => Classes; IObservable IStyleable.StyleDetach { get; } - public IObservable GetObservable(PerspexProperty property) + public object GetValue(PerspexProperty property) { throw new NotImplementedException(); } - public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority) + public T GetValue(PerspexProperty property) { throw new NotImplementedException(); } @@ -64,27 +51,12 @@ namespace Perspex.Styling.UnitTests throw new NotImplementedException(); } - public IObservable GetObservable(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public bool IsRegistered(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public void ClearValue(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public object GetValue(PerspexProperty property) + public void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue) { throw new NotImplementedException(); } - public bool IsSet(PerspexProperty property) + public IDisposable Bind(PerspexProperty property, IObservable source, BindingPriority priority) { throw new NotImplementedException(); } @@ -94,22 +66,7 @@ namespace Perspex.Styling.UnitTests throw new NotImplementedException(); } - public T GetValue(PerspexProperty property) - { - throw new NotImplementedException(); - } - - public void SetValue(PerspexProperty property, T value, BindingPriority priority = BindingPriority.LocalValue) - { - throw new NotImplementedException(); - } - - public IDisposable BindTwoWay(PerspexProperty property, PerspexObject source, PerspexProperty sourceProperty, BindingPriority priority = BindingPriority.LocalValue) - { - throw new NotImplementedException(); - } - - public IDisposable BindTwoWay(PerspexProperty property, ISubject source, BindingPriority priority = BindingPriority.LocalValue) + public bool IsSet(PerspexProperty property) { throw new NotImplementedException(); }