gomu/gomu.go

154 lines
2.9 KiB
Go
Raw Normal View History

2020-06-18 14:30:20 +08:00
package main
import (
2020-07-23 15:15:39 +08:00
"sync"
2020-06-18 14:30:20 +08:00
"github.com/rivo/tview"
2020-08-02 16:02:07 +08:00
"github.com/ztrue/tracerr"
2020-06-18 14:30:20 +08:00
)
2020-08-22 22:05:28 +08:00
const VERSION = "v1.5.5"
2020-07-23 15:15:39 +08:00
var gomu *Gomu
type Gomu struct {
2020-08-18 16:09:12 +08:00
app *tview.Application
playingBar *PlayingBar
queue *Queue
playlist *Playlist
player *Player
pages *tview.Pages
2020-08-22 14:41:03 +08:00
colors *Colors
command Command
2020-08-18 16:09:12 +08:00
// popups is used to manage focus between popups and panels
2020-08-22 14:41:03 +08:00
popups Stack
prevPanel Panel
panels []Panel
args Args
2020-08-22 14:41:03 +08:00
isSuspend bool
mu sync.Mutex
2020-07-23 15:15:39 +08:00
}
// Creates new instance of gomu with default values
func newGomu() *Gomu {
gomu := &Gomu{
colors: newColor(),
command: newCommand(),
2020-07-23 15:15:39 +08:00
}
return gomu
}
// Initialize childrens/panels this is seperated from
2020-08-22 14:41:03 +08:00
// constructor function `newGomu` so that we can
2020-07-23 15:15:39 +08:00
// test independently
2020-08-11 13:39:02 +08:00
func (g *Gomu) initPanels(app *tview.Application, args Args) {
2020-07-23 15:15:39 +08:00
g.app = app
g.playingBar = newPlayingBar()
g.queue = newQueue()
2020-08-11 13:39:02 +08:00
g.playlist = newPlaylist(args)
2020-07-23 15:15:39 +08:00
g.player = newPlayer()
g.pages = tview.NewPages()
g.panels = []Panel{g.playlist, g.queue, g.playingBar}
}
2020-07-23 15:34:56 +08:00
// Cycle between panels
2020-07-23 15:15:39 +08:00
func (g *Gomu) cyclePanels() Panel {
var anyChildHasFocus bool
for i, child := range g.panels {
if child.HasFocus() {
2020-06-18 14:30:20 +08:00
2020-07-23 15:15:39 +08:00
anyChildHasFocus = true
2020-07-23 15:15:39 +08:00
var nextChild Panel
2020-07-18 15:43:20 +08:00
2020-07-23 15:15:39 +08:00
// if its the last element set the child back to one
if i == len(g.panels)-1 {
nextChild = g.panels[0]
} else {
nextChild = g.panels[i+1]
}
2020-06-24 20:09:47 +08:00
2020-07-23 15:15:39 +08:00
g.setFocusPanel(nextChild)
2020-06-18 14:30:20 +08:00
2020-07-23 15:15:39 +08:00
g.prevPanel = nextChild
return nextChild
}
}
first := g.panels[0]
if !anyChildHasFocus {
g.setFocusPanel(first)
}
g.prevPanel = first
return first
}
2020-07-23 15:34:56 +08:00
// Changes title and border color when focusing panel
2020-07-23 15:15:39 +08:00
// and changes color of the previous panel as well
func (g *Gomu) setFocusPanel(panel Panel) {
g.app.SetFocus(panel.(tview.Primitive))
2020-08-22 14:41:03 +08:00
panel.SetBorderColor(g.colors.accent)
panel.SetTitleColor(g.colors.accent)
2020-07-23 15:15:39 +08:00
if g.prevPanel == nil {
return
}
g.setUnfocusPanel(g.prevPanel)
}
// Safely write the IsSuspend state, IsSuspend is used to indicate if we
// are going to suspend the app. This should be used to widgets or
// texts that keeps rendering continuosly or possible to render when the app
// is going to suspend.
// Returns true if app is not in suspend
func (g *Gomu) suspend() bool {
g.mu.Lock()
defer g.mu.Unlock()
if g.isSuspend {
return false
}
g.isSuspend = true
return true
}
// The opposite of Suspend. Returns true if app is in suspend
func (g *Gomu) unsuspend() bool {
g.mu.Lock()
defer g.mu.Unlock()
if !g.isSuspend {
return false
}
g.isSuspend = false
return true
}
2020-06-18 14:30:20 +08:00
2020-07-23 15:34:56 +08:00
// Removes the color of the given panel
2020-07-23 15:15:39 +08:00
func (g *Gomu) setUnfocusPanel(panel Panel) {
2020-08-22 14:41:03 +08:00
g.prevPanel.SetBorderColor(g.colors.foreground)
g.prevPanel.SetTitleColor((g.colors.foreground))
2020-06-18 14:30:20 +08:00
}
2020-08-02 16:02:07 +08:00
// Quit the application and do the neccessary clean up
func (g *Gomu) quit(args Args) error {
2020-08-02 16:02:07 +08:00
if !*args.empty {
err := gomu.queue.saveQueue()
if err != nil {
return tracerr.Wrap(err)
}
2020-08-02 16:02:07 +08:00
}
gomu.app.Stop()
return nil
}