Browse Source

WIP

Not sure where I am - went away for 2 weeks and forgot what I was doing.
oops.
pull/4/head
Steven Kirk 12 years ago
parent
commit
18bbb2c41e
  1. 18
      Perspex.UnitTests/Styling/ActivatorTests.cs
  2. 15
      Perspex/Controls/ContentControl.cs
  3. 32
      Perspex/Controls/Control.cs
  4. 7
      Perspex/Controls/ControlTemplate.cs
  5. 32
      Perspex/Controls/TestBorder.cs
  6. 13
      Perspex/IObservableDescription.cs
  7. 4
      Perspex/Perspex.csproj
  8. 10
      Perspex/PerspexObject.cs
  9. 4
      Perspex/Styling/Selector.cs
  10. 5
      Perspex/Styling/Selectors.cs
  11. 14
      Perspex/Styling/StyleActivator.cs
  12. 67
      Perspex/Themes/Default/ButtonStyle.cs
  13. 2
      Perspex/Themes/Default/DefaultTheme.cs
  14. 20
      Perspex/Visual.cs
  15. 2
      Perspex/VisualExtensions.cs
  16. 31
      TestApplication/Program.cs

18
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<bool>(false) };
var target = new Activator(inputs, ActivatorMode.And);
var target = new Activator(inputs, "", ActivatorMode.And);
var result = new TestObserver<bool>();
target.Subscribe(result);
@ -42,7 +42,7 @@
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs, ActivatorMode.And);
var target = new Activator(inputs, "", ActivatorMode.And);
var result = new TestObserver<bool>();
target.Subscribe(result);
@ -67,7 +67,7 @@
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs, ActivatorMode.And);
var target = new Activator(inputs, "", ActivatorMode.And);
var result = new TestObserver<bool>();
target.Subscribe(result);
@ -93,7 +93,7 @@
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs, ActivatorMode.And);
var target = new Activator(inputs, "", ActivatorMode.And);
var result = new TestObserver<bool>();
target.Subscribe(result);
@ -110,7 +110,7 @@
public void Activator_Or_Should_Follow_Single_Input()
{
var inputs = new[] { new TestSubject<bool>(false) };
var target = new Activator(inputs, ActivatorMode.Or);
var target = new Activator(inputs, "", ActivatorMode.Or);
var result = new TestObserver<bool>();
target.Subscribe(result);
@ -134,7 +134,7 @@
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs, ActivatorMode.Or);
var target = new Activator(inputs, "", ActivatorMode.Or);
var result = new TestObserver<bool>();
target.Subscribe(result);
@ -158,7 +158,7 @@
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs, ActivatorMode.Or);
var target = new Activator(inputs, "", ActivatorMode.Or);
var result = new TestObserver<bool>();
target.Subscribe(result);
@ -183,7 +183,7 @@
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs, ActivatorMode.Or);
var target = new Activator(inputs, "", ActivatorMode.Or);
var result = new TestObserver<bool>();
target.Subscribe(result);

15
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

32
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<Control, Control> values)
private void AttachStyles(Control styleContainer)
{
Contract.Requires<ArgumentNullException>(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);
}
}
}

7
Perspex/Controls/ControlTemplate.cs

@ -45,9 +45,12 @@ namespace Perspex.Controls
control.TemplatedParent = templatedParent;
foreach (Control child in ((IVisual)control).VisualChildren.OfType<Control>())
if (!(control is ContentPresenter))
{
this.SetTemplatedParent(child, templatedParent);
foreach (Control child in ((IVisual)control).VisualChildren.OfType<Control>())
{
this.SetTemplatedParent(child, templatedParent);
}
}
}
}

32
Perspex/Controls/TestBorder.cs

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

13
Perspex/IObservableDescription.cs

@ -0,0 +1,13 @@
// -----------------------------------------------------------------------
// <copyright file="IObservableDescription.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex
{
public interface IObservableDescription
{
string Description { get; }
}
}

4
Perspex/Perspex.csproj

@ -71,6 +71,7 @@
<Compile Include="Application.cs" />
<Compile Include="Classes.cs" />
<Compile Include="Contract.cs" />
<Compile Include="Controls\TestBorder.cs" />
<Compile Include="Controls\Border.cs" />
<Compile Include="Controls\Button.cs" />
<Compile Include="Controls\ContentPresenter.cs" />
@ -83,8 +84,9 @@
<Compile Include="ILogical.cs" />
<Compile Include="Input\MouseEventArgs.cs" />
<Compile Include="Interactive.cs" />
<Compile Include="IObservableDescription.cs" />
<Compile Include="IVisual.cs" />
<Compile Include="Styling\Activator.cs" />
<Compile Include="Styling\StyleActivator.cs" />
<Compile Include="Styling\IStyleable.cs" />
<Compile Include="Styling\IStyle.cs" />
<Compile Include="PriorityValue.cs" />

10
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<object> BoxObservable<T>(IObservable<T> observable)

4
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<IObservable<bool>> inputs = new List<IObservable<bool>>();
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()

5
Perspex/Styling/Selectors.cs

@ -47,7 +47,10 @@ namespace Perspex.Styling
}
}
return new Activator(descendentMatches, ActivatorMode.Or);
return new StyleActivator(
descendentMatches,
"Descendent",
ActivatorMode.Or);
},
};
}

14
Perspex/Styling/Activator.cs → Perspex/Styling/StyleActivator.cs

@ -17,7 +17,7 @@ namespace Perspex.Styling
Or,
}
public class Activator : IObservable<bool>
public class StyleActivator : IObservable<bool>, IObservableDescription
{
ActivatorMode mode;
@ -29,10 +29,14 @@ namespace Perspex.Styling
bool last = false;
public Activator(IEnumerable<IObservable<bool>> inputs, ActivatorMode mode = ActivatorMode.And)
public StyleActivator(
IEnumerable<IObservable<bool>> inputs,
string description,
ActivatorMode mode = ActivatorMode.And)
{
int i = 0;
this.Description = description;
this.mode = mode;
foreach (IObservable<bool> input in inputs)
@ -50,6 +54,12 @@ namespace Perspex.Styling
}
}
public string Description
{
get;
private set;
}
public IDisposable Subscribe(IObserver<bool> observer)
{
Contract.Requires<ArgumentNullException>(observer != null);

67
Perspex/Themes/Default/ButtonStyle.cs

@ -13,41 +13,48 @@ namespace Perspex.Themes.Default
{
public ButtonStyle()
{
//this.AddRange(new[]
//{
// new Style(x => x.OfType<Button>())
// {
// Setters = new[]
// {
// new Setter(Button.BackgroundProperty, new SolidColorBrush(0xffdddddd)),
// new Setter(Button.BorderBrushProperty, new SolidColorBrush(0xff707070)),
// new Setter(Button.BorderThicknessProperty, 2.0),
// new Setter(Button.ForegroundProperty, new SolidColorBrush(0xff000000)),
// new Setter(Button.TemplateProperty, ControlTemplate.Create<Button>(this.Template)),
// },
// },
// new Style(x => x.OfType<Button>().Class(":mouseover"))
// {
// Setters = new[]
// {
// new Setter (Button.BackgroundProperty, new SolidColorBrush(0xffbee6fd)),
// new Setter (Button.BorderBrushProperty, new SolidColorBrush(0xff3c7fb1)),
// },
// },
// new Style(x => x.OfType<Button>().Class(":pressed"))
// {
// Setters = new[]
// {
// new Setter (Button.BackgroundProperty, new SolidColorBrush(0xffc4e5f6)),
// new Setter (Button.BorderBrushProperty, new SolidColorBrush(0xff2c628b)),
// },
// },
//});
this.AddRange(new[]
{
new Style(new Selector().OfType<Button>())
{
Setters = new[]
{
new Setter(Button.TemplateProperty, ControlTemplate.Create<Button>(this.Template)),
},
},
new Style(new Selector().OfType<Button>().Template().Id("border"))
{
Setters = new[]
{
new Setter(Button.BackgroundProperty, new SolidColorBrush(0xffdddddd)),
new Setter(Button.BorderBrushProperty, new SolidColorBrush(0xff707070)),
new Setter(Button.BorderThicknessProperty, 2.0),
new Setter(Button.ForegroundProperty, new SolidColorBrush(0xff000000)),
},
},
new Style(new Selector().OfType<Button>().Class(":mouseover").Template().Id("border"))
{
Setters = new[]
{
new Setter (Button.BackgroundProperty, new SolidColorBrush(0xffbee6fd)),
new Setter (Button.BorderBrushProperty, new SolidColorBrush(0xff3c7fb1)),
},
},
new Style(new Selector().OfType<Button>().Class(":pressed").Template().Id("border"))
{
Setters = new[]
{
new Setter (Button.BackgroundProperty, new SolidColorBrush(0xffc4e5f6)),
new Setter (Button.BorderBrushProperty, new SolidColorBrush(0xff2c628b)),
},
},
});
}
private Control Template(Button control)
{
Border border = new Border();
border.Id = "border";
border.SetValue(Border.BackgroundProperty, control.GetObservable(Button.BackgroundProperty));
border.SetValue(Border.BorderBrushProperty, control.GetObservable(Button.BorderBrushProperty));
border.SetValue(Border.BorderThicknessProperty, control.GetObservable(Button.BorderThicknessProperty));

2
Perspex/Themes/Default/DefaultTheme.cs

@ -11,7 +11,7 @@ namespace Perspex.Themes.Default
{
public DefaultTheme()
{
this.Add(new ButtonStyle());
//this.Add(new ButtonStyle());
}
}
}

20
Perspex/Visual.cs

@ -10,6 +10,7 @@ namespace Perspex
using System.Collections.Generic;
using System.Linq;
using Perspex.Controls;
using Perspex.Layout;
using Perspex.Media;
public abstract class Visual : PerspexObject, IVisual, ILogical
@ -71,8 +72,15 @@ namespace Perspex
{
if (this.visualParent != value)
{
IVisual oldValue = this.visualParent;
this.visualParent = value;
this.InheritanceParent = (PerspexObject)value;
this.VisualParentChanged(oldValue, value);
if (this.GetVisualAncestor<ILayoutRoot>() != null)
{
this.AttachedToVisualTree();
}
}
}
}
@ -81,5 +89,17 @@ namespace Perspex
{
Contract.Requires<ArgumentNullException>(context != null);
}
protected virtual void AttachedToVisualTree()
{
foreach (Visual child in ((IVisual)this).VisualChildren.OfType<Visual>())
{
child.AttachedToVisualTree();
}
}
protected virtual void VisualParentChanged(IVisual oldValue, IVisual newValue)
{
}
}
}

2
Perspex/VisualExtensions.cs

@ -11,7 +11,7 @@ namespace Perspex
public static class VisualExtensions
{
public static T GetVisualAncestor<T>(this IVisual visual) where T : Visual
public static T GetVisualAncestor<T>(this IVisual visual) where T : class
{
Contract.Requires<NullReferenceException>(visual != null);

31
TestApplication/Program.cs

@ -50,12 +50,33 @@ namespace TestApplication
Window window = new Window
{
Content = new Button
{
Content = "Hello World",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Content = new TestBorder
{
Styles = new Styles
{
new Style(new Selector().OfType<TestBorder>())
{
Setters = new[]
{
new Setter(TestBorder.BackgroundProperty, new SolidColorBrush(0xff0000ff)),
}
},
//new Style(new Selector().OfType<TestBorder>().Class(":mouseover"))
//{
// Setters = new[]
// {
// new Setter(TestBorder.BackgroundProperty, new SolidColorBrush(0xffff0000)),
// }
//},
}
},
//Content = new Button
//{
// Content = "Hello World",
// HorizontalAlignment = HorizontalAlignment.Center,
// VerticalAlignment = VerticalAlignment.Center,
//},
};
window.Show();

Loading…
Cancel
Save