diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index f5497ed..0460a40 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -413,3 +413,19 @@ func TestKeyedRowsMoveInsteadOfRepatching(t *testing.T) { t.Errorf("row order = %q", order) } } + +func TestColumnGapIsOneLineAndSkipsEmpty(t *testing.T) { + ctx := page(t, 1000) + line := eval[float64](t, ctx, `byText("gap-a").getBoundingClientRect().height`) + space := func(a, b string) float64 { + return eval[float64](t, ctx, fmt.Sprintf( + `byText(%q).getBoundingClientRect().top - byText(%q).getBoundingClientRect().bottom`, b, a)) + } + plain, withEmpty := space("gap-a", "gap-b"), space("gap-c", "gap-d") + if diff := plain - line; diff < -1 || diff > 1 { + t.Errorf("Gap:1 column leaves %.1fpx between lines %.1fpx tall, want one line", plain, line) + } + if diff := withEmpty - plain; diff < -0.5 || diff > 0.5 { + t.Errorf("a hidden When adds space: %.1fpx with it, %.1fpx without", withEmpty, plain) + } +} diff --git a/e2e/probe/main.go b/e2e/probe/main.go index a3167e7..2467525 100644 --- a/e2e/probe/main.go +++ b/e2e/probe/main.go @@ -94,6 +94,9 @@ func (m model) View() wui.Element { return wui.Box(wui.Column, shifting, + wui.BoxStyled(wui.Column, 1, wui.Style{}, wui.Text("gap-a"), wui.Text("gap-b")), + wui.BoxStyled(wui.Column, 1, wui.Style{}, + wui.Text("gap-c"), wui.When(false, func() wui.Element { return wui.Text("hidden") }), wui.Text("gap-d")), wui.Button("Prepend", func() wui.Msg { return prependMsg{} }, wui.WithID("prepend")), wui.List(false, wui.Map(m.rows, func(_ int, r string) wui.Element { return wui.Keyed(r, wui.Text("row-"+r)) diff --git a/gap_test.go b/gap_test.go new file mode 100644 index 0000000..d4ed48b --- /dev/null +++ b/gap_test.go @@ -0,0 +1,53 @@ +//go:build !js + +package wui + +import ( + "strings" + "testing" +) + +func TestBoxGapAndEmptyChildren(t *testing.T) { + hidden := When(false, func() Element { return Text("x") }) + tests := []struct { + name string + el Element + want string + }{ + {"column gap 1", BoxStyled(Column, 1, Style{}, Text("a"), Text("b")), "a\n \nb"}, + {"column gap 2", BoxStyled(Column, 2, Style{}, Text("a"), Text("b")), "a\n \n \nb"}, + {"column gap 1 with hidden When", BoxStyled(Column, 1, Style{}, Text("a"), hidden, Text("b")), "a\n \nb"}, + {"column gap 0 with Empty", BoxStyled(Column, 0, Style{}, Text("a"), Empty(), Text("b")), "a\nb"}, + {"column with keyed Empty", BoxStyled(Column, 1, Style{}, Text("a"), Keyed("k", Empty()), Text("b")), "a\n \nb"}, + {"row gap 1 with Empty", BoxStyled(Row, 1, Style{}, Text("a"), Empty(), Text("b")), "a b"}, + {"leading and trailing Empty", BoxStyled(Column, 1, Style{}, Empty(), Text("a"), Empty()), "a"}, + {"only Empty children", BoxStyled(Column, 2, Style{}, Empty(), Empty()), ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := RenderTUISized(tt.el, 20, 10); got != tt.want { + t.Errorf("got %q, want %q", got, tt.want) + } + }) + } +} + +func TestCollapsedWrapRowKeepsItsGap(t *testing.T) { + el := BoxEl{Direction: Row, Gap: 2, Wrap: true, Style: Style{Width: 10}, Children: []Element{ + BoxEl{Style: Style{Width: 8}, Children: []Element{Text("left")}}, + BoxEl{Style: Style{Width: 8}, Children: []Element{Text("right")}}, + }} + lines := strings.Split(RenderTUISized(el, 40, 10), "\n") + if len(lines) != 4 || strings.TrimSpace(lines[0]) != "left" || strings.TrimSpace(lines[3]) != "right" { + t.Errorf("collapsed Gap:2 row should put 2 blank lines between panes, got %q", lines) + } +} + +func TestFillHeightColumnWithGapFitsItsHeight(t *testing.T) { + el := BoxStyled(Column, 1, Style{Height: Fill}, + Text("top"), Grow(1, Text("middle")), Text("bottom")) + out := RenderTUISized(el, 20, 10) + if got := len(strings.Split(out, "\n")); got != 10 { + t.Errorf("Fill-height column with Gap:1 is %d lines in a 10-line terminal:\n%s", got, out) + } +} diff --git a/render_tui.go b/render_tui.go index 580c27c..095ff66 100644 --- a/render_tui.go +++ b/render_tui.go @@ -628,6 +628,7 @@ func (r *tuiRenderer) resolveHeight(h int) int { } func (r *tuiRenderer) renderBox(e BoxEl) string { + children := laidOutChildren(e.Children) width := r.resolveWidth(e.Style.Width) height := r.resolveHeight(e.Style.Height) @@ -660,12 +661,12 @@ func (r *tuiRenderer) renderBox(e BoxEl) string { childAvail = r.availWidth() - e.Style.Padding[1] - e.Style.Padding[3] - borderCells(e.Style) } shareAvail := childAvail - if shareAvail > 0 && e.Direction == Row && len(e.Children) > 1 { - shareAvail = (shareAvail - e.Gap*(len(e.Children)-1)) / len(e.Children) + if shareAvail > 0 && e.Direction == Row && len(children) > 1 { + shareAvail = (shareAvail - e.Gap*(len(children)-1)) / len(children) } keysBefore, picksBefore := r.buttonKeys.clone(), len(r.picks) - parts := make([]string, 0, len(e.Children)) - for _, c := range e.Children { + parts := make([]string, 0, len(children)) + for _, c := range children { w := shareAvail if s, ok := styleOf(c); ok && s.Width == Fill { w = childAvail @@ -678,13 +679,13 @@ func (r *tuiRenderer) renderBox(e BoxEl) string { } dir := e.Direction - if e.Wrap && dir == Row && innerW > 0 && !rowFits(e.Children, parts, e.Gap, innerW) { + if e.Wrap && dir == Row && innerW > 0 && !rowFits(children, parts, e.Gap, innerW) { // The children do not fit side by side: collapse to a // vertical stack, the TUI's flex-wrap. Growing children take // the full width so the collapsed panes stay aligned. dir = Column r.buttonKeys, r.picks = keysBefore, r.picks[:picksBefore] - for i, c := range e.Children { + for i, c := range children { parts[i] = r.withAvail(childAvail, func() string { return r.renderEl(c) }) if s, ok := styleOf(c); ok && s.Grow > 0 { parts[i] = lipgloss.NewStyle().Width(innerW).Render(parts[i]) @@ -701,20 +702,21 @@ func (r *tuiRenderer) renderBox(e BoxEl) string { if distW <= 0 { distW = r.availWidth() - e.Style.Padding[1] - e.Style.Padding[3] - borderCells(e.Style) } - distributeRow(e.Children, parts, e.Gap, distW) + distributeRow(children, parts, e.Gap, distW) if e.Align == AlignStretch && innerH > 0 { for i := range parts { parts[i] = lipgloss.NewStyle().Height(innerH).Render(parts[i]) } } } else { - distributeColumn(e.Children, parts, e.Gap, innerH) + distributeColumn(children, parts, e.Gap, innerH) } - if e.Gap > 0 { + if e.Gap > 0 && len(parts) > 1 { gap := strings.Repeat(" ", e.Gap) if dir == Column { - gap = strings.Repeat("\n", e.Gap) + // JoinVertical draws a part of n newlines as n+1 lines. + gap = strings.Repeat("\n", e.Gap-1) } joined := make([]string, 0, len(parts)*2-1) for i, p := range parts { @@ -795,6 +797,21 @@ func orElse(v, fallback int) int { return fallback } +// laidOutChildren returns the children a Box lays out: everything but +// Empty (and nil) children, which take no slot and no gap, as an empty +// comment node is not a flex item in the browser. +func laidOutChildren(children []Element) []Element { + out := children[:0:0] + for _, c := range children { + switch unkeyed(c).(type) { + case nil, EmptyEl: + continue + } + out = append(out, c) + } + return out +} + // flexWeight returns the flex-grow share of a Box child: its style's // Grow, with a zero-size (Flex) spacer defaulting to one share. func flexWeight(c Element) int {