-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
auxcontrols.go
137 lines (115 loc) · 2.79 KB
/
auxcontrols.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
package widgets
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
// The "aux" controls for playback, positioned to the right
// of the BottomPanel. Currently only volume control.
type AuxControls struct {
widget.BaseWidget
VolumeControl *VolumeControl
container *fyne.Container
}
func NewAuxControls(initialVolume int) *AuxControls {
a := &AuxControls{
VolumeControl: NewVolumeControl(initialVolume),
}
a.container = container.NewHBox(layout.NewSpacer(), a.VolumeControl)
return a
}
func (a *AuxControls) CreateRenderer() fyne.WidgetRenderer {
a.ExtendBaseWidget(a)
return widget.NewSimpleRenderer(a.container)
}
type volumeSlider struct {
widget.Slider
Width float32
}
func NewVolumeSlider(width float32) *volumeSlider {
v := &volumeSlider{
Slider: widget.Slider{
Min: 0,
Max: 100,
Step: 1,
Orientation: widget.Horizontal,
Value: 100,
},
Width: width,
}
v.ExtendBaseWidget(v)
return v
}
func (v *volumeSlider) MinSize() fyne.Size {
h := v.Slider.MinSize().Height
return fyne.NewSize(v.Width, h)
}
func (v *volumeSlider) Scrolled(e *fyne.ScrollEvent) {
v.SetValue(v.Value + float64(0.5*e.Scrolled.DY))
}
type VolumeControl struct {
widget.BaseWidget
icon *TappableIcon
slider *volumeSlider
OnVolumeChanged func(int)
muted bool
lastVol int
container *fyne.Container
}
func NewVolumeControl(initialVol int) *VolumeControl {
v := &VolumeControl{}
v.ExtendBaseWidget(v)
v.icon = NewTappbaleIcon(theme.VolumeUpIcon())
v.icon.OnTapped = v.toggleMute
v.slider = NewVolumeSlider(100)
v.lastVol = initialVol
v.slider.Step = 1
v.slider.Orientation = widget.Horizontal
v.slider.Value = float64(v.lastVol)
v.slider.OnChanged = v.onChanged
v.container = container.NewHBox(container.NewCenter(v.icon), v.slider)
return v
}
func (v *VolumeControl) onChanged(volume float64) {
vol := int(volume)
v.lastVol = vol
v.muted = false
v.updateIconForVolume(vol)
if v.OnVolumeChanged != nil {
v.OnVolumeChanged(vol)
}
}
func (v *VolumeControl) toggleMute() {
if !v.muted {
v.muted = true
v.lastVol = int(v.slider.Value)
v.SetVolume(0)
} else {
v.muted = false
v.SetVolume(v.lastVol)
}
}
func (v *VolumeControl) CreateRenderer() fyne.WidgetRenderer {
v.ExtendBaseWidget(v)
return widget.NewSimpleRenderer(v.container)
}
func (v *VolumeControl) SetVolume(vol int) {
v.slider.Value = float64(vol)
v.slider.Refresh()
v.updateIconForVolume(vol)
if v.OnVolumeChanged != nil {
v.OnVolumeChanged(vol)
}
}
func (v *VolumeControl) updateIconForVolume(vol int) {
if vol <= 0 {
v.icon.Resource = theme.VolumeMuteIcon()
} else if vol < 50 {
v.icon.Resource = theme.VolumeDownIcon()
} else {
v.icon.Resource = theme.VolumeUpIcon()
}
v.icon.Refresh()
}