From 95944c92c6d9171e3a8a2619e62bbfa29d48bfef Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 19 Jan 2024 22:43:07 -0500 Subject: refine widget screen space allocation --- go.mod | 2 +- gui/widget/text.go | 9 +++------ main.go | 27 +++++++++++++++++++++++++-- 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) } -- cgit v1.2.3