aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gui/widget/text.go23
-rw-r--r--gui/widget/widget.go19
-rw-r--r--main.go15
3 files changed, 48 insertions, 9 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)
diff --git a/main.go b/main.go
index c6c522f..eb2e094 100644
--- a/main.go
+++ b/main.go
@@ -21,7 +21,20 @@ func run() {
displacementChan = make(chan float64)
)
- go widget.Input(mux.MakeEnv(), image.Rect(20, 20, 100, 40), displacementChan)
+ pad := 10
+ r := image.Rect(pad, pad, pad+widget.TextWidth(6), pad+widget.TextHeight())
+ go widget.Input(
+ displacementChan,
+ r,
+ mux.MakeEnv(),
+ )
+ r = image.Rect(
+ r.Max.X+pad,
+ r.Min.Y,
+ r.Max.X+pad+widget.TextWidth(len("cc")),
+ r.Max.Y,
+ )
+ go widget.Label("cc", r, mux.MakeEnv())
Loop:
for event := range env.Events() {