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

Merge branch 'master' into windows

This commit is contained in:
Chris Cummer 2018-07-31 11:46:02 -04:00 committed by GitHub
commit d8c3189a3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 20 deletions

View File

@ -88,11 +88,11 @@ func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
focusTracker.Prev() focusTracker.Prev()
case tcell.KeyEsc: case tcell.KeyEsc:
focusTracker.None() focusTracker.None()
//default:
//return event
} }
focusTracker.FocusOn(string(event.Rune())) if focusTracker.FocusOn(string(event.Rune())) {
return nil
}
return event return event
} }

View File

@ -8,7 +8,7 @@ type FocusState int
const ( const (
Widget FocusState = iota Widget FocusState = iota
NonWidget AppBoard
NeverFocused NeverFocused
) )
@ -25,6 +25,10 @@ type FocusTracker struct {
// AssignHotKeys assigns an alphabetic keyboard character to each focusable // AssignHotKeys assigns an alphabetic keyboard character to each focusable
// widget so that the widget can be brought into focus by pressing that keyboard key // widget so that the widget can be brought into focus by pressing that keyboard key
func (tracker *FocusTracker) AssignHotKeys() { func (tracker *FocusTracker) AssignHotKeys() {
if !tracker.withShortcuts() {
return
}
i := 0 i := 0
for _, focusable := range tracker.focusables() { 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() { for idx, focusable := range tracker.focusables() {
if focusable.FocusChar() == char { if focusable.FocusChar() == char {
tracker.blur(tracker.Idx) tracker.blur(tracker.Idx)
tracker.Idx = idx tracker.Idx = idx
tracker.focus(tracker.Idx) tracker.focus(tracker.Idx)
hasFocusable = true
break break
} }
} }
return hasFocusable
} }
// Next sets the focus on the next widget in the widget list. If the current widget is // 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. // the last widget, sets focus on the first widget.
func (tracker *FocusTracker) Next() { func (tracker *FocusTracker) Next() {
if tracker.focusState() == NonWidget { if tracker.focusState() == AppBoard {
return return
} }
@ -58,7 +72,7 @@ func (tracker *FocusTracker) Next() {
// None removes focus from the currently-focused widget. // None removes focus from the currently-focused widget.
func (tracker *FocusTracker) None() { func (tracker *FocusTracker) None() {
if tracker.focusState() == NonWidget { if tracker.focusState() == AppBoard {
return 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 // 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. // the last widget, sets focus on the last widget.
func (tracker *FocusTracker) Prev() { func (tracker *FocusTracker) Prev() {
if tracker.focusState() == NonWidget { if tracker.focusState() == AppBoard {
return return
} }
@ -135,17 +149,6 @@ func (tracker *FocusTracker) focusableAt(idx int) Wtfable {
return tracker.focusables()[idx] 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 { func (tracker *FocusTracker) focusState() FocusState {
if tracker.Idx < 0 { if tracker.Idx < 0 {
return NeverFocused 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)
} }