column.go raw

   1  package gel
   2  
   3  import (
   4  	l "github.com/p9c/gio/layout"
   5  )
   6  
   7  type ColumnRow struct {
   8  	Label string
   9  	W     l.Widget
  10  }
  11  
  12  type Rows []ColumnRow
  13  
  14  type Column struct {
  15  	*Window
  16  	rows              []ColumnRow
  17  	font              string
  18  	scale             float32
  19  	color, background string
  20  	list              *List
  21  }
  22  
  23  func (w *Window) Column(rows Rows, font string, scale float32, color string,
  24  	background string) *Column {
  25  	return &Column{Window: w, rows: rows, font: font, scale: scale, color: color,
  26  		background: background, list: w.List()}
  27  }
  28  
  29  func (c *Column) Fn(gtx l.Context) l.Dimensions {
  30  	max, list := c.List(gtx)
  31  	out := c.Theme.SliceToWidget(list, l.Vertical)
  32  	gtx.Constraints.Max.X = max
  33  	return out(gtx)
  34  }
  35  
  36  func (c *Column) List(gtx l.Context) (max int, out []l.Widget) {
  37  	le := func(gtx l.Context, index int) l.Dimensions {
  38  		return c.H6(c.rows[index].Label).Font(c.font).TextScale(c.scale).Fn(gtx)
  39  	}
  40  	// render the widgets onto a second context to get their dimensions
  41  	gtx1 := CopyContextDimensionsWithMaxAxis(gtx, l.Vertical)
  42  	// generate the dimensions for all the list elements
  43  	dims := GetDimensionList(gtx1, len(c.rows), le)
  44  	D.S(dims)
  45  	for i := range dims {
  46  		if dims[i].Size.X > max {
  47  			max = dims[i].Size.X
  48  		}
  49  	}
  50  	for x := range c.rows {
  51  		i := x
  52  		_ = i
  53  		out = append(out, func(gtx l.Context) l.Dimensions {
  54  			return c.Flex(). // AlignEnd().SpaceStart().
  55  				Rigid(
  56  					c.Fill("red", l.Center, c.TextSize.V, 0, EmptySpace(max-dims[i].Size.X, dims[i].Size.Y)).Fn,
  57  				).
  58  				Rigid(
  59  					c.Inset(0.5, func(gtx l.Context) l.Dimensions {
  60  						// D.Ln("max!", m)
  61  						// // gtx.Constraints.Max.X = max
  62  						// gtx.Constraints.Max.Y = dims[i].Size.Y
  63  						// gtx.Constraints.Min.Y = dims[i].Size.Y
  64  						// gtx.Constraints.Min.X = max
  65  						// gtx.Constraints.Constrain(image.Point{X: max, Y: dims[i].Size.Y})
  66  						// gtx.Constraints.Max.X = max
  67  						// gtx.Constraints.Min.Y = 0
  68  						// gtx.Constraints.Max.Y = dims[i].Size.Y
  69  						// gtx.Constraints.Constrain(dims[i].Size)
  70  						dms := c.Label().
  71  							Text(c.rows[i].Label).
  72  							Font(c.font).
  73  							TextScale(c.scale).
  74  							Color(c.color).
  75  							Fn(gtx)
  76  						return dms
  77  					}).Fn,
  78  				).
  79  				Rigid(
  80  					c.Inset(0.5,
  81  						c.rows[i].W,
  82  					).Fn,
  83  				).
  84  				Fn(gtx)
  85  			// return c.Theme.Fill("Primary",
  86  			// 	c.Theme.Flex().AlignEnd().SpaceBetween().
  87  			// 		Rigid(
  88  			// 		).Fn,
  89  			// ).
  90  			// Fn(gtx)
  91  		})
  92  	}
  93  	// le = func(gtx l.Context, index int) l.Dimensions {
  94  	// 	return out[index](gtx)
  95  	// }
  96  	return max, out
  97  	
  98  	// // render the widgets onto a second context to get their dimensions
  99  	// gtx1 = CopyContextDimensionsWithMaxAxis(gtx, gtx.Constraints.Max, l.Vertical)
 100  	// dim := GetDimension(gtx1, c.Theme.SliceToWidget(out, l.Vertical))
 101  	// max = dim.Size.X
 102  	// return
 103  }
 104