aboutsummaryrefslogtreecommitdiffstats
path: root/gui/widget
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-01-17 16:11:26 -0500
committerSam Anthony <sam@samanthony.xyz>2024-01-17 16:11:26 -0500
commitbd9bce9f1278743e961a18cab688fdb09876560c (patch)
tree9daec1c7725051930eb9db7904e50e6bd82d4ee8 /gui/widget
parent0a696233d721c64d1bb53c7429624313113fa6f5 (diff)
downloadvolute-bd9bce9f1278743e961a18cab688fdb09876560c.zip
add text label widget
Diffstat (limited to 'gui/widget')
-rw-r--r--gui/widget/text.go23
-rw-r--r--gui/widget/widget.go19
2 files changed, 34 insertions, 8 deletions
diff --git a/gui/widget/text.go b/gui/widget/text.go
index 4723eda..5e37d70 100644
--- a/gui/widget/text.go
+++ b/gui/widget/text.go
@@ -14,11 +14,12 @@ import (
)
var (
- FONT = goregular.TTF
- FONT_SIZE float64 = 15
- DPI float64 = 72
- BG_COLOR = image.White
- TEXT_COLOR = image.Black
+ FONT = goregular.TTF
+ FONT_SIZE = 15
+ DPI = 72
+ PADDING = 3
+ BG_COLOR = image.White
+ TEXT_COLOR = image.Black
)
var face *concurrentFace
@@ -29,8 +30,8 @@ func init() {
log.Fatal(err)
}
fce, err := opentype.NewFace(fnt, &opentype.FaceOptions{
- Size: FONT_SIZE,
- DPI: DPI,
+ Size: float64(FONT_SIZE),
+ DPI: float64(DPI),
})
if err != nil {
log.Fatal(err)
@@ -38,6 +39,14 @@ func init() {
face = &concurrentFace{sync.Mutex{}, fce}
}
+func TextWidth(nchars int) int {
+ return nchars*FONT_SIZE + 2*PADDING // very rough estimation
+}
+
+func TextHeight() int {
+ return FONT_SIZE + 2*PADDING
+}
+
func drawText(text []byte, dst draw.Image, r image.Rectangle) {
drawer := font.Drawer{
Src: TEXT_COLOR,
diff --git a/gui/widget/widget.go b/gui/widget/widget.go
index fbf866a..82738bd 100644
--- a/gui/widget/widget.go
+++ b/gui/widget/widget.go
@@ -11,7 +11,24 @@ import (
"volute/gui/win"
)
-func Input(env gui.Env, r image.Rectangle, val chan<- float64) {
+func Label(text string, r image.Rectangle, env gui.Env) {
+ redraw := func(drw draw.Image) image.Rectangle {
+ drawText([]byte(text), drw, r)
+ return r
+ }
+ env.Draw() <- redraw
+ for event := range env.Events() {
+ switch event := event.(type) {
+ case win.WiFocus:
+ if event.Focused {
+ env.Draw() <- redraw
+ }
+ }
+ }
+ close(env.Draw())
+}
+
+func Input(val chan<- float64, r image.Rectangle, env gui.Env) {
redraw := func(text []byte) func(draw.Image) image.Rectangle {
return func(drw draw.Image) image.Rectangle {
drawText(text, drw, r)