shithub: mycel

Download patch

ref: f15e2c892c85fa50584582961c99fa04ef17b0a9
parent: fc060bb0db3672c1cb6cfe4ceab1227f86746945
author: Philip Silva <philip.silva@protonmail.com>
date: Wed Jul 28 09:38:56 EDT 2021

Simplify tables

- always use Grid
- prevent individual columns from taking away too much space

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -1014,59 +1014,40 @@
 func (t *Table) Element(r int, b *Browser, n *nodes.Node) *Element {
 	numRows := len(t.rows)
 	numCols := t.numColsMax()
-	useOneGrid := t.numColsMin() == t.numColsMax()
 
 	if numCols == 0 {
 		return nil
 	}
 
-	if useOneGrid {
-		uis := make([]duit.UI, 0, numRows*numCols)
+	uis := make([]duit.UI, 0, numRows*numCols)
 
-		for _, row := range t.rows {
-			for _, td := range row.columns {
-				uis = append(uis, NodeToBox(r+1, b, td))
-			}
+	for _, row := range t.rows {
+		for _, td := range row.columns {
+			uis = append(uis, NodeToBox(r+1, b, td))
 		}
-
-		halign := make([]duit.Halign, 0, len(uis))
-		valign := make([]duit.Valign, 0, len(uis))
-
-		for i := 0; i < numCols; i++ {
-			halign = append(halign, duit.HalignLeft)
-			valign = append(valign, duit.ValignTop)
+		for i := len(row.columns); i < numCols; i++ {
+			uis = append(uis, &duitx.Label{})
 		}
+	}
 
-		return NewElement(
-			&duitx.Grid{
-				Columns: numCols,
-				Padding: duit.NSpace(numCols, duit.SpaceXY(0, 3)),
-				Halign:  halign,
-				Valign:  valign,
-				Kids:    duit.NewKids(uis...),
-			},
-			n,
-		)
-	} else {
-		seqs := make([]*Element, 0, len(t.rows))
+	halign := make([]duit.Halign, 0, len(uis))
+	valign := make([]duit.Valign, 0, len(uis))
 
-		for _, row := range t.rows {
-			rowEls := make([]*Element, 0, len(row.columns))
-			for _, col := range row.columns {
-				ui := NodeToBox(r+1, b, col)
-				if ui != nil {
-					el := NewElement(ui, col)
-					rowEls = append(rowEls, el)
-				}
-			}
-
-			if len(rowEls) > 0 {
-				seq := horizontalSeq(nil, false, rowEls)
-				seqs = append(seqs, NewElement(seq, row.n))
-			}
-		}
-		return NewElement(verticalSeq(seqs), n)
+	for i := 0; i < numCols; i++ {
+		halign = append(halign, duit.HalignLeft)
+		valign = append(valign, duit.ValignTop)
 	}
+
+	return NewElement(
+		&duitx.Grid{
+			Columns: numCols,
+			Padding: duit.NSpace(numCols, duit.SpaceXY(0, 3)),
+			Halign:  halign,
+			Valign:  valign,
+			Kids:    duit.NewKids(uis...),
+		},
+		n,
+	)
 }
 
 type TableRow struct {
--- a/browser/duitx/grid.go
+++ b/browser/duitx/grid.go
@@ -23,6 +23,7 @@
 import (
 	"fmt"
 	"image"
+	"math"
 
 	"9fans.net/go/draw"
 	"github.com/mjl-/duit"
@@ -90,7 +91,14 @@
 		space := spaces[col]
 		for i := col; i < len(ui.Kids); i += ui.Columns {
 			k := ui.Kids[i]
-			k.UI.Layout(dui, k, image.Pt(sizeAvail.X-width-space.Dx(), sizeAvail.Y-space.Dy()), true)
+
+			// Prevent wide columns to use up all space at once
+			colsLeft := len(ui.Kids)-i
+			wAvail := sizeAvail.X-width-space.Dx()
+			wAvail = int(float64(wAvail) / (float64(colsLeft)/math.Phi))
+			yAvail := sizeAvail.Y-space.Dy()
+
+			k.UI.Layout(dui, k, image.Pt(wAvail, yAvail), true)
 			newDx = maximum(newDx, k.R.Dx()+space.Dx())
 		}
 		ui.widths[col] = newDx