Browse Source

Styles now applied properly.

pull/4/head
Steven Kirk 12 years ago
parent
commit
766c0890de
  1. 5
      Perspex.UnitTests/TestRoot.cs
  2. 32
      Perspex/Controls/TestBorder.cs
  3. 1
      Perspex/Perspex.csproj
  4. 5
      Perspex/PerspexProperty.cs
  5. 9
      Perspex/Styling/Style.cs
  6. 22
      Perspex/Styling/StyleActivator.cs
  7. 18
      Perspex/Styling/Styler.cs
  8. 3
      Perspex/Themes/Default/ButtonStyle.cs
  9. 2
      Perspex/Themes/Default/DefaultTheme.cs
  10. 22
      TestApplication/Program.cs

5
Perspex.UnitTests/TestRoot.cs

@ -9,6 +9,7 @@ namespace Perspex.UnitTests
using System;
using System.Collections.Generic;
using System.Reactive.Disposables;
using Moq;
using Perspex.Controls;
using Perspex.Layout;
@ -16,12 +17,12 @@ namespace Perspex.UnitTests
{
public Size ClientSize
{
get { throw new NotImplementedException(); }
get { return new Size(100, 100); }
}
public ILayoutManager LayoutManager
{
get { throw new NotImplementedException(); }
get { return new Mock<ILayoutManager>().Object; }
}
}
}

32
Perspex/Controls/TestBorder.cs

@ -1,32 +0,0 @@
// -----------------------------------------------------------------------
// <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));
}
}
}
}

1
Perspex/Perspex.csproj

@ -71,7 +71,6 @@
<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" />

5
Perspex/PerspexProperty.cs

@ -162,6 +162,11 @@ namespace Perspex
this.defaultValues.Add(type, defaultValue);
}
public override string ToString()
{
return this.Name;
}
}
/// <summary>

9
Perspex/Styling/Style.cs

@ -41,11 +41,14 @@ namespace Perspex.Styling
public void Attach(IStyleable control)
{
string description = "Style " + this.Selector.ToString();
IObservable<bool> activator = this.Selector.GetActivator(control);
StyleActivator activator = this.Selector.GetActivator(control);
foreach (Setter setter in this.Setters)
if (!(activator.CurrentValue == false && activator.HasCompleted))
{
control.SetValue(setter.Property, setter.Value, activator);
foreach (Setter setter in this.Setters)
{
control.SetValue(setter.Property, setter.Value, activator);
}
}
}
}

22
Perspex/Styling/StyleActivator.cs

@ -27,8 +27,6 @@ namespace Perspex.Styling
List<IObserver<bool>> observers = new List<IObserver<bool>>();
bool last = false;
public StyleActivator(
IEnumerable<IObservable<bool>> inputs,
string description,
@ -54,18 +52,30 @@ namespace Perspex.Styling
}
}
public bool CurrentValue
{
get;
private set;
}
public string Description
{
get;
private set;
}
public bool HasCompleted
{
get;
private set;
}
public IDisposable Subscribe(IObserver<bool> observer)
{
Contract.Requires<ArgumentNullException>(observer != null);
this.observers.Add(observer);
observer.OnNext(last);
observer.OnNext(CurrentValue);
return Disposable.Create(() => this.observers.Remove(observer));
}
@ -87,10 +97,10 @@ namespace Perspex.Styling
throw new InvalidOperationException("Invalid Activator mode.");
}
if (current != last)
if (current != CurrentValue)
{
this.Push(current);
last = current;
CurrentValue = current;
}
}
@ -107,6 +117,8 @@ namespace Perspex.Styling
{
subscription.Dispose();
}
this.HasCompleted = true;
}
}

18
Perspex/Styling/Styler.cs

@ -18,7 +18,23 @@ namespace Perspex.Styling
{
IVisual visual = control as IVisual;
IStyled styleContainer = visual.GetVisualAncestorOrSelf<IStyled>();
styleContainer.Styles.Attach(control);
Application.Current.Styles.Attach(control);
this.ApplyStyles(control, styleContainer);
}
private void ApplyStyles(IStyleable control, IStyled container)
{
if (container != null)
{
IVisual visual = container as IVisual;
if (visual != null)
{
this.ApplyStyles(control, visual.GetVisualAncestor<IStyled>());
}
container.Styles.Attach(control);
}
}
}
}

3
Perspex/Themes/Default/ButtonStyle.cs

@ -55,9 +55,6 @@ namespace Perspex.Themes.Default
{
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));
border.Padding = new Thickness(3);
ContentPresenter contentPresenter = new ContentPresenter();
contentPresenter.SetValue(ContentPresenter.ContentProperty, control.GetObservable(Button.ContentProperty));

2
Perspex/Themes/Default/DefaultTheme.cs

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

22
TestApplication/Program.cs

@ -50,28 +50,6 @@ namespace TestApplication
Window window = new Window
{
//Content = new TestBorder
//{
// Margin = new Thickness(10),
// 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",

Loading…
Cancel
Save