gomu/popup.go

247 lines
5.5 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
2020-07-03 00:50:32 +08:00
)
2020-06-27 00:09:59 +08:00
2020-07-04 09:47:41 +08:00
// gets popup timeout from config file
func getPopupTimeout() time.Duration {
dur := viper.GetString("popup_timeout")
m, err := time.ParseDuration(dur)
if err != nil {
appLog(err)
}
return m
}
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).
SetBackgroundColor(gomu.PopupBg).
2020-06-19 16:42:30 +08:00
AddButtons([]string{"yes", "no"}).
SetButtonBackgroundColor(gomu.PopupBg).
SetButtonTextColor(gomu.AccentColor).
2020-06-19 16:42:30 +08:00
SetDoneFunc(handler)
2020-06-19 16:22:20 +08:00
gomu.Pages.AddPage("confirmation-popup", center(modal, 40, 10), true, true)
gomu.App.SetFocus(modal)
2020-06-19 16:22:20 +08:00
}
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).
SetTextColor(gomu.AccentColor)
2020-06-26 17:09:15 +08:00
textView.SetTextAlign(tview.AlignCenter).SetBackgroundColor(gomu.PopupBg)
2020-06-26 17:09:15 +08:00
box := tview.NewFrame(textView).SetBorders(1, 1, 1, 1, 1, 1)
box.SetTitle(title).SetBorder(true).SetBackgroundColor(gomu.PopupBg)
2020-06-26 17:09:15 +08:00
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
gomu.Pages.AddPage(popupId, topRight(box, 70, 7), true, true)
gomu.App.SetFocus(gomu.PrevPanel.(tview.Primitive))
2020-06-26 17:09:15 +08:00
go func() {
time.Sleep(timeout)
gomu.Pages.RemovePage(popupId)
2020-07-10 22:05:52 +08:00
gomu.App.SetFocus(gomu.PrevPanel.(tview.Primitive))
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",
)
2020-07-04 09:47:41 +08:00
timedPopup(" Volume ", progress, getPopupTimeout())
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(gomu.PopupBg).SetTitle(" Help ").
2020-06-27 17:16:49 +08:00
SetBorder(true)
list.SetSelectedBackgroundColor(gomu.PopupBg).
SetSelectedTextColor(gomu.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:
gomu.Pages.RemovePage("help-page")
gomu.App.SetFocus(gomu.PrevPanel.(tview.Primitive))
2020-07-01 17:48:56 +08:00
}
2020-06-27 00:09:59 +08:00
return nil
})
gomu.Pages.AddPage("help-page", center(list, 50, 30), true, true)
gomu.App.SetFocus(list)
2020-06-27 00:09:59 +08:00
}
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(gomu.PopupBg).SetBorder(true).SetTitle(" Ytdl ")
inputField.SetFieldBackgroundColor(gomu.AccentColor).SetFieldTextColor(tcell.ColorBlack)
2020-06-27 17:16:49 +08:00
inputField.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
url := inputField.GetText()
Ytdl(url, selPlaylist)
gomu.Pages.RemovePage("download-input-popup")
2020-06-27 17:16:49 +08:00
case tcell.KeyEscape:
gomu.Pages.RemovePage("download-input-popup")
2020-06-27 17:16:49 +08:00
}
gomu.App.SetFocus(gomu.PrevPanel.(tview.Primitive))
2020-07-02 16:11:25 +08:00
2020-06-27 17:16:49 +08:00
})
gomu.Pages.AddPage("download-input-popup", center(inputField, 50, 4), true, true)
gomu.App.SetFocus(inputField)
2020-06-27 17:16:49 +08:00
}
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(gomu.PopupBg).SetBorder(true).SetTitle(" New Playlist ")
inputField.SetFieldBackgroundColor(gomu.AccentColor).SetFieldTextColor(tcell.ColorBlack)
2020-07-01 17:48:56 +08:00
inputField.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
playListName := inputField.GetText()
err := gomu.Playlist.CreatePlaylist(playListName)
2020-07-03 00:50:32 +08:00
if err != nil {
appLog(err)
}
gomu.Pages.RemovePage("mkdir-input-popup")
gomu.App.SetFocus(gomu.PrevPanel.(tview.Primitive))
2020-07-01 17:48:56 +08:00
case tcell.KeyEsc:
gomu.Pages.RemovePage("mkdir-input-popup")
gomu.App.SetFocus(gomu.PrevPanel.(tview.Primitive))
2020-07-01 17:48:56 +08:00
}
})
gomu.Pages.AddPage("mkdir-input-popup", center(inputField, 50, 4), true, true)
gomu.App.SetFocus(inputField)
2020-07-01 17:48:56 +08:00
}