From 18bbb2c41e3bd03e57d55c57413b34fc43a9ac70 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 12 Mar 2014 23:17:39 +0100 Subject: [PATCH] WIP Not sure where I am - went away for 2 weeks and forgot what I was doing. oops. --- Perspex.UnitTests/Styling/ActivatorTests.cs | 18 ++--- Perspex/Controls/ContentControl.cs | 15 ----- Perspex/Controls/Control.cs | 32 +++------ Perspex/Controls/ControlTemplate.cs | 7 +- Perspex/Controls/TestBorder.cs | 32 +++++++++ Perspex/IObservableDescription.cs | 13 ++++ Perspex/Perspex.csproj | 4 +- Perspex/PerspexObject.cs | 10 ++- Perspex/Styling/Selector.cs | 4 +- Perspex/Styling/Selectors.cs | 5 +- .../{Activator.cs => StyleActivator.cs} | 14 +++- Perspex/Themes/Default/ButtonStyle.cs | 67 ++++++++++--------- Perspex/Themes/Default/DefaultTheme.cs | 2 +- Perspex/Visual.cs | 20 ++++++ Perspex/VisualExtensions.cs | 2 +- TestApplication/Program.cs | 31 +++++++-- 16 files changed, 183 insertions(+), 93 deletions(-) create mode 100644 Perspex/Controls/TestBorder.cs create mode 100644 Perspex/IObservableDescription.cs rename Perspex/Styling/{Activator.cs => StyleActivator.cs} (89%) diff --git a/Perspex.UnitTests/Styling/ActivatorTests.cs b/Perspex.UnitTests/Styling/ActivatorTests.cs index e21abe7500..fa96a9abd5 100644 --- a/Perspex.UnitTests/Styling/ActivatorTests.cs +++ b/Perspex.UnitTests/Styling/ActivatorTests.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using Perspex.Styling; - using Activator = Perspex.Styling.Activator; + using Activator = Perspex.Styling.StyleActivator; [TestClass] public class ActivatorTests @@ -18,7 +18,7 @@ public void Activator_And_Should_Follow_Single_Input() { var inputs = new[] { new TestSubject(false) }; - var target = new Activator(inputs, ActivatorMode.And); + var target = new Activator(inputs, "", ActivatorMode.And); var result = new TestObserver(); target.Subscribe(result); @@ -42,7 +42,7 @@ new TestSubject(false), new TestSubject(true), }; - var target = new Activator(inputs, ActivatorMode.And); + var target = new Activator(inputs, "", ActivatorMode.And); var result = new TestObserver(); target.Subscribe(result); @@ -67,7 +67,7 @@ new TestSubject(false), new TestSubject(true), }; - var target = new Activator(inputs, ActivatorMode.And); + var target = new Activator(inputs, "", ActivatorMode.And); var result = new TestObserver(); target.Subscribe(result); @@ -93,7 +93,7 @@ new TestSubject(false), new TestSubject(true), }; - var target = new Activator(inputs, ActivatorMode.And); + var target = new Activator(inputs, "", ActivatorMode.And); var result = new TestObserver(); target.Subscribe(result); @@ -110,7 +110,7 @@ public void Activator_Or_Should_Follow_Single_Input() { var inputs = new[] { new TestSubject(false) }; - var target = new Activator(inputs, ActivatorMode.Or); + var target = new Activator(inputs, "", ActivatorMode.Or); var result = new TestObserver(); target.Subscribe(result); @@ -134,7 +134,7 @@ new TestSubject(false), new TestSubject(true), }; - var target = new Activator(inputs, ActivatorMode.Or); + var target = new Activator(inputs, "", ActivatorMode.Or); var result = new TestObserver(); target.Subscribe(result); @@ -158,7 +158,7 @@ new TestSubject(false), new TestSubject(true), }; - var target = new Activator(inputs, ActivatorMode.Or); + var target = new Activator(inputs, "", ActivatorMode.Or); var result = new TestObserver(); target.Subscribe(result); @@ -183,7 +183,7 @@ new TestSubject(false), new TestSubject(true), }; - var target = new Activator(inputs, ActivatorMode.Or); + var target = new Activator(inputs, "", ActivatorMode.Or); var result = new TestObserver(); target.Subscribe(result); diff --git a/Perspex/Controls/ContentControl.cs b/Perspex/Controls/ContentControl.cs index 0c2fe58a57..ac6bd3be39 100644 --- a/Perspex/Controls/ContentControl.cs +++ b/Perspex/Controls/ContentControl.cs @@ -17,21 +17,6 @@ namespace Perspex.Controls public ContentControl() { - this.GetObservable(ContentProperty).Subscribe(x => - { - IVisual visual = x as IVisual; - ILogical logical = x as ILogical; - - if (visual != null) - { - visual.VisualParent = this; - } - - if (logical != null) - { - logical.LogicalParent = this; - } - }); } public object Content diff --git a/Perspex/Controls/Control.cs b/Perspex/Controls/Control.cs index 35fc25040b..a3df4f0684 100644 --- a/Perspex/Controls/Control.cs +++ b/Perspex/Controls/Control.cs @@ -9,6 +9,7 @@ namespace Perspex.Controls using System; using System.Collections.Generic; using System.Collections.ObjectModel; + using System.Linq; using System.Reactive.Linq; using Perspex.Input; using Perspex.Layout; @@ -75,8 +76,6 @@ namespace Perspex.Controls this.classes.BeforeChanged.Subscribe(x => this.BeginDeferStyleChanges()); this.classes.AfterChanged.Subscribe(x => this.EndDeferStyleChanges()); - this.GetObservableWithHistory(ParentPropertyRW).Subscribe(this.ParentChanged); - this.GetObservable(IsMouseOverProperty).Subscribe(x => { if (x) @@ -294,32 +293,23 @@ namespace Perspex.Controls protected abstract Size MeasureContent(Size availableSize); - private void AttachStyles(Control control) + protected override void AttachedToVisualTree() { - if (control != null) - { - Control parent = control.Parent; - this.AttachStyles(parent); - control.Styles.Attach(this); - } - else - { - Application.Current.Styles.Attach(this); - } + this.AttachStyles(this); + base.AttachedToVisualTree(); } - private void ParentChanged(Tuple values) + private void AttachStyles(Control styleContainer) { - Contract.Requires(values != null); - - if (values.Item1 != null) + if (styleContainer != null) { - ////this.DetatchStyles(values.Item1); + Control parent = styleContainer.Parent; + this.AttachStyles(parent); + styleContainer.Styles.Attach(this); } - - if (values.Item2 != null) + else { - this.AttachStyles(this); + Application.Current.Styles.Attach(this); } } } diff --git a/Perspex/Controls/ControlTemplate.cs b/Perspex/Controls/ControlTemplate.cs index c8c92457aa..2347239afc 100644 --- a/Perspex/Controls/ControlTemplate.cs +++ b/Perspex/Controls/ControlTemplate.cs @@ -45,9 +45,12 @@ namespace Perspex.Controls control.TemplatedParent = templatedParent; - foreach (Control child in ((IVisual)control).VisualChildren.OfType()) + if (!(control is ContentPresenter)) { - this.SetTemplatedParent(child, templatedParent); + foreach (Control child in ((IVisual)control).VisualChildren.OfType()) + { + this.SetTemplatedParent(child, templatedParent); + } } } } diff --git a/Perspex/Controls/TestBorder.cs b/Perspex/Controls/TestBorder.cs new file mode 100644 index 0000000000..f143b26820 --- /dev/null +++ b/Perspex/Controls/TestBorder.cs @@ -0,0 +1,32 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls +{ + using System; + using System.Reactive.Linq; + using Perspex.Media; + + public class TestBorder : Decorator + { + public override void Render(IDrawingContext context) + { + Brush background = this.Background; + Brush borderBrush = this.BorderBrush; + double borderThickness = this.BorderThickness; + + if (background != null) + { + context.FillRectange(background, new Rect(this.Bounds.Size)); + } + + if (borderBrush != null && borderThickness > 0) + { + context.DrawRectange(new Pen(borderBrush, borderThickness), new Rect(this.Bounds.Size)); + } + } + } +} diff --git a/Perspex/IObservableDescription.cs b/Perspex/IObservableDescription.cs new file mode 100644 index 0000000000..fb7fb3e415 --- /dev/null +++ b/Perspex/IObservableDescription.cs @@ -0,0 +1,13 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex +{ + public interface IObservableDescription + { + string Description { get; } + } +} diff --git a/Perspex/Perspex.csproj b/Perspex/Perspex.csproj index 1e76696427..864390aea9 100644 --- a/Perspex/Perspex.csproj +++ b/Perspex/Perspex.csproj @@ -71,6 +71,7 @@ + @@ -83,8 +84,9 @@ + - + diff --git a/Perspex/PerspexObject.cs b/Perspex/PerspexObject.cs index 2974fa827a..217e454704 100644 --- a/Perspex/PerspexObject.cs +++ b/Perspex/PerspexObject.cs @@ -456,12 +456,16 @@ namespace Perspex } v.AddStyle(activator, value); - + + IObservableDescription description = activator as IObservableDescription; + string bindingDescription = description != null ? description.Description : "[unnamed]"; + this.Log().Debug(string.Format( - "Bound value of {0}.{1} (#{2:x8}) to style", + "Bound value of {0}.{1} (#{2:x8}) to style '{3}'", this.GetType().Name, property.Name, - this.GetHashCode())); + this.GetHashCode(), + bindingDescription)); } private static IObservable BoxObservable(IObservable observable) diff --git a/Perspex/Styling/Selector.cs b/Perspex/Styling/Selector.cs index 59ddc03f08..516608c46c 100644 --- a/Perspex/Styling/Selector.cs +++ b/Perspex/Styling/Selector.cs @@ -59,7 +59,7 @@ namespace Perspex.Styling return this.stopTraversal ? null : this.Previous; } - public Activator GetActivator(IStyleable control) + public StyleActivator GetActivator(IStyleable control) { List> inputs = new List>(); Selector selector = this; @@ -79,7 +79,7 @@ namespace Perspex.Styling selector = selector.MovePrevious(); } - return new Activator(inputs); + return new StyleActivator(inputs, this.ToString()); } public override string ToString() diff --git a/Perspex/Styling/Selectors.cs b/Perspex/Styling/Selectors.cs index ac312db342..935911fa97 100644 --- a/Perspex/Styling/Selectors.cs +++ b/Perspex/Styling/Selectors.cs @@ -47,7 +47,10 @@ namespace Perspex.Styling } } - return new Activator(descendentMatches, ActivatorMode.Or); + return new StyleActivator( + descendentMatches, + "Descendent", + ActivatorMode.Or); }, }; } diff --git a/Perspex/Styling/Activator.cs b/Perspex/Styling/StyleActivator.cs similarity index 89% rename from Perspex/Styling/Activator.cs rename to Perspex/Styling/StyleActivator.cs index 5b4e5b88c5..e05a8ab3e1 100644 --- a/Perspex/Styling/Activator.cs +++ b/Perspex/Styling/StyleActivator.cs @@ -17,7 +17,7 @@ namespace Perspex.Styling Or, } - public class Activator : IObservable + public class StyleActivator : IObservable, IObservableDescription { ActivatorMode mode; @@ -29,10 +29,14 @@ namespace Perspex.Styling bool last = false; - public Activator(IEnumerable> inputs, ActivatorMode mode = ActivatorMode.And) + public StyleActivator( + IEnumerable> inputs, + string description, + ActivatorMode mode = ActivatorMode.And) { int i = 0; + this.Description = description; this.mode = mode; foreach (IObservable input in inputs) @@ -50,6 +54,12 @@ namespace Perspex.Styling } } + public string Description + { + get; + private set; + } + public IDisposable Subscribe(IObserver observer) { Contract.Requires(observer != null); diff --git a/Perspex/Themes/Default/ButtonStyle.cs b/Perspex/Themes/Default/ButtonStyle.cs index 45242dac7a..df4ff3cadc 100644 --- a/Perspex/Themes/Default/ButtonStyle.cs +++ b/Perspex/Themes/Default/ButtonStyle.cs @@ -13,41 +13,48 @@ namespace Perspex.Themes.Default { public ButtonStyle() { - //this.AddRange(new[] - //{ - // new Style(x => x.OfType