Documentation
¶
Index ¶
- type Node
- type RowState
- type Tree
- func (t *Tree) Expand(k any, open bool)
- func (t *Tree) Expanded(k any) bool
- func (t *Tree) Focus()
- func (t *Tree) Layout(gtx layout.Context) layout.Dimensions
- func (t *Tree) OnActivate(fn func(Node)) *Tree
- func (t *Tree) OnCollapse(fn func(Node)) *Tree
- func (t *Tree) OnExpand(fn func(Node)) *Tree
- func (t *Tree) OnSelect(fn func(Node)) *Tree
- func (t *Tree) Reveal(k any)
- func (t *Tree) Select(k any)
- func (t *Tree) Selected() (key any, ok bool)
- func (t *Tree) Toggle(k any)
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 ¶
Expand opens (or, with open false, closes) the node with key, running OnExpand/OnCollapse. Ancestors aren't opened; see Reveal.
func (*Tree) Layout ¶
func (t *Tree) Layout(gtx layout.Context) layout.Dimensions
Layout lays out the visible rows.
func (*Tree) OnActivate ¶
OnActivate sets a handler run on a double click or Enter. It returns t for chaining.
func (*Tree) OnCollapse ¶
OnCollapse sets a handler run when a node is collapsed. It returns t for chaining.
func (*Tree) OnExpand ¶
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 ¶
OnSelect sets a handler run when the selection changes. It returns t for chaining.
func (*Tree) Reveal ¶
Reveal opens every ancestor of the node with key, selects it and scrolls it into view. Only loaded nodes can be revealed.