aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-01-19 22:43:07 -0500
committerSam Anthony <sam@samanthony.xyz>2024-01-19 22:43:07 -0500
commit95944c92c6d9171e3a8a2619e62bbfa29d48bfef (patch)
treee48c3bf421d69ef48ea9dade43f86ff9dc30e05b
parent5d107e1443baa6762bbb30b62de68a25c011cf79 (diff)
downloadvolute-95944c92c6d9171e3a8a2619e62bbfa29d48bfef.zip
refine widget screen space allocation
-rw-r--r--go.mod2
-rw-r--r--gui/widget/text.go9
-rw-r--r--main.go27
3 files changed, 29 insertions, 9 deletions
diff --git a/go.mod b/go.mod
index 8a14268..25fa890 100644
--- a/go.mod
+++ b/go.mod
@@ -1,6 +1,6 @@
module volute
-go 1.18
+go 1.21
require (
github.com/BurntSushi/toml v1.1.0
diff --git a/gui/widget/text.go b/gui/widget/text.go
index 6205bab..1b40096 100644
--- a/gui/widget/text.go
+++ b/gui/widget/text.go
@@ -38,12 +38,9 @@ func init() {
face = &concurrentFace{sync.Mutex{}, fce}
}
-func TextWidth(nchars int) int {
- return nchars*FONT_SIZE + 2*PAD // very rough estimation
-}
-
-func TextHeight() int {
- return FONT_SIZE + 2*PAD
+func TextSize(text string) image.Point {
+ bounds := textBounds([]byte(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 drawText(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color) {
diff --git a/main.go b/main.go
index f1bf3b4..9c16af1 100644
--- a/main.go
+++ b/main.go
@@ -38,8 +38,8 @@ func run() {
Rows: []int{2, 8, 8},
Background: color.Gray{255},
Gap: 1,
- Split: layout.EvenSplit,
- SplitRows: layout.EvenSplit,
+ Split: split,
+ SplitRows: splitRows,
Margin: 0,
Border: 0,
BorderColor: color.Gray{16},
@@ -97,6 +97,29 @@ Loop:
}
}
+func split(elements int, space int) []int {
+ bounds := make([]int, elements)
+ widths := []int{
+ widget.TextSize("displacement (cc)").X,
+ widget.TextSize("123456").X,
+ }
+ for i := 0; i < elements && space > 0; i++ {
+ bounds[i] = min(widths[min(i, len(widths)-1)], space)
+ space -= bounds[i]
+ }
+ return bounds
+}
+
+func splitRows(elements int, space int) []int {
+ bounds := make([]int, elements)
+ height := widget.TextSize("1").Y
+ for i := 0; i < elements && space > 0; i++ {
+ bounds[i] = min(height, space)
+ space -= bounds[i]
+ }
+ return bounds
+}
+
func main() {
mainthread.Run(run)
}