diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-05-12 17:14:30 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-05-12 17:14:30 -0400 |
| commit | 61a63fec579f88766c6be0d392bb4eaed3b8564a (patch) | |
| tree | 8f5645e06b737035623599a86767f17035b1fa68 /gui/widget | |
| parent | c0dc325c1b599719172590d36d8702d31b49c18d (diff) | |
| download | volute-61a63fec579f88766c6be0d392bb4eaed3b8564a.zip | |
button widget
Diffstat (limited to 'gui/widget')
| -rw-r--r-- | gui/widget/button.go | 62 | ||||
| -rw-r--r-- | gui/widget/tree.go | 1 |
2 files changed, 62 insertions, 1 deletions
diff --git a/gui/widget/button.go b/gui/widget/button.go new file mode 100644 index 0000000..fe2f30b --- /dev/null +++ b/gui/widget/button.go @@ -0,0 +1,62 @@ +package widget + +import ( + "image" + "image/draw" + "sync" + + "volute/gui" + "volute/gui/text" + "volute/gui/win" +) + +func Button[T any]( + signal chan<- T, + val T, + label string, + r image.Rectangle, + focus FocusSlave, + env gui.Env, + wg *sync.WaitGroup, +) { + defer wg.Done() + defer close(signal) + + focused := false + env.Draw() <- buttonDraw(label, focused, r) + for { + select { + case _, ok := <-focus.gain: + if !ok { + return + } + focused = true + env.Draw() <- buttonDraw(label, focused, r) + case dir, ok := <-focus.lose: + if !ok { + return + } + focus.yield <- dir + focused = false + env.Draw() <- buttonDraw(label, focused, r) + case event, ok := <-env.Events(): + if !ok { + return + } + if event, ok := event.(win.KbDown); ok && focused && event.Key == win.KeyEnter { + signal <- val + } + } + } +} + +func buttonDraw(label string, focused bool, r image.Rectangle) func(drw draw.Image) image.Rectangle { + return func(drw draw.Image) image.Rectangle { + if focused { + text.Draw(label, drw, r, BLACK, FOCUS_COLOR, text.ALIGN_LEFT) + } else { + text.Draw(label, drw, r, BLACK, WHITE, text.ALIGN_LEFT) + } + return r + } +} diff --git a/gui/widget/tree.go b/gui/widget/tree.go index 9e4af13..ff4d139 100644 --- a/gui/widget/tree.go +++ b/gui/widget/tree.go @@ -54,7 +54,6 @@ func flatten[T any](root Node[T], depth int) []string { make([]rune, 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)...) |