在Fyne中如何有条件地显示/隐藏工具栏按钮?

huangapple go评论78阅读模式
英文:

How to conditionally show/hide toolbar buttons in Fyne?

问题

我正在尝试让用户录制音频。当他们没有录制时,我想要一个"录制"按钮,当他们正在录制时,将其更改为"停止录制"按钮。此外,我期望这样做可以起作用,但是两个按钮始终都显示在工具栏上:

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()

我尝试添加了Refresh()的调用,但没有任何效果。

如何根据条件显示/隐藏工具栏按钮?

英文:

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?

答案1

得分: 0

当你调用.ToolbarObject()时,它会创建一个新的实例,该实例并不在工具栏中。
要操作工具栏的内容,你需要使用工具栏的API,或者直接使用ToolbarAction上暴露的API。

从你的代码来看,最好使用SetIcon来改变外观。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月14日 15:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76012531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定