mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
ui/layout: avoid accumulating rounding errors from Flexible
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+9
-4
@@ -26,6 +26,8 @@ type Flex struct {
|
|||||||
mode flexMode
|
mode flexMode
|
||||||
size int
|
size int
|
||||||
rigidSize int
|
rigidSize int
|
||||||
|
// fraction is the rounding error from a Flexible weighting.
|
||||||
|
fraction float32
|
||||||
maxCross int
|
maxCross int
|
||||||
maxBaseline int
|
maxBaseline int
|
||||||
}
|
}
|
||||||
@@ -111,11 +113,14 @@ func (f *Flex) Flexible(weight float32) Constraints {
|
|||||||
mainc := axisMainConstraint(f.Axis, f.cs)
|
mainc := axisMainConstraint(f.Axis, f.cs)
|
||||||
var flexSize int
|
var flexSize int
|
||||||
if mainc.Max > f.size {
|
if mainc.Max > f.size {
|
||||||
maxSize := mainc.Max - f.size
|
|
||||||
flexSize = mainc.Max - f.rigidSize
|
flexSize = mainc.Max - f.rigidSize
|
||||||
flexSize = int(float32(flexSize)*weight + .5)
|
// Apply weight and add any leftover fraction from a
|
||||||
if flexSize > maxSize {
|
// previous Flexible.
|
||||||
flexSize = maxSize
|
size := float32(flexSize)*weight + f.fraction
|
||||||
|
flexSize = int(size + .5)
|
||||||
|
f.fraction = size - float32(flexSize)
|
||||||
|
if max := mainc.Max - f.size; flexSize > max {
|
||||||
|
flexSize = max
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
submainc := Constraint{Max: flexSize}
|
submainc := Constraint{Max: flexSize}
|
||||||
|
|||||||
Reference in New Issue
Block a user