summaryrefslogtreecommitdiff
path: root/src/rhlibrary/player_widget.go
blob: 41d2ddfc89298ab2ed3c575f24ec3356df5d9b0b (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
//  rhlibrary
//
//  The Radio Helsinki Rivendell Library
//
//
//  Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of rhlibrary.
//
//  rhlibrary is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  rhlibrary is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with rhlibrary. If not, see <http://www.gnu.org/licenses/>.
//

package main

import (
	"time"

	"code.helsinki.at/rhrd-go/player"
	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
)

var (
	playIcon  *gtk.Image = nil
	pauseIcon *gtk.Image = nil
)

func addPlayPauseButton(box *gtk.Box, p *player.Player) (err error) {
	var btn *gtk.Button
	if btn, err = gtk.ButtonNew(); err != nil {
		return
	}
	if playIcon, err = gtk.ImageNewFromIconName("media-playback-start", gtk.ICON_SIZE_LARGE_TOOLBAR); err != nil {
		return
	}
	if pauseIcon, err = gtk.ImageNewFromIconName("media-playback-pause", gtk.ICON_SIZE_LARGE_TOOLBAR); err != nil {
		return
	}
	btn.SetSizeRequest(42, 42)
	btn.SetImage(playIcon)
	btn.SetSensitive(false)

	btn.Connect("clicked", func(_ *gtk.Button) {
		if err := p.PlayPause(); err != nil {
			rhdl.Println(err) // TODO: feedback at GUI?
		}
	})

	p.AddStateChangeHandler(func(state player.State, _ interface{}) bool {
		glib.IdleAdd(func() {
			switch state {
			case player.IDLE:
				btn.SetImage(playIcon)
				btn.SetSensitive(false)
			case player.PAUSED:
				btn.SetImage(playIcon)
				btn.SetSensitive(true)
			case player.PLAYING:
				btn.SetImage(pauseIcon)
				btn.SetSensitive(true)
			}
		})
		return true
	}, nil)

	box.PackStart(btn, false, false, 0)
	return
}

func addStopButton(box *gtk.Box, p *player.Player) (err error) {
	var btn *gtk.Button
	if btn, err = gtk.ButtonNewFromIconName("media-playback-stop", gtk.ICON_SIZE_LARGE_TOOLBAR); err != nil {
		return
	}
	btn.SetSizeRequest(42, 42)
	btn.SetSensitive(false)

	btn.Connect("clicked", func(_ *gtk.Button) {
		if err := p.Stop(); err != nil {
			rhdl.Println(err) // TODO: feedback at GUI?
		}
	})

	p.AddStateChangeHandler(func(state player.State, _ interface{}) bool {
		glib.IdleAdd(func() {
			switch state {
			case player.IDLE:
				btn.SetSensitive(false)
			case player.PAUSED:
				btn.SetSensitive(true)
			case player.PLAYING:
				btn.SetSensitive(true)
			}
		})
		return true
	}, nil)

	box.PackStart(btn, false, false, 0)
	return
}

func addButtons(grid *gtk.Grid, p *player.Player) (err error) {
	var box *gtk.Box
	if box, err = gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 2); err != nil {
		return
	}

	if err = addPlayPauseButton(box, p); err != nil {
		return
	}
	if err = addStopButton(box, p); err != nil {
		return
	}

	grid.Attach(box, 1, 1, 1, 1)
	return
}

func addMeter(grid *gtk.Grid, p *player.Player) error {
	widget, err := getVUMeterWidget(p)
	if err != nil {
		return err
	}

	grid.Attach(widget, 2, 1, 1, 1)
	return nil
}

func getPlayTimeString(duration, pos time.Duration, countdown bool) string {
	t := time.Unix(0, 0).UTC()
	if countdown {
		return t.Add(duration).Add(-1 * pos).Format("-15:04:05.0")
	}
	return t.Add(pos).Format(" 15:04:05.0")
}

func addPlayTime(grid *gtk.Grid, p *player.Player) (err error) {
	var btn *gtk.Button
	if btn, err = gtk.ButtonNew(); err != nil {
		return err
	}

	countdown := false
	duration := time.Duration(0)
	pos := time.Duration(0)
	p.AddUpdateHandler(func(d time.Duration, p time.Duration, _ player.Meter, _ interface{}) bool {
		glib.IdleAdd(func() {
			duration = d
			pos = p
			btn.SetLabel(getPlayTimeString(duration, pos, countdown))
		})
		return true
	}, nil)

	btn.SetSizeRequest(128, 42)
	btn.SetRelief(gtk.RELIEF_NONE)
	btn.SetFocusOnClick(false)
	btn.SetLabel(getPlayTimeString(duration, pos, countdown))

	btn.Connect("clicked", func(_ *gtk.Button) {
		countdown = !countdown
		btn.SetLabel(getPlayTimeString(duration, pos, countdown))
	})
	if err = setCssStyle(&btn.Widget, ".button { border: 1px solid; border-color: #a1a1a1; font-weight: bold; font-size: 1.3em; }"); err != nil {
		return
	}

	grid.Attach(btn, 3, 1, 1, 1)
	return
}

func addScrubber(grid *gtk.Grid, p *player.Player) (err error) {
	var scale *gtk.Scale
	if scale, err = gtk.ScaleNew(gtk.ORIENTATION_HORIZONTAL, nil); err != nil {
		return
	}
	if err = scale.SetProperty("draw-value", false); err != nil {
		return
	}
	scale.SetRange(0, 1)
	scale.SetValue(0)

	scale.Connect("change-value", func(s *gtk.Scale, _ interface{}, pos float64) {
		if err := p.Seek(pos); err != nil {
			rhdl.Println(err) // TODO: feedback at GUI?
		}
	})
	scale.SetSensitive(false)

	p.AddUpdateHandler(func(duration time.Duration, pos time.Duration, _ player.Meter, _ interface{}) bool {
		glib.IdleAdd(func() {
			if duration > 0 {
				scale.SetValue(float64(pos) / float64(duration))
			} else {
				scale.SetValue(0)
			}
		})
		return true
	}, nil)

	p.AddStateChangeHandler(func(state player.State, _ interface{}) bool {
		glib.IdleAdd(func() {
			switch state {
			case player.IDLE:
				scale.SetSensitive(false)
			case player.PAUSED:
				scale.SetSensitive(true)
			case player.PLAYING:
				scale.SetSensitive(true)
			}
		})
		return true
	}, nil)

	grid.Attach(scale, 1, 2, 3, 1)
	return
}

func getPlayerWidget(p *player.Player) (gtk.IWidget, error) {
	var err error
	var grid *gtk.Grid
	if grid, err = gtk.GridNew(); err != nil {
		return nil, err
	}
	grid.SetOrientation(gtk.ORIENTATION_HORIZONTAL)
	grid.SetRowSpacing(8)
	grid.SetColumnSpacing(13)

	if err = addButtons(grid, p); err != nil {
		return nil, err
	}
	if err = addMeter(grid, p); err != nil {
		return nil, err
	}
	if err = addPlayTime(grid, p); err != nil {
		return nil, err
	}
	if err = addScrubber(grid, p); err != nil {
		return nil, err
	}

	grid.SetHAlign(gtk.ALIGN_CENTER)
	return grid, nil
}