Сделайте прокрутку окна в Go с GXUI
Я пытаюсь добавить полосы прокрутки в окна моего приложения в Go с помощью GXUI.
Скажи, у меня есть этот код:
package main
import (
"fmt"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/samples/flags"
"github.com/google/gxui/themes/dark"
)
func appMain(driver gxui.Driver) {
theme := dark.CreateTheme(driver)
window := theme.CreateWindow(800, 600, "Grid")
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(driver.Terminate)
row := theme.CreateLinearLayout()
row.SetDirection(gxui.LeftToRight)
for c := 0; c < 4; c++ {
col := theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
for r := 0; r < 100; r++ {
cell := theme.CreateLabel()
cell.SetText(fmt.Sprintf("%d", r*4+c))
col.AddChild(cell)
}
row.AddChild(col)
}
window.AddChild(row)
}
func main() {
gl.StartDriver(appMain)
}
Когда я запускаю его, я получаю это окно:
Как заставить окно иметь полосу прокрутки, чтобы я мог видеть все строки?
2 ответа
Следующий код использует ScrollLayout для добавления полосы прокрутки к окну. Хитрость заключается в том, чтобы сделать ScrollLayout дочерним элементом окна и сделать следующий виджет (в данном случае LinearLayout) дочерним для ScrollLayout.
package main
import (
"fmt"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/samples/flags"
"github.com/google/gxui/themes/dark"
)
func appMain(driver gxui.Driver) {
theme := dark.CreateTheme(driver)
window := theme.CreateWindow(800, 600, "Grid")
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(driver.Terminate)
sl := theme.CreateScrollLayout()
row := theme.CreateLinearLayout()
row.SetDirection(gxui.LeftToRight)
for c := 0; c < 4; c++ {
col := theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
for r := 0; r < 100; r++ {
cell := theme.CreateLabel()
cell.SetText(fmt.Sprintf("%d", r*4+c))
col.AddChild(cell)
}
row.AddChild(col)
}
sl.SetChild(row)
window.AddChild(sl)
}
func main() {
gl.StartDriver(appMain)
}
Обратите внимание, что мой компьютер вызывал проблемы с отображением, когда я увеличивал количество строк (самые правые столбцы начали обрезаться), но другие люди не сталкивались с этой проблемой, так что, скорее всего, это связано с неправильной установкой на моем конце.
Я не смог сделать с помощью ScrollLayout, но могу предложить этот вариант на основе примеров из github.
package main
import (
"fmt"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/math"
"github.com/google/gxui/samples/flags"
"github.com/google/gxui/themes/dark"
)
type customAdapter struct {
gxui.AdapterBase
}
func (a *customAdapter) Count() int {
return 1000
}
func (a *customAdapter) ItemAt(index int) gxui.AdapterItem {
return index
}
func (a *customAdapter) ItemIndex(item gxui.AdapterItem) int {
return item.(int)
}
func (a *customAdapter) Size(theme gxui.Theme) math.Size {
return math.Size{W: 200, H: 25}
}
func (a *customAdapter) Create(theme gxui.Theme, index int) gxui.Control {
layout1 := theme.CreateLinearLayout()
layout1.SetDirection(gxui.LeftToRight)
for c := 0; c < 4; c++ {
col := theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
cell := theme.CreateLabel()
cell.SetText(fmt.Sprintf("%d", index*4+c))
col.AddChild(cell)
layout1.AddChild(col)
}
return layout1
}
func appMain(driver gxui.Driver) {
theme := dark.CreateTheme(driver)
window := theme.CreateWindow(600, 400, "Grid")
window.BorderPen()
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(driver.Terminate)
adapter := &customAdapter{}
list := theme.CreateList()
list.SetAdapter(adapter)
list.SetOrientation(gxui.Vertical)
window.AddChild(list)
}
func main() {
gl.StartDriver(appMain)
}
Каждая строка помещается в список, их количество и размер указываются в переопределенных методах. Преимущество в том, что в списке уже есть полоса прокрутки.