-
-
Notifications
You must be signed in to change notification settings - Fork 464
/
KeyboardCommandHandler.cs
65 lines (54 loc) · 2.27 KB
/
KeyboardCommandHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// ---------------------------------------------------------------------------------------------
// Copyright (c) 2019-2024, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ---------------------------------------------------------------------------------------------
namespace Notepads.Commands
{
using System.Collections.Generic;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
public sealed class KeyboardCommandHandler : ICommandHandler<KeyRoutedEventArgs>
{
public readonly ICollection<IKeyboardCommand<KeyRoutedEventArgs>> Commands;
private IKeyboardCommand<KeyRoutedEventArgs> _lastCommand;
public KeyboardCommandHandler(ICollection<IKeyboardCommand<KeyRoutedEventArgs>> commands)
{
Commands = commands;
}
public CommandHandlerResult Handle(KeyRoutedEventArgs args)
{
var ctrlDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
var altDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down);
var shiftDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
var shouldHandle = false;
var shouldSwallow = false;
foreach (var command in Commands)
{
if (command.Hit(ctrlDown, altDown, shiftDown, args.Key))
{
if (command.ShouldExecute(_lastCommand))
{
command.Execute(args);
}
if (command.ShouldSwallowAfterExecution())
{
shouldSwallow = true;
}
if (command.ShouldHandleAfterExecution())
{
shouldHandle = true;
}
_lastCommand = command;
break;
}
}
if (!shouldHandle)
{
_lastCommand = null;
}
return new CommandHandlerResult(shouldHandle, shouldSwallow);
}
}
}