widget.NewMultiLineEntry(): 如何滚动到底部?

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

widget.NewMultiLineEntry(): How to scroll to the bottom?

问题

我正在使用widget.NewMultiLineEntry()在我的GUI应用程序中创建一个多行文本输入小部件。我正在寻找一种在添加新内容时自动或手动滚动到小部件底部的方法。我该如何实现这种行为?这种行为是否可能?

logsBox := widget.NewMultiLineEntry()
logsBox.Wrapping = fyne.TextTruncate
logsBox.SetMinRowsVisible(3)
logsBox.SetPlaceHolder("等待日志...")
logsBox.OnChanged = func(newMsg string) {
	// ...
}

logsBox.SetText("第一行\n第二行\n第三行\n第四行")
// TODO: 如何手动滚动到底部?
英文:

I'm using widget.NewMultiLineEntry() in my GUI application to create a multi-line text entry widget. I'm looking for a way to automatically or manually scroll to the bottom of the widget whenever new content is added. How can I achieve this behavior? Is it even possible?

logsBox := widget.NewMultiLineEntry()
logsBox.Wrapping = fyne.TextTruncate
logsBox.SetMinRowsVisible(3)
logsBox.SetPlaceHolder("Waiting for logs...")
logsBox.OnChanged = func(newMsg string) {
	// ...
}

logsBox.SetText("Row 1\nRow 2\nRow 3\nRow 4")
// TODO: Scroll to the bottom manually somehow?

答案1

得分: 1

看起来我们可以使用CursorRow属性来解决这个问题:

focusedItem := logsWindow.Canvas().Focused()
// 如果用户没有聚焦在文本区域上,则滚动到末尾
if focusedItem == nil || focusedItem != logsBox {
    logsBox.CursorRow = len(logsBox.Text) - 1 // 将光标设置到末尾
}
英文:

Looks like we can use CursorRow property to solve this:

focusedItem := logsWindow.Canvas().Focused()
// If the user is not focused on the text area then scroll to the end
if focusedItem == nil || focusedItem != logsBox {
    logsBox.CursorRow = len(logsBox.Text) - 1 // Sets the cursor to the end
}

huangapple
  • 本文由 发表于 2023年2月14日 08:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442455.html
匿名

发表评论

匿名网友

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

确定