1
0
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:
Chris Cummer
2019-05-06 08:56:01 -07:00
parent a9c5dc3be8
commit 2d0706c40b
45 changed files with 589 additions and 745 deletions

View File

@@ -0,0 +1,12 @@
package weather
import "github.com/gdamore/tcell"
func (widget *Widget) initializeKeyboardControls() {
widget.SetKeyboardChar("/", widget.ShowHelp)
widget.SetKeyboardChar("h", widget.Prev)
widget.SetKeyboardChar("l", widget.Next)
widget.SetKeyboardKey(tcell.KeyLeft, widget.Prev)
widget.SetKeyboardKey(tcell.KeyRight, widget.Next)
}

View File

@@ -2,7 +2,6 @@ package weather
import (
owm "github.com/briandowns/openweathermap"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)
@@ -21,6 +20,7 @@ const HelpText = `
// Widget is the container for weather data.
type Widget struct {
wtf.HelpfulWidget
wtf.KeyboardWidget
wtf.TextWidget
// APIKey string
@@ -31,11 +31,12 @@ type Widget struct {
settings *Settings
}
// NewWidget creates and returns a new instance of the weather Widget.
// NewWidget creates and returns a new instance of the weather 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),
Idx: 0,
@@ -43,8 +44,10 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
settings: settings,
}
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.HelpfulWidget.SetView(widget.View)
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget
}
@@ -144,28 +147,3 @@ func (widget *Widget) currentWeather(cityCode int) (*owm.CurrentWeatherData, err
return weather, nil
}
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch string(event.Rune()) {
case "/":
widget.ShowHelp()
return nil
case "h":
widget.Prev()
return nil
case "l":
widget.Next()
return nil
}
switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
return nil
case tcell.KeyRight:
widget.Next()
return nil
default:
return event
}
}