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

Better focus check code using FocustState enum

This commit is contained in:
Chris Cummer 2018-05-07 16:44:16 -07:00
parent 6be6afaf2c
commit 6158a055da

View File

@ -4,6 +4,14 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
) )
type FocusState int
const (
Widget FocusState = iota
NonWidget
NeverFocused
)
// FocusTracker is used by the app to track which onscreen widget currently has focus, // FocusTracker is used by the app to track which onscreen widget currently has focus,
// and to move focus between widgets. // and to move focus between widgets.
type FocusTracker struct { type FocusTracker struct {
@ -17,11 +25,13 @@ type FocusTracker struct {
// 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.widgetHasFocus() { if tracker.focusState() == NonWidget {
tracker.blur(tracker.Idx) return
tracker.increment()
tracker.focus(tracker.Idx)
} }
tracker.blur(tracker.Idx)
tracker.increment()
tracker.focus(tracker.Idx)
} }
// None removes focus from the currently-focused widget. // None removes focus from the currently-focused widget.
@ -32,11 +42,13 @@ 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.widgetHasFocus() { if tracker.focusState() == NonWidget {
tracker.blur(tracker.Idx) return
tracker.decrement()
tracker.focus(tracker.Idx)
} }
tracker.blur(tracker.Idx)
tracker.decrement()
tracker.focus(tracker.Idx)
} }
func (tracker *FocusTracker) Refocus() { func (tracker *FocusTracker) Refocus() {
@ -107,16 +119,17 @@ func (tracker *FocusTracker) increment() {
// widgetHasFocus returns true if one of the widgets currently has the app's focus, // 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) // false if none of them do (ie: perhaps a modal dialog currently has it instead)
func (tracker *FocusTracker) widgetHasFocus() bool { // If there's no index, it returns true because focus has never been assigned
func (tracker *FocusTracker) focusState() FocusState {
if tracker.Idx < 0 { if tracker.Idx < 0 {
return true return NeverFocused
} }
for _, widget := range tracker.Widgets { for _, widget := range tracker.Widgets {
if widget.TextView() == tracker.App.GetFocus() { if widget.TextView() == tracker.App.GetFocus() {
return true return Widget
} }
} }
return false return NonWidget
} }