forked from noisetorch/NoiseTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
284 lines (249 loc) · 6.84 KB
/
ui.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"fmt"
"image/color"
"log"
"os/exec"
"time"
"github.com/aarzilli/nucular"
"github.com/aarzilli/nucular/label"
"github.com/lawl/pulseaudio"
)
type ntcontext struct {
inputList []input
noiseSupressorState int
paClient *pulseaudio.Client
librnnoise string
sourceListColdWidthIndex int
useBuiltinRNNoise bool
config *config
loadingScreen bool
licenseScreen bool
versionScreen bool
licenseTextArea nucular.TextEditor
masterWindow *nucular.MasterWindow
update updateui
reloadRequired bool
}
var green = color.RGBA{34, 187, 69, 255}
var red = color.RGBA{255, 70, 70, 255}
var orange = color.RGBA{255, 140, 0, 255}
func updatefn(ctx *ntcontext, w *nucular.Window) {
if !ctx.paClient.Connected() {
connectScreen(ctx, w)
return
}
if ctx.loadingScreen {
loadingScreen(ctx, w)
return
}
if ctx.licenseScreen {
licenseScreen(ctx, w)
return
}
if ctx.versionScreen {
versionScreen(ctx, w)
return
}
w.MenubarBegin()
w.Row(10).Dynamic(2)
if w := w.Menu(label.TA("About", "LC"), 120, nil); w != nil {
w.Row(10).Dynamic(1)
if w.MenuItem(label.T("Licenses")) {
ctx.licenseScreen = true
}
w.Row(10).Dynamic(1)
if w.MenuItem(label.T("Source code")) {
exec.Command("xdg-open", "https://github.com/lawl/NoiseTorch").Run()
}
if w.MenuItem(label.T("Version")) {
ctx.versionScreen = true
}
}
if w := w.Menu(label.TA("Support NoiseTorch on PATREON", "RC"), 120, nil); w != nil {
exec.Command("xdg-open", "https://patreon.com/lawl").Run()
w.Close()
}
w.MenubarEnd()
w.Row(15).Dynamic(1)
if ctx.noiseSupressorState == loaded {
w.LabelColored("NoiseTorch active", "RC", green)
} else if ctx.noiseSupressorState == unloaded {
w.LabelColored("NoiseTorch inactive", "RC", red)
} else if ctx.noiseSupressorState == inconsistent {
w.LabelColored("Inconsistent state, please unload first.", "RC", orange)
}
if ctx.update.available && !ctx.update.triggered {
w.Row(20).Ratio(0.9, 0.1)
w.LabelColored("Update available! Click to install version: "+ctx.update.serverVersion, "LC", green)
if w.ButtonText("Update") {
ctx.update.triggered = true
go update(ctx)
(*ctx.masterWindow).Changed()
}
}
if ctx.update.triggered {
w.Row(20).Dynamic(1)
w.Label(ctx.update.updatingText, "CC")
}
if w.TreePush(nucular.TreeTab, "Settings", true) {
w.Row(15).Dynamic(2)
if w.CheckboxText("Display Monitor Sources", &ctx.config.DisplayMonitorSources) {
ctx.sourceListColdWidthIndex++ //recompute the with because of new elements
go writeConfig(ctx.config)
}
w.Spacing(1)
w.Row(25).Ratio(0.5, 0.45, 0.05)
w.Label("Voice Activation Threshold", "LC")
if w.Input().Mouse.HoveringRect(w.LastWidgetBounds) {
w.Tooltip("If you have a decent microphone, you can usually turn this all the way up.")
}
if w.SliderInt(0, &ctx.config.Threshold, 95, 1) {
go writeConfig(ctx.config)
ctx.reloadRequired = true
}
w.Label(fmt.Sprintf("%d%%", ctx.config.Threshold), "RC")
if ctx.reloadRequired {
w.Row(20).Dynamic(1)
w.LabelColored("Reloading NoiseTorch is required to apply these changes.", "LC", orange)
}
w.TreePop()
}
if w.TreePush(nucular.TreeTab, "Select Device", true) {
w.Row(15).Dynamic(1)
w.Label("Select an input device below:", "LC")
for i := range ctx.inputList {
el := &ctx.inputList[i]
if el.isMonitor && !ctx.config.DisplayMonitorSources {
continue
}
w.Row(15).Static()
w.LayoutFitWidth(0, 0)
if w.CheckboxText("", &el.checked) {
ensureOnlyOneInputSelected(&ctx.inputList, el)
}
w.LayoutFitWidth(ctx.sourceListColdWidthIndex, 0)
if el.dynamicLatency {
w.Label(el.Name, "LC")
} else {
w.LabelColored("(incompatible?) "+el.Name, "LC", orange)
}
}
w.Row(30).Dynamic(1)
w.Spacing(1)
w.Row(25).Dynamic(2)
if ctx.noiseSupressorState != unloaded {
if w.ButtonText("Unload NoiseTorch") {
ctx.loadingScreen = true
ctx.reloadRequired = false
go func() { // don't block the UI thread, just display a working screen so user can't run multiple loads/unloads
if err := unloadSupressor(ctx.paClient); err != nil {
log.Println(err)
}
//wait until PA reports it has actually loaded it, timeout at 10s
for i := 0; i < 20; i++ {
if supressorState(ctx.paClient) != unloaded {
time.Sleep(time.Millisecond * 500)
}
}
ctx.loadingScreen = false
(*ctx.masterWindow).Changed()
}()
}
} else {
w.Spacing(1)
}
txt := "Load NoiseTorch"
if ctx.noiseSupressorState == loaded {
txt = "Reload NoiseTorch"
}
if inp, ok := inputSelection(ctx); ok && ctx.noiseSupressorState != inconsistent {
if w.ButtonText(txt) {
ctx.loadingScreen = true
ctx.reloadRequired = false
go func() { // don't block the UI thread, just display a working screen so user can't run multiple loads/unloads
if ctx.noiseSupressorState == loaded {
if err := unloadSupressor(ctx.paClient); err != nil {
log.Println(err)
}
}
if err := loadSupressor(ctx, inp); err != nil {
log.Println(err)
}
//wait until PA reports it has actually loaded it, timeout at 10s
for i := 0; i < 20; i++ {
if supressorState(ctx.paClient) != loaded {
time.Sleep(time.Millisecond * 500)
}
}
ctx.loadingScreen = false
(*ctx.masterWindow).Changed()
}()
}
} else {
w.Spacing(1)
}
w.TreePop()
}
}
func ensureOnlyOneInputSelected(inps *[]input, current *input) {
if current.checked != true {
return
}
for i := range *inps {
el := &(*inps)[i]
el.checked = false
}
current.checked = true
}
func inputSelection(ctx *ntcontext) (input, bool) {
for _, in := range ctx.inputList {
if in.checked {
return in, true
}
}
return input{}, false
}
func loadingScreen(ctx *ntcontext, w *nucular.Window) {
w.Row(50).Dynamic(1)
w.Label("Working...", "CB")
w.Row(50).Dynamic(1)
w.Label("(this may take a few seconds)", "CB")
}
func licenseScreen(ctx *ntcontext, w *nucular.Window) {
w.Row(255).Dynamic(1)
field := &ctx.licenseTextArea
field.Flags |= nucular.EditMultiline
if len(field.Buffer) < 1 {
field.Buffer = []rune(licenseString)
}
field.Edit(w)
w.Row(20).Dynamic(2)
w.Spacing(1)
if w.ButtonText("OK") {
ctx.licenseScreen = false
}
}
func versionScreen(ctx *ntcontext, w *nucular.Window) {
w.Row(50).Dynamic(1)
w.Label("Version", "CB")
w.Row(50).Dynamic(1)
w.Label(version, "CB")
w.Row(50).Dynamic(1)
w.Spacing(1)
w.Row(20).Dynamic(2)
w.Spacing(1)
if w.ButtonText("OK") {
ctx.versionScreen = false
}
}
func connectScreen(ctx *ntcontext, w *nucular.Window) {
w.Row(50).Dynamic(1)
w.Label("Connecting to pulseaudio...", "CB")
}
func resetUI(ctx *ntcontext) {
ctx.loadingScreen = false
if ctx.masterWindow != nil {
(*ctx.masterWindow).Changed()
}
}