Files
MujicaUI/tree.go
T
ZacharyZhang-NY 8880865021 Refactor: results expose Element as a named field
Every component result type now names its element Element instead of
embedding *ui.Element. The eleven element-plus-changed results
(AccordionView, CarouselView, CollapsibleView, ImageViewerView,
PaginationResult, StepsResult, SidebarResult, TabsResult, RatingParts,
FileDropZoneParts, TransferParts) fold into ChoiceResult.

Fixes four Gallery demos where .Width(...).Changed() read the element's
flag instead of the component's: Accordion, MultiSelect, Cascader,
TreeSelect. TestDataAccordionDemoLogsChange covers the Accordion.
2026-10-05 13:35:53 -04:00

220 lines
6.5 KiB
Go

package mujica
import (
"log/slog"
"github.com/egoist/mygo/ui"
"mujicaui/icons"
"mujicaui/internal"
"mujicaui/theme"
)
// TreeOptions configure a Tree of items of type K, which are also their keys.
type TreeOptions[K comparable] struct {
Roots []K
// Children returns an item's children: nil for a leaf, empty while unloaded; required.
Children func(item K) []K
// Label returns an item's text, read out and matched by typing; required.
Label func(item K) string
// ItemStatus returns the load state of an item's children; nil means loaded.
ItemStatus func(item K) DataStatus
// Load asks for an item's children when it opens DataUnloaded, and on retry.
Load func(item K)
Multiple bool
// Empty replaces the built-in "No items" text.
Empty string
Status DataStatus
Error string
}
// TreeState is a Tree's open items, selection and place.
type TreeState[K comparable] struct {
// Selection holds the chosen items of a Multiple tree.
Selection ui.Selection[K]
tree treeCore[K]
}
// Selected returns the chosen item, or the last chosen of a Multiple tree.
func (s *TreeState[K]) Selected() (K, bool) { return s.tree.sel, s.tree.has }
// SetOpen opens or closes an item.
func (s *TreeState[K]) SetOpen(item K, open bool) { s.tree.setOpen(item, open) }
// IsOpen reports whether an item shows its children.
func (s *TreeState[K]) IsOpen(item K) bool { return s.tree.outline.Open.Has(item) }
// treeCore is the outline state shared by Tree and TreeTable.
type treeCore[K comparable] struct {
outline ui.OutlineState[K]
cursor int
started bool
sel K
has bool
// asked holds the open items whose children Load was asked for.
asked map[K]bool
}
func (t *treeCore[K]) setOpen(item K, open bool) {
if open {
t.outline.Open.Add(item)
} else {
t.outline.Open.Remove(item)
}
}
// begin wires the outline's selection before it is built.
func (t *treeCore[K]) begin(label func(K) string, sel *ui.Selection[K], multiple bool) {
if !t.started {
t.started, t.cursor, t.asked = true, -1, map[K]bool{}
}
t.outline.List.Selected = &t.cursor
t.outline.List.Label = func(i int) string { return label(t.outline.Item(i)) }
t.outline.List.Selection = nil
if multiple {
t.outline.List.Selection = sel
}
}
// clear drops the choice and row state when the tree has no items.
func (t *treeCore[K]) clear(sel *ui.Selection[K]) {
var zero K
t.cursor, t.sel, t.has = -1, zero, false
sel.Clear()
clear(t.asked)
t.outline = ui.OutlineState[K]{Open: t.outline.Open}
}
// end reads a new choice, or finds the chosen item again after rows moved.
func (t *treeCore[K]) end(c *ui.Context, changed bool) {
rows := t.outline.Rows()
at := t.cursor >= 0 && t.cursor < rows
if changed || !t.has {
t.has = at
if at {
t.sel = t.outline.Item(t.cursor)
}
return
}
if at && t.outline.Item(t.cursor) == t.sel {
return
}
t.cursor = -1
for i := range rows {
if t.outline.Item(i) == t.sel {
t.cursor = i
break
}
}
if t.cursor < 0 {
t.has = false
return
}
// The rows showed the old place: build them again at the new one.
c.Invalidate()
slog.Debug("mujica: tree selection moved", "row", t.cursor)
}
// chosen reports whether item shows as chosen.
func (t *treeCore[K]) chosen(item K, sel *ui.Selection[K], multiple bool) bool {
if multiple {
return sel.Has(item)
}
return t.has && t.sel == item
}
// itemStatus asks for unloaded children of an open item and shows loading or failure.
func (t *treeCore[K]) itemStatus(c *ui.Context, item K, status func(K) DataStatus, load func(K)) {
if status == nil {
return
}
st := status(item)
open := t.outline.Open.Has(item)
if st != DataUnloaded || !open {
delete(t.asked, item)
}
if st == DataUnloaded && open && !t.asked[item] {
if load == nil {
panic("mujica: tree item unloaded without Load")
}
t.asked[item] = true
slog.Debug("mujica: tree load", "item", item)
load(item)
}
k := Tokens(c)
u := Density(c).Unit()
switch st {
case DataLoading:
Spinner(c, SpinnerOptions{Size: 14, Label: msg(c, "list.loading"), ShowLabel: true})
case DataFailed:
ui.Row(c).Gap(u).AlignItems(ui.Center).TextColor(k.Danger).Children(func() {
ui.Icon(c, icons.Must("circle-alert")).Size(14, 14)
ui.Text(c, msg(c, "list.failed")).FontSize(FontSize(c, theme.CaptionSize)).SingleLine()
})
if Button(c, msg(c, "list.retry"), ButtonOptions{Variant: Ghost}).MinHeight(theme.MinHitSize).Clicked() {
slog.Debug("mujica: tree retry", "item", item)
load(item)
}
}
}
// TreeView is a built Tree and what happened to it in this frame.
type TreeView struct {
Element *ui.Element
changed, submitted, retry bool
}
// Changed reports a new selection.
func (v TreeView) Changed() bool { return v.changed }
// Submitted reports Enter or a double click on an item.
func (v TreeView) Submitted() bool { return v.submitted }
// Retried reports a click on the retry action of a failed first load.
func (v TreeView) Retried() bool { return v.retry }
// Tree shows items in a hierarchy, building only the rows in view.
func Tree[K comparable](c *ui.Context, s *TreeState[K], opts TreeOptions[K]) TreeView {
if opts.Children == nil || opts.Label == nil {
panic("mujica: Tree needs Children and Label")
}
k := Tokens(c)
d := Density(c)
u := d.Unit()
t := &s.tree
t.begin(opts.Label, &s.Selection, opts.Multiple)
var v TreeView
v.Element = ui.Column(c).MinHeight(0)
v.Element.Children(func() {
if len(opts.Roots) == 0 {
t.clear(&s.Selection)
v.retry = dataPlaceholder(c, opts.Status, opts.Error, opts.Empty, msg(c, "list.empty"))
return
}
// Outline rows pad by one unit above and below.
h := d.ControlHeight() - 2*c.Theme().Space(1)
restore := dataSelectTheme(c, 0)
e := ui.Outline(c, &t.outline, opts.Roots, opts.Children, func(item K) {
ui.Row(c).Grow(1).MinWidth(0).MinHeight(h).Gap(u * 2).AlignItems(ui.Center).Children(func() {
ui.Text(c, opts.Label(item)).SingleLine().Grow(1).MinWidth(0)
t.itemStatus(c, item, opts.ItemStatus, opts.Load)
if t.chosen(item, &s.Selection, opts.Multiple) {
ui.Icon(c, icons.Must("check")).Size(16, 16).Shrink(0).TextColor(k.AccentText)
}
})
})
restore()
e.Grow(1).MinHeight(0).Padding(u).Background(k.Surface).Border(theme.BorderWidth, k.Border).Radius(theme.ControlRadius)
internal.Ring(e, k.Focus, theme.ControlRadius)
v.changed, v.submitted = e.Changed(), e.Submitted()
v.retry = dataFooter(c, opts.Status, opts.Error)
})
if len(opts.Roots) > 0 {
t.end(c, v.changed)
}
if v.changed {
slog.Debug("mujica: tree changed", "row", t.cursor, "count", s.Selection.Len())
}
return v
}