Tree
Nested rows that expand and collapse, built directly or virtualized.
Loading the live example…
import (
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
"github.com/oneo-me/onevra/source/icons/lucide"
)
type projectTree struct {
source, docs bool
selected string
}
func (p *projectTree) View(c *ui.Context) {
folder := func(c *ui.Context, size float32) {
components.Icon(c, lucide.Folder).Size(size, size).TextColor(c.Theme().Accent)
}
file := func(name string) {
if components.TreeItem(c, name, nil, p.selected == name, nil).Clicked() {
p.selected = name
}
}
ui.Column(c).Width(260).Children(func() {
components.Tree(c, func() {
if components.TreeItemWithLeading(c, "source", &p.source, p.selected == "source", folder, func() {
file("main.go")
file("app.go")
file("go.mod")
}).Clicked() {
p.selected = "source"
}
if components.TreeItemWithLeading(c, "docs", &p.docs, p.selected == "docs", folder, func() {
file("build.md")
}).Clicked() {
p.selected = "docs"
}
file("README.md")
}).Label("Project").FillWidth()
})
}components.Tree builds TreeItem rows; a row with an open binding and children is a branch. Clicking the arrow or double-clicking the row toggles it, and Left and Right move between parents and children. The application owns expansion and selection.
Large trees
components.TreeVirtualized builds only the visible rows from roots and a children function. Expansion is keyed, and Option or Shift with Left and Right expands recursively.
Loading the live example…
import (
"fmt"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
)
// assets holds 10,000 files, of which only the visible rows are built.
var largeProjectFiles = func() []string {
files := make([]string, 10000)
for i := range files {
files[i] = fmt.Sprintf("image-%05d.png", i+1)
}
return files
}()
type largeProject struct {
tree components.TreeVirtualizedState[string]
}
func (p *largeProject) View(c *ui.Context) {
components.TreeVirtualized(c, &p.tree, []string{"assets", "README.md"},
func(name string) []string {
if name == "assets" {
return largeProjectFiles
}
return nil // a leaf
},
func(name string) {
components.Text(c, name).SingleLine()
},
).Width(300).Height(260).Label("Large project").Border(1, c.Theme().Border).Radius(c.Theme().Radius)
}API
Tree
func Tree(c *ui.Context, children func()) ui.ElementTree builds a non-virtualized tree from Onevra rows and MyGo primitives. Applications own expansion and selection. Place it in a ScrollView when scrolling is needed. Up/Down and Home/End move focus; Left/Right navigate parents and children or collapse/expand branches. MyGo owns input dispatch, focus, layout and rendering, not the tree's appearance.
pkg.go.devTreeItem
func TreeItem(c *ui.Context, label string, open *bool, selected bool, children func()) ui.ElementTreeItem builds a text row and, when open, its nested TreeItems. A nil open or children makes a leaf. Clicking the arrow or double-clicking the row toggles expansion; Clicked lets the application select or activate the row. Selection uses the Sidebar's neutral tint, without a hover face.
pkg.go.devTreeItemWithLeading
func TreeItemWithLeading(c *ui.Context, label string, open *bool, selected bool, leading func(c *ui.Context, size float32), children func()) ui.ElementTreeItemWithLeading is a TreeItem with an application-built leading slot between its disclosure arrow and label, for example a file type or host mark. A nil builder leaves no leading space. The builder receives the theme-scaled icon size as a suggestion and owns its content's size and appearance. Keep it non-interactive and free of I/O or background work.
pkg.go.devTreeVirtualized
func TreeVirtualized[K comparable](c *ui.Context, state *TreeVirtualizedState[K], roots []K, children func(K) []K, row func(K)) ui.ElementTreeVirtualized builds Onevra tree rows in MyGo's virtual scrolling container. Give it a height or fill a bounded parent. children returns nil for leaves; row builds content after the arrow. Row builders may run during layout and must not perform application work. Only row UI is virtualized: expanded nodes are still traversed each frame.
An arrow click or row double click toggles a branch. Enter activates the current row (and toggles branches); List.OnSubmit also reports activation. Left/Right close/open branches or select parents/children. Option on macOS, Shift elsewhere, with Left/Right applies expansion recursively. Alt-clicking an arrow does the same. All row styling is local; the frame theme is untouched.
pkg.go.devTreeVirtualizedState
TreeVirtualizedState keeps keyed expansion, list selection and scrolling. Use one state per tree and do not copy it after first use. Keys must be unique throughout an acyclic tree. List.Selected optionally exposes the cursor; otherwise an internal cursor is used. List.Selection enables multi-selection.
Prop
Type
Methods
func (s *TreeVirtualizedState[K]) Depth(row int) intfunc (s *TreeVirtualizedState[K]) Item(row int) Kfunc (s *TreeVirtualizedState[K]) Rows() intRows reports the number of expanded rows in the last build.