Browse Source

Added FocusManager.

pull/4/head
Steven Kirk 12 years ago
parent
commit
406ad96736
  1. 8
      Perspex/Application.cs
  2. 5
      Perspex/Controls/Button.cs
  3. 10
      Perspex/Controls/Control.cs
  4. 6
      Perspex/Controls/TextBox.cs
  5. 31
      Perspex/Input/FocusManager.cs
  6. 21
      Perspex/Input/IFocusManager.cs
  7. 2
      Perspex/Input/IFocusable.cs
  8. 4
      Perspex/Input/InputManager.cs
  9. 2
      Perspex/Perspex.csproj

8
Perspex/Application.cs

@ -17,6 +17,7 @@ namespace Perspex
public Application()
{
Current = this;
this.FocusManager = new FocusManager();
this.InputManager = new InputManager();
}
@ -26,6 +27,12 @@ namespace Perspex
private set;
}
public IFocusManager FocusManager
{
get;
private set;
}
public InputManager InputManager
{
get;
@ -53,6 +60,7 @@ namespace Perspex
public void RegisterServices()
{
Styler styler = new Styler();
Locator.CurrentMutable.Register(() => this.FocusManager, typeof(IFocusManager));
Locator.CurrentMutable.Register(() => this.InputManager, typeof(IInputManager));
Locator.CurrentMutable.Register(() => styler, typeof(IStyler));
}

5
Perspex/Controls/Button.cs

@ -13,6 +13,11 @@ namespace Perspex.Controls
public static readonly RoutedEvent<RoutedEventArgs> ClickEvent =
RoutedEvent.Register<Button, RoutedEventArgs>("Click", RoutingStrategy.Bubble);
static Button()
{
FocusableProperty.OverrideDefaultValue(typeof(Button), true);
}
public Button()
{
this.PointerPressed += (s, e) =>

10
Perspex/Controls/Control.cs

@ -246,7 +246,7 @@ namespace Perspex.Controls
public bool IsFocused
{
get { return this.GetValue(IsFocusedProperty); }
set { this.SetValue(IsFocusedProperty, value); }
internal set { this.SetValue(IsFocusedProperty, value); }
}
public string Id
@ -356,6 +356,12 @@ namespace Perspex.Controls
set { this.SetValue(WidthProperty, value); }
}
bool IFocusable.IsFocused
{
get { return this.GetValue(IsFocusedProperty); }
set { this.SetValue(IsFocusedProperty, value); }
}
ILogical ILogical.LogicalParent
{
get { return this.Parent; }
@ -385,7 +391,7 @@ namespace Perspex.Controls
public void Focus()
{
this.IsFocused = true;
Locator.Current.GetService<IFocusManager>().Focus(this);
}
public void InvalidateArrange()

6
Perspex/Controls/TextBox.cs

@ -19,10 +19,14 @@ namespace Perspex.Controls
private TextBoxView textBoxView;
static TextBox()
{
FocusableProperty.OverrideDefaultValue(typeof(TextBox), true);
}
public TextBox()
{
this.GetObservable(TextProperty).Subscribe(_ => this.InvalidateVisual());
FocusableProperty.OverrideDefaultValue(typeof(TextBox), true);
}
public int CaretIndex

31
Perspex/Input/FocusManager.cs

@ -0,0 +1,31 @@
// -----------------------------------------------------------------------
// <copyright file="IFocusManager.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Input
{
public class FocusManager : IFocusManager
{
public IFocusable Current
{
get;
private set;
}
public void Focus(IFocusable control)
{
if (this.Current != null)
{
this.Current.IsFocused = false;
}
if (control != null)
{
control.IsFocused = true;
this.Current = control;
}
}
}
}

21
Perspex/Input/IFocusManager.cs

@ -0,0 +1,21 @@
// -----------------------------------------------------------------------
// <copyright file="IFocusManager.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.Linq;
using System.Text;
using System.Threading.Tasks;
public interface IFocusManager
{
IFocusable Current { get; }
void Focus(IFocusable focusable);
}
}

2
Perspex/Input/IFocusable.cs

@ -10,7 +10,7 @@ namespace Perspex.Input
{
bool Focusable { get; }
bool IsFocused { get; }
bool IsFocused { get; set; }
void Focus();
}

4
Perspex/Input/InputManager.cs

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

2
Perspex/Perspex.csproj

@ -84,6 +84,8 @@
<Compile Include="Controls\LogicalChildren.cs" />
<Compile Include="Controls\Panel.cs" />
<Compile Include="Controls\StackPanel.cs" />
<Compile Include="Input\IFocusManager.cs" />
<Compile Include="Input\FocusManager.cs" />
<Compile Include="Input\IFocusable.cs" />
<Compile Include="Input\IMouseDevice.cs" />
<Compile Include="Input\IPointerDevice.cs" />

Loading…
Cancel
Save