add popup messages

This commit is contained in:
raziman 2020-06-26 17:09:15 +08:00
parent 2b9d1758e1
commit c96e3416a3
5 changed files with 107 additions and 30 deletions

View File

@ -3,6 +3,7 @@
package main package main
import ( import (
"fmt"
"os" "os"
"time" "time"
@ -10,6 +11,7 @@ import (
"github.com/faiface/beep/effects" "github.com/faiface/beep/effects"
"github.com/faiface/beep/mp3" "github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker" "github.com/faiface/beep/speaker"
"github.com/spf13/viper"
) )
type Song struct { type Song struct {
@ -74,6 +76,12 @@ func (p *Player) Run() {
song := &Song{name: GetName(f.Name()), path: first} song := &Song{name: GetName(f.Name()), path: first}
p.currentSong = *song p.currentSong = *song
popupMessage := fmt.Sprintf("%s\n\n[ %s ]", song.name, fmtDuration(p.length))
timeout := viper.GetInt("popup_timeout")
timeoutPopup(" Current Song ", popupMessage, time.Second*time.Duration(timeout))
done := make(chan bool, 1) done := make(chan bool, 1)
p.done = done p.done = done
@ -95,6 +103,7 @@ func (p *Player) Run() {
Silent: false, Silent: false,
} }
// sets the volume of previous player // sets the volume of previous player
volume.Volume += p.volume volume.Volume += p.volume
@ -172,13 +181,19 @@ func (p *Player) CurrentSong() Song {
return p.currentSong return p.currentSong
} }
// volume up and volume down using -0.5 or +0.5 // volume up and volume down using -0.5 or +0.5
func (p *Player) Volume(v float64) { func (p *Player) Volume(v float64) float64 {
if p._volume == nil {
p.volume += v
return v
}
speaker.Lock() speaker.Lock()
p._volume.Volume += v p._volume.Volume += v
p.volume = p._volume.Volume p.volume = p._volume.Volume
speaker.Unlock() speaker.Unlock()
return p.volume
} }
func (p *Player) TogglePause() { func (p *Player) TogglePause() {
@ -196,4 +211,3 @@ func (p *Player) Skip() {
p.done <- true p.done <- true
} }
} }

View File

@ -14,11 +14,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var (
textColor = tcell.ColorWhite
accentColor = tcell.ColorDarkCyan
)
type AudioFile struct { type AudioFile struct {
Name string Name string
Path string Path string
@ -72,7 +67,6 @@ func InitPlaylist() *Playlist {
node.SetExpanded(!node.IsExpanded()) node.SetExpanded(!node.IsExpanded())
}) })
playlist.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { playlist.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
currNode := playlist.GetCurrentNode() currNode := playlist.GetCurrentNode()
@ -109,6 +103,11 @@ func InitPlaylist() *Playlist {
case 'L': case 'L':
if !viper.GetBool("confirm_bulk_add") {
playlist.addAllToQueue(playlist.GetCurrentNode())
return e
}
confirmationPopup( confirmationPopup(
"Are you sure to add this whole directory into queue?", "Are you sure to add this whole directory into queue?",
func(_ int, label string) { func(_ int, label string) {

View File

@ -3,7 +3,10 @@
package main package main
import ( import (
"github.com/gdamore/tcell" "fmt"
"strings"
"time"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
@ -14,10 +17,10 @@ func confirmationPopup(
modal := tview.NewModal(). modal := tview.NewModal().
SetText(text). SetText(text).
SetBackgroundColor(tcell.ColorDarkCyan). SetBackgroundColor(popupBg).
AddButtons([]string{"yes", "no"}). AddButtons([]string{"yes", "no"}).
SetButtonBackgroundColor(tcell.ColorDarkCyan). SetButtonBackgroundColor(popupBg).
SetButtonTextColor(tcell.ColorBlack). SetButtonTextColor(accentColor).
SetDoneFunc(handler) SetDoneFunc(handler)
pages.AddPage("confirmation-popup", center(modal, 40, 10), true, true) pages.AddPage("confirmation-popup", center(modal, 40, 10), true, true)
@ -34,3 +37,51 @@ func center(p tview.Primitive, width, height int) tview.Primitive {
AddItem(nil, 0, 1, false), width, 1, false). AddItem(nil, 0, 1, false), width, 1, false).
AddItem(nil, 0, 1, false) AddItem(nil, 0, 1, false)
} }
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)
}
func timeoutPopup(title string, desc string, timeout time.Duration) {
textView := tview.NewTextView().
SetText(fmt.Sprintf("%s", desc)).
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)
pages.AddPage("timeout-popup", topRight(box, 70, 7), true, true)
app.SetFocus(prevPanel.(tview.Primitive))
go func() {
time.Sleep(timeout)
pages.RemovePage("timeout-popup")
app.SetFocus(prevPanel.(tview.Primitive))
}()
}
func volumePopup(volume float64) {
vol := int(volume * 10) + 50
progress := fmt.Sprintf("\n%d |%s%s| %s",
vol,
strings.Repeat("█", vol),
strings.Repeat("-", 50-vol),
"50",
)
timeoutPopup(" Volume ", progress, time.Second * 5)
}

View File

@ -11,12 +11,16 @@ import (
) )
var ( var (
app *tview.Application app *tview.Application
playingBar *PlayingBar playingBar *PlayingBar
queue *Queue queue *Queue
playlist *Playlist playlist *Playlist
player *Player player *Player
pages *tview.Pages pages *tview.Pages
prevPanel Children
popupBg = tcell.GetColor("#0A0F14")
textColor = tcell.ColorWhite
accentColor = tcell.ColorDarkCyan
) )
func start(application *tview.Application) { func start(application *tview.Application) {
@ -40,16 +44,18 @@ func start(application *tview.Application) {
flex := Layout() flex := Layout()
pages = tview.NewPages().AddPage("main", flex, true, true) pages = tview.NewPages().AddPage("main", flex, true, true)
playlist.SetBorderColor(accentColor)
playlist.SetTitleColor(accentColor)
prevPanel = playlist
childrens := []Children{playlist, queue, playingBar} childrens := []Children{playlist, queue, playingBar}
application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() { switch event.Key() {
// cycle through each section // cycle through each section
case tcell.KeyTAB: case tcell.KeyTAB:
cycleChildren(application, childrens) prevPanel = cycleChildren(application, childrens)
} }
@ -74,10 +80,18 @@ func start(application *tview.Application) {
player.TogglePause() player.TogglePause()
case '+': case '+':
player.Volume(0.5) v := int(player.volume * 10) + 50
if v < 50 {
vol := player.Volume(0.5)
volumePopup(vol)
}
case '-': case '-':
player.Volume(-0.5) v := int(player.volume * 10) + 50
if v > 0 {
vol := player.Volume(-0.5)
volumePopup(vol)
}
case 'n': case 'n':
player.Skip() player.Skip()
@ -94,7 +108,7 @@ func start(application *tview.Application) {
}) })
// main loop // main loop
if err := application.SetRoot(pages, true).SetFocus(flex).Run(); err != nil { if err := application.SetRoot(pages, true).SetFocus(playlist).Run(); err != nil {
log(err.Error()) log(err.Error())
} }
} }
@ -139,7 +153,8 @@ func cycleChildren(app *tview.Application, childrens []Children) Children {
return nextChild return nextChild
} }
} }
first := childrens[0]
first := childrens[0]
if anyChildHasFocus == false { if anyChildHasFocus == false {
@ -171,6 +186,8 @@ func readConfig() {
viper.SetDefault("music_dir", "~/music") viper.SetDefault("music_dir", "~/music")
viper.SetDefault("confirm_on_exit", true) viper.SetDefault("confirm_on_exit", true)
viper.SetDefault("confirm_bulk_add", true)
viper.SetDefault("popup_timeout", 10)
// creates gomu config dir if does not exist // creates gomu config dir if does not exist
if _, err := os.Stat(configPath); err != nil { if _, err := os.Stat(configPath); err != nil {

View File

@ -71,8 +71,6 @@ func expandTilde(_path string) string {
} }
// gets the length of the song in the queue // gets the length of the song in the queue
func GetLength(audioPath string) (time.Duration, error) { func GetLength(audioPath string) (time.Duration, error) {
@ -95,8 +93,6 @@ func GetLength(audioPath string) (time.Duration, error) {
return format.SampleRate.D(streamer.Len()), nil return format.SampleRate.D(streamer.Len()), nil
} }
// detects the filetype of file // detects the filetype of file
func GetFileContentType(out *os.File) (string, error) { func GetFileContentType(out *os.File) (string, error) {