Fix grid widths when using weighted widths

If the column weights do not collectively divide the extent of the grid
layout then some width was not used and so would not be redrawn,
resulting in previous drawings showing through.

This fixes this by checking if there is any remainingExact width and if
there is it is assigned to the weighted columns by their proportion from
left to right.
This commit is contained in:
Jeffas 2019-07-16 20:21:28 +01:00 committed by Drew DeVault
parent 06af5391a3
commit 4991c344ab

View file

@ -134,15 +134,26 @@ func (grid *Grid) reflow(ctx *Context) {
} }
} }
offset := 0 offset := 0
remainingExact := 0
if weight > 0 {
remainingExact = (extent - exact) % weight
}
for _, spec := range *specs { for _, spec := range *specs {
layout := gridLayout{Offset: offset} layout := gridLayout{Offset: offset}
if spec.Strategy == SIZE_EXACT { if spec.Strategy == SIZE_EXACT {
layout.Size = spec.Size layout.Size = spec.Size
} else if spec.Strategy == SIZE_WEIGHT { } else if spec.Strategy == SIZE_WEIGHT {
size := float64(spec.Size) / float64(weight) proportion := float64(spec.Size) / float64(weight)
size *= float64(extent - exact) size := proportion * float64(extent-exact)
if remainingExact > 0 {
extraExact := int(math.Ceil(proportion * float64(remainingExact)))
layout.Size = int(math.Floor(size)) + extraExact
remainingExact -= extraExact
} else {
layout.Size = int(math.Floor(size)) layout.Size = int(math.Floor(size))
} }
}
offset += layout.Size offset += layout.Size
*layouts = append(*layouts, layout) *layouts = append(*layouts, layout)
} }