-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.go
More file actions
244 lines (225 loc) · 7.37 KB
/
layout.go
File metadata and controls
244 lines (225 loc) · 7.37 KB
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
package main
import (
"gioui.org/app"
_ "gioui.org/font/gofont"
"gioui.org/font/opentype"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"gioui.org/x/colorpicker"
"gioui.org/x/explorer"
"golang.org/x/image/font/gofont/gomono"
"image"
"image/color"
"log"
"runtime"
)
var fileDialog *explorer.Explorer
var openButton, backButton, fwdButton, playButton, stopButton widget.Clickable
var progressClickable widget.Clickable
var volumeSlider widget.Float // widget state for the slider
var playbackProgress float32
var isManualSeeking bool
var manualSeekPosition float32
var itemSpacing = unit.Dp(5)
var showDialog widget.Bool
var isHqMode widget.Bool
type C = layout.Context
type D = layout.Dimensions
func init() {
//Parse the GoMono TTF data.
face, err := opentype.Parse(gomono.TTF)
if err != nil {
log.Fatalf("failed to parse GoMono TTF: %v", err)
}
monoCollection := []text.FontFace{ // wrap as a collection
{
Font: face.Font(),
Face: face,
},
}
// TODO: Currently if we set the waveform color inside render dialog we can't type values
// This definitely is not the proper method of setting up the color pickers...
th := material.NewTheme()
th.Shaper = text.NewShaper(text.WithCollection(monoCollection))
th.Fg = color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
// TODO: currently theme visability is poor (!)
ps1 = colorpicker.Picker(th, &mState1, "Left")
ps2 = colorpicker.Picker(th, &mState2, "Right")
ps1.SetColor(waveformColor1)
ps2.SetColor(waveformColor2)
isHqMode.Value = runtime.GOOS != "js" // Default to HQ mode on non-wasm
}
func openFileDialog(w *app.Window) {
if fileDialog == nil {
fileDialog = explorer.NewExplorer(w)
}
// Open file dialog for a single audio file
reader, err := fileDialog.ChooseFile(".wav", ".flac", ".mp3")
if err != nil {
log.Println("Error selecting file:", err)
return
}
eject()
currentReader = reader
play(w) // keep playing with new reader
}
func updateProgressBar(pUnit *playbackUnit) {
playbackProgress = pUnit.getProgressFloat()
}
func resetProgressBar() {
playbackProgress = 0
}
func render(gtx layout.Context, th *material.Theme, e app.FrameEvent) {
// Draw dark gray background.
paint.ColorOp{Color: color.NRGBA{R: 30, G: 30, B: 30, A: 255}}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
// Outer horizontal flex: left for waveform/progress, right for buttons.
layout.Flex{
Axis: layout.Horizontal,
Spacing: layout.SpaceStart,
}.Layout(gtx,
// Left column: waveform on top, progress bar at bottom.
layout.Flexed(1, func(gtx C) D {
return layout.Flex{
Axis: layout.Vertical,
Spacing: layout.SpaceStart,
}.Layout(gtx,
layout.Flexed(1, func(gtx C) D {
return layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return renderWaveform(gtx, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)
}))
}),
layout.Rigid(func(gtx C) D {
if showDialog.Value {
return renderDialog(gtx, th)
}
return layout.Dimensions{}
}),
layout.Rigid(func(gtx C) D { // Mid buttons
return layout.Flex{
Axis: layout.Horizontal,
Spacing: layout.SpaceSides,
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
gtx.Constraints.Max.X = gtx.Dp(150)
return material.Button(th, &openButton, "Open").Layout(gtx)
}),
layout.Rigid(layout.Spacer{Width: itemSpacing}.Layout),
layout.Rigid(func(gtx C) D {
if currentState == Playing {
return material.Button(th, &stopButton, "Stop").Layout(gtx)
}
return material.Button(th, &playButton, "Play").Layout(gtx)
}),
layout.Rigid(layout.Spacer{Width: itemSpacing}.Layout),
layout.Rigid(func(gtx C) D {
return material.Button(th, &backButton, "Back").Layout(gtx)
}),
layout.Rigid(layout.Spacer{Width: itemSpacing}.Layout),
layout.Rigid(func(gtx C) D {
return material.Button(th, &fwdButton, "Forward").Layout(gtx)
}),
layout.Rigid(layout.Spacer{Width: itemSpacing}.Layout),
layout.Rigid(func(gtx C) D {
return material.CheckBox(th, &showDialog, "Options").Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
slider := material.Slider(th, &volumeSlider) // Default value set in Main
gtx.Constraints.Min.X = gtx.Dp(150)
gtx.Constraints.Max.X = gtx.Dp(150)
return slider.Layout(gtx)
}),
)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: unit.Dp(5), Right: unit.Dp(5), Top: unit.Dp(4), Bottom: unit.Dp(4)}.Layout(gtx, func(gtx C) D {
const progressBarHeight = 10
gtx.Constraints.Min.Y = gtx.Dp(progressBarHeight)
gtx.Constraints.Max.Y = gtx.Dp(progressBarHeight)
return progressClickable.Layout(gtx, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
gtx2 := gtx
gtx2.Constraints.Min.Y = gtx.Dp(progressBarHeight)
gtx2.Constraints.Max.Y = gtx.Dp(progressBarHeight)
if isManualSeeking {
return material.ProgressBar(th, manualSeekPosition).Layout(gtx2)
}
return material.ProgressBar(th, playbackProgress).Layout(gtx2)
})
})
})
}),
)
}),
)
e.Frame(gtx.Ops)
}
var mState1 colorpicker.State
var mState2 colorpicker.State
var ps1 colorpicker.PickerStyle
var ps2 colorpicker.PickerStyle
func renderDialog(gtx layout.Context, th *material.Theme) layout.Dimensions {
// Draw a semi-transparent overlay background.
paint.Fill(gtx.Ops, color.NRGBA{R: 0, G: 0, B: 0, A: 180})
const width = 500
const height = 270
// Dialog position offset
return layout.Center.Layout(gtx, func(gtx C) D {
size := image.Pt(gtx.Dp(width), gtx.Dp(height))
rect := clip.RRect{ // Rounded rectangle for the dialog.
Rect: image.Rectangle{Max: size},
SE: gtx.Dp(12), SW: gtx.Dp(12),
NE: gtx.Dp(12), NW: gtx.Dp(12),
}
paint.FillShape(gtx.Ops, th.Bg, rect.Op(gtx.Ops)) // Dialog Background
// Use an inset to provide inner margins.
return layout.Inset{
Top: unit.Dp(20),
Bottom: unit.Dp(20),
Left: unit.Dp(16),
Right: unit.Dp(16),
}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Axis: layout.Vertical,
Spacing: layout.SpaceAround,
Alignment: layout.Middle,
}.Layout(gtx,
layout.Rigid(material.Body1(th, "Waveform Colors:").Layout),
layout.Rigid(layout.Spacer{Height: itemSpacing}.Layout),
// Side x Side color pickers
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
const totalWidth = width - (16 * 2) // dialog width - left/right inset
return layout.Flex{
Axis: layout.Horizontal,
Spacing: layout.SpaceStart,
Alignment: layout.Middle,
}.Layout(gtx,
// First color picker.
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Max.X = gtx.Dp(totalWidth / 2)
dims := ps1.Layout(gtx)
waveformColor1 = ps1.State.Color()
return dims
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Max.X = gtx.Dp(totalWidth / 2)
dims := ps2.Layout(gtx)
waveformColor2 = ps2.State.Color()
return dims
}),
)
}),
layout.Rigid(layout.Spacer{Height: itemSpacing}.Layout),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return material.CheckBox(th, &isHqMode, "HQ Mode").Layout(gtx)
}),
)
})
})
}