diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-05-10 13:07:16 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-05-10 13:07:16 -0400 |
| commit | 88c23a05e891f5f2a4cb96c493bc464fcb841109 (patch) | |
| tree | 84bae61bdd2e12ef9827820fa256e3fdee180276 /gui/widget/tree.go | |
| parent | d463bb90ad629dfc8f1cd4f4b6b9590b0008832d (diff) | |
| download | volute-88c23a05e891f5f2a4cb96c493bc464fcb841109.zip | |
move tree widget to separate file
Diffstat (limited to 'gui/widget/tree.go')
| -rw-r--r-- | gui/widget/tree.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/gui/widget/tree.go b/gui/widget/tree.go new file mode 100644 index 0000000..25ecb2e --- /dev/null +++ b/gui/widget/tree.go @@ -0,0 +1,70 @@ +package widget + +import ( + "sync" + + "image" + "image/color" + + "volute/gui" + "volute/gui/layout" +) + +type Node[T any] struct { + Label string + Value T + Children []Node[T] + + expanded bool +} + +func Tree[T any](trees []Node[T], r image.Rectangle, focus FocusSlave, mux *gui.Mux, wg *sync.WaitGroup) { + defer wg.Done() + + var nodes []string + for _, root := range trees { + nodes = append(nodes, flatten(root, 0)...) + } + + bounds := layout.Grid{ + Rows: populate(make([]int, len(nodes)), 1), + Background: color.Gray{255}, + Gap: 1, + Split: layout.EvenSplit, + SplitRows: layout.TextRowSplit, + Margin: 0, + Border: 0, + BorderColor: color.Gray{16}, + Flip: false, + }.Lay(r) + for i := range nodes { + wg.Add(1) + go Label(nodes[i], bounds[i], mux.MakeEnv(), wg) + } + + /* + globalFocus := focus; + localFocus := NewFocusMaster([]int{1, 1, 1}); + defer localFocus.Close() + */ + // TODO +} + +func flatten[T any](root Node[T], depth int) []string { + indent := string(populate(make([]byte, 2*depth), ' ')) + nodes := []string{indent + root.Label} + root.expanded = true // TODO: remove me + if root.expanded { + for _, c := range root.Children { + nodes = append(nodes, flatten(c, depth+1)...) + } + } + return nodes +} + +func populate[T any](arr []T, v T) []T { + for i := range arr { + arr[i] = v + } + return arr +} |