treeview

package
v1.0.31-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node interface {
	// Key identifies the node across frames; it must be comparable and
	// unique in the tree (a path, an ID).
	Key() any
	// Children returns the loaded children. It is only called for
	// expanded nodes.
	Children() []Node
	// HasChildren reports whether the node can expand, before its
	// children are loaded - so a folder shows its arrow and loads its
	// contents when first expanded (see OnExpand).
	HasChildren() bool
}

Node is one item of the tree.

type RowState

type RowState struct {
	// Depth is the nesting level, 0 for roots.
	Depth int
	// Expanded reports whether the node is open.
	Expanded bool
	// Selected reports whether the node is the selection.
	Selected bool
	// Hovered reports whether the pointer is over the row.
	Hovered bool
	// Loading reports an expanded node whose children haven't loaded
	// yet (HasChildren but no Children).
	Loading bool
	// Focused reports whether the tree has keyboard focus.
	Focused bool
}

RowState is what a row is drawn with.

type Tree

type Tree struct {
	// Contextual gives it a context menu (Context).
	elements.Contextual
	// contains filtered or unexported fields
}

Tree is a tree of expandable rows. Create it once and lay it out every frame:

tree := giode.TreeView(func() []giode.TreeNode { return roots },
	func(n giode.TreeNode, s giode.TreeRowState) giode.Element {
		return giode.Text(n.(*Folder).Name)
	}).
	OnExpand(func(n giode.TreeNode) { n.(*Folder).LoadChildren() })

The tree owns expansion and selection, keyed by Node.Key, and draws the indentation, expand arrows, selection and hover backgrounds; the row function draws only the content. Only visible rows are built, so trees of any size stay fast. Keyboard (once focused): Up/Down move the selection, Right expands (or moves into a node), Left collapses (or moves to the parent), Enter activates, PageUp/PageDown and Home/End jump. A click selects, a double click activates, a click on the arrow toggles.

func New

func New(roots func() []Node, draw func(n Node, s RowState) elements.Element, st ...styles.Styles) *Tree

New returns a tree of roots() drawn with draw. The styles argument is optional: Background (default transparent), Height (row height in dp, default 28), ScrollBar, Color (arrows, default theme.text) and BorderColor (selection color, default theme.primary).

func (*Tree) Expand

func (t *Tree) Expand(k any, open bool)

Expand opens (or, with open false, closes) the node with key, running OnExpand/OnCollapse. Ancestors aren't opened; see Reveal.

func (*Tree) Expanded

func (t *Tree) Expanded(k any) bool

Expanded reports whether the node with key is open.

func (*Tree) Focus

func (t *Tree) Focus()

Focus gives the tree keyboard focus on the next frame.

func (*Tree) Layout

func (t *Tree) Layout(gtx layout.Context) layout.Dimensions

Layout lays out the visible rows.

func (*Tree) OnActivate

func (t *Tree) OnActivate(fn func(Node)) *Tree

OnActivate sets a handler run on a double click or Enter. It returns t for chaining.

func (*Tree) OnCollapse

func (t *Tree) OnCollapse(fn func(Node)) *Tree

OnCollapse sets a handler run when a node is collapsed. It returns t for chaining.

func (*Tree) OnExpand

func (t *Tree) OnExpand(fn func(Node)) *Tree

OnExpand sets a handler run when a node is expanded - the place to load its children lazily. It returns t for chaining.

func (*Tree) OnSelect

func (t *Tree) OnSelect(fn func(Node)) *Tree

OnSelect sets a handler run when the selection changes. It returns t for chaining.

func (*Tree) Reveal

func (t *Tree) Reveal(k any)

Reveal opens every ancestor of the node with key, selects it and scrolls it into view. Only loaded nodes can be revealed.

func (*Tree) Select

func (t *Tree) Select(k any)

Select makes the node with key the selection, running OnSelect.

func (*Tree) Selected

func (t *Tree) Selected() (key any, ok bool)

Selected returns the selected key.

func (*Tree) Toggle

func (t *Tree) Toggle(k any)

Toggle opens a closed node and closes an open one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL