From c96e3416a34c4b51f6fdd5a311af9c841118293e Mon Sep 17 00:00:00 2001 From: raziman Date: Fri, 26 Jun 2020 17:09:15 +0800 Subject: [PATCH] add popup messages --- player.go | 20 +++++++++++++++--- playlist.go | 11 +++++----- popup.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++---- start.go | 43 ++++++++++++++++++++++++++------------ utils.go | 4 ---- 5 files changed, 107 insertions(+), 30 deletions(-) diff --git a/player.go b/player.go index 3987760..d96f46f 100644 --- a/player.go +++ b/player.go @@ -3,6 +3,7 @@ package main import ( + "fmt" "os" "time" @@ -10,6 +11,7 @@ import ( "github.com/faiface/beep/effects" "github.com/faiface/beep/mp3" "github.com/faiface/beep/speaker" + "github.com/spf13/viper" ) type Song struct { @@ -74,6 +76,12 @@ func (p *Player) Run() { song := &Song{name: GetName(f.Name()), path: first} 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) p.done = done @@ -95,6 +103,7 @@ func (p *Player) Run() { Silent: false, } + // sets the volume of previous player volume.Volume += p.volume @@ -172,13 +181,19 @@ func (p *Player) CurrentSong() Song { return p.currentSong } - // 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() p._volume.Volume += v p.volume = p._volume.Volume speaker.Unlock() + return p.volume } func (p *Player) TogglePause() { @@ -196,4 +211,3 @@ func (p *Player) Skip() { p.done <- true } } - diff --git a/playlist.go b/playlist.go index 26940d8..7581046 100644 --- a/playlist.go +++ b/playlist.go @@ -14,11 +14,6 @@ import ( "github.com/spf13/viper" ) -var ( - textColor = tcell.ColorWhite - accentColor = tcell.ColorDarkCyan -) - type AudioFile struct { Name string Path string @@ -72,7 +67,6 @@ func InitPlaylist() *Playlist { node.SetExpanded(!node.IsExpanded()) }) - playlist.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { currNode := playlist.GetCurrentNode() @@ -109,6 +103,11 @@ func InitPlaylist() *Playlist { case 'L': + if !viper.GetBool("confirm_bulk_add") { + playlist.addAllToQueue(playlist.GetCurrentNode()) + return e + } + confirmationPopup( "Are you sure to add this whole directory into queue?", func(_ int, label string) { diff --git a/popup.go b/popup.go index 336b3b9..b4f8d4d 100644 --- a/popup.go +++ b/popup.go @@ -3,7 +3,10 @@ package main import ( - "github.com/gdamore/tcell" + "fmt" + "strings" + "time" + "github.com/rivo/tview" ) @@ -14,10 +17,10 @@ func confirmationPopup( modal := tview.NewModal(). SetText(text). - SetBackgroundColor(tcell.ColorDarkCyan). + SetBackgroundColor(popupBg). AddButtons([]string{"yes", "no"}). - SetButtonBackgroundColor(tcell.ColorDarkCyan). - SetButtonTextColor(tcell.ColorBlack). + SetButtonBackgroundColor(popupBg). + SetButtonTextColor(accentColor). SetDoneFunc(handler) 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) } + +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) + +} diff --git a/start.go b/start.go index f9cb1d4..b16f0ca 100644 --- a/start.go +++ b/start.go @@ -11,12 +11,16 @@ import ( ) var ( - app *tview.Application - playingBar *PlayingBar - queue *Queue - playlist *Playlist - player *Player - pages *tview.Pages + app *tview.Application + playingBar *PlayingBar + queue *Queue + playlist *Playlist + player *Player + pages *tview.Pages + prevPanel Children + popupBg = tcell.GetColor("#0A0F14") + textColor = tcell.ColorWhite + accentColor = tcell.ColorDarkCyan ) func start(application *tview.Application) { @@ -40,16 +44,18 @@ func start(application *tview.Application) { flex := Layout() pages = tview.NewPages().AddPage("main", flex, true, true) + playlist.SetBorderColor(accentColor) + playlist.SetTitleColor(accentColor) + prevPanel = playlist + childrens := []Children{playlist, queue, playingBar} - - application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { // cycle through each section case tcell.KeyTAB: - cycleChildren(application, childrens) + prevPanel = cycleChildren(application, childrens) } @@ -74,10 +80,18 @@ func start(application *tview.Application) { player.TogglePause() case '+': - player.Volume(0.5) + v := int(player.volume * 10) + 50 + if v < 50 { + vol := player.Volume(0.5) + volumePopup(vol) + } case '-': - player.Volume(-0.5) + v := int(player.volume * 10) + 50 + if v > 0 { + vol := player.Volume(-0.5) + volumePopup(vol) + } case 'n': player.Skip() @@ -94,7 +108,7 @@ func start(application *tview.Application) { }) // 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()) } } @@ -139,7 +153,8 @@ func cycleChildren(app *tview.Application, childrens []Children) Children { return nextChild } } -first := childrens[0] + + first := childrens[0] if anyChildHasFocus == false { @@ -171,6 +186,8 @@ func readConfig() { viper.SetDefault("music_dir", "~/music") 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 if _, err := os.Stat(configPath); err != nil { diff --git a/utils.go b/utils.go index a807abd..4dc3d12 100644 --- a/utils.go +++ b/utils.go @@ -71,8 +71,6 @@ func expandTilde(_path string) string { } - - // gets the length of the song in the queue 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 } - - // detects the filetype of file func GetFileContentType(out *os.File) (string, error) {