// -----------------------------------------------------------------------
//
// Copyright 2015 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
using Perspex.Interactivity;
using System;
namespace Perspex.Input
{
///
/// Handles access keys for a window.
///
public class AccessKeyHandler : IAccessKeyHandler
{
///
/// The window to which the handler belongs.
///
private IInputRoot owner;
///
/// Whether access keys are currently being shown;
///
private bool showingAccessKeys;
///
/// Gets or sets the window's main menu.
///
public IMainMenu MainMenu { get; set; }
///
/// Sets the owner of the access key handler.
///
/// The owner.
///
/// This method can only be called once, typically by the owner itself on creation.
///
public void SetOwner(IInputRoot owner)
{
if (this.owner != null)
{
throw new InvalidOperationException("AccessKeyHandler owner has already been set.");
}
this.owner = owner;
this.owner.AddHandler(InputElement.KeyDownEvent, this.OnKeyDown);
this.owner.AddHandler(InputElement.PointerPressedEvent, this.OnPreviewPointerPressed, RoutingStrategies.Tunnel);
}
///
/// Handles Alt and F10 key presses in the window.
///
/// The event sender.
/// The event args.
protected virtual void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftAlt || e.Key == Key.F10)
{
this.owner.ShowAccessKeys = this.showingAccessKeys = true;
e.Handled = true;
}
}
///
/// Handles pointer presses in the window.
///
/// The event sender.
/// The event args.
protected virtual void OnPreviewPointerPressed(object sender, PointerEventArgs e)
{
if (this.showingAccessKeys)
{
this.owner.ShowAccessKeys = false;
}
}
}
}