-
Notifications
You must be signed in to change notification settings - Fork 15
/
gomu.go
152 lines (121 loc) · 2.95 KB
/
gomu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"github.com/rivo/tview"
"github.com/ztrue/tracerr"
"github.com/issadarkthing/gomu/anko"
"github.com/issadarkthing/gomu/hook"
"github.com/issadarkthing/gomu/player"
)
// VERSION is version information of gomu
var VERSION = "N/A"
var gomu *Gomu
// Gomu is the application from tview
type Gomu struct {
app *tview.Application
playingBar *PlayingBar
queue *Queue
playlist *Playlist
player *player.Player
pages *tview.Pages
colors *Colors
command Command
// popups is used to manage focus between popups and panels
popups Stack
prevPanel Panel
panels []Panel
args Args
anko *anko.Anko
hook *hook.EventHook
}
// Creates new instance of gomu with default values
func newGomu() *Gomu {
gomu := &Gomu{
command: newCommand(),
anko: anko.NewAnko(),
hook: hook.NewEventHook(),
}
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, args Args) {
g.app = app
g.playingBar = newPlayingBar()
g.queue = newQueue()
g.playlist = newPlaylist(args)
g.player = player.New(g.anko.GetInt("General.volume"))
g.pages = tview.NewPages()
g.panels = []Panel{g.playlist, g.queue, g.playingBar}
}
// Cycle between panels
func (g *Gomu) cyclePanels() Panel {
var anyChildHasFocus bool
for i, child := range g.panels {
if child.HasFocus() {
anyChildHasFocus = true
var nextChild Panel
// 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]
}
g.setFocusPanel(nextChild)
g.prevPanel = nextChild
return nextChild
}
}
first := g.panels[0]
if !anyChildHasFocus {
g.setFocusPanel(first)
}
g.prevPanel = first
return first
}
func (g *Gomu) cyclePanels2() Panel {
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
}
}
// Changes title and border color when focusing panel
// and changes color of the previous panel as well
func (g *Gomu) setFocusPanel(panel Panel) {
g.app.SetFocus(panel.(tview.Primitive))
panel.SetBorderColor(g.colors.accent)
panel.SetTitleColor(g.colors.accent)
if g.prevPanel == nil {
return
}
if g.prevPanel != panel {
g.setUnfocusPanel(g.prevPanel)
}
}
// Removes the color of the given panel
func (g *Gomu) setUnfocusPanel(panel Panel) {
g.prevPanel.SetBorderColor(g.colors.foreground)
g.prevPanel.SetTitleColor(g.colors.foreground)
}
// Quit the application and do the neccessary clean up
func (g *Gomu) quit(args Args) error {
if !*args.empty {
err := gomu.queue.saveQueue()
if err != nil {
return tracerr.Wrap(err)
}
}
gomu.app.Stop()
return nil
}