diff options
| -rw-r--r-- | gui/text/text.go | 10 | ||||
| -rw-r--r-- | gui/widget/input.go | 9 | ||||
| -rw-r--r-- | gui/widget/label.go | 2 | ||||
| -rw-r--r-- | gui/widget/output.go | 2 | ||||
| -rw-r--r-- | gui/widget/tree.go | 6 |
5 files changed, 16 insertions, 13 deletions
diff --git a/gui/text/text.go b/gui/text/text.go index 942dce2..7dc4928 100644 --- a/gui/text/text.go +++ b/gui/text/text.go @@ -46,11 +46,11 @@ const ( ) func Size(text string) image.Point { - bounds := textBounds([]byte(text), font.Drawer{Face: face}) + bounds := textBounds(text, font.Drawer{Face: face}) return image.Point{bounds.Max.X - bounds.Min.X + 2*PAD, bounds.Max.Y - bounds.Min.Y + 2*PAD} } -func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color, align Align) { +func Draw(text string, dst draw.Image, r image.Rectangle, fg, bg color.Color, align Align) { drawer := font.Drawer{ Src: &image.Uniform{fg}, Face: face, @@ -65,7 +65,7 @@ func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color, al textImg := image.NewRGBA(bounds) draw.Draw(textImg, bounds, &image.Uniform{bg}, image.ZP, draw.Src) drawer.Dst = textImg - drawer.DrawBytes(text) + drawer.DrawString(text) leftCentre := image.Pt(bounds.Min.X, (bounds.Min.Y+bounds.Max.Y)/2) var target image.Point @@ -79,8 +79,8 @@ func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color, al draw.Draw(dst, bounds.Add(delta).Intersect(r), textImg, bounds.Min, draw.Src) } -func textBounds(text []byte, drawer font.Drawer) image.Rectangle { - b, _ := drawer.BoundBytes(text) +func textBounds(text string, drawer font.Drawer) image.Rectangle { + b, _ := drawer.BoundString(text) return image.Rect( b.Min.X.Floor(), b.Min.Y.Floor(), diff --git a/gui/widget/input.go b/gui/widget/input.go index f47f587..1e15da9 100644 --- a/gui/widget/input.go +++ b/gui/widget/input.go @@ -1,7 +1,6 @@ package widget import ( - "fmt" "image" "image/draw" "sync" @@ -16,7 +15,7 @@ func Input(val chan<- uint, r image.Rectangle, focus FocusSlave, env gui.Env, wg defer close(env.Draw()) defer close(val) - text := []byte{'0'} + text := "0" focused := false env.Draw() <- inputDraw(text, focused, r) Loop: @@ -46,7 +45,7 @@ Loop: } case win.KbType: if focused && isDigit(event.Rune) { - text = fmt.Appendf(text, "%c", event.Rune) + text = text + string(event.Rune) env.Draw() <- inputDraw(text, focused, r) val <- atoi(text) } @@ -61,7 +60,7 @@ Loop: } } -func inputDraw(str []byte, focused bool, r image.Rectangle) func(draw.Image) image.Rectangle { +func inputDraw(str string, focused bool, r image.Rectangle) func(draw.Image) image.Rectangle { return func(drw draw.Image) image.Rectangle { if focused { text.Draw(str, drw, r, GREEN, FOCUS_COLOR, text.ALIGN_RIGHT) @@ -76,7 +75,7 @@ func isDigit(r rune) bool { return '0' <= r && r <= '9' } -func atoi(s []byte) uint { +func atoi(s string) uint { var n uint = 0 for _, d := range s { n = n*10 + uint(d-'0') diff --git a/gui/widget/label.go b/gui/widget/label.go index fe451f8..7a583bc 100644 --- a/gui/widget/label.go +++ b/gui/widget/label.go @@ -15,7 +15,7 @@ func Label(str string, r image.Rectangle, env gui.Env, wg *sync.WaitGroup) { defer close(env.Draw()) redraw := func(drw draw.Image) image.Rectangle { - text.Draw([]byte(str), drw, r, BLACK, WHITE, text.ALIGN_LEFT) + text.Draw(str, drw, r, BLACK, WHITE, text.ALIGN_LEFT) return r } diff --git a/gui/widget/output.go b/gui/widget/output.go index ecca18c..16aacca 100644 --- a/gui/widget/output.go +++ b/gui/widget/output.go @@ -35,7 +35,7 @@ Loop: 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, text.ALIGN_RIGHT) + text.Draw(fmt.Sprintf("%.3f", v), drw, r, BLACK, WHITE, text.ALIGN_RIGHT) return r } } diff --git a/gui/widget/tree.go b/gui/widget/tree.go index 14d7b9d..633d440 100644 --- a/gui/widget/tree.go +++ b/gui/widget/tree.go @@ -9,6 +9,8 @@ import ( "volute/gui/layout" ) +const INDENT = 2 + type Node[T any] struct { Label string Value T @@ -50,7 +52,9 @@ func Tree[T any](trees []Node[T], r image.Rectangle, focus FocusSlave, mux *gui. } func flatten[T any](root Node[T], depth int) []string { - indent := string(populate(make([]byte, 2*depth), ' ')) + indent := string(populate( + make([]rune, INDENT*depth), + '─')) nodes := []string{indent + root.Label} root.expanded = true // TODO: remove me if root.expanded { |