From 5f5c752f5da348db854b1672709ef0e8723c7be3 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sat, 6 Feb 2021 07:10:30 -0800 Subject: [PATCH] Better key chord matching --- IDE/src/IDEApp.bf | 154 ++++++++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 67 deletions(-) diff --git a/IDE/src/IDEApp.bf b/IDE/src/IDEApp.bf index fadb8c6a..0cdaae19 100644 --- a/IDE/src/IDEApp.bf +++ b/IDE/src/IDEApp.bf @@ -7428,83 +7428,103 @@ namespace IDE return; } } - - var keyState = scope KeyState(); - keyState.mKeyCode = evt.mKeyCode; - keyState.mKeyFlags = evt.mKeyFlags; - - var curKeyMap = mCommands.mKeyMap; - bool hadChordState = mKeyChordState != null; - if (mKeyChordState != null) - curKeyMap = mKeyChordState.mCommandMap; - DeleteAndNullify!(mKeyChordState); - - KeyState matchedKey; - IDECommandBase commandBase; - if (curKeyMap.mMap.TryGet(keyState, out matchedKey, out commandBase)) + + if ((mKeyChordState != null) && (evt.mKeyCode.IsModifier)) { - if (var commandMap = commandBase as CommandMap) + // Ignore + } + else + { + var keyState = scope KeyState(); + keyState.mKeyCode = evt.mKeyCode; + keyState.mKeyFlags = evt.mKeyFlags; + + var curKeyMap = mCommands.mKeyMap; + + bool hadChordState = mKeyChordState != null; + if (mKeyChordState != null) + curKeyMap = mKeyChordState.mCommandMap; + var prevKeyChordState = mKeyChordState; + defer delete prevKeyChordState; + mKeyChordState = null; + + KeyState matchedKey; + IDECommandBase commandBase; + + bool hadMatch = curKeyMap.mMap.TryGet(keyState, out matchedKey, out commandBase); + if ((!hadMatch) && (prevKeyChordState != null)) { - mKeyChordState = new .(); - mKeyChordState.mCommandMap = commandMap; - mKeyChordState.mKeyState = matchedKey; - evt.mHandled = true; - return; + // If we have a "Ctrl+A, Ctrl+B" style sequence then also try to match that against "Ctrl+A, B" + KeyState rawKeyState = keyState; + rawKeyState.mKeyFlags &= ~prevKeyChordState.mKeyState.mKeyFlags; + hadMatch = curKeyMap.mMap.TryGet(rawKeyState, out matchedKey, out commandBase); } - else if (var command = commandBase as IDECommand) + + if (hadMatch) { - bool foundMatch = false; - if (useFlags != .None) - { - var checkCommand = command; - while (checkCommand != null) - { - bool matches = checkCommand.mContextFlags == .None; - if (checkCommand.mContextFlags.HasFlag(.Editor)) - matches |= useFlags.HasFlag(.Editor); - if (checkCommand.mContextFlags.HasFlag(.MainWindow)) - matches |= useFlags.HasFlag(.MainWindow); - if (checkCommand.mContextFlags.HasFlag(.WorkWindow)) - matches |= useFlags.HasFlag(.WorkWindow); - - if (matches) - { - checkCommand.mAction(); - foundMatch = true; - } - checkCommand = checkCommand.mNext; - } - } - - if (!foundMatch) - { - var checkCommand = command; - while (checkCommand != null) - { - if (checkCommand.mContextFlags == .None) - { - checkCommand.mAction(); - foundMatch = true; - } - checkCommand = checkCommand.mNext; - } - } - - if (foundMatch) + if (var commandMap = commandBase as CommandMap) { + mKeyChordState = new .(); + mKeyChordState.mCommandMap = commandMap; + mKeyChordState.mKeyState = matchedKey; evt.mHandled = true; return; } + else if (var command = commandBase as IDECommand) + { + bool foundMatch = false; + if (useFlags != .None) + { + var checkCommand = command; + while (checkCommand != null) + { + bool matches = checkCommand.mContextFlags == .None; + if (checkCommand.mContextFlags.HasFlag(.Editor)) + matches |= useFlags.HasFlag(.Editor); + if (checkCommand.mContextFlags.HasFlag(.MainWindow)) + matches |= useFlags.HasFlag(.MainWindow); + if (checkCommand.mContextFlags.HasFlag(.WorkWindow)) + matches |= useFlags.HasFlag(.WorkWindow); + + if (matches) + { + checkCommand.mAction(); + foundMatch = true; + } + checkCommand = checkCommand.mNext; + } + } + + if (!foundMatch) + { + var checkCommand = command; + while (checkCommand != null) + { + if (checkCommand.mContextFlags == .None) + { + checkCommand.mAction(); + foundMatch = true; + } + checkCommand = checkCommand.mNext; + } + } + + if (foundMatch) + { + evt.mHandled = true; + return; + } + } } - } - else if (!evt.mKeyCode.IsModifier) - { - // Not found - if (hadChordState) + else { - Beep(.Error); - evt.mHandled = true; - return; + // Not found + if (hadChordState) + { + Beep(.Error); + evt.mHandled = true; + return; + } } }