gomu/gomu.go

153 lines
3.0 KiB
Go
Raw Permalink Normal View History

2020-06-18 14:30:20 +08:00
package main
import (
"github.com/rivo/tview"
2020-08-02 16:02:07 +08:00
"github.com/ztrue/tracerr"
2021-02-26 11:18:58 +08:00
"github.com/issadarkthing/gomu/anko"
"github.com/issadarkthing/gomu/hook"
"github.com/issadarkthing/gomu/player"
2020-06-18 14:30:20 +08:00
)
2021-03-25 12:01:12 +08:00
// VERSION is version information of gomu
2020-09-05 17:06:23 +08:00
var VERSION = "N/A"
2020-07-23 15:15:39 +08:00
var gomu *Gomu
2021-03-25 12:01:12 +08:00
// Gomu is the application from tview
2020-07-23 15:15:39 +08:00
type Gomu struct {
2020-08-18 16:09:12 +08:00
app *tview.Application
playingBar *PlayingBar
queue *Queue
playlist *Playlist
2021-02-26 11:18:58 +08:00
player *player.Player
2020-08-18 16:09:12 +08:00
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
2021-02-20 11:18:03 +08:00
anko *anko.Anko
2021-02-19 12:08:56 +08:00
hook *hook.EventHook
2020-07-23 15:15:39 +08:00
}
// Creates new instance of gomu with default values
func newGomu() *Gomu {
gomu := &Gomu{
command: newCommand(),
2021-02-14 14:35:36 +08:00
anko: anko.NewAnko(),
2021-02-19 12:08:56 +08:00
hook: hook.NewEventHook(),
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)
2021-02-26 11:18:58 +08:00
g.player = player.New(g.anko.GetInt("General.volume"))
2020-07-23 15:15:39 +08:00
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
}
func (g *Gomu) cyclePanels2() Panel {
2021-02-03 11:34:50 +08:00
first := g.panels[0]
second := g.panels[1]
if first.HasFocus() {
g.setFocusPanel(second)
g.prevPanel = second
return second
} else if second.HasFocus() {
g.setFocusPanel(first)
g.prevPanel = first
return first
} else {
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
}
2021-02-03 11:34:50 +08:00
if g.prevPanel != panel {
g.setUnfocusPanel(g.prevPanel)
}
2020-07-23 15:15:39 +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.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 {
2021-04-06 14:55:40 +08:00
err := gomu.queue.saveQueue()
if err != nil {
return tracerr.Wrap(err)
}
2020-08-02 16:02:07 +08:00
}
gomu.app.Stop()
return nil
}