Browse Source

Add :mouseover pseudoclass.

pull/4/head
grokys 13 years ago
parent
commit
4675ec41d6
  1. 7
      Perspex.Windows/Window.cs
  2. 26
      Perspex/Controls/Control.cs
  3. 1
      Perspex/Perspex.csproj
  4. 30
      Perspex/PerspexList.cs
  5. 24
      Perspex/Selectors.cs
  6. 9
      Perspex/Visual.cs
  7. 4
      TestApplication/Program.cs

7
Perspex.Windows/Window.cs

@ -134,7 +134,12 @@ namespace Perspex.Windows
private void MouseMove(Visual visual, Point p) private void MouseMove(Visual visual, Point p)
{ {
visual.IsMouseOver = visual.Bounds.Contains(p); Control control = visual as Control;
if (control != null)
{
control.IsMouseOver = visual.Bounds.Contains(p);
}
foreach (Visual child in visual.VisualChildren) foreach (Visual child in visual.VisualChildren)
{ {

26
Perspex/Controls/Control.cs

@ -44,6 +44,9 @@ namespace Perspex.Controls
public static readonly PerspexProperty<double> BorderThicknessProperty = public static readonly PerspexProperty<double> BorderThicknessProperty =
PerspexProperty.Register<Control, double>("BorderThickness"); PerspexProperty.Register<Control, double>("BorderThickness");
public static readonly PerspexProperty<bool> IsMouseOverProperty =
PerspexProperty.Register<Visual, bool>("IsMouseOver");
public static readonly PerspexProperty<HorizontalAlignment> HorizontalAlignmentProperty = public static readonly PerspexProperty<HorizontalAlignment> HorizontalAlignmentProperty =
PerspexProperty.Register<Control, HorizontalAlignment>("HorizontalAlignment"); PerspexProperty.Register<Control, HorizontalAlignment>("HorizontalAlignment");
@ -58,10 +61,21 @@ namespace Perspex.Controls
public Control() public Control()
{ {
this.Classes = new ObservableCollection<string>(); this.Classes = new PerspexList<string>();
this.Styles = new ObservableCollection<Style>();
this.GetObservableWithHistory(ParentPropertyRW).Subscribe(this.ParentChanged); this.GetObservableWithHistory(ParentPropertyRW).Subscribe(this.ParentChanged);
this.GetObservable(IsMouseOverProperty).Subscribe(x =>
{
if (x)
{
this.Classes.Add(":mouseover");
}
else
{
this.Classes.Remove(":mouseover");
}
});
// Hacky hack hack! // Hacky hack hack!
this.GetObservable(BackgroundProperty).Skip(1).Subscribe(_ => this.InvalidateMeasure()); this.GetObservable(BackgroundProperty).Skip(1).Subscribe(_ => this.InvalidateMeasure());
} }
@ -84,7 +98,7 @@ namespace Perspex.Controls
set { this.SetValue(BorderThicknessProperty, value); } set { this.SetValue(BorderThicknessProperty, value); }
} }
public ObservableCollection<string> Classes public PerspexList<string> Classes
{ {
get; get;
private set; private set;
@ -102,6 +116,12 @@ namespace Perspex.Controls
set; set;
} }
public bool IsMouseOver
{
get { return this.GetValue(IsMouseOverProperty); }
set { this.SetValue(IsMouseOverProperty, value); }
}
public HorizontalAlignment HorizontalAlignment public HorizontalAlignment HorizontalAlignment
{ {
get { return this.GetValue(HorizontalAlignmentProperty); } get { return this.GetValue(HorizontalAlignmentProperty); }

1
Perspex/Perspex.csproj

@ -87,6 +87,7 @@
<Compile Include="Media\Matrix.cs" /> <Compile Include="Media\Matrix.cs" />
<Compile Include="Media\Pen.cs" /> <Compile Include="Media\Pen.cs" />
<Compile Include="Media\SolidColorBrush.cs" /> <Compile Include="Media\SolidColorBrush.cs" />
<Compile Include="PerspexList.cs" />
<Compile Include="PerspexObject.cs" /> <Compile Include="PerspexObject.cs" />
<Compile Include="PerspexProperty.cs" /> <Compile Include="PerspexProperty.cs" />
<Compile Include="PerspexPropertyChangedEventArgs.cs" /> <Compile Include="PerspexPropertyChangedEventArgs.cs" />

30
Perspex/PerspexList.cs

@ -0,0 +1,30 @@
namespace Perspex
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
public class PerspexList<T> : ObservableCollection<T>
{
public PerspexList()
{
this.Changed = Observable.FromEvent<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
handler => (sender, e) => handler(e),
handler => this.CollectionChanged += handler,
handler => this.CollectionChanged -= handler);
}
public IObservable<NotifyCollectionChangedEventArgs> Changed
{
get;
private set;
}
}
}

24
Perspex/Selectors.cs

@ -8,6 +8,7 @@ namespace Perspex
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Linq; using System.Linq;
using System.Reactive.Linq; using System.Reactive.Linq;
@ -34,6 +35,29 @@ namespace Perspex
} }
} }
public static Match Class(this Match selector, string name)
{
Contract.Requires<ArgumentNullException>(name != null);
if (selector != null)
{
IObservable<bool> match = Observable
.Return(selector.Control.Classes.Contains(name))
.Concat(selector.Control.Classes.Changed.Select(e => selector.Control.Classes.Contains(name)));
return new Match
{
Control = selector.Control,
Observable = match,
Previous = selector,
};
}
else
{
return null;
}
}
public static Match PropertyEquals<T>(this Match selector, PerspexProperty<T> property, T value) public static Match PropertyEquals<T>(this Match selector, PerspexProperty<T> property, T value)
{ {
Contract.Requires<ArgumentNullException>(property != null); Contract.Requires<ArgumentNullException>(property != null);

9
Perspex/Visual.cs

@ -17,9 +17,6 @@ namespace Perspex
public static readonly ReadOnlyPerspexProperty<Rect> BoundsProperty = public static readonly ReadOnlyPerspexProperty<Rect> BoundsProperty =
new ReadOnlyPerspexProperty<Rect>(BoundsPropertyRW); new ReadOnlyPerspexProperty<Rect>(BoundsPropertyRW);
public static readonly PerspexProperty<bool> IsMouseOverProperty =
PerspexProperty.Register<Visual, bool>("IsMouseOver");
private static readonly PerspexProperty<Rect> BoundsPropertyRW = private static readonly PerspexProperty<Rect> BoundsPropertyRW =
PerspexProperty.Register<Visual, Rect>("Bounds"); PerspexProperty.Register<Visual, Rect>("Bounds");
@ -31,12 +28,6 @@ namespace Perspex
protected set { this.SetValue(BoundsPropertyRW, value); } protected set { this.SetValue(BoundsPropertyRW, value); }
} }
public bool IsMouseOver
{
get { return this.GetValue(IsMouseOverProperty); }
set { this.SetValue(IsMouseOverProperty, value); }
}
public virtual IEnumerable<Visual> VisualChildren public virtual IEnumerable<Visual> VisualChildren
{ {
get { return Enumerable.Empty<Visual>(); } get { return Enumerable.Empty<Visual>(); }

4
TestApplication/Program.cs

@ -22,11 +22,11 @@ namespace TestApplication
Window window = new Window Window window = new Window
{ {
Styles = new ObservableCollection<Style> Styles = new[]
{ {
new Style new Style
{ {
Selector = x => x.Select<Button>().PropertyEquals(Visual.IsMouseOverProperty, true), Selector = x => x.Select<Button>().Class(":mouseover"),
Setters = new[] Setters = new[]
{ {
new Setter new Setter

Loading…
Cancel
Save