GridView
A virtualized, responsive grid of cells, such as photos or files.
Loading the live example…
import (
"fmt"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
"github.com/oneo-me/onevra/source/icons/lucide"
)
type fileGrid struct {
grid components.GridState
selected int
}
func (g *fileGrid) View(c *ui.Context) {
t := c.Theme()
g.grid.Selected = &g.selected
g.grid.Label = func(i int) string { return fmt.Sprintf("Photo %d", i+1) }
// Cells are at least 96 points wide; the grid fits as many per row as it can.
components.GridView(c, &g.grid, 500, 96, 88, func(i int) {
ui.Column(c).Fill().Center().Gap(t.Space(1)).Children(func() {
components.Icon(c, lucide.Image).Size(28, 28).TextColor(t.Accent)
components.Text(c, fmt.Sprintf("Photo %d", i+1)).FontSize(t.Rem(0.8))
})
}).Fill().Label("Photos").Padding(t.Space(3))
}Give each cell a minimum width and a height; the grid fits as many per row as the width allows and builds only the visible cells. Selection and activation follow GridState.
API
GridView
func GridView(c *ui.Context, s *GridState, count int, minWidth, height float32, item func(int)) ui.ElementGridView builds a virtualized, responsive grid with neutral, hover-free selection. minWidth and height describe each cell's content area in DIPs; a transparent two-DIP inset on each side is reserved by MyGo. Values below one are clamped to one. Item spacing follows MyGo's theme-scaled grid gap. Give the grid a height or fill a bounded parent. Builders may run during layout and must not perform application work. A nil state scrolls without selection. MyGo owns layout, virtualization, scrolling and stable cell keys.
pkg.go.devGridState
GridState keeps selection and scrolling for a GridView. Keep one state per grid; do not copy it after first use. Keys must be unique and comparable. A nil Selected and Selection disables selection. Selection holds item keys; Selected optionally exposes the keyboard cursor. OnChange and OnSubmit, rather than Element.Changed/Submitted, report selection and activation. Drag reordering and MyGo's private collection accessibility actions are not exposed by this composition.
Prop
Type
Methods
func (s *GridState) Focus(c *ui.Context)func (s *GridState) FocusWithin(c *ui.Context) boolfunc (s *GridState) Focused(c *ui.Context) boolfunc (s *GridState) ScrollIntoView(item int)func (s *GridState) ScrollTo(item int, align ui.Align)