Skip to content

Commit

Permalink
implement help
Browse files Browse the repository at this point in the history
  • Loading branch information
spezifisch committed Nov 5, 2023
1 parent 73238d2 commit 8d80387
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 3 deletions.
26 changes: 25 additions & 1 deletion gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Ui struct {
// modals
addToPlaylistList *tview.List
messageBox *tview.Modal
helpModal tview.Primitive
helpWidget *HelpWidget

starIdList map[string]struct{}

Expand All @@ -63,6 +65,7 @@ const (
PageNewPlaylist = "newPlaylist"
PageAddToPlaylist = "addToPlaylist"
PageMessageBox = "messageBox"
PageHelpBox = "helpBox"
)

func InitGui(indexes *[]subsonic.SubsonicIndex,
Expand Down Expand Up @@ -100,7 +103,8 @@ func InitGui(indexes *[]subsonic.SubsonicIndex,
SetDynamicColors(true).
SetScrollable(false)

ui.menuWidget = NewMenuWidget(ui)
ui.menuWidget = ui.createMenuWidget()
ui.helpWidget = ui.createHelpWidget()

// same as 'playlistList' except for the addToPlaylistModal
// - we need a specific version of this because we need different keybinds
Expand All @@ -115,6 +119,13 @@ func InitGui(indexes *[]subsonic.SubsonicIndex,
return event
})

// help box modal
ui.helpModal = makeModal(ui.helpWidget.Root, 80, 30)
ui.helpWidget.Root.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
ui.CloseHelp()
return event
})

// top bar: status text
topBarFlex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(ui.startStopStatus, 0, 1, false).
Expand All @@ -139,6 +150,7 @@ func InitGui(indexes *[]subsonic.SubsonicIndex,
AddPage(PageNewPlaylist, ui.playlistPage.NewPlaylistModal, true, false).
AddPage(PageAddToPlaylist, ui.browserPage.AddToPlaylistModal, true, false).
AddPage(PageMessageBox, ui.messageBox, true, false).
AddPage(PageHelpBox, ui.helpModal, true, false).
AddPage(PageLog, ui.logPage.Root, true, false)

rootFlex := tview.NewFlex().
Expand Down Expand Up @@ -171,6 +183,18 @@ func (ui *Ui) Run() error {
return ui.app.Run()
}

func (ui *Ui) ShowHelp() {
activePage := ui.menuWidget.GetActivePage()
ui.helpWidget.RenderHelp(activePage)

ui.pages.ShowPage(PageHelpBox)
ui.app.SetFocus(ui.helpModal)
}

func (ui *Ui) CloseHelp() {
ui.pages.HidePage(PageHelpBox)
}

func (ui *Ui) showMessageBox(text string) {
ui.pages.ShowPage(PageMessageBox)
ui.messageBox.SetText(text)
Expand Down
3 changes: 3 additions & 0 deletions gui_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (ui *Ui) handlePageInput(event *tcell.EventKey) *tcell.EventKey {
case '4':
ui.ShowPage(PageLog)

case '?':
ui.ShowHelp()

case 'Q':
ui.Quit()

Expand Down
34 changes: 34 additions & 0 deletions help_text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

const helpPlayback = `
p play/pause
P stop
> next song
-/= volume down/volume up
,/. seek -10/+10 seconds
r add 50 random songs to queue
`

const helpPageBrowser = `
ENTER play song (clears current queue)
a add album or song to queue
A add song to playlist
y toggle star on song/album
R refresh the list
/ Search artists
n Continue search forward
N Continue search backwards
ESC Close search
`

const helpPageQueue = `
d/DEL remove currently selected song from the queue
D remove all songs from queue
y toggle star on song
`

const helpPagePlaylists = `
n new playlist
d delete playlist
a add playlist or song to queue
`
76 changes: 76 additions & 0 deletions widget_help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"strings"

"github.com/rivo/tview"
)

type HelpWidget struct {
Root *tview.Flex

helpBook *tview.Flex
leftColumn, rightColumn *tview.TextView

// external references
ui *Ui
}

func (ui *Ui) createHelpWidget() (m *HelpWidget) {
m = &HelpWidget{
ui: ui,
}

// two help columns side by side
m.leftColumn = tview.NewTextView().SetTextAlign(tview.AlignLeft)
m.rightColumn = tview.NewTextView().SetTextAlign(tview.AlignLeft)
m.helpBook = tview.NewFlex().
SetDirection(tview.FlexColumn)

// button at the bottom
closeButton := tview.NewButton("Close")
closeButton.SetSelectedFunc(func() {
ui.CloseHelp()
})

m.Root = tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(m.helpBook, 0, 1, false).
AddItem(closeButton, 1, 0, true)

m.Root.Box.SetBorder(true).SetTitle(" Help ")

return
}

func (h *HelpWidget) RenderHelp(context string) {
leftText := "[Playback]\n" + tview.Escape(strings.TrimSpace(helpPlayback))
h.leftColumn.SetText(leftText)

rightText := ""
switch context {
case PageBrowser:
rightText = "[Browser]\n" + tview.Escape(strings.TrimSpace(helpPageBrowser))

case PageQueue:
rightText = "[Queue]\n" + tview.Escape(strings.TrimSpace(helpPageQueue))

case PagePlaylists:
rightText = "[Playlists]\n" + tview.Escape(strings.TrimSpace(helpPagePlaylists))

case PageLog:
fallthrough
default:
// no text
}

h.rightColumn.SetText(rightText)

h.helpBook.Clear()
if rightText != "" {
h.helpBook.AddItem(h.leftColumn, 38, 0, false).
AddItem(h.rightColumn, 0, 1, true) // gets focus for scrolling
} else {
h.helpBook.AddItem(h.leftColumn, 0, 1, false)
}
}
8 changes: 6 additions & 2 deletions widget_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type MenuWidget struct {

var buttonOrder = []string{PageBrowser, PageQueue, PagePlaylists, PageLog}

func NewMenuWidget(ui *Ui) (m *MenuWidget) {
func (ui *Ui) createMenuWidget() (m *MenuWidget) {
m = &MenuWidget{
activeButton: buttonOrder[0],
buttons: make(map[string]*tview.Button),
Expand Down Expand Up @@ -54,7 +54,7 @@ func NewMenuWidget(ui *Ui) (m *MenuWidget) {
SetStyle(m.buttonStyle).
SetActivatedStyle(m.buttonStyle).
SetSelectedFunc(func() {
ui.showMessageBox("not yet implemented, sorry")
ui.ShowHelp()
})

m.buttonsRight = tview.NewFlex().
Expand Down Expand Up @@ -118,3 +118,7 @@ func (m *MenuWidget) SetActivePage(name string) {
m.activeButton = name
m.updatePageButtons()
}

func (m *MenuWidget) GetActivePage() string {
return m.activeButton
}

0 comments on commit 8d80387

Please sign in to comment.