From 4b788dd78310c02dcd7f075740ca1a28896b969d Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 16 Mar 2023 20:55:11 -0400 Subject: [PATCH] Horizontal layout does not use integer math anymore Instead it uses fixed.Int26_6. Could have used floats but then we would need a new point datatype and we already have utility functions for fixed point math --- layouts/basic/horizontal.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/layouts/basic/horizontal.go b/layouts/basic/horizontal.go index 0819241..26f2233 100644 --- a/layouts/basic/horizontal.go +++ b/layouts/basic/horizontal.go @@ -1,8 +1,10 @@ package basicLayouts import "image" +import "golang.org/x/image/math/fixed" import "git.tebibyte.media/sashakoshka/tomo/artist" import "git.tebibyte.media/sashakoshka/tomo/layouts" +import "git.tebibyte.media/sashakoshka/tomo/fixedutil" // Horizontal arranges elements horizontally. Elements at the start of the entry // list will be positioned on the left, and elements at the end of the entry @@ -30,19 +32,21 @@ func (layout Horizontal) Arrange ( entries, margin, padding, bounds.Dx()) // set the size and position of each element - dot := bounds.Min + dot := fixedutil.Pt(bounds.Min) for index, entry := range entries { - if index > 0 && layout.Gap { dot.X += margin.X } + if index > 0 && layout.Gap { dot.X += fixed.I(margin.X) } - entry.Bounds.Min = dot - entryWidth := 0 + entry.Bounds.Min = fixedutil.FloorPt(dot) + entryWidth := fixed.Int26_6(0) if entry.Expand { entryWidth = expandingElementWidth } else { - entryWidth, _ = entry.MinimumSize() + min, _ := entry.MinimumSize() + entryWidth = fixed.I(min) } dot.X += entryWidth - entry.Bounds.Max = entry.Bounds.Min.Add(image.Pt(entryWidth, bounds.Dy())) + entry.Bounds.Max = entry.Bounds.Min.Add ( + image.Pt(entryWidth.Floor(), bounds.Dy())) entries[index] = entry } @@ -81,7 +85,7 @@ func (layout Horizontal) expandingElementWidth ( padding artist.Inset, freeSpace int, ) ( - width int, + width fixed.Int26_6, ) { expandingElements := 0 @@ -100,7 +104,7 @@ func (layout Horizontal) expandingElementWidth ( } if expandingElements > 0 { - width = freeSpace / expandingElements + width = fixed.I(freeSpace) / fixed.Int26_6(expandingElements) } return }