gomu/gomu.go

139 lines
2.7 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-07-23 15:15:39 +08:00
"github.com/gdamore/tcell"
2020-06-18 14:30:20 +08:00
"github.com/rivo/tview"
)
2020-07-23 15:15:39 +08:00
const VERSION = "v1.4.0"
var gomu *Gomu
type Gomu struct {
app *tview.Application
playingBar *PlayingBar
queue *Queue
playlist *Playlist
player *Player
pages *tview.Pages
2020-07-24 14:49:58 +08:00
popups Stack
2020-07-23 15:15:39 +08:00
prevPanel Panel
popupBg tcell.Color
textColor tcell.Color
accentColor tcell.Color
panels []Panel
isSuspend bool
mu sync.Mutex
}
// Creates new instance of gomu with default values
func newGomu() *Gomu {
gomu := &Gomu{
popupBg: tcell.GetColor("#0A0F14"),
textColor: tcell.ColorWhite,
accentColor: tcell.ColorDarkCyan,
}
return gomu
}
// Initialize childrens/panels this is seperated from
// constructor function `NewGomu` so that we can
// test independently
func (g *Gomu) initPanels(app *tview.Application) {
g.app = app
g.playingBar = newPlayingBar()
g.queue = newQueue()
g.playlist = newPlaylist()
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))
panel.SetBorderColor(g.accentColor)
panel.SetTitleColor(g.accentColor)
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) {
g.prevPanel.SetBorderColor(g.textColor)
g.prevPanel.SetTitleColor((g.textColor))
2020-06-18 14:30:20 +08:00
}