Skip to content

Commit

Permalink
Merge branch 'master' into travisci-pro-toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
ruggi committed Jul 31, 2018
2 parents 8a69e75 + ac08d7a commit e480687
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
49 changes: 32 additions & 17 deletions wtf/focus_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type FocusState int

const (
Widget FocusState = iota
NonWidget
AppBoard
NeverFocused
)

Expand All @@ -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() {
Expand All @@ -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
}

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

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

Expand Down Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit e480687

Please sign in to comment.