diff --git a/main.go b/main.go index 1614ec190..2145e1fa4 100644 --- a/main.go +++ b/main.go @@ -88,11 +88,11 @@ func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { focusTracker.Prev() case tcell.KeyEsc: focusTracker.None() - //default: - //return event } - focusTracker.FocusOn(string(event.Rune())) + if focusTracker.FocusOn(string(event.Rune())) { + return nil + } return event } diff --git a/wtf/focus_tracker.go b/wtf/focus_tracker.go index 44703b180..11a18f52c 100644 --- a/wtf/focus_tracker.go +++ b/wtf/focus_tracker.go @@ -8,7 +8,7 @@ type FocusState int const ( Widget FocusState = iota - NonWidget + AppBoard NeverFocused ) @@ -25,6 +25,10 @@ type FocusTracker struct { // AssignHotKeys assigns an alphabetic keyboard character to each focusable // widget so that the widget can be brought into focus by pressing that keyboard key func (tracker *FocusTracker) AssignHotKeys() { + if !tracker.withShortcuts() { + return + } + i := 0 for _, focusable := range tracker.focusables() { @@ -33,21 +37,31 @@ func (tracker *FocusTracker) AssignHotKeys() { } } -func (tracker *FocusTracker) FocusOn(char string) { +func (tracker *FocusTracker) FocusOn(char string) bool { + if !tracker.withShortcuts() { + return false + } + + hasFocusable := false + for idx, focusable := range tracker.focusables() { if focusable.FocusChar() == char { tracker.blur(tracker.Idx) tracker.Idx = idx tracker.focus(tracker.Idx) + + hasFocusable = true break } } + + return hasFocusable } // Next sets the focus on the next widget in the widget list. If the current widget is // the last widget, sets focus on the first widget. func (tracker *FocusTracker) Next() { - if tracker.focusState() == NonWidget { + if tracker.focusState() == AppBoard { return } @@ -58,7 +72,7 @@ func (tracker *FocusTracker) Next() { // None removes focus from the currently-focused widget. func (tracker *FocusTracker) None() { - if tracker.focusState() == NonWidget { + if tracker.focusState() == AppBoard { return } @@ -68,7 +82,7 @@ func (tracker *FocusTracker) None() { // Prev sets the focus on the previous widget in the widget list. If the current widget is // the last widget, sets focus on the last widget. func (tracker *FocusTracker) Prev() { - if tracker.focusState() == NonWidget { + if tracker.focusState() == AppBoard { return } @@ -135,17 +149,6 @@ func (tracker *FocusTracker) focusableAt(idx int) Wtfable { return tracker.focusables()[idx] } -func (tracker *FocusTracker) increment() { - tracker.Idx = tracker.Idx + 1 - - if tracker.Idx == len(tracker.focusables()) { - tracker.Idx = 0 - } -} - -// widgetHasFocus returns true if one of the widgets currently has the app's focus, -// false if none of them do (ie: perhaps a modal dialog currently has it instead) -// If there's no index, it returns true because focus has never been assigned func (tracker *FocusTracker) focusState() FocusState { if tracker.Idx < 0 { return NeverFocused @@ -157,5 +160,17 @@ func (tracker *FocusTracker) focusState() FocusState { } } - return NonWidget + return AppBoard +} + +func (tracker *FocusTracker) increment() { + tracker.Idx = tracker.Idx + 1 + + if tracker.Idx == len(tracker.focusables()) { + tracker.Idx = 0 + } +} + +func (tracker *FocusTracker) withShortcuts() bool { + return Config.UBool("wtf.navigation.shortcuts", true) }