Browse Source

Added keyboard navigation for ListBox.

Need to override it for TreeView.
pull/10/head
Steven Kirk 12 years ago
parent
commit
a5a6f094cf
  1. 2
      Perspex.Controls/Generators/ItemContainerGenerator.cs
  2. 78
      Perspex.Controls/Primitives/SelectingItemsControl.cs
  3. 54
      Perspex.Input/FocusNavigationDirection.cs
  4. 4
      Perspex.Input/InputElement.cs
  5. 1
      Perspex.Input/Perspex.Input.csproj

2
Perspex.Controls/Generators/ItemContainerGenerator.cs

@ -33,7 +33,7 @@ namespace Perspex.Controls.Generators
return this.state;
}
set
private set
{
if (this.state != value)
{

78
Perspex.Controls/Primitives/SelectingItemsControl.cs

@ -7,6 +7,7 @@
namespace Perspex.Controls.Primitives
{
using System;
using System.Collections;
using System.Linq;
using Perspex.Controls.Presenters;
using Perspex.Input;
@ -19,6 +20,8 @@ namespace Perspex.Controls.Primitives
static SelectingItemsControl()
{
FocusableProperty.OverrideDefaultValue(typeof(SelectingItemsControl), true);
SelectedItemProperty.Changed.Subscribe(x =>
{
var control = x.Sender as SelectingItemsControl;
@ -36,6 +39,81 @@ namespace Perspex.Controls.Primitives
set { this.SetValue(SelectedItemProperty, value); }
}
protected static int GetIndexOfItem(IEnumerable items, object item)
{
if (items != null)
{
int index = 0;
foreach (var i in items)
{
if (object.ReferenceEquals(i, item))
{
return index;
}
++index;
}
}
return -1;
}
protected int GetIndexOfItem(object item)
{
return GetIndexOfItem(this.Items, item);
}
protected virtual void MoveSelection(FocusNavigationDirection direction)
{
if (this.SelectedItem != null)
{
int offset = 0;
switch (direction)
{
case FocusNavigationDirection.Up:
offset = -1;
break;
case FocusNavigationDirection.Down:
offset = 1;
break;
}
if (offset != 0)
{
var currentIndex = GetIndexOfItem(this.SelectedItem);
var index = currentIndex + offset;
if (index >= 0 && index < this.Items.Cast<object>().Count())
{
this.SelectedItem = this.Items.Cast<object>().ElementAt(index);
}
}
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Up:
this.MoveSelection(FocusNavigationDirection.Up);
break;
case Key.Down:
this.MoveSelection(FocusNavigationDirection.Down);
break;
case Key.Left:
this.MoveSelection(FocusNavigationDirection.Left);
break;
case Key.Right:
this.MoveSelection(FocusNavigationDirection.Right);
break;
}
e.Handled = true;
}
protected override void OnPointerPressed(PointerEventArgs e)
{
IVisual source = (IVisual)e.Source;

54
Perspex.Input/FocusNavigationDirection.cs

@ -0,0 +1,54 @@
// -----------------------------------------------------------------------
// <copyright file="FocusNavigationDirection.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Input
{
/// <summary>
/// Describes how focus should be moved.
/// </summary>
public enum FocusNavigationDirection
{
/// <summary>
/// Move the focus to the next control in the tab order.
/// </summary>
Next,
/// <summary>
/// Move the focus to the previous control in the tab order.
/// </summary>
Previous,
/// <summary>
/// Move the focus to the first control in the tab order.
/// </summary>
First,
/// <summary>
/// Move the focus to the last control in the tab order.
/// </summary>
Last,
/// <summary>
/// Move the focus to the left.
/// </summary>
Left,
/// <summary>
/// Move the focus to the right.
/// </summary>
Right,
/// <summary>
/// Move the focus up.
/// </summary>
Up,
/// <summary>
/// Move the focus down.
/// </summary>
Down,
}
}

4
Perspex.Input/InputElement.cs

@ -135,12 +135,12 @@ namespace Perspex.Input
public void Focus()
{
Locator.Current.GetService<IFocusManager>().Focus(this);
FocusManager.Instance.Focus(this);
}
protected virtual void OnGotFocus(RoutedEventArgs e)
{
this.IsFocused = true;
this.IsFocused = e.OriginalSource == this;
}
protected virtual void OnLostFocus(RoutedEventArgs e)

1
Perspex.Input/Perspex.Input.csproj

@ -54,6 +54,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="FocusManager.cs" />
<Compile Include="FocusNavigationDirection.cs" />
<Compile Include="ICloseable.cs" />
<Compile Include="IFocusManager.cs" />
<Compile Include="IFocusScope.cs" />

Loading…
Cancel
Save