shithub: mycel

Download patch

ref: 8970feb8e2dd65bdbda47006affff7753a7ccf60
parent: f15e2c892c85fa50584582961c99fa04ef17b0a9
author: Philip Silva <philip.silva@protonmail.com>
date: Thu Jul 29 08:08:40 EDT 2021

Revert "Simplify tables"

This reverts commit f15e2c892c85fa50584582961c99fa04ef17b0a9.

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -1014,40 +1014,59 @@
 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
 	}
 
-	uis := make([]duit.UI, 0, numRows*numCols)
+	if useOneGrid {
+		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))
+			}
 		}
-		for i := len(row.columns); i < numCols; i++ {
-			uis = append(uis, &duitx.Label{})
+
+		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)
 		}
-	}
 
-	halign := make([]duit.Halign, 0, len(uis))
-	valign := make([]duit.Valign, 0, len(uis))
+		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))
 
-	for i := 0; i < numCols; i++ {
-		halign = append(halign, duit.HalignLeft)
-		valign = append(valign, duit.ValignTop)
-	}
+		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)
+				}
+			}
 
-	return NewElement(
-		&duitx.Grid{
-			Columns: numCols,
-			Padding: duit.NSpace(numCols, duit.SpaceXY(0, 3)),
-			Halign:  halign,
-			Valign:  valign,
-			Kids:    duit.NewKids(uis...),
-		},
-		n,
-	)
+			if len(rowEls) > 0 {
+				seq := horizontalSeq(nil, false, rowEls)
+				seqs = append(seqs, NewElement(seq, row.n))
+			}
+		}
+		return NewElement(verticalSeq(seqs), n)
+	}
 }
 
 type TableRow struct {
--- a/browser/duitx/grid.go
+++ b/browser/duitx/grid.go
@@ -23,7 +23,6 @@
 import (
 	"fmt"
 	"image"
-	"math"
 
 	"9fans.net/go/draw"
 	"github.com/mjl-/duit"
@@ -91,14 +90,7 @@
 		space := spaces[col]
 		for i := col; i < len(ui.Kids); i += ui.Columns {
 			k := ui.Kids[i]
-
-			// 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)
+			k.UI.Layout(dui, k, image.Pt(sizeAvail.X-width-space.Dx(), sizeAvail.Y-space.Dy()), true)
 			newDx = maximum(newDx, k.R.Dx()+space.Dx())
 		}
 		ui.widths[col] = newDx