From 27d5d35316dd574ea40b584115e8100dab857730 Mon Sep 17 00:00:00 2001 From: grokys Date: Sun, 2 Feb 2014 16:28:03 +0100 Subject: [PATCH] Added Application class. And some more work on styling. --- Perspex/Application.cs | 42 ++++++++++++++++++++ Perspex/Controls/Control.cs | 72 +++++++++++++++++++---------------- Perspex/Controls/TextBlock.cs | 12 ------ Perspex/Perspex.csproj | 1 + Perspex/Setter.cs | 10 +++++ Perspex/Style.cs | 8 +++- Perspex/Styles.cs | 19 ++++++--- TestApplication/Program.cs | 31 +++++++++------ 8 files changed, 132 insertions(+), 63 deletions(-) create mode 100644 Perspex/Application.cs diff --git a/Perspex/Application.cs b/Perspex/Application.cs new file mode 100644 index 0000000000..1d26c4daf5 --- /dev/null +++ b/Perspex/Application.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Perspex +{ + public class Application + { + private Styles styles; + + public Application() + { + Current = this; + } + + public static Application Current + { + get; + private set; + } + + public Styles Styles + { + get + { + if (this.styles == null) + { + this.styles = new Styles(); + } + + return this.styles; + } + + set + { + this.styles = value; + } + } + } +} diff --git a/Perspex/Controls/Control.cs b/Perspex/Controls/Control.cs index 200f29c0f5..ff9ad7ed45 100644 --- a/Perspex/Controls/Control.cs +++ b/Perspex/Controls/Control.cs @@ -36,7 +36,7 @@ namespace Perspex.Controls new ReadOnlyPerspexProperty(ParentPropertyRW); public static readonly PerspexProperty BackgroundProperty = - PerspexProperty.Register("Background"); + PerspexProperty.Register("Background", inherits: true); public static readonly PerspexProperty BorderBrushProperty = PerspexProperty.Register("BorderBrush"); @@ -44,18 +44,21 @@ namespace Perspex.Controls public static readonly PerspexProperty BorderThicknessProperty = PerspexProperty.Register("BorderThickness"); + public static readonly PerspexProperty ForegroundProperty = + PerspexProperty.Register("Foreground", new SolidColorBrush(0xff000000), true); + public static readonly PerspexProperty IsMouseOverProperty = PerspexProperty.Register("IsMouseOver"); public static readonly PerspexProperty HorizontalAlignmentProperty = PerspexProperty.Register("HorizontalAlignment"); - public static readonly PerspexProperty VerticalAlignmentProperty = - PerspexProperty.Register("VerticalAlignment"); - public static readonly PerspexProperty MarginProperty = PerspexProperty.Register("Margin"); + public static readonly PerspexProperty VerticalAlignmentProperty = + PerspexProperty.Register("VerticalAlignment"); + internal static readonly PerspexProperty ParentPropertyRW = PerspexProperty.Register("Parent"); @@ -106,22 +109,10 @@ namespace Perspex.Controls private set; } - public Styles Styles + public Brush Foreground { - get - { - if (this.styles == null) - { - this.styles = new Styles(); - } - - return this.styles; - } - - set - { - this.styles = value; - } + get { return this.GetValue(ForegroundProperty); } + set { this.SetValue(ForegroundProperty, value); } } public Size? DesiredSize @@ -142,12 +133,6 @@ namespace Perspex.Controls set { this.SetValue(HorizontalAlignmentProperty, value); } } - public VerticalAlignment VerticalAlignment - { - get { return this.GetValue(VerticalAlignmentProperty); } - set { this.SetValue(VerticalAlignmentProperty, value); } - } - public Thickness Margin { get { return this.GetValue(MarginProperty); } @@ -160,6 +145,30 @@ namespace Perspex.Controls internal set { this.SetValue(ParentPropertyRW, value); } } + public Styles Styles + { + get + { + if (this.styles == null) + { + this.styles = new Styles(); + } + + return this.styles; + } + + set + { + this.styles = value; + } + } + + public VerticalAlignment VerticalAlignment + { + get { return this.GetValue(VerticalAlignmentProperty); } + set { this.SetValue(VerticalAlignmentProperty, value); } + } + public ILayoutRoot GetLayoutRoot() { Control c = this; @@ -214,18 +223,15 @@ namespace Perspex.Controls private void AttachStyles(Control control) { - Contract.Requires(control != null); - - Control parent = control.Parent; - - if (parent != null) + if (control != null) { + Control parent = control.Parent; this.AttachStyles(parent); + control.Styles.Attach(this); } - - foreach (Style style in control.Styles) + else { - style.Attach(this); + Application.Current.Styles.Attach(this); } } diff --git a/Perspex/Controls/TextBlock.cs b/Perspex/Controls/TextBlock.cs index 5dfbcbc608..454bcb8a38 100644 --- a/Perspex/Controls/TextBlock.cs +++ b/Perspex/Controls/TextBlock.cs @@ -15,12 +15,6 @@ namespace Perspex.Controls "FontSize", inherits: true); - public static readonly PerspexProperty ForegroundProperty = - PerspexProperty.Register( - "Foreground", - defaultValue: new SolidColorBrush(0xff000000), - inherits: true); - public static readonly PerspexProperty TextProperty = PerspexProperty.Register("Text"); @@ -30,12 +24,6 @@ namespace Perspex.Controls set { this.SetValue(FontSizeProperty, value); } } - public Brush Foreground - { - get { return this.GetValue(ForegroundProperty); } - set { this.SetValue(ForegroundProperty, value); } - } - public string Text { get { return this.GetValue(TextProperty); } diff --git a/Perspex/Perspex.csproj b/Perspex/Perspex.csproj index 396cb188b9..35c78dac72 100644 --- a/Perspex/Perspex.csproj +++ b/Perspex/Perspex.csproj @@ -68,6 +68,7 @@ 4 + diff --git a/Perspex/Setter.cs b/Perspex/Setter.cs index cfc4ca8488..7d8f591284 100644 --- a/Perspex/Setter.cs +++ b/Perspex/Setter.cs @@ -16,6 +16,16 @@ namespace Perspex { private object oldValue; + public Setter() + { + } + + public Setter(PerspexProperty property, object value) + { + this.Property = property; + this.Value = value; + } + public PerspexProperty Property { get; diff --git a/Perspex/Style.cs b/Perspex/Style.cs index 70ec45bd32..abdd9317ea 100644 --- a/Perspex/Style.cs +++ b/Perspex/Style.cs @@ -15,13 +15,17 @@ namespace Perspex public class Style { - private bool applied; - public Style() { this.Setters = new List(); } + public Style(Func selector) + : this() + { + this.Selector = selector; + } + public Func Selector { get; diff --git a/Perspex/Styles.cs b/Perspex/Styles.cs index 3138db7db8..cbf431e408 100644 --- a/Perspex/Styles.cs +++ b/Perspex/Styles.cs @@ -1,12 +1,21 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- namespace Perspex { + using Perspex.Controls; + public class Styles : PerspexList