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

Move widget and FocusTracker creation out of main to simplify main's internals

This commit is contained in:
Chris Cummer
2019-04-23 21:06:26 -07:00
parent a18625f427
commit 40cdfe7146
4 changed files with 270 additions and 260 deletions

View File

@@ -3,8 +3,9 @@ package wtf
import (
"bytes"
"fmt"
"github.com/rivo/tview"
"strings"
"github.com/rivo/tview"
)
//BarGraph lets make graphs

View File

@@ -20,44 +20,20 @@ type FocusTracker struct {
Widgets []Wtfable
}
/* -------------------- Exported Functions -------------------- */
// 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.useNavShortcuts() {
return
func NewFocusTracker(app *tview.Application, widgets []Wtfable) FocusTracker {
focusTracker := FocusTracker{
App: app,
Idx: -1,
Widgets: widgets,
}
usedKeys := make(map[string]bool)
focusables := tracker.focusables()
i := 1
focusTracker.assignHotKeys()
for _, focusable := range focusables {
if focusable.FocusChar() != "" {
usedKeys[focusable.FocusChar()] = true
}
}
for _, focusable := range focusables {
if focusable.FocusChar() != "" {
continue
}
if _, foundKey := usedKeys[string('0'+i)]; foundKey {
for ; foundKey; _, foundKey = usedKeys[string('0'+i)] {
i++
}
}
// Don't have nav characters > "9"
if i >= 10 {
break
}
focusable.SetFocusChar(string('0' + i))
i++
}
return focusTracker
}
/* -------------------- Exported Functions -------------------- */
func (tracker *FocusTracker) FocusOn(char string) bool {
if !tracker.useNavShortcuts() {
return false
@@ -122,6 +98,42 @@ func (tracker *FocusTracker) Refocus() {
/* -------------------- Unexported Functions -------------------- */
// 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.useNavShortcuts() {
return
}
usedKeys := make(map[string]bool)
focusables := tracker.focusables()
i := 1
for _, focusable := range focusables {
if focusable.FocusChar() != "" {
usedKeys[focusable.FocusChar()] = true
}
}
for _, focusable := range focusables {
if focusable.FocusChar() != "" {
continue
}
if _, foundKey := usedKeys[string('0'+i)]; foundKey {
for ; foundKey; _, foundKey = usedKeys[string('0'+i)] {
i++
}
}
// Don't have nav characters > "9"
if i >= 10 {
break
}
focusable.SetFocusChar(string('0' + i))
i++
}
}
func (tracker *FocusTracker) blur(idx int) {
widget := tracker.focusableAt(idx)
if widget == nil {