From 9bad6b1b91b1e5aa693f16b107370ace4839b08f Mon Sep 17 00:00:00 2001 From: grokys Date: Wed, 5 Feb 2014 02:33:23 +0100 Subject: [PATCH] Selectors return linked list of matches again. --- .../Styling/SelectorTests_InTemplateOf.cs | 77 ------------------- Perspex.UnitTests/Styling/TestControlBase.cs | 6 +- Perspex/{ => Controls}/ControlTemplate.cs | 3 +- Perspex/Perspex.csproj | 2 +- Perspex/Styling/Activator.cs | 25 +++--- Perspex/Styling/Match.cs | 32 +++++--- Perspex/Styling/Selectors.cs | 48 ++++-------- 7 files changed, 58 insertions(+), 135 deletions(-) delete mode 100644 Perspex.UnitTests/Styling/SelectorTests_InTemplateOf.cs rename Perspex/{ => Controls}/ControlTemplate.cs (96%) diff --git a/Perspex.UnitTests/Styling/SelectorTests_InTemplateOf.cs b/Perspex.UnitTests/Styling/SelectorTests_InTemplateOf.cs deleted file mode 100644 index d3210e1807..0000000000 --- a/Perspex.UnitTests/Styling/SelectorTests_InTemplateOf.cs +++ /dev/null @@ -1,77 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.UnitTests.Styling -{ - using System; - using System.Linq; - using System.Reactive.Linq; - using Microsoft.VisualStudio.TestTools.UnitTesting; - using Perspex.Controls; - using Perspex.Styling; - - [TestClass] - public class SelectorTests_InTemplateOf - { - [TestMethod] - public void Remove_These() - { - Assert.Fail(); - } - - //[TestMethod] - //public void InTemplateOf_Matches_Control_Of_Correct_Type() - //{ - // var control = new Control1 { TemplatedParent = new TemplatedControl1() }; - // var target = control.Select().InTemplateOf(); - - // CollectionAssert.AreEqual(new[] { true }, target.GetActivator().Take(1).ToEnumerable().ToArray()); - //} - - //[TestMethod] - //public void InTemplateOf_Doesnt_Match_Control_Of_Wrong_Type() - //{ - // var control = new Control1 { TemplatedParent = new TemplatedControl1() }; - // var target = control.Select().InTemplateOf(); - - // CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray()); - //} - - //[TestMethod] - //public void When_InTemplateOf_Matches_Control_Other_Selectors_Are_Subscribed() - //{ - // var control = new Control1 { TemplatedParent = new TemplatedControl1() }; - // var target = control.Select().InTemplateOf().SubscribeCheck(); - - // var result = target.GetActivator().ToEnumerable().Take(1).ToArray(); - - // Assert.AreEqual(1, control.SubscribeCheckObservable.SubscribedCount); - //} - - //[TestMethod] - //public void When_InTemplateOf_Doesnt_Match_Control_Other_Selectors_Are_Not_Subscribed() - //{ - // var control = new Control1 { TemplatedParent = new TemplatedControl1() }; - // var target = control.Select().InTemplateOf().SubscribeCheck(); - - // var result = target.GetActivator().ToEnumerable().Take(1).ToArray(); - - // Assert.AreEqual(0, control.SubscribeCheckObservable.SubscribedCount); - //} - - //public class Control1 : TestControlBase - //{ - //} - - //public class TemplatedControl1 : ITemplatedControl - //{ - //} - - //public class TemplatedControl2 : ITemplatedControl - //{ - //} - } -} diff --git a/Perspex.UnitTests/Styling/TestControlBase.cs b/Perspex.UnitTests/Styling/TestControlBase.cs index dd643d8f5e..d33427da95 100644 --- a/Perspex.UnitTests/Styling/TestControlBase.cs +++ b/Perspex.UnitTests/Styling/TestControlBase.cs @@ -53,8 +53,10 @@ namespace Perspex.UnitTests.Styling { public static Match SubscribeCheck(this Match match) { - match.Observables.Add(((TestControlBase)match.Control).SubscribeCheckObservable); - return match; + return new Match(match) + { + Observable = ((TestControlBase)match.Control).SubscribeCheckObservable, + }; } } } diff --git a/Perspex/ControlTemplate.cs b/Perspex/Controls/ControlTemplate.cs similarity index 96% rename from Perspex/ControlTemplate.cs rename to Perspex/Controls/ControlTemplate.cs index 9c8f6212d8..60e177a6b5 100644 --- a/Perspex/ControlTemplate.cs +++ b/Perspex/Controls/ControlTemplate.cs @@ -4,9 +4,10 @@ // // ----------------------------------------------------------------------- -namespace Perspex +namespace Perspex.Controls { using System; + using System.Diagnostics.Contracts; using System.Linq; using Perspex.Controls; diff --git a/Perspex/Perspex.csproj b/Perspex/Perspex.csproj index 8142a6f8f5..8a1839b596 100644 --- a/Perspex/Perspex.csproj +++ b/Perspex/Perspex.csproj @@ -74,12 +74,12 @@ + - diff --git a/Perspex/Styling/Activator.cs b/Perspex/Styling/Activator.cs index 8ec9ebe196..29a7d117b5 100644 --- a/Perspex/Styling/Activator.cs +++ b/Perspex/Styling/Activator.cs @@ -21,24 +21,27 @@ namespace Perspex.Styling bool last = false; - public Activator(IList> observables) + public Activator(Match match) { int i = 0; - foreach (IObservable o in observables.Reverse()) + while (match != null) { int iCaptured = i; - this.values.Add(false); - - IDisposable subscription = o.Subscribe( - x => this.Update(iCaptured, x), - x => this.Finish(iCaptured), - () => this.Finish(iCaptured)); - - this.subscriptions.Add(subscription); + if (match.Observable != null) + { + this.values.Add(false); + + IDisposable subscription = match.Observable.Subscribe( + x => this.Update(iCaptured, x), + x => this.Finish(iCaptured), + () => this.Finish(iCaptured)); + this.subscriptions.Add(subscription); + ++i; + } - ++i; + match = match.Previous; } } diff --git a/Perspex/Styling/Match.cs b/Perspex/Styling/Match.cs index c55c263d28..426c174a7d 100644 --- a/Perspex/Styling/Match.cs +++ b/Perspex/Styling/Match.cs @@ -10,6 +10,7 @@ namespace Perspex.Styling using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; + using System.Text; using Perspex.Controls; public class Match @@ -17,15 +18,13 @@ namespace Perspex.Styling public Match(IStyleable control) { this.Control = control; - this.Observables = new List>(); } - public Match(Match source) + public Match(Match previous) { - this.Control = source.Control; - this.InTemplate = source.InTemplate; - this.Observables = source.Observables; - this.SelectorString = SelectorString; + this.Control = previous.Control; + this.InTemplate = previous.InTemplate; + this.Previous = previous; } public IStyleable Control @@ -40,7 +39,13 @@ namespace Perspex.Styling set; } - public List> Observables + public IObservable Observable + { + get; + set; + } + + public Match Previous { get; set; @@ -54,12 +59,21 @@ namespace Perspex.Styling public Activator GetActivator() { - return new Activator(this.Observables); + return new Activator(this); } public override string ToString() { - return this.SelectorString; + Match match = this; + StringBuilder b = new StringBuilder(); + + while (match != null) + { + b.Append(match.SelectorString); + match = match.Previous; + } + + return b.ToString(); } } } diff --git a/Perspex/Styling/Selectors.cs b/Perspex/Styling/Selectors.cs index 9db57eed6c..6fd6f09680 100644 --- a/Perspex/Styling/Selectors.cs +++ b/Perspex/Styling/Selectors.cs @@ -24,45 +24,23 @@ namespace Perspex.Styling Contract.Requires(match != null); Contract.Requires(name != null); - match.Observables.Add(Observable - .Return(match.Control.Classes.Contains(name)) - .Concat(match.Control.Classes.Changed.Select(e => match.Control.Classes.Contains(name)))); - match.SelectorString += (name[0] == ':') ? name : '.' + name; - - return match; - } - - public static Match Id(this Match match, string id) - { - Contract.Requires(match != null); - - if (!match.InTemplate) - { - match.Observables.Add(Observable.Return( - match.Control.TemplatedParent == null && - match.Control.Id == id)); - } - else + return new Match(match) { - match.Observables.Add(Observable.Return( - match.Control.TemplatedParent != null && - match.Control.Id == id)); - } - - match.SelectorString += '#' + id; - return match; + Observable = Observable + .Return(match.Control.Classes.Contains(name)) + .Concat(match.Control.Classes.Changed.Select(e => match.Control.Classes.Contains(name))), + SelectorString = (name[0] == ':') ? name : '.' + name, + }; } - public static Match InTemplateOf(this Match match) where T : ITemplatedControl + public static Match Id(this Match match, string id) { Contract.Requires(match != null); - match.Observables.Add(Observable.Return(match.Control.TemplatedParent is T)); - match.SelectorString += '%' + typeof(T).Name; - return new Match(match) { - InTemplate = true, + Observable = Observable.Return(match.Control.TemplatedParent == null && match.Control.Id == id), + SelectorString = '#' + id, }; } @@ -70,9 +48,11 @@ namespace Perspex.Styling { Contract.Requires(match != null); - match.Observables.Add(Observable.Return(match.Control is T)); - match.SelectorString += typeof(T).Name; - return match; + return new Match(match) + { + Observable = Observable.Return(match.Control is T), + SelectorString = typeof(T).Name, + }; } } }