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

5
Perspex/Controls/Button.cs

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

10
Perspex/Controls/Control.cs

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

6
Perspex/Controls/TextBox.cs

@ -19,10 +19,14 @@ namespace Perspex.Controls
private TextBoxView textBoxView; private TextBoxView textBoxView;
static TextBox()
{
FocusableProperty.OverrideDefaultValue(typeof(TextBox), true);
}
public TextBox() public TextBox()
{ {
this.GetObservable(TextProperty).Subscribe(_ => this.InvalidateVisual()); this.GetObservable(TextProperty).Subscribe(_ => this.InvalidateVisual());
FocusableProperty.OverrideDefaultValue(typeof(TextBox), true);
} }
public int CaretIndex 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 Focusable { get; }
bool IsFocused { get; } bool IsFocused { get; set; }
void Focus(); void Focus();
} }

4
Perspex/Input/InputManager.cs

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

2
Perspex/Perspex.csproj

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

Loading…
Cancel
Save