// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
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 FocusableProperty =
PerspexProperty.Register("Focusable");
public static readonly PerspexProperty IsFocusedProperty =
PerspexProperty.Register("IsFocused", false);
public static readonly PerspexProperty IsPointerOverProperty =
PerspexProperty.Register("IsPointerOver");
public static readonly RoutedEvent GotFocusEvent =
RoutedEvent.Register("GotFocus", RoutingStrategy.Bubble);
public static readonly RoutedEvent LostFocusEvent =
RoutedEvent.Register("LostFocus", RoutingStrategy.Bubble);
public static readonly RoutedEvent KeyDownEvent =
RoutedEvent.Register("KeyDown", RoutingStrategy.Bubble);
public static readonly RoutedEvent PointerEnterEvent =
RoutedEvent.Register("PointerEnter", RoutingStrategy.Direct);
public static readonly RoutedEvent PointerLeaveEvent =
RoutedEvent.Register("PointerLeave", RoutingStrategy.Direct);
public static readonly RoutedEvent PointerPressedEvent =
RoutedEvent.Register("PointerPressed", RoutingStrategy.Bubble);
public static readonly RoutedEvent PointerReleasedEvent =
RoutedEvent.Register("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 GotFocus
{
add { this.AddHandler(GotFocusEvent, value); }
remove { this.RemoveHandler(GotFocusEvent, value); }
}
public event EventHandler LostFocus
{
add { this.AddHandler(LostFocusEvent, value); }
remove { this.RemoveHandler(LostFocusEvent, value); }
}
public event EventHandler KeyDown
{
add { this.AddHandler(KeyDownEvent, value); }
remove { this.RemoveHandler(KeyDownEvent, value); }
}
public event EventHandler PointerEnter
{
add { this.AddHandler(PointerEnterEvent, value); }
remove { this.RemoveHandler(PointerEnterEvent, value); }
}
public event EventHandler PointerLeave
{
add { this.AddHandler(PointerLeaveEvent, value); }
remove { this.RemoveHandler(PointerLeaveEvent, value); }
}
public event EventHandler PointerPressed
{
add { this.AddHandler(PointerPressedEvent, value); }
remove { this.RemoveHandler(PointerPressedEvent, value); }
}
public event EventHandler 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().Focus(this);
}
}
}