mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-427 Use new keyboard implementation for widgets. Closes #427
This commit is contained in:
28
modules/nbascore/keyboard.go
Normal file
28
modules/nbascore/keyboard.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package nbascore
|
||||
|
||||
import "github.com/gdamore/tcell"
|
||||
|
||||
func (widget *Widget) initializeKeyboardControls() {
|
||||
widget.SetKeyboardChar("/", widget.ShowHelp)
|
||||
widget.SetKeyboardChar("h", widget.prev)
|
||||
widget.SetKeyboardChar("l", widget.next)
|
||||
widget.SetKeyboardChar("c", widget.center)
|
||||
|
||||
widget.SetKeyboardKey(tcell.KeyLeft, widget.prev)
|
||||
widget.SetKeyboardKey(tcell.KeyRight, widget.next)
|
||||
}
|
||||
|
||||
func (widget *Widget) center() {
|
||||
offset = 0
|
||||
widget.Refresh()
|
||||
}
|
||||
|
||||
func (widget *Widget) next() {
|
||||
offset++
|
||||
widget.Refresh()
|
||||
}
|
||||
|
||||
func (widget *Widget) prev() {
|
||||
offset--
|
||||
widget.Refresh()
|
||||
}
|
||||
@@ -3,13 +3,13 @@ package nbascore
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
const HelpText = `
|
||||
@@ -19,8 +19,10 @@ const HelpText = `
|
||||
c: Go back to current day
|
||||
`
|
||||
|
||||
// A Widget represents an NBA Score widget
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.KeyboardWidget
|
||||
wtf.TextWidget
|
||||
|
||||
app *tview.Application
|
||||
@@ -31,19 +33,24 @@ type Widget struct {
|
||||
|
||||
var offset = 0
|
||||
|
||||
// NewWidget creates a new instance of a widget
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
KeyboardWidget: wtf.NewKeyboardWidget(),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
app: app,
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
|
||||
widget.View.SetScrollable(true)
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
@@ -142,36 +149,3 @@ func (widget *Widget) nbascore() {
|
||||
widget.View.SetText(allGame)
|
||||
|
||||
}
|
||||
|
||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch (string)(event.Rune()) {
|
||||
case "h":
|
||||
offset--
|
||||
widget.Refresh()
|
||||
return nil
|
||||
case "l":
|
||||
offset++
|
||||
widget.Refresh()
|
||||
return nil
|
||||
case "c":
|
||||
offset = 0
|
||||
widget.Refresh()
|
||||
return nil
|
||||
case "/":
|
||||
widget.ShowHelp()
|
||||
return nil
|
||||
}
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyLeft:
|
||||
offset--
|
||||
widget.Refresh()
|
||||
return nil
|
||||
case tcell.KeyRight:
|
||||
offset++
|
||||
widget.Refresh()
|
||||
return nil
|
||||
default:
|
||||
return event
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user