diff options
author | xengineering <me@xengineering.eu> | 2024-10-08 18:05:31 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-10-08 20:03:20 +0200 |
commit | 9a31ee11242c8827eca1330a568362904c29d890 (patch) | |
tree | bbc11de3cdad809f1fd599c2aecbedfec6899e7b | |
parent | e678c6edd21ff50a93d7635575c8c135f2196f0f (diff) | |
download | soundbox-app-9a31ee11242c8827eca1330a568362904c29d890.tar soundbox-app-9a31ee11242c8827eca1330a568362904c29d890.tar.zst soundbox-app-9a31ee11242c8827eca1330a568362904c29d890.zip |
Select URL with radio buttons based on config
This is way more convenient than pasting a URL into an editor.
-rw-r--r-- | main.go | 50 |
1 files changed, 31 insertions, 19 deletions
@@ -40,8 +40,9 @@ type State struct { sync.Mutex Config Config Theme *material.Theme + IsPlaying bool Title string - UrlEditor widget.Editor + UrlSelector widget.Enum PlayPauseButton widget.Clickable PlayPauseButtonText string PlayerContext context.Context @@ -58,6 +59,8 @@ func NewUi(config Config) *Ui { ui.State.Config = config + ui.State.IsPlaying = false + ui.State.Theme = material.NewTheme() ui.Window = new(app.Window) @@ -99,15 +102,19 @@ func (ui *Ui) HandleInputs(gtx layout.Context) { defer ui.State.Unlock() if ui.State.PlayPauseButton.Clicked(gtx) { - if ui.State.UrlEditor.ReadOnly { + if ui.State.IsPlaying { 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)) + if ui.State.UrlSelector.Value == "" { + log.Println("A URL has to be selected.") + } 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.UrlSelector.Value, targets, ui) } - go play(ui.State.PlayerContext, ui.State.UrlEditor.Text(), targets, ui) } } } @@ -127,19 +134,24 @@ func (ui *Ui) Layout(gtx layout.Context) layout.Dimensions { 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) + entries := []layout.FlexChild{ + layout.Rigid(h1.Layout), + layout.Rigid(layout.Spacer{Height: unit.Dp(25)}.Layout), + } + + for _, url := range ui.State.Config.URLs { + entries = append(entries, layout.Rigid(material.RadioButton( + ui.State.Theme, &ui.State.UrlSelector, + url.Url, url.Name).Layout)) + } + + entries = append(entries, layout.Rigid(layout.Spacer{Height: unit.Dp(25)}.Layout)) + entries = append(entries, layout.Rigid(button.Layout)) + 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), - ) + return flex.Layout(gtx, entries...) }) } @@ -149,8 +161,8 @@ func play(ctx context.Context, url string, targets []net.HardwareAddr, ui *Ui) { defer ui.Window.Invalidate() defer ui.State.Unlock() - ui.State.UrlEditor.ReadOnly = isPlaying - if isPlaying { + ui.State.IsPlaying = isPlaying + if ui.State.IsPlaying { ui.State.PlayPauseButtonText = "Stop" } else { ui.State.PlayPauseButtonText = "Play" |