Browse Source

Selectors return linked list of matches again.

pull/4/head
grokys 12 years ago
parent
commit
9bad6b1b91
  1. 77
      Perspex.UnitTests/Styling/SelectorTests_InTemplateOf.cs
  2. 6
      Perspex.UnitTests/Styling/TestControlBase.cs
  3. 3
      Perspex/Controls/ControlTemplate.cs
  4. 2
      Perspex/Perspex.csproj
  5. 25
      Perspex/Styling/Activator.cs
  6. 32
      Perspex/Styling/Match.cs
  7. 48
      Perspex/Styling/Selectors.cs

77
Perspex.UnitTests/Styling/SelectorTests_InTemplateOf.cs

@ -1,77 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="SelectorTests.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
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<TemplatedControl1>();
// 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<TemplatedControl2>();
// 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<TemplatedControl1>().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<TemplatedControl2>().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
//{
//}
}
}

6
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,
};
}
}
}

3
Perspex/ControlTemplate.cs → Perspex/Controls/ControlTemplate.cs

@ -4,9 +4,10 @@
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex
namespace Perspex.Controls
{
using System;
using System.Diagnostics.Contracts;
using System.Linq;
using Perspex.Controls;

2
Perspex/Perspex.csproj

@ -74,12 +74,12 @@
<Compile Include="Controls\Border.cs" />
<Compile Include="Controls\Button.cs" />
<Compile Include="Controls\ContentPresenter.cs" />
<Compile Include="Controls\ControlTemplate.cs" />
<Compile Include="Controls\Decorator.cs" />
<Compile Include="Controls\ContentControl.cs" />
<Compile Include="Controls\Control.cs" />
<Compile Include="Controls\ITemplatedControl.cs" />
<Compile Include="Controls\TextBlock.cs" />
<Compile Include="ControlTemplate.cs" />
<Compile Include="ILogical.cs" />
<Compile Include="Input\MouseEventArgs.cs" />
<Compile Include="Interactive.cs" />

25
Perspex/Styling/Activator.cs

@ -21,24 +21,27 @@ namespace Perspex.Styling
bool last = false;
public Activator(IList<IObservable<bool>> observables)
public Activator(Match match)
{
int i = 0;
foreach (IObservable<bool> 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;
}
}

32
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<IObservable<bool>>();
}
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<IObservable<bool>> Observables
public IObservable<bool> 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();
}
}
}

48
Perspex/Styling/Selectors.cs

@ -24,45 +24,23 @@ namespace Perspex.Styling
Contract.Requires<ArgumentNullException>(match != null);
Contract.Requires<ArgumentNullException>(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<ArgumentNullException>(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<T>(this Match match) where T : ITemplatedControl
public static Match Id(this Match match, string id)
{
Contract.Requires<ArgumentNullException>(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<ArgumentNullException>(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,
};
}
}
}

Loading…
Cancel
Save