diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-05-10 13:14:17 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-05-10 13:14:17 -0400 |
| commit | 97a7a7cd2afa18b73f5982ecb3ec08c0392b6d1e (patch) | |
| tree | 56a903355753f10a54560aaa236ea90289f8c6bc /gui/widget/output.go | |
| parent | 528616ddbc1111c627df85ee38d406b2e7dc0000 (diff) | |
| download | volute-97a7a7cd2afa18b73f5982ecb3ec08c0392b6d1e.zip | |
move output widget to separate file
Diffstat (limited to 'gui/widget/output.go')
| -rw-r--r-- | gui/widget/output.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/gui/widget/output.go b/gui/widget/output.go new file mode 100644 index 0000000..0d03c0c --- /dev/null +++ b/gui/widget/output.go @@ -0,0 +1,42 @@ +package widget + +import ( + "fmt" + "sync" + + "image" + "image/draw" + + "volute/gui" + "volute/gui/text" + "volute/gui/win" +) + +func Output(val <-chan float64, r image.Rectangle, env gui.Env, wg *sync.WaitGroup) { + defer wg.Done() + defer close(env.Draw()) + + var v float64 = 0.0 + env.Draw() <- outputDraw(v, r) +Loop: + for { + select { + case v = <-val: + env.Draw() <- outputDraw(v, r) + case event, ok := <-env.Events(): + if !ok { // channel closed + break Loop + } + if event, ok := event.(win.WiFocus); ok && event.Focused { + env.Draw() <- outputDraw(v, r) + } + } + } +} + +func outputDraw(v float64, r image.Rectangle) func(draw.Image) image.Rectangle { + return func(drw draw.Image) image.Rectangle { + text.Draw([]byte(fmt.Sprintf("%.3f", v)), drw, r, BLACK, WHITE) + return r + } +} |