gomu/popup.go

234 lines
5.2 KiB
Go
Raw Normal View History

2020-06-22 00:05:56 +08:00
// Copyright (C) 2020 Raziman
2020-06-19 16:22:20 +08:00
package main
import (
2020-06-26 17:09:15 +08:00
"fmt"
"strings"
"time"
2020-06-27 00:09:59 +08:00
"github.com/gdamore/tcell"
2020-06-19 16:22:20 +08:00
"github.com/rivo/tview"
2020-06-28 14:03:30 +08:00
"github.com/spf13/viper"
2020-06-19 16:22:20 +08:00
)
2020-06-27 00:09:59 +08:00
// this is used to make the popup unique
// this mitigates the issue of closing all popups when timeout ends
2020-06-28 14:03:30 +08:00
var (
popupCounter = 0
popupTimeout = time.Duration(viper.GetInt("popup_timeout")) * time.Second
2020-07-03 00:50:32 +08:00
)
2020-06-27 00:09:59 +08:00
2020-06-19 16:22:20 +08:00
func confirmationPopup(
text string,
2020-06-19 16:42:30 +08:00
handler func(buttonIndex int, buttonLabel string),
2020-06-19 16:22:20 +08:00
) {
modal := tview.NewModal().
2020-06-19 16:42:30 +08:00
SetText(text).
2020-06-26 17:09:15 +08:00
SetBackgroundColor(popupBg).
2020-06-19 16:42:30 +08:00
AddButtons([]string{"yes", "no"}).
2020-06-26 17:09:15 +08:00
SetButtonBackgroundColor(popupBg).
SetButtonTextColor(accentColor).
2020-06-19 16:42:30 +08:00
SetDoneFunc(handler)
2020-06-19 16:22:20 +08:00
pages.AddPage("confirmation-popup", center(modal, 40, 10), true, true)
app.SetFocus(modal)
}
func center(p tview.Primitive, width, height int) tview.Primitive {
return tview.NewFlex().
2020-06-19 16:42:30 +08:00
AddItem(nil, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
2020-06-19 16:22:20 +08:00
AddItem(nil, 0, 1, false).
2020-06-19 16:42:30 +08:00
AddItem(p, height, 1, false).
AddItem(nil, 0, 1, false), width, 1, false).
AddItem(nil, 0, 1, false)
2020-06-19 16:22:20 +08:00
}
2020-06-26 17:09:15 +08:00
func topRight(p tview.Primitive, width, height int) tview.Primitive {
return tview.NewFlex().
AddItem(nil, 0, 23, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(p, height, 1, false).
AddItem(nil, 0, 15, false), width, 1, false).
AddItem(nil, 0, 1, false)
}
2020-06-28 14:03:30 +08:00
func timedPopup(title string, desc string, timeout time.Duration) {
2020-06-26 17:09:15 +08:00
textView := tview.NewTextView().
2020-07-03 00:50:32 +08:00
SetText(desc).
2020-06-26 17:09:15 +08:00
SetTextColor(accentColor)
textView.SetTextAlign(tview.AlignCenter).SetBackgroundColor(popupBg)
box := tview.NewFrame(textView).SetBorders(1, 1, 1, 1, 1, 1)
box.SetTitle(title).SetBorder(true).SetBackgroundColor(popupBg)
2020-06-27 00:09:59 +08:00
popupId := fmt.Sprintf("%s %d", "timeout-popup", popupCounter)
2020-06-27 17:16:49 +08:00
popupCounter++
2020-06-27 00:09:59 +08:00
pages.AddPage(popupId, topRight(box, 70, 7), true, true)
2020-06-26 17:09:15 +08:00
app.SetFocus(prevPanel.(tview.Primitive))
go func() {
time.Sleep(timeout)
2020-06-27 00:09:59 +08:00
pages.RemovePage(popupId)
2020-06-26 17:09:15 +08:00
app.SetFocus(prevPanel.(tview.Primitive))
2020-07-02 16:11:25 +08:00
app.Draw()
2020-06-26 17:09:15 +08:00
}()
}
func volumePopup(volume float64) {
2020-06-27 17:16:49 +08:00
vol := int(volume*10) + 50
2020-06-26 17:09:15 +08:00
progress := fmt.Sprintf("\n%d |%s%s| %s",
vol,
strings.Repeat("█", vol),
strings.Repeat("-", 50-vol),
"50",
)
timedPopup(" Volume ", progress, popupTimeout)
2020-06-26 17:09:15 +08:00
}
2020-06-27 00:09:59 +08:00
func helpPopup() {
helpText := []string{
"j down",
"k up",
"tab change panel",
"space toggle play/pause",
2020-07-03 12:35:29 +08:00
"esc close popup",
2020-06-27 00:09:59 +08:00
"n skip",
"q quit",
"l add song to queue",
"L add playlist to queue",
"h close node in playlist",
2020-07-01 17:48:56 +08:00
"d remove from queue",
2020-07-03 12:35:29 +08:00
"D delete playlist",
2020-06-27 00:09:59 +08:00
"+ volume up",
"- volume down",
"? toggle help",
2020-06-27 17:16:49 +08:00
"Y download audio",
2020-07-03 12:35:29 +08:00
"a create playlist",
2020-06-27 00:09:59 +08:00
}
list := tview.NewList().ShowSecondaryText(false)
list.SetBackgroundColor(popupBg).SetTitle(" Help ").
2020-06-27 17:16:49 +08:00
SetBorder(true)
2020-06-27 00:09:59 +08:00
list.SetSelectedBackgroundColor(popupBg).
2020-06-27 17:16:49 +08:00
SetSelectedTextColor(accentColor)
2020-06-27 00:09:59 +08:00
for _, v := range helpText {
list.AddItem(v, "", 0, nil)
}
prev := func() {
currIndex := list.GetCurrentItem()
list.SetCurrentItem(currIndex - 1)
}
next := func() {
currIndex := list.GetCurrentItem()
idx := currIndex + 1
if currIndex == list.GetItemCount()-1 {
idx = 0
}
list.SetCurrentItem(idx)
}
list.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
switch e.Rune() {
case 'j':
next()
case 'k':
prev()
}
2020-07-03 00:50:32 +08:00
2020-07-01 17:48:56 +08:00
switch e.Key() {
case tcell.KeyEsc:
pages.RemovePage("help-page")
app.SetFocus(prevPanel.(tview.Primitive))
}
2020-06-27 00:09:59 +08:00
return nil
})
pages.AddPage("help-page", center(list, 50, 30), true, true)
app.SetFocus(list)
}
2020-06-27 17:16:49 +08:00
2020-07-02 16:11:25 +08:00
func downloadMusicPopup(selPlaylist *tview.TreeNode) {
2020-06-27 17:16:49 +08:00
inputField := tview.NewInputField().
SetLabel("Enter a url: ").
SetFieldWidth(0).
SetAcceptanceFunc(tview.InputFieldMaxLength(50))
inputField.SetBackgroundColor(popupBg).SetBorder(true).SetTitle(" Ytdl ")
inputField.SetFieldBackgroundColor(accentColor).SetFieldTextColor(tcell.ColorBlack)
inputField.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
url := inputField.GetText()
Ytdl(url, selPlaylist)
pages.RemovePage("download-input-popup")
2020-06-27 17:16:49 +08:00
case tcell.KeyEscape:
pages.RemovePage("download-input-popup")
2020-06-27 17:16:49 +08:00
}
2020-07-02 16:11:25 +08:00
app.SetFocus(prevPanel.(tview.Primitive))
2020-06-27 17:16:49 +08:00
})
pages.AddPage("download-input-popup", center(inputField, 50, 4), true, true)
2020-06-27 17:16:49 +08:00
app.SetFocus(inputField)
}
2020-07-01 17:48:56 +08:00
func CreatePlaylistPopup() {
2020-07-03 00:50:32 +08:00
2020-07-01 17:48:56 +08:00
inputField := tview.NewInputField().
SetLabel("Enter a playlist name: ").
SetFieldWidth(0).
SetAcceptanceFunc(tview.InputFieldMaxLength(50))
inputField.SetBackgroundColor(popupBg).SetBorder(true).SetTitle(" New Playlist ")
inputField.SetFieldBackgroundColor(accentColor).SetFieldTextColor(tcell.ColorBlack)
inputField.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
playListName := inputField.GetText()
2020-07-03 00:50:32 +08:00
err := playlist.CreatePlaylist(playListName)
if err != nil {
appLog(err)
}
pages.RemovePage("mkdir-input-popup")
2020-07-01 17:48:56 +08:00
app.SetFocus(prevPanel.(tview.Primitive))
case tcell.KeyEsc:
pages.RemovePage("mkdir-input-popup")
2020-07-02 16:11:25 +08:00
app.SetFocus(prevPanel.(tview.Primitive))
2020-07-01 17:48:56 +08:00
}
})
pages.AddPage("mkdir-input-popup", center(inputField, 50, 4), true, true)
2020-07-01 17:48:56 +08:00
app.SetFocus(inputField)
}