Debian Codegraph @

lib/ui/grid.go

raw
1 package ui
2
3 import (
4 "math"
5 "sync"
6
7 "go.rockorager.dev/vaxis"
8 )
9
10 type Grid struct {
11 rows []GridSpec
12 rowLayout []gridLayout
13 columns []GridSpec
14 columnLayout []gridLayout
15
16 // Protected by mutex
17 cells []*GridCell
18 mutex sync.RWMutex
19 }
20
21 const (
22 SIZE_EXACT = iota
23 SIZE_WEIGHT = iota
24 )
25
26 // Specifies the layout of a single row or column
27 type GridSpec struct {
28 // One of SIZE_EXACT or SIZE_WEIGHT
29 Strategy int
30
31 // If Strategy = SIZE_EXACT, this function returns the number of cells
32 // this row/col shall occupy. If SIZE_WEIGHT, the space left after all
33 // exact rows/cols are measured is distributed amongst the remainder
34 // weighted by the value returned by this function.
35 Size func() int
36 }
37
38 // Used to cache layout of each row/column
39 type gridLayout struct {
40 Offset int
41 Size int
42 }
43
44 type GridCell struct {
45 Row int
46 Column int
47 RowSpan int
48 ColSpan int
49 Content Drawable
50 }
51
52 func NewGrid() *Grid {
53 return &Grid{}
54 }
55
56 // MakeGrid creates a grid with the specified number of columns and rows. Each
57 // cell has a size of 1.
58 func MakeGrid(numRows, numCols, rowStrategy, colStrategy int) *Grid {
59 rows := make([]GridSpec, numRows)
60 for i := range numRows {
61 rows[i] = GridSpec{rowStrategy, Const(1)}
62 }
63 cols := make([]GridSpec, numCols)
64 for i := range numCols {
65 cols[i] = GridSpec{colStrategy, Const(1)}
66 }
67 return NewGrid().Rows(rows).Columns(cols)
68 }
69
70 func (cell *GridCell) At(row, col int) *GridCell {
71 cell.Row = row
72 cell.Column = col
73 return cell
74 }
75
76 func (cell *GridCell) Span(rows, cols int) *GridCell {
77 cell.RowSpan = rows
78 cell.ColSpan = cols
79 return cell
80 }
81
82 func (grid *Grid) Rows(spec []GridSpec) *Grid {
83 grid.rows = spec
84 return grid
85 }
86
87 func (grid *Grid) Columns(spec []GridSpec) *Grid {
88 grid.columns = spec
89 return grid
90 }
91
92 func (grid *Grid) Draw(ctx *Context) {
93 grid.reflow(ctx)
94
95 grid.mutex.RLock()
96 defer grid.mutex.RUnlock()
97
98 for _, cell := range grid.cells {
99 rows := grid.rowLayout[cell.Row : cell.Row+cell.RowSpan]
100 cols := grid.columnLayout[cell.Column : cell.Column+cell.ColSpan]
101 x := cols[0].Offset
102 y := rows[0].Offset
103 if x < 0 || y < 0 {
104 continue
105 }
106
107 width := 0
108 height := 0
109 for _, col := range cols {
110 width += col.Size
111 }
112 for _, row := range rows {
113 height += row.Size
114 }
115 if x+width > ctx.Width() {
116 width = ctx.Width() - x
117 }
118 if y+height > ctx.Height() {
119 height = ctx.Height() - y
120 }
121 if width <= 0 || height <= 0 {
122 continue
123 }
124 subctx := ctx.Subcontext(x, y, width, height)
125 if cell.Content != nil {
126 cell.Content.Draw(subctx)
127 }
128 }
129 }
130
131 func (grid *Grid) MouseEvent(localX int, localY int, event vaxis.Event) {
132 if event, ok := event.(vaxis.Mouse); ok {
133
134 grid.mutex.RLock()
135 defer grid.mutex.RUnlock()
136
137 if grid.rowLayout == nil || grid.columnLayout == nil {
138 // This can happen if the grid has not been drawn yet
139 // (and reflow() never called); just ignore the event.
140 return
141 }
142
143 for _, cell := range grid.cells {
144 rows := grid.rowLayout[cell.Row : cell.Row+cell.RowSpan]
145 cols := grid.columnLayout[cell.Column : cell.Column+cell.ColSpan]
146 x := cols[0].Offset
147 y := rows[0].Offset
148 width := 0
149 height := 0
150 for _, col := range cols {
151 width += col.Size
152 }
153 for _, row := range rows {
154 height += row.Size
155 }
156 if x <= localX && localX < x+width && y <= localY && localY < y+height {
157 switch content := cell.Content.(type) {
158 case MouseableDrawableInteractive:
159 content.MouseEvent(localX-x, localY-y, event)
160 case Mouseable:
161 content.MouseEvent(localX-x, localY-y, event)
162 case MouseHandler:
163 content.MouseEvent(localX-x, localY-y, event)
164 }
165 }
166 }
167 }
168 }
169
170 func (grid *Grid) reflow(ctx *Context) {
171 grid.mutex.Lock()
172 defer grid.mutex.Unlock()
173
174 grid.rowLayout = nil
175 grid.columnLayout = nil
176 flow := func(specs *[]GridSpec, layouts *[]gridLayout, extent int) {
177 exact := 0
178 weight := 0
179 nweights := 0
180 for _, spec := range *specs {
181 if spec.Strategy == SIZE_EXACT {
182 exact += spec.Size()
183 } else if spec.Strategy == SIZE_WEIGHT {
184 nweights += 1
185 weight += spec.Size()
186 }
187 }
188 offset := 0
189 remainingExact := 0
190 if weight > 0 {
191 remainingExact = (extent - exact) % weight
192 }
193 for _, spec := range *specs {
194 layout := gridLayout{Offset: offset}
195 if spec.Strategy == SIZE_EXACT {
196 layout.Size = spec.Size()
197 } else if spec.Strategy == SIZE_WEIGHT {
198 proportion := float64(spec.Size()) / float64(weight)
199 size := proportion * float64(extent-exact)
200 if remainingExact > 0 {
201 extraExact := int(math.Ceil(proportion * float64(remainingExact)))
202 layout.Size = int(math.Floor(size)) + extraExact
203 remainingExact -= extraExact
204
205 } else {
206 layout.Size = int(math.Floor(size))
207 }
208 }
209 offset += layout.Size
210 *layouts = append(*layouts, layout)
211 }
212 }
213 flow(&grid.rows, &grid.rowLayout, ctx.Height())
214 flow(&grid.columns, &grid.columnLayout, ctx.Width())
215 }
216
217 func (grid *Grid) Invalidate() {
218 Invalidate()
219 }
220
221 func (grid *Grid) AddChild(content Drawable) *GridCell {
222 cell := &GridCell{
223 RowSpan: 1,
224 ColSpan: 1,
225 Content: content,
226 }
227 grid.mutex.Lock()
228 grid.cells = append(grid.cells, cell)
229 grid.mutex.Unlock()
230 grid.Invalidate()
231 return cell
232 }
233
234 func (grid *Grid) RemoveChild(content Drawable) {
235 grid.mutex.Lock()
236 for i, cell := range grid.cells {
237 if cell.Content == content {
238 grid.cells = append(grid.cells[:i], grid.cells[i+1:]...)
239 break
240 }
241 }
242 grid.mutex.Unlock()
243 grid.Invalidate()
244 }
245
246 func (grid *Grid) ReplaceChild(old Drawable, new Drawable) {
247 grid.mutex.Lock()
248 for i, cell := range grid.cells {
249 if cell.Content == old {
250 grid.cells[i] = &GridCell{
251 RowSpan: cell.RowSpan,
252 ColSpan: cell.ColSpan,
253 Row: cell.Row,
254 Column: cell.Column,
255 Content: new,
256 }
257 break
258 }
259 }
260 grid.mutex.Unlock()
261 grid.Invalidate()
262 }
263
264 func Const(i int) func() int {
265 return func() int { return i }
266 }