Browse Source

Don't create a copy of the array unless necessary.

pull/5055/head
Steven Kirk 5 years ago
parent
commit
4285c3d0d1
  1. 23
      src/Avalonia.Input/KeyboardDevice.cs

23
src/Avalonia.Input/KeyboardDevice.cs

@ -218,15 +218,28 @@ namespace Avalonia.Input
var bindings = (currentHandler as IInputElement)?.KeyBindings; var bindings = (currentHandler as IInputElement)?.KeyBindings;
if (bindings != null) if (bindings != null)
{ {
// Create a copy of the KeyBindings list. KeyBinding[]? bindingsCopy = null;
// Create a copy of the KeyBindings list if there's a binding which matches the event.
// If we don't do this the foreach loop will throw an InvalidOperationException when the KeyBindings list is changed. // If we don't do this the foreach loop will throw an InvalidOperationException when the KeyBindings list is changed.
// This can happen when a new view is loaded which adds its own KeyBindings to the handler. // This can happen when a new view is loaded which adds its own KeyBindings to the handler.
var cpy = bindings.ToArray(); foreach (var binding in bindings)
foreach (var binding in cpy)
{ {
if (ev.Handled) if (binding.Gesture?.Matches(ev) == true)
{
bindingsCopy = bindings.ToArray();
break; break;
binding.TryHandle(ev); }
}
if (bindingsCopy is object)
{
foreach (var binding in bindingsCopy)
{
if (ev.Handled)
break;
binding.TryHandle(ev);
}
} }
} }
currentHandler = currentHandler.VisualParent; currentHandler = currentHandler.VisualParent;

Loading…
Cancel
Save