Browse Source

Moved input stuff into InputElement

Out of CONTROL dude!!!
pull/4/head
Steven Kirk 12 years ago
parent
commit
88287b14ba
  1. 109
      Perspex/Controls/Control.cs
  2. 4
      Perspex/Input/FocusManager.cs
  3. 4
      Perspex/Input/IFocusManager.cs
  4. 17
      Perspex/Input/IFocusable.cs
  5. 35
      Perspex/Input/IInputElement.cs
  6. 125
      Perspex/Input/InputElement.cs
  7. 4
      Perspex/Input/InputManager.cs
  8. 6
      Perspex/Input/MouseDevice.cs
  9. 3
      Perspex/Perspex.csproj

109
Perspex/Controls/Control.cs

@ -17,7 +17,7 @@ namespace Perspex.Controls
using Perspex.Styling;
using Splat;
public class Control : Interactive, IFocusable, ILogical, IStyleable, IStyled
public class Control : InputElement, ILogical, IStyleable, IStyled
{
public static readonly PerspexProperty<Brush> BackgroundProperty =
PerspexProperty.Register<Control, Brush>("Background", inherits: true);
@ -28,9 +28,6 @@ namespace Perspex.Controls
public static readonly PerspexProperty<double> BorderThicknessProperty =
PerspexProperty.Register<Control, double>("BorderThickness");
public static readonly PerspexProperty<bool> FocusableProperty =
PerspexProperty.Register<Control, bool>("Focusable");
public static readonly PerspexProperty<double> FontSizeProperty =
PerspexProperty.Register<Control, double>(
"FontSize",
@ -40,36 +37,9 @@ namespace Perspex.Controls
public static readonly PerspexProperty<Brush> ForegroundProperty =
PerspexProperty.Register<Control, Brush>("Foreground", new SolidColorBrush(0xff000000), true);
public static readonly PerspexProperty<bool> IsFocusedProperty =
PerspexProperty.Register<Control, bool>("IsFocused", false);
public static readonly PerspexProperty<bool> IsPointerOverProperty =
PerspexProperty.Register<Control, bool>("IsPointerOver");
public static readonly PerspexProperty<Control> ParentProperty =
PerspexProperty.Register<Control, Control>("Parent");
public static readonly RoutedEvent<RoutedEventArgs> GotFocusEvent =
RoutedEvent.Register<Control, RoutedEventArgs>("GotFocus", RoutingStrategy.Bubble);
public static readonly RoutedEvent<RoutedEventArgs> LostFocusEvent =
RoutedEvent.Register<Control, RoutedEventArgs>("LostFocus", RoutingStrategy.Bubble);
public static readonly RoutedEvent<KeyEventArgs> KeyDownEvent =
RoutedEvent.Register<Control, KeyEventArgs>("KeyDown", RoutingStrategy.Bubble);
public static readonly RoutedEvent<PointerEventArgs> PointerEnterEvent =
RoutedEvent.Register<Control, PointerEventArgs>("PointerEnter", RoutingStrategy.Direct);
public static readonly RoutedEvent<PointerEventArgs> PointerLeaveEvent =
RoutedEvent.Register<Control, PointerEventArgs>("PointerLeave", RoutingStrategy.Direct);
public static readonly RoutedEvent<PointerEventArgs> PointerPressedEvent =
RoutedEvent.Register<Control, PointerEventArgs>("PointerPressed", RoutingStrategy.Bubble);
public static readonly RoutedEvent<PointerEventArgs> PointerReleasedEvent =
RoutedEvent.Register<Control, PointerEventArgs>("PointerReleased", RoutingStrategy.Bubble);
private Classes classes;
private string id;
@ -84,56 +54,10 @@ namespace Perspex.Controls
public Control()
{
this.classes = new Classes();
this.GotFocus += (s, e) => this.IsFocused = true;
this.LostFocus += (s, e) => this.IsFocused = false;
this.PointerEnter += (s, e) => this.IsPointerOver = true;
this.PointerLeave += (s, e) => this.IsPointerOver = false;
this.AddPseudoClass(IsPointerOverProperty, ":pointerover");
this.AddPseudoClass(IsFocusedProperty, ":focus");
}
public event EventHandler<RoutedEventArgs> GotFocus
{
add { this.AddHandler(GotFocusEvent, value); }
remove { this.RemoveHandler(GotFocusEvent, value); }
}
public event EventHandler<RoutedEventArgs> LostFocus
{
add { this.AddHandler(LostFocusEvent, value); }
remove { this.RemoveHandler(LostFocusEvent, value); }
}
public event EventHandler<KeyEventArgs> KeyDown
{
add { this.AddHandler(KeyDownEvent, value); }
remove { this.RemoveHandler(KeyDownEvent, value); }
}
public event EventHandler<PointerEventArgs> PointerEnter
{
add { this.AddHandler(PointerEnterEvent, value); }
remove { this.RemoveHandler(PointerEnterEvent, value); }
}
public event EventHandler<PointerEventArgs> PointerLeave
{
add { this.AddHandler(PointerLeaveEvent, value); }
remove { this.RemoveHandler(PointerLeaveEvent, value); }
}
public event EventHandler<PointerEventArgs> PointerPressed
{
add { this.AddHandler(PointerPressedEvent, value); }
remove { this.RemoveHandler(PointerPressedEvent, value); }
}
public event EventHandler<PointerEventArgs> PointerReleased
{
add { this.AddHandler(PointerReleasedEvent, value); }
remove { this.RemoveHandler(PointerReleasedEvent, value); }
}
public Brush Background
{
get { return this.GetValue(BackgroundProperty); }
@ -175,24 +99,12 @@ namespace Perspex.Controls
set { this.SetValue(FontSizeProperty, value); }
}
public bool Focusable
{
get { return this.GetValue(FocusableProperty); }
set { this.SetValue(FocusableProperty, value); }
}
public Brush Foreground
{
get { return this.GetValue(ForegroundProperty); }
set { this.SetValue(ForegroundProperty, value); }
}
public bool IsFocused
{
get { return this.GetValue(IsFocusedProperty); }
private set { this.SetValue(IsFocusedProperty, value); }
}
public string Id
{
get
@ -216,12 +128,6 @@ namespace Perspex.Controls
}
}
public bool IsPointerOver
{
get { return this.GetValue(IsPointerOverProperty); }
internal set { this.SetValue(IsPointerOverProperty, value); }
}
public Control Parent
{
get { return this.GetValue(ParentProperty); }
@ -263,9 +169,11 @@ namespace Perspex.Controls
get { return Enumerable.Empty<ILogical>(); }
}
public void Focus()
protected override void AttachedToVisualTree()
{
Locator.Current.GetService<IFocusManager>().Focus(this);
IStyler styler = Locator.Current.GetService<IStyler>();
styler.ApplyStyles(this);
base.AttachedToVisualTree();
}
protected void AddPseudoClass(PerspexProperty<bool> property, string className)
@ -282,12 +190,5 @@ namespace Perspex.Controls
}
});
}
protected override void AttachedToVisualTree()
{
IStyler styler = Locator.Current.GetService<IStyler>();
styler.ApplyStyles(this);
base.AttachedToVisualTree();
}
}
}

4
Perspex/Input/FocusManager.cs

@ -10,13 +10,13 @@ namespace Perspex.Input
{
public class FocusManager : IFocusManager
{
public IFocusable Current
public IInputElement Current
{
get;
private set;
}
public void Focus(IFocusable control)
public void Focus(IInputElement control)
{
Interactive current = this.Current as Interactive;
Interactive next = control as Interactive;

4
Perspex/Input/IFocusManager.cs

@ -14,8 +14,8 @@ namespace Perspex.Input
public interface IFocusManager
{
IFocusable Current { get; }
IInputElement Current { get; }
void Focus(IFocusable focusable);
void Focus(IInputElement focusable);
}
}

17
Perspex/Input/IFocusable.cs

@ -1,17 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="IFocusable.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Input
{
public interface IFocusable
{
bool Focusable { get; }
bool IsFocused { get; }
void Focus();
}
}

35
Perspex/Input/IInputElement.cs

@ -0,0 +1,35 @@
// -----------------------------------------------------------------------
// <copyright file="IInputElement.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Input
{
using System;
public interface IInputElement
{
event EventHandler<RoutedEventArgs> GotFocus;
event EventHandler<RoutedEventArgs> LostFocus;
event EventHandler<KeyEventArgs> KeyDown;
event EventHandler<PointerEventArgs> PointerEnter;
event EventHandler<PointerEventArgs> PointerLeave;
event EventHandler<PointerEventArgs> PointerPressed;
event EventHandler<PointerEventArgs> PointerReleased;
bool Focusable { get; }
bool IsFocused { get; }
bool IsPointerOver { get; }
void Focus();
}
}

125
Perspex/Input/InputElement.cs

@ -0,0 +1,125 @@
// -----------------------------------------------------------------------
// <copyright file="Control.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Input
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using Perspex.Input;
using Perspex.Layout;
using Perspex.Media;
using Perspex.Styling;
using Splat;
public class InputElement : Interactive, IInputElement
{
public static readonly PerspexProperty<bool> FocusableProperty =
PerspexProperty.Register<InputElement, bool>("Focusable");
public static readonly PerspexProperty<bool> IsFocusedProperty =
PerspexProperty.Register<InputElement, bool>("IsFocused", false);
public static readonly PerspexProperty<bool> IsPointerOverProperty =
PerspexProperty.Register<InputElement, bool>("IsPointerOver");
public static readonly RoutedEvent<RoutedEventArgs> GotFocusEvent =
RoutedEvent.Register<InputElement, RoutedEventArgs>("GotFocus", RoutingStrategy.Bubble);
public static readonly RoutedEvent<RoutedEventArgs> LostFocusEvent =
RoutedEvent.Register<InputElement, RoutedEventArgs>("LostFocus", RoutingStrategy.Bubble);
public static readonly RoutedEvent<KeyEventArgs> KeyDownEvent =
RoutedEvent.Register<InputElement, KeyEventArgs>("KeyDown", RoutingStrategy.Bubble);
public static readonly RoutedEvent<PointerEventArgs> PointerEnterEvent =
RoutedEvent.Register<InputElement, PointerEventArgs>("PointerEnter", RoutingStrategy.Direct);
public static readonly RoutedEvent<PointerEventArgs> PointerLeaveEvent =
RoutedEvent.Register<InputElement, PointerEventArgs>("PointerLeave", RoutingStrategy.Direct);
public static readonly RoutedEvent<PointerEventArgs> PointerPressedEvent =
RoutedEvent.Register<InputElement, PointerEventArgs>("PointerPressed", RoutingStrategy.Bubble);
public static readonly RoutedEvent<PointerEventArgs> PointerReleasedEvent =
RoutedEvent.Register<InputElement, PointerEventArgs>("PointerReleased", RoutingStrategy.Bubble);
public InputElement()
{
this.GotFocus += (s, e) => this.IsFocused = true;
this.LostFocus += (s, e) => this.IsFocused = false;
this.PointerEnter += (s, e) => this.IsPointerOver = true;
this.PointerLeave += (s, e) => this.IsPointerOver = false;
}
public event EventHandler<RoutedEventArgs> GotFocus
{
add { this.AddHandler(GotFocusEvent, value); }
remove { this.RemoveHandler(GotFocusEvent, value); }
}
public event EventHandler<RoutedEventArgs> LostFocus
{
add { this.AddHandler(LostFocusEvent, value); }
remove { this.RemoveHandler(LostFocusEvent, value); }
}
public event EventHandler<KeyEventArgs> KeyDown
{
add { this.AddHandler(KeyDownEvent, value); }
remove { this.RemoveHandler(KeyDownEvent, value); }
}
public event EventHandler<PointerEventArgs> PointerEnter
{
add { this.AddHandler(PointerEnterEvent, value); }
remove { this.RemoveHandler(PointerEnterEvent, value); }
}
public event EventHandler<PointerEventArgs> PointerLeave
{
add { this.AddHandler(PointerLeaveEvent, value); }
remove { this.RemoveHandler(PointerLeaveEvent, value); }
}
public event EventHandler<PointerEventArgs> PointerPressed
{
add { this.AddHandler(PointerPressedEvent, value); }
remove { this.RemoveHandler(PointerPressedEvent, value); }
}
public event EventHandler<PointerEventArgs> PointerReleased
{
add { this.AddHandler(PointerReleasedEvent, value); }
remove { this.RemoveHandler(PointerReleasedEvent, value); }
}
public bool Focusable
{
get { return this.GetValue(FocusableProperty); }
set { this.SetValue(FocusableProperty, value); }
}
public bool IsFocused
{
get { return this.GetValue(IsFocusedProperty); }
private set { this.SetValue(IsFocusedProperty, value); }
}
public bool IsPointerOver
{
get { return this.GetValue(IsPointerOverProperty); }
internal set { this.SetValue(IsPointerOverProperty, value); }
}
public void Focus()
{
Locator.Current.GetService<IFocusManager>().Focus(this);
}
}
}

4
Perspex/Input/InputManager.cs

@ -37,7 +37,7 @@ namespace Perspex.Input
{
PointerEventArgs e = new PointerEventArgs
{
RoutedEvent = Control.PointerLeaveEvent,
RoutedEvent = InputElement.PointerLeaveEvent,
Device = device,
OriginalSource = control,
Source = control,
@ -51,7 +51,7 @@ namespace Perspex.Input
{
PointerEventArgs e = new PointerEventArgs
{
RoutedEvent = Control.PointerEnterEvent,
RoutedEvent = InputElement.PointerEnterEvent,
Device = device,
OriginalSource = control,
Source = control,

6
Perspex/Input/MouseDevice.cs

@ -105,10 +105,10 @@ namespace Perspex.Input
if (hit != null)
{
Interactive interactive = this.Captured ?? (hit as Interactive) ?? hit.GetVisualAncestor<Interactive>();
IFocusable focusable =
this.Captured as IFocusable ??
IInputElement focusable =
this.Captured as IInputElement ??
hit.GetVisualAncestorsAndSelf()
.OfType<IFocusable>()
.OfType<IInputElement>()
.FirstOrDefault(x => x.Focusable);
if (interactive != null)

3
Perspex/Perspex.csproj

@ -84,9 +84,10 @@
<Compile Include="Controls\LogicalChildren.cs" />
<Compile Include="Controls\Panel.cs" />
<Compile Include="Controls\StackPanel.cs" />
<Compile Include="Input\InputElement.cs" />
<Compile Include="Input\IFocusManager.cs" />
<Compile Include="Input\FocusManager.cs" />
<Compile Include="Input\IFocusable.cs" />
<Compile Include="Input\IInputElement.cs" />
<Compile Include="Input\Key.cs" />
<Compile Include="Input\KeyboardDevice.cs" />
<Compile Include="Input\IKeyboardDevice.cs" />

Loading…
Cancel
Save