aerc/ui/grid.go

182 lines
3.7 KiB
Go
Raw Normal View History

2018-02-16 06:05:07 +01:00
package ui
import (
"fmt"
"math"
)
2018-02-16 06:05:07 +01:00
type Grid struct {
Rows []DimSpec
2018-02-17 21:21:22 +01:00
rowLayout []dimLayout
2018-02-16 06:05:07 +01:00
Columns []DimSpec
2018-02-17 21:21:22 +01:00
columnLayout []dimLayout
2018-02-16 06:05:07 +01:00
Cells []*GridCell
onInvalidate func(d Drawable)
2018-02-17 21:21:22 +01:00
invalid bool
2018-02-16 06:05:07 +01:00
}
const (
SIZE_EXACT = iota
SIZE_WEIGHT = iota
)
// Specifies the layout of a single row or column
type DimSpec struct {
// One of SIZE_EXACT or SIZE_WEIGHT
2018-02-17 21:21:22 +01:00
Strategy int
2018-02-16 06:05:07 +01:00
// If Strategy = SIZE_EXACT, this is the number of cells this dim shall
// occupy. If SIZE_WEIGHT, the space left after all exact dims are measured
// is distributed amonst the remaining dims weighted by this value.
2018-02-17 21:21:22 +01:00
Size int
}
// Used to cache layout of each row/column
type dimLayout struct {
Offset int
Size int
2018-02-16 06:05:07 +01:00
}
type GridCell struct {
2018-02-17 21:21:22 +01:00
Row int
Column int
RowSpan int
ColSpan int
2018-02-16 06:05:07 +01:00
Content Drawable
invalid bool
}
func NewGrid() *Grid {
return &Grid{invalid: true}
}
func (cell *GridCell) At(row, col int) *GridCell {
cell.Row = row
cell.Column = col
return cell
}
func (cell *GridCell) Span(rows, cols int) *GridCell {
cell.RowSpan = rows
cell.ColSpan = cols
return cell
}
2018-02-17 21:21:22 +01:00
func (grid *Grid) Draw(ctx *Context) {
invalid := grid.invalid
if invalid {
grid.reflow(ctx)
}
for _, cell := range grid.Cells {
if !cell.invalid && !invalid {
continue
}
rows := grid.rowLayout[cell.Row : cell.Row+cell.RowSpan]
cols := grid.columnLayout[cell.Column : cell.Column+cell.ColSpan]
2018-02-17 21:21:22 +01:00
x := cols[0].Offset
y := rows[0].Offset
width := 0
height := 0
for _, col := range cols {
width += col.Size
}
for _, row := range rows {
height += row.Size
2018-02-17 21:21:22 +01:00
}
subctx := ctx.Subcontext(x, y, width, height)
cell.Content.Draw(subctx)
}
}
func (grid *Grid) reflow(ctx *Context) {
grid.rowLayout = nil
grid.columnLayout = nil
flow := func(specs *[]DimSpec, layouts *[]dimLayout, extent int) {
exact := 0
weight := 0
nweights := 0
2018-02-17 21:21:22 +01:00
for _, dim := range *specs {
if dim.Strategy == SIZE_EXACT {
exact += dim.Size
} else if dim.Strategy == SIZE_WEIGHT {
nweights += 1
2018-02-17 21:21:22 +01:00
weight += dim.Size
}
}
offset := 0
for _, dim := range *specs {
layout := dimLayout{Offset: offset}
if dim.Strategy == SIZE_EXACT {
layout.Size = dim.Size
} else if dim.Strategy == SIZE_WEIGHT {
size := float64(dim.Size) / float64(weight)
size *= float64(extent - exact)
layout.Size = int(math.Floor(size))
2018-02-17 21:21:22 +01:00
}
offset += layout.Size
2018-02-17 21:21:22 +01:00
*layouts = append(*layouts, layout)
}
}
flow(&grid.Rows, &grid.rowLayout, ctx.Height())
flow(&grid.Columns, &grid.columnLayout, ctx.Width())
2018-02-17 21:21:22 +01:00
grid.invalid = false
}
func (grid *Grid) invalidateLayout() {
2018-02-17 21:21:22 +01:00
grid.invalid = true
if grid.onInvalidate != nil {
grid.onInvalidate(grid)
}
}
func (grid *Grid) Invalidate() {
grid.invalidateLayout()
for _, cell := range grid.Cells {
cell.Content.Invalidate()
}
2018-02-16 06:05:07 +01:00
}
func (grid *Grid) OnInvalidate(onInvalidate func(d Drawable)) {
grid.onInvalidate = onInvalidate
}
func (grid *Grid) AddChild(content Drawable) *GridCell {
cell := &GridCell{
RowSpan: 1,
ColSpan: 1,
Content: content,
invalid: true,
}
2018-02-16 06:05:07 +01:00
grid.Cells = append(grid.Cells, cell)
cell.Content.OnInvalidate(grid.cellInvalidated)
cell.invalid = true
grid.invalidateLayout()
return cell
2018-02-16 06:05:07 +01:00
}
func (grid *Grid) RemoveChild(cell *GridCell) {
for i, _cell := range grid.Cells {
if _cell == cell {
grid.Cells = append(grid.Cells[:i], grid.Cells[i+1:]...)
break
}
}
grid.invalidateLayout()
2018-02-16 06:05:07 +01:00
}
func (grid *Grid) cellInvalidated(drawable Drawable) {
var cell *GridCell
for _, cell = range grid.Cells {
if cell.Content == drawable {
break
}
cell = nil
}
if cell == nil {
panic(fmt.Errorf("Attempted to invalidate unknown cell"))
}
cell.invalid = true
if grid.onInvalidate != nil {
grid.onInvalidate(grid)
}
}