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:
42
modules/spotifyweb/keyboard.go
Normal file
42
modules/spotifyweb/keyboard.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package spotifyweb
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (widget *Widget) initializeKeyboardControls() {
|
||||
widget.SetKeyboardChar("/", widget.ShowHelp)
|
||||
widget.SetKeyboardChar("h", widget.selectPrevious)
|
||||
widget.SetKeyboardChar("l", widget.selectNext)
|
||||
widget.SetKeyboardChar(" ", widget.playPause)
|
||||
widget.SetKeyboardChar("s", widget.toggleShuffle)
|
||||
}
|
||||
|
||||
func (widget *Widget) selectPrevious() {
|
||||
widget.client.Previous()
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
widget.Refresh()
|
||||
}
|
||||
|
||||
func (widget *Widget) selectNext() {
|
||||
widget.client.Next()
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
widget.Refresh()
|
||||
}
|
||||
|
||||
func (widget *Widget) playPause() {
|
||||
if widget.playerState.CurrentlyPlaying.Playing {
|
||||
widget.client.Pause()
|
||||
} else {
|
||||
widget.client.Play()
|
||||
}
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
widget.Refresh()
|
||||
}
|
||||
|
||||
func (widget *Widget) toggleShuffle() {
|
||||
widget.playerState.ShuffleState = !widget.playerState.ShuffleState
|
||||
widget.client.Shuffle(widget.playerState.ShuffleState)
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
widget.Refresh()
|
||||
}
|
||||
@@ -4,9 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/logger"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
@@ -45,6 +43,7 @@ type Info struct {
|
||||
// Widget is the struct used by all WTF widgets to transfer to the main widget controller
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.KeyboardWidget
|
||||
wtf.TextWidget
|
||||
|
||||
Info
|
||||
@@ -94,8 +93,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
var playerState *spotify.PlayerState
|
||||
|
||||
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),
|
||||
|
||||
Info: Info{},
|
||||
|
||||
@@ -141,11 +141,15 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
|
||||
widget.settings.common.RefreshInterval = 5
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
widget.View.SetInputCapture(widget.captureInput)
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetWordWrap(true)
|
||||
widget.View.SetTitle("[green]Spotify Web[white]")
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
@@ -192,40 +196,6 @@ func (w *Widget) render() {
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Widget) captureInput(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch (string)(event.Rune()) {
|
||||
case "/":
|
||||
w.ShowHelp()
|
||||
return nil
|
||||
case "h":
|
||||
w.client.Previous()
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
w.Refresh()
|
||||
return nil
|
||||
case "l":
|
||||
w.client.Next()
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
w.Refresh()
|
||||
return nil
|
||||
case " ":
|
||||
if w.playerState.CurrentlyPlaying.Playing {
|
||||
w.client.Pause()
|
||||
} else {
|
||||
w.client.Play()
|
||||
}
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
w.Refresh()
|
||||
return nil
|
||||
case "s":
|
||||
w.playerState.ShuffleState = !w.playerState.ShuffleState
|
||||
w.client.Shuffle(w.playerState.ShuffleState)
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
w.Refresh()
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Widget) createOutput() string {
|
||||
output := wtf.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.Width())
|
||||
output += wtf.CenterText(fmt.Sprintf("[green]Title:[white] %v\n", w.Info.Title), w.Width())
|
||||
|
||||
Reference in New Issue
Block a user