I'm trying to let users record audio. I want a 'record' button when they aren't recording, and when they are recording to change it to a 'stop recording' button. Furthermore, I expected this to work, but both buttons always show on the toolbar:
var startRecording, stopRecordingFunc func()
recordButton := widget.NewToolbarAction(theme.MediaRecordIcon(), startRecording)
stopButton := widget.NewToolbarAction(theme.MediaStopIcon(), stopRecordingFunc)
startRecording = func() {
if !recording {
recording = true
stopRecording = make(chan struct{})
recordWg.Add(1)
go func() {
recordAudio("output.wav", stopRecording, &recordWg)
recording = false
recordButton.ToolbarObject().Show()
stopButton.ToolbarObject().Hide()
}()
recordButton.ToolbarObject().Hide()
stopButton.ToolbarObject().Show()
}
}
stopRecordingFunc = func() {
if recording {
close(stopRecording)
recordWg.Wait()
recording = false
recordButton.ToolbarObject().Show()
stopButton.ToolbarObject().Hide()
}
}
recordButton.OnActivated = startRecording
stopButton.OnActivated = stopRecordingFunc
toolbar := widget.NewToolbar(recordButton, stopButton)
stopButton.ToolbarObject().Hide()
I've tried adding calls to Refresh()
but it makes no difference.
How can I conditionally show/hide toolbar buttons?
When you call .ToolbarObject()
it is creating a new instance - which is not in the toolbar at all.
To manipulate the contents of a toolbar you need to use the toolbar APIs, or those exposed directly on the ToolbarAction
.
From your code it looks like what you would be best using is SetIcon
to change the appearance.