Debian Codegraph @

grid.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // mauview - A Go TUI library based on tcell.
2 // Copyright © 2019 Tulir Asokan
3 //
4 // This Source Code Form is subject to the terms of the Mozilla Public
5 // License, v. 2.0. If a copy of the MPL was not distributed with this
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 package mauview
9
10 import (
•11 "github.com/gdamore/tcell/v2"
12 )
13
14 type gridChild struct {
15 genericChild
16 relWidth int
17 relHeight int
18 relX int
19 relY int
20 }
21
22 type Grid struct {
23 screen Screen
24 children []*gridChild
25 focused *gridChild
26
27 focusReceived bool
28
29 prevWidth int
30 prevHeight int
31 forceResize bool
32
33 columnWidths []int
34 rowHeights []int
35
36 onFocusChanged func(from, to Component)
37 }
38
39 func NewGrid() *Grid {
40 return &Grid{
41 children: []*gridChild{},
42 focused: nil,
43 prevWidth: -1,
44 prevHeight: -1,
45 forceResize: false,
46 columnWidths: []int{-1},
47 rowHeights: []int{-1},
48 }
49 }
50
51 func extend(arr []int, newSize int) []int {
52 newArr := make([]int, newSize)
53 copy(newArr, arr)
54 for i := len(arr); i < len(newArr); i++ {
55 newArr[i] = -1
56 }
57 return newArr
58 }
59
60 func (form *Form) SetOnFocusChanged(fn func(from, to Component)) *Form {
61 form.onFocusChanged = fn
62 return form
63 }
64
65 func (grid *Grid) createChild(comp Component, x, y, width, height int) *gridChild {
66 if x+width >= len(grid.columnWidths) {
67 grid.columnWidths = extend(grid.columnWidths, x+width)
68 }
69 if y+height >= len(grid.rowHeights) {
70 grid.rowHeights = extend(grid.rowHeights, y+height)
71 }
72 return &gridChild{
73 genericChild: genericChild{
74 screen: &ProxyScreen{Parent: grid.screen, Style: tcell.StyleDefault},
75 target: comp,
76 },
77 relWidth: width,
78 relHeight: height,
79 relX: x,
80 relY: y,
81 }
82 }
83
84 func (grid *Grid) addChild(child *gridChild) {
85 if child.relX+child.relWidth >= len(grid.columnWidths) {
86 grid.columnWidths = extend(grid.columnWidths, child.relX+child.relWidth)
87 }
88 if child.relY+child.relHeight >= len(grid.rowHeights) {
89 grid.rowHeights = extend(grid.rowHeights, child.relY+child.relHeight)
90 }
91 grid.children = append(grid.children, child)
92 grid.forceResize = true
93 }
94
95 func (grid *Grid) AddComponent(comp Component, x, y, width, height int) *Grid {
96 grid.addChild(grid.createChild(comp, x, y, width, height))
97 return grid
98 }
99
100 func (grid *Grid) RemoveComponent(comp Component) *Grid {
101 for index := len(grid.children) - 1; index >= 0; index-- {
102 if grid.children[index].target == comp {
103 grid.children = append(grid.children[:index], grid.children[index+1:]...)
104 }
105 }
106 return grid
107 }
108
109 func (grid *Grid) SetColumn(col, width int) *Grid {
110 if col >= len(grid.columnWidths) {
111 grid.columnWidths = extend(grid.columnWidths, col+1)
112 }
113 grid.columnWidths[col] = width
114 return grid
115 }
116
117 func (grid *Grid) SetRow(row, height int) *Grid {
118 if row >= len(grid.rowHeights) {
119 grid.rowHeights = extend(grid.rowHeights, row+1)
120 }
121 grid.rowHeights[row] = height
122 return grid
123 }
124
125 func (grid *Grid) SetColumns(columns []int) *Grid {
126 grid.columnWidths = columns
127 return grid
128 }
129
130 func (grid *Grid) SetRows(rows []int) *Grid {
131 grid.rowHeights = rows
132 return grid
133 }
134
135 func pnSum(arr []int) (int, int) {
136 positive := 0
137 negative := 0
138 for _, i := range arr {
139 if i < 0 {
140 negative -= i
141 } else {
142 positive += i
143 }
144 }
145 return positive, negative
146 }
147
148 func fillDynamic(arr []int, size, dynamicItems int) []int {
149 if dynamicItems == 0 {
150 return arr
151 }
152 part := size / dynamicItems
153 remainder := size % dynamicItems
154 newArr := make([]int, len(arr))
155 for i, val := range arr {
156 if val < 0 {
157 newArr[i] = part * -val
158 if remainder > 0 {
159 remainder--
160 newArr[i]++
161 }
162 } else {
163 newArr[i] = val
164 }
165 }
166 return newArr
167 }
168
169 func (grid *Grid) OnResize(width, height int) {
170 absColWidth, dynamicColumns := pnSum(grid.columnWidths)
171 columnWidths := fillDynamic(grid.columnWidths, width-absColWidth, dynamicColumns)
172 absRowHeight, dynamicRows := pnSum(grid.rowHeights)
173 rowHeights := fillDynamic(grid.rowHeights, height-absRowHeight, dynamicRows)
174 for _, child := range grid.children {
175 child.screen.OffsetX, _ = pnSum(columnWidths[:child.relX])
176 child.screen.OffsetY, _ = pnSum(rowHeights[:child.relY])
177 child.screen.Width, _ = pnSum(columnWidths[child.relX : child.relX+child.relWidth])
178 child.screen.Height, _ = pnSum(rowHeights[child.relY : child.relY+child.relHeight])
179 }
180 grid.prevWidth, grid.prevHeight = width, height
181 }
182
183 func (grid *Grid) Draw(screen Screen) {
184 width, height := screen.Size()
185 if grid.forceResize || grid.prevWidth != width || grid.prevHeight != height {
186 grid.OnResize(screen.Size())
187 }
188 grid.forceResize = false
189 screen.Clear()
190 screenChanged := false
191 if screen != grid.screen {
192 grid.screen = screen
193 screenChanged = true
194 }
195 for _, child := range grid.children {
196 if screenChanged {
197 child.screen.Parent = screen
198 }
199 if grid.focused == nil || child != grid.focused {
200 child.target.Draw(child.screen)
201 }
202 }
203 if grid.focused != nil {
204 grid.focused.target.Draw(grid.focused.screen)
205 }
206 }
207
208 func (grid *Grid) OnKeyEvent(event KeyEvent) bool {
209 if grid.focused != nil {
210 return grid.focused.target.OnKeyEvent(event)
211 }
212 return false
213 }
214
215 func (grid *Grid) OnPasteEvent(event PasteEvent) bool {
216 if grid.focused != nil {
217 return grid.focused.target.OnPasteEvent(event)
218 }
219 return false
220 }
221
222 func (grid *Grid) setFocused(item *gridChild) {
223 if grid.focused != nil {
224 grid.focused.Blur()
225 }
226 var prevFocus, newFocus Component
227 if grid.focused != nil {
228 prevFocus = grid.focused.target
229 }
230 if item != nil {
231 newFocus = item.target
232 }
233 grid.focused = item
234 if grid.focusReceived && grid.focused != nil {
235 grid.focused.Focus()
236 }
237 if grid.onFocusChanged != nil {
238 grid.onFocusChanged(prevFocus, newFocus)
239 }
240 }
241 func (grid *Grid) OnMouseEvent(event MouseEvent) bool {
242 if grid.focused != nil && grid.focused.screen.IsInArea(event.Position()) {
243 screen := grid.focused.screen
244 return grid.focused.target.OnMouseEvent(OffsetMouseEvent(event, -screen.OffsetX, -screen.OffsetY))
245 }
246 for _, child := range grid.children {
247 if child.screen.IsInArea(event.Position()) {
248 focusChanged := false
249 if event.Buttons() == tcell.Button1 && !event.HasMotion() {
250 grid.setFocused(child)
251 focusChanged = true
252 }
253 return child.target.OnMouseEvent(OffsetMouseEvent(event, -child.screen.OffsetX, -child.screen.OffsetY)) ||
254 focusChanged
255
256 }
257 }
258 if event.Buttons() == tcell.Button1 && !event.HasMotion() && grid.focused != nil {
259 grid.setFocused(nil)
260 return true
261 }
262 return false
263 }
264
265 func (grid *Grid) Focus() {
266 grid.focusReceived = true
267 if grid.focused != nil {
268 grid.focused.Focus()
269 }
270 }
271
272 func (grid *Grid) Blur() {
273 if grid.focused != nil {
274 grid.setFocused(nil)
275 }
276 grid.focusReceived = false
277 }