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
|
package main
import (
"context"
"image/color"
"log"
"net"
"os"
"sync"
"gioui.org/app"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"xengineering.eu/soundbox"
)
func main() {
config, err := loadConfig()
if err != nil {
log.Fatal(err)
}
ui := NewUi(config)
go func() {
err := ui.Run()
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
type State struct {
sync.Mutex
Config GlobalConfig
Theme *material.Theme
Title string
UrlEditor widget.Editor
PlayPauseButton widget.Clickable
PlayPauseButtonText string
PlayerContext context.Context
PlayerCancel context.CancelFunc
}
type Ui struct {
Window *app.Window
State State
}
func NewUi(config GlobalConfig) *Ui {
ui := Ui{}
ui.State.Config = config
ui.State.Theme = material.NewTheme()
ui.Window = new(app.Window)
ui.Window.Option(app.Title("soundbox app"))
ui.State.Title = "soundbox"
ui.State.PlayPauseButtonText = "Play"
return &ui
}
func (ui *Ui) Cleanup() {
ui.State.Lock()
defer ui.State.Unlock()
if ui.State.PlayerCancel != nil {
ui.State.PlayerCancel()
}
}
func (ui *Ui) Run() error {
defer ui.Cleanup()
var ops op.Ops
for {
switch e := ui.Window.Event().(type) {
case app.DestroyEvent:
return e.Err
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
ui.HandleInputs(gtx)
ui.Layout(gtx)
e.Frame(gtx.Ops)
}
}
}
func (ui *Ui) HandleInputs(gtx layout.Context) {
ui.State.Lock()
defer ui.State.Unlock()
if ui.State.PlayPauseButton.Clicked(gtx) {
if ui.State.UrlEditor.ReadOnly {
ui.State.PlayerCancel()
} else {
ui.State.PlayerContext, ui.State.PlayerCancel = context.WithCancel(context.Background())
var targets []net.HardwareAddr
for _, entry := range ui.State.Config.Soundboxes {
targets = append(targets, net.HardwareAddr(entry.Mac))
}
go play(ui.State.PlayerContext, ui.State.UrlEditor.Text(), targets, ui)
}
}
}
func (ui *Ui) Layout(gtx layout.Context) layout.Dimensions {
ui.State.Lock()
defer ui.State.Unlock()
inset := layout.UniformInset(unit.Dp(10))
flex := layout.Flex{
Axis: layout.Vertical,
Spacing: layout.SpaceEnd,
}
h1 := material.H1(ui.State.Theme, ui.State.Title)
h1.Color = color.NRGBA{R: 88, G: 88, B: 88, A: 255}
h1.Alignment = text.Middle
editor := material.Editor(ui.State.Theme, &ui.State.UrlEditor, "Audio stream URL")
editor.Editor.Alignment = text.Middle
button := material.Button(ui.State.Theme, &ui.State.PlayPauseButton, ui.State.PlayPauseButtonText)
return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return flex.Layout(gtx,
layout.Rigid(h1.Layout),
layout.Rigid(layout.Spacer{Height: unit.Dp(25)}.Layout),
layout.Rigid(editor.Layout),
layout.Rigid(layout.Spacer{Height: unit.Dp(25)}.Layout),
layout.Rigid(button.Layout),
)
})
}
func play(ctx context.Context, url string, targets []net.HardwareAddr, ui *Ui) {
setPlayingState := func(isPlaying bool) {
ui.State.Lock()
defer ui.Window.Invalidate()
defer ui.State.Unlock()
ui.State.UrlEditor.ReadOnly = isPlaying
if isPlaying {
ui.State.PlayPauseButtonText = "Stop"
} else {
ui.State.PlayPauseButtonText = "Play"
}
}
setPlayingState(true)
defer setPlayingState(false)
err := soundbox.StreamURLContext(ctx, url, targets)
if err != nil {
log.Println(err)
}
}
|