A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

113 lines
3.6 KiB

// -----------------------------------------------------------------------
// <copyright file="FocusManager.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 Perspex.Interactivity;
using Perspex.VisualTree;
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 { return KeyboardDevice.Instance.FocusedElement; }
}
public IFocusScope Scope
{
get;
private set;
}
/// <summary>
/// Focuses a control.
/// </summary>
/// <param name="control">The control to focus.</param>
/// <param name="keyboardNavigated">
/// Whether the control was focused by a keypress (e.g. the Tab key).
/// </param>
public void Focus(IInputElement control, bool keyboardNavigated = false)
{
if (control != null)
{
var scope = control.GetSelfAndVisualAncestors()
.OfType<IFocusScope>()
.FirstOrDefault();
if (scope != null)
{
this.SetFocusedElement(scope, control, keyboardNavigated);
}
}
else
{
this.SetFocusedElement(this.Scope, null);
}
}
/// <summary>
/// Sets the currently focused element in the specified scope.
/// </summary>
/// <param name="scope">The focus scope.</param>
/// <param name="element">The element to focus. May be null.</param>
/// <param name="keyboardNavigated">
/// Whether the control was focused by a keypress (e.g. the Tab key).
/// </param>
/// <remarks>
/// If the specified scope is the current <see cref="Scope"/> then the keyboard focus
/// will change.
/// </remarks>
public void SetFocusedElement(
IFocusScope scope,
IInputElement element,
bool keyboardNavigated = false)
{
Contract.Requires<ArgumentNullException>(scope != null);
this.focusScopes[scope] = element;
if (this.Scope == scope)
{
KeyboardDevice.Instance.SetFocusedElement(element, keyboardNavigated);
}
}
/// <summary>
/// Notifies the focus manager of a change in focus scope.
/// </summary>
/// <param name="scope">The new focus scope.</param>
public void SetFocusScope(IFocusScope scope)
{
Contract.Requires<ArgumentNullException>(scope != null);
IInputElement e;
if (!this.focusScopes.TryGetValue(scope, out e))
{
// 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);
}
}
}