Browse Source

Added hacky tab keyboard navigation.

This is a temporary workaround to #30 - it's very hacky:

- Tab key handling is built into TextBox - all other controls don't
work. It needs to be moved into InputElement.
- It only traverses siblings.
- Setting focus to template children is a hack.
- Many more problems.
pull/39/head
Steven Kirk 12 years ago
parent
commit
3c591482a9
  1. 10
      Perspex.Controls/Presenters/TextPresenter.cs
  2. 23
      Perspex.Controls/TextBox.cs
  3. 49
      Perspex.Input/KeyboardNavigation.cs
  4. 1
      Perspex.Input/Perspex.Input.csproj
  5. 1
      Perspex.Themes.Default/TextBoxStyle.cs

10
Perspex.Controls/Presenters/TextPresenter.cs

@ -286,6 +286,16 @@ namespace Perspex.Controls.Presenters
{
goto default;
}
else if ((modifiers & ModifierKeys.Shift) == 0)
{
// TODO: Keyboard navigation should be moved to InputElement. This is just a
// hack to get TextBox tab navigation working.
KeyboardNavigation.MoveNext((IInputElement)this.TemplatedParent);
}
else
{
KeyboardNavigation.MovePrevious((IInputElement)this.TemplatedParent);
}
break;

23
Perspex.Controls/TextBox.cs

@ -8,7 +8,11 @@ namespace Perspex.Controls
{
using System;
using System.Reactive.Linq;
using Perspex.Controls.Presenters;
using Perspex.Controls.Primitives;
using Perspex.Controls.Templates;
using Perspex.Input;
using Perspex.Interactivity;
public class TextBox : TemplatedControl
{
@ -33,6 +37,13 @@ namespace Perspex.Controls
public static readonly PerspexProperty<TextWrapping> TextWrappingProperty =
TextBlock.TextWrappingProperty.AddOwner<TextBox>();
private TextPresenter presenter;
static TextBox()
{
FocusableProperty.OverrideDefaultValue(typeof(TextBox), true);
}
public TextBox()
{
var canScrollHorizontally = this.GetObservable(AcceptsReturnProperty)
@ -100,5 +111,17 @@ namespace Perspex.Controls
var length = (text != null) ? text.Length : 0;
return Math.Max(0, Math.Min(length, value));
}
protected override void OnTemplateApplied()
{
this.presenter = this.GetTemplateChild<TextPresenter>("textPresenter");
}
protected override void OnGotFocus(RoutedEventArgs e)
{
// TODO: There needs to be a better way of setting focus to a templated child.
base.OnGotFocus(e);
FocusManager.Instance.Focus(this.presenter);
}
}
}

49
Perspex.Input/KeyboardNavigation.cs

@ -0,0 +1,49 @@
// -----------------------------------------------------------------------
// <copyright file="FocusNavigationDirection.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Input
{
using System.Linq;
using Perspex.VisualTree;
/// <summary>
/// TODO: This class is a temporary stop-gap just to add tab handling. Should be replaced
/// with something better thought out and testable etc.
/// </summary>
public static class KeyboardNavigation
{
public static void MoveNext(IInputElement element)
{
var siblings = element.GetVisualSiblings().OfType<IInputElement>()
.Where(x => x.Focusable)
.SkipWhile(x => x != element)
.Skip(1);
var next = siblings.FirstOrDefault();
if (next != null)
{
FocusManager.Instance.Focus(next);
}
}
public static void MovePrevious(IInputElement element)
{
var siblings = element.GetVisualSiblings().OfType<IInputElement>()
.Where(x => x.Focusable)
.Reverse()
.SkipWhile(x => x != element)
.Skip(1);
var next = siblings.FirstOrDefault();
if (next != null)
{
FocusManager.Instance.Focus(next);
}
}
}
}

1
Perspex.Input/Perspex.Input.csproj

@ -72,6 +72,7 @@
<Compile Include="IPointerDevice.cs" />
<Compile Include="Key.cs" />
<Compile Include="KeyboardDevice.cs" />
<Compile Include="KeyboardNavigation.cs" />
<Compile Include="Platform\IPlatformSettings.cs" />
<Compile Include="PointerPressEventArgs.cs" />
<Compile Include="PointerWheelEventArgs.cs" />

1
Perspex.Themes.Default/TextBoxStyle.cs

@ -55,6 +55,7 @@ namespace Perspex.Themes.Default
[~ScrollViewer.VerticalScrollBarVisibilityProperty] = control[~ScrollViewer.VerticalScrollBarVisibilityProperty],
Content = new TextPresenter
{
Id = "textPresenter",
[~TextPresenter.AcceptsReturnProperty] = control[~TextBox.AcceptsReturnProperty],
[~TextPresenter.AcceptsTabProperty] = control[~TextBox.AcceptsTabProperty],
[~TextPresenter.CaretIndexProperty] = control[~TextBox.CaretIndexProperty],

Loading…
Cancel
Save