Data: components 069-080
This commit is contained in:
71 files changed
+5400
-1
No files matched your search
@@ -0,0 +1,183 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// end reads the chosen item after the outline was built.
|
||||
func (t *treeCore[K]) end() {
|
||||
t.has = t.cursor >= 0 && t.cursor < t.outline.Rows()
|
||||
if t.has {
|
||||
t.sel = t.outline.Item(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.cursor >= 0 && t.cursor < t.outline.Rows() && t.outline.Item(t.cursor) == 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(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 {
|
||||
*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.Children(func() {
|
||||
if len(opts.Roots) == 0 {
|
||||
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)
|
||||
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)
|
||||
}
|
||||
})
|
||||
})
|
||||
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)
|
||||
})
|
||||
t.end()
|
||||
if v.changed {
|
||||
slog.Debug("mujica: tree changed", "row", t.cursor, "count", s.Selection.Len())
|
||||
}
|
||||
return v
|
||||
}
|
||||
Reference in new issue
Block a user