complete scrobble settings implementation

This commit is contained in:
Drew Weymouth
2023-04-01 09:29:50 -07:00
parent 19202dc0e8
commit 7aaa82e52a
+94 -25
View File
@@ -1,6 +1,8 @@
package dialogs package dialogs
import ( import (
"math"
"strconv"
"supersonic/backend" "supersonic/backend"
"supersonic/ui/widgets" "supersonic/ui/widgets"
"unicode" "unicode"
@@ -30,32 +32,9 @@ func NewSettingsDialog(config *backend.Config) *SettingsDialog {
s := &SettingsDialog{config: config} s := &SettingsDialog{config: config}
s.ExtendBaseWidget(s) s.ExtendBaseWidget(s)
replayGainSelect := widget.NewSelect([]string{"None", "Album", "Track"}, nil)
percentEntry := widgets.NewTextRestrictedEntry(func(text string, r rune) bool {
return unicode.IsDigit(r) && len(text) < 2
})
percentEntry.SetMinCharWidth(2)
tabs := container.NewAppTabs( tabs := container.NewAppTabs(
container.NewTabItem("General", container.NewVBox( s.createGeneralTab(),
widget.NewRichText(&widget.TextSegment{Text: "Scrobbling", Style: boldStyle}), s.createPlaybackTab(),
widget.NewCheckWithData("Send playback statistics to server", binding.BindBool(&config.Scrobbling.Enabled)),
container.NewHBox(
widget.NewLabel("Scrobble when"),
percentEntry,
widget.NewLabel("percent of track is played"),
),
)),
container.NewTabItem("Playback", container.NewVBox(
widget.NewRichText(&widget.TextSegment{Text: "ReplayGain", Style: boldStyle}),
container.New(layout.NewFormLayout(),
widget.NewLabel("ReplayGain mode"), replayGainSelect,
widget.NewLabel("Prevent clipping"), widget.NewCheckWithData("", binding.BindBool(&config.ReplayGain.PreventClipping)),
),
widget.NewSeparator(),
widget.NewCheckWithData("Audio exclusive mode", binding.BindBool(&config.LocalPlayback.AudioExclusive)),
)),
) )
s.content = container.NewVBox(tabs, widget.NewSeparator(), s.content = container.NewVBox(tabs, widget.NewSeparator(),
container.NewHBox(layout.NewSpacer(), widget.NewButton("Close", func() { container.NewHBox(layout.NewSpacer(), widget.NewButton("Close", func() {
@@ -67,6 +46,96 @@ func NewSettingsDialog(config *backend.Config) *SettingsDialog {
return s return s
} }
func (s *SettingsDialog) createGeneralTab() *container.TabItem {
twoDigitValidator := func(text string, r rune) bool {
return unicode.IsDigit(r) && len(text) < 2
}
percentEntry := widgets.NewTextRestrictedEntry(twoDigitValidator)
percentEntry.SetMinCharWidth(2)
percentEntry.OnChanged = func(str string) {
if i, err := strconv.Atoi(str); err == nil {
s.config.Scrobbling.ThresholdPercent = i
}
}
percentEntry.Text = strconv.Itoa(s.config.Scrobbling.ThresholdPercent)
durationEntry := widgets.NewTextRestrictedEntry(twoDigitValidator)
durationEntry.SetMinCharWidth(2)
durationEntry.OnChanged = func(str string) {
if i, err := strconv.Atoi(str); err == nil {
s.config.Scrobbling.ThresholdTimeSeconds = i * 60
}
}
if secs := s.config.Scrobbling.ThresholdTimeSeconds; secs >= 0 {
val := int(math.Round(float64(secs) / 60.))
durationEntry.Text = strconv.Itoa(val)
}
lastScrobbleText := durationEntry.Text
if lastScrobbleText == "" {
lastScrobbleText = "4" // default scrobble minutes
}
durationEnabled := widget.NewCheck("or when", func(checked bool) {
if !checked {
s.config.Scrobbling.ThresholdTimeSeconds = -1
lastScrobbleText = durationEntry.Text
durationEntry.Text = ""
durationEntry.Disable()
} else {
durationEntry.Text = lastScrobbleText
durationEntry.Enable()
durationEntry.Refresh()
durationEntry.OnChanged(durationEntry.Text)
}
})
durationEnabled.Checked = s.config.Scrobbling.ThresholdTimeSeconds >= 0
scrobbleEnabled := widget.NewCheckWithData("Send playback statistics to server", binding.BindBool(&s.config.Scrobbling.Enabled))
scrobbleEnabled.OnChanged = func(checked bool) {
if !checked {
percentEntry.Disable()
durationEnabled.Disable()
durationEntry.Disable()
} else {
percentEntry.Enable()
durationEnabled.Enable()
if durationEnabled.Checked {
durationEntry.Enable()
}
}
}
return container.NewTabItem("General", container.NewVBox(
widget.NewRichText(&widget.TextSegment{Text: "Scrobbling", Style: boldStyle}),
scrobbleEnabled,
container.NewHBox(
widget.NewLabel("Scrobble when"),
percentEntry,
widget.NewLabel("percent of track is played"),
),
container.NewHBox(
durationEnabled,
durationEntry,
widget.NewLabel("minutes of track have been played"),
),
))
}
func (s *SettingsDialog) createPlaybackTab() *container.TabItem {
replayGainSelect := widget.NewSelect([]string{"None", "Album", "Track"}, nil)
return container.NewTabItem("Playback", container.NewVBox(
widget.NewRichText(&widget.TextSegment{Text: "ReplayGain", Style: boldStyle}),
container.New(layout.NewFormLayout(),
widget.NewLabel("ReplayGain mode"), replayGainSelect,
widget.NewLabel("Prevent clipping"), widget.NewCheckWithData("", binding.BindBool(&s.config.ReplayGain.PreventClipping)),
),
widget.NewSeparator(),
widget.NewCheckWithData("Audio exclusive mode", binding.BindBool(&s.config.LocalPlayback.AudioExclusive)),
))
}
func (s *SettingsDialog) CreateRenderer() fyne.WidgetRenderer { func (s *SettingsDialog) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(s.content) return widget.NewSimpleRenderer(s.content)
} }