Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions base/flex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,32 @@ func TestViewTrimReleasesLargePageNodes(t *testing.T) {
t.Fatalf("nodes capacity after a large page = %d, want the spike released", got)
}
}

// A row as wide as it measured gives each child its whole width, even where float32
// sums the sizes to a few ulps less when it shares them out: a label that fits exactly
// is not cut to an ellipsis, with or without padding of its own.
func TestARowAtItsMeasuredWidthKeepsAnEllipsizedLabelWhole(t *testing.T) {
for _, pad := range []float32{0, 3} {
for i := range 2000 {
scale := 0.5 + float32(i)*0.000731
canvas := &gfx.Canvas{}
canvas.SetTree(&tree)
canvas.SetMeasurer(func(Value string, Size, Weight float32) float32 { return float32(len(Value)) * Size * scale })
var view View
var label Box
f := view.Begin(canvas, gfx.LightTheme(), gfx.Rect{Width: 400, Height: 40})
f.Row().Height(40).Gap(6).Children(func() {
f.Spacer()
f.Row().PadX(9).Gap(5).Children(func() {
f.Slot().Width(12)
label = f.Text("High").FontSize(12).PadX(pad).Ellipsis()
f.Slot().Width(11)
})
})
f.End()
if got := f.Lines[f.Nodes[label.GetI()].LineFirst]; got != "High" {
t.Fatalf("with a glyph %v wide and padding %v, the label reads %q in %v, want High", 12*scale, pad, got, label.Rect().Width)
}
}
}
}
21 changes: 19 additions & 2 deletions base/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ func (f *Frame) measure(axis int) {
n.TextDecor &^= textTruncated
first := int32(len(f.Lines))
// An empty text has no line at all, as WrapLines gives it none; anything
// else that fits is one line and needs no breaking.
if n.Text != "" && n.Natural <= n.Rect.Width-n.PadX() && strings.IndexByte(n.Text, '\n') < 0 {
// else that fits is one line and needs no breaking. The width it needs is
// summed as its preferred width was, so a text given exactly that fits.
if n.Text != "" && n.Natural+n.PadX() <= n.Rect.Width && strings.IndexByte(n.Text, '\n') < 0 {
f.Lines = append(f.Lines, n.Text)
} else {
f.Lines = f.wrapText(f.Lines, n)
Expand Down Expand Up @@ -378,6 +379,15 @@ func shrinksToRoom(n *Node, extra *Extra) bool {
return breaksAnywhere(n, extra) && n.Size[AxisX].Kind == dimAuto && n.Flags&flagBasis == 0
}

// roundingSlack is the most that summing terms sizes of a container of the given
// size can be off by in float32: two ulps of the size for each term, the insets and
// gaps included. It is far below a device pixel, so a shortfall within it is never a
// real lack of room.
func roundingSlack(size float32, terms int) float32 {
const ulp = 1.0 / (1 << 23) // the float32 machine epsilon
return float32(2*(terms+3)) * ulp * max(size, 1)
}

func offset(align Align, free float32) float32 {
switch align {
case Center:
Expand Down Expand Up @@ -455,6 +465,13 @@ func (f *Frame) solveFlow(n *Node, First, stop int32, axis int, start, avail flo
room = max(avail, baseSum+frozenSize+fixed)
}
free := room - frozenSize - baseSum - fixed
if free < 0 && -free <= roundingSlack(room, count) {
// A container placed at the size it measured sums the same sizes again here,
// in another order, and float32 can leave it a few ulps short. That is
// rounding, not a lack of room: taking it from a child would cut a text that
// fits exactly down to an ellipsis.
free = 0
}
var violation float32
for c := First; c != stop; c = f.Nodes[c].Next {
child := &f.Nodes[c]
Expand Down
Loading