Browse Source

Improve focus a little bit.

pull/10/head
Steven Kirk 11 years ago
parent
commit
2468e061c5
  1. 4
      Perspex.Controls/Window.cs
  2. 100
      Perspex.Input/FocusManager.cs
  3. 12
      Perspex.Input/IFocusManager.cs
  4. 12
      Perspex.Input/IFocusScope.cs
  5. 1
      Perspex.Input/Perspex.Input.csproj

4
Perspex.Controls/Window.cs

@ -18,7 +18,7 @@ namespace Perspex.Controls
using Perspex.Threading;
using Splat;
public class Window : ContentControl, ILayoutRoot, IRenderRoot, ICloseable
public class Window : ContentControl, ILayoutRoot, IRenderRoot, ICloseable, IFocusScope
{
public static readonly PerspexProperty<Size> ClientSizeProperty =
PerspexProperty.Register<Window, Size>("ClientSize");
@ -121,6 +121,8 @@ namespace Perspex.Controls
{
this.Activated(this, EventArgs.Empty);
}
FocusManager.Instance.SetFocusScope(this);
}
private void HandleClosed()

100
Perspex.Input/FocusManager.cs

@ -6,50 +6,108 @@
namespace Perspex.Input
{
using System;
using System.Collections.Generic;
using System.Linq;
using Perspex.Interactivity;
using Splat;
public class FocusManager : IFocusManager
{
private Dictionary<IFocusScope, IInputElement> focusScopes =
new Dictionary<IFocusScope, IInputElement>();
public static IFocusManager Instance
{
get { return Locator.Current.GetService<IFocusManager>(); }
}
public IInputElement Current
{
get;
private set;
}
public IFocusScope Scope
{
get;
private set;
}
/// <summary>
/// Focuses a control.
/// </summary>
/// <param name="control">The control to focus.</param>
public void Focus(IInputElement control)
{
IInteractive current = this.Current as IInteractive;
IInteractive next = control as IInteractive;
Contract.Requires<ArgumentNullException>(control != null);
if (current != null)
var current = this.Current as IInteractive;
var next = control as IInteractive;
var scope = control.GetSelfAndVisualAncestors()
.OfType<IFocusScope>()
.FirstOrDefault();
if (scope != null)
{
current.RaiseEvent(new RoutedEventArgs
this.focusScopes[scope] = control;
if (current != null)
{
RoutedEvent = InputElement.LostFocusEvent,
Source = current,
OriginalSource = current,
});
}
current.RaiseEvent(new RoutedEventArgs
{
RoutedEvent = InputElement.LostFocusEvent,
Source = current,
OriginalSource = current,
});
}
this.Current = control;
this.Current = control;
IKeyboardDevice keyboard = Locator.Current.GetService<IKeyboardDevice>();
IKeyboardDevice keyboard = Locator.Current.GetService<IKeyboardDevice>();
if (keyboard != null)
{
keyboard.FocusedElement = control;
if (keyboard != null)
{
keyboard.FocusedElement = control;
}
if (next != null)
{
next.RaiseEvent(new RoutedEventArgs
{
RoutedEvent = InputElement.GotFocusEvent,
Source = next,
OriginalSource = next,
});
}
}
}
if (next != null)
/// <summary>
/// Notifies the focus manager of a change in focus scope.
/// </summary>
/// <param name="scope">The new focus scope.</param>
/// <remarks>
/// This should not be called by client code. It is called by an <see cref="IFocusScope"/>
/// when it activates, e.g. when a Window is activated.
/// </remarks>
public void SetFocusScope(IFocusScope scope)
{
Contract.Requires<ArgumentNullException>(scope != null);
IInputElement e;
if (!this.focusScopes.TryGetValue(scope, out e))
{
next.RaiseEvent(new RoutedEventArgs
{
RoutedEvent = InputElement.GotFocusEvent,
Source = next,
OriginalSource = next,
});
// TODO: Make this do something useful, i.e. select the first focusable
// control, select a control that the user has specified to have default
// focus etc.
e = scope as IInputElement;
this.focusScopes.Add(scope, e);
}
this.Scope = scope;
this.Focus(e);
}
}
}

12
Perspex.Input/IFocusManager.cs

@ -16,6 +16,18 @@ namespace Perspex.Input
{
IInputElement Current { get; }
IFocusScope Scope { get; }
void Focus(IInputElement focusable);
/// <summary>
/// Notifies the focus manager of a change in focus scope.
/// </summary>
/// <param name="scope">The new focus scope.</param>
/// <remarks>
/// This should not be called by client code. It is called by an <see cref="IFocusScope"/>
/// when it activates, e.g. when a Window is activated.
/// </remarks>
void SetFocusScope(IFocusScope scope);
}
}

12
Perspex.Input/IFocusScope.cs

@ -0,0 +1,12 @@
// -----------------------------------------------------------------------
// <copyright file="IFocusScope.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Input
{
public interface IFocusScope
{
}
}

1
Perspex.Input/Perspex.Input.csproj

@ -56,6 +56,7 @@
<Compile Include="FocusManager.cs" />
<Compile Include="ICloseable.cs" />
<Compile Include="IFocusManager.cs" />
<Compile Include="IFocusScope.cs" />
<Compile Include="IInputDevice.cs" />
<Compile Include="IInputElement.cs" />
<Compile Include="IInputManager.cs" />

Loading…
Cancel
Save