From 69f982e26f2181d88a77f1214947722dcce1d26f Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 5 Apr 2022 13:12:03 +0200 Subject: [PATCH] io/router: don't panic on focus moves when there is nothing to focus Signed-off-by: Elias Naur --- io/router/key.go | 3 +++ io/router/key_test.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/io/router/key.go b/io/router/key.go index 1bbdb1a4..18f6bf27 100644 --- a/io/router/key.go +++ b/io/router/key.go @@ -171,6 +171,9 @@ func (q *keyQueue) updateFocusLayout() { // MoveFocus attempts to move the focus in the direction of dir, returning true if it succeeds. func (q *keyQueue) MoveFocus(dir FocusDirection, events *handlerEvents) bool { + if len(q.dirOrder) == 0 { + return false + } order := 0 if q.focus != nil { order = q.handlers[q.focus].dirOrder diff --git a/io/router/key_test.go b/io/router/key_test.go index b0cd799d..49763599 100644 --- a/io/router/key_test.go +++ b/io/router/key_test.go @@ -311,6 +311,11 @@ func TestFocusClick(t *testing.T) { assertEventPointerTypeSequence(t, r.Events(h), pointer.Cancel, pointer.Press, pointer.Release) } +func TestNoFocus(t *testing.T) { + r := new(Router) + r.MoveFocus(FocusForward) +} + func assertKeyEvent(t *testing.T, events []event.Event, expected bool, expectedInputs ...event.Event) { t.Helper() var evtFocus int