1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Nav shortcuts are now configurable

This commit is contained in:
Chris Cummer 2018-07-31 08:44:52 -07:00
parent 985c41d3b6
commit ac08d7a6d4
2 changed files with 35 additions and 20 deletions

View File

@ -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
}

View File

@ -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)
}