From 9f669f94a6d591d1ebeea70efca5e6eb4a8ac823 Mon Sep 17 00:00:00 2001 From: raziman Date: Fri, 24 Jul 2020 14:49:58 +0800 Subject: [PATCH] implement stack popup --- gomu.go | 2 +- playlist.go | 5 ++-- popup.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/gomu.go b/gomu.go index ba05960..49d3e23 100644 --- a/gomu.go +++ b/gomu.go @@ -18,7 +18,7 @@ type Gomu struct { playlist *Playlist player *Player pages *tview.Pages - popups []tview.Primitive + popups Stack prevPanel Panel popupBg tcell.Color textColor tcell.Color diff --git a/playlist.go b/playlist.go index 4402bff..a0586cd 100644 --- a/playlist.go +++ b/playlist.go @@ -118,7 +118,6 @@ func newPlaylist() *Playlist { case 'a': name, _ := gomu.pages.GetFrontPage() - if name != "mkdir-popup" { createPlaylistPopup() } @@ -229,7 +228,7 @@ func (p *Playlist) deleteSong(audioFile *AudioFile) (err error) { if buttonName == "no" || buttonName == "" { gomu.pages.RemovePage("confirmation-popup") - gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive)) + gomu.popups.pop() return } @@ -251,7 +250,7 @@ func (p *Playlist) deleteSong(audioFile *AudioFile) (err error) { } gomu.pages.RemovePage("confirmation-popup") - gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive)) + gomu.popups.pop() }) return nil diff --git a/popup.go b/popup.go index d6963ad..30b72ce 100644 --- a/popup.go +++ b/popup.go @@ -18,6 +18,49 @@ var ( popupCounter = 0 ) +// Simple stack data structure +type Stack struct { + popups []tview.Primitive +} + +// Push popup to the stack and focus +func (s *Stack) push(p tview.Primitive) { + s.popups = append(s.popups, p) + gomu.app.SetFocus(p) +} + +// Show item on the top of the stack +func (s *Stack) peekTop() tview.Primitive { + + if len(s.popups)-1 < 0 { + return nil + } + + return s.popups[len(s.popups)-1] +} + +// Remove popup from the stack and focus previous popup +func (s *Stack) pop() tview.Primitive { + + if len(s.popups) == 0 { + return nil + } + + last := s.popups[len(s.popups)-1] + res := s.popups[:len(s.popups)-1] + s.popups = res + + // focus previous popup + if len(s.popups) > 0 { + gomu.app.SetFocus(s.popups[len(s.popups)-1]) + } else { + // focus the panel if no popup left + gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive)) + } + + return last +} + // Gets popup timeout from config file func getPopupTimeout() time.Duration { @@ -48,7 +91,8 @@ func confirmationPopup( gomu.pages. AddPage("confirmation-popup", center(modal, 40, 10), true, true) - gomu.app.SetFocus(modal) + + gomu.popups.push(modal) } @@ -107,7 +151,16 @@ func timedPopup( time.Sleep(timeout) gomu.pages.RemovePage(popupId) gomu.app.Draw() - gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive)) + + // timed popup shouldn't get focused + // this here check if another popup exists and focus that instead of panel + // if none continue focus panel + topPopup := gomu.popups.peekTop() + if topPopup == nil { + gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive)) + } else { + gomu.app.SetFocus(topPopup) + } }() } @@ -186,7 +239,7 @@ func helpPopup(panel Panel) { }) gomu.pages.AddPage("help-page", center(list, 50, 30), true, true) - gomu.app.SetFocus(list) + gomu.popups.push(list) } // Input popup. Takes video url from youtube to be downloaded @@ -226,7 +279,7 @@ func downloadMusicPopup(selPlaylist *tview.TreeNode) { gomu.pages. AddPage("download-input-popup", center(inputField, 50, 4), true, true) - gomu.app.SetFocus(inputField) + gomu.popups.push(inputField) } @@ -257,17 +310,18 @@ func createPlaylistPopup() { } gomu.pages.RemovePage("mkdir-input-popup") - gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive)) + gomu.popups.pop() case tcell.KeyEsc: gomu.pages.RemovePage("mkdir-input-popup") - gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive)) + gomu.popups.pop() } }) gomu.pages. AddPage("mkdir-input-popup", center(inputField, 50, 4), true, true) - gomu.app.SetFocus(inputField) + + gomu.popups.push(inputField) }