Debian Codegraph @

internal/views/splits.go

raw
1 package views
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 type SplitType uint8
9
10 const (
11 STVert = 0
12 STHoriz = 1
13 STUndef = 2
14 )
15
16 var idcounter uint64
17
18 // NewID returns a new unique id
19 func NewID() uint64 {
20 idcounter++
21 return idcounter
22 }
23
24 // A View is a size and location of a split
25 type View struct {
26 X, Y int
27 W, H int
28 }
29
30 // A Node describes a split in the tree
31 // If a node is a leaf node then it corresponds to a buffer that is being
32 // displayed otherwise it has a number of children of the opposite type
33 // (vertical splits have horizontal children and vice versa)
34 type Node struct {
35 View
36
37 Kind SplitType
38
39 parent *Node
40 children []*Node
41
42 // Nodes can be marked as non resizable if they shouldn't be rescaled
43 // when the terminal window is resized or when a new split is added
44 // Only the splits on the edges of the screen can be marked as non resizable
45 canResize bool
46 // A node may also be marked with proportional scaling. This means that when
47 // the window is resized the split maintains its proportions
48 propScale bool
49
50 // Defines the proportion of the screen this node should take up if propScale is
51 // on
52 propW, propH float64
53 // The id is unique for each leaf node and provides a way to keep track of a split
54 // The id cannot be 0
55 id uint64
56 }
57
58 // NewNode returns a new node with the given specifications
59 func NewNode(Kind SplitType, x, y, w, h int, parent *Node, id uint64) *Node {
60 n := new(Node)
61 n.Kind = Kind
62 n.canResize = true
63 n.propScale = true
64 n.X, n.Y, n.W, n.H = x, y, w, h
65 n.children = make([]*Node, 0)
66 n.parent = parent
67 n.id = id
68 if parent != nil {
69 n.propW, n.propH = float64(w)/float64(parent.W), float64(h)/float64(parent.H)
70 } else {
71 n.propW, n.propH = 1, 1
72 }
73
74 return n
75 }
76
77 // NewRoot returns an empty Node with a size and location
78 // The type of the node will be determined by the first action on the node
79 // In other words, a lone split is neither horizontal nor vertical, it only
80 // becomes one or the other after a vsplit or hsplit is made
81 func NewRoot(x, y, w, h int) *Node {
82 n1 := NewNode(STUndef, x, y, w, h, nil, NewID())
83
84 return n1
85 }
86
87 // IsLeaf returns if this node is a leaf node
88 func (n *Node) IsLeaf() bool {
89 return len(n.children) == 0
90 }
91
92 // ID returns this node's id or 0 if it is not viewable
93 func (n *Node) ID() uint64 {
94 if n.IsLeaf() {
95 return n.id
96 }
97 return 0
98 }
99
100 // CanResize returns if this node can be resized
101 func (n *Node) CanResize() bool {
102 return n.canResize
103 }
104
105 // PropScale returns if this node is proportionally scaled
106 func (n *Node) PropScale() bool {
107 return n.propScale
108 }
109
110 // SetResize sets the resize flag
111 func (n *Node) SetResize(b bool) {
112 n.canResize = b
113 }
114
115 // SetPropScale sets the propScale flag
116 func (n *Node) SetPropScale(b bool) {
117 n.propScale = b
118 }
119
120 // Children returns this node's children
121 func (n *Node) Children() []*Node {
122 return n.children
123 }
124
125 // GetNode returns the node with the given id in the tree of children
126 // that this node has access to or nil if the node with that id cannot be found
127 func (n *Node) GetNode(id uint64) *Node {
128 if n.id == id && n.IsLeaf() {
129 return n
130 }
131 for _, c := range n.children {
132 if c.id == id && c.IsLeaf() {
133 return c
134 }
135 gc := c.GetNode(id)
136 if gc != nil {
137 return gc
138 }
139 }
140 return nil
141 }
142
143 func (n *Node) vResizeSplit(i int, size int) bool {
144 if i < 0 || i >= len(n.children) {
145 return false
146 }
147 var c1, c2 *Node
148 if i == len(n.children)-1 {
149 c1, c2 = n.children[i-1], n.children[i]
150 } else {
151 c1, c2 = n.children[i], n.children[i+1]
152 }
153 toth := c1.H + c2.H
154 if size >= toth {
155 return false
156 }
157 c2.Y = c1.Y + size
158 c1.Resize(c1.W, size)
159 c2.Resize(c2.W, toth-size)
160 n.markSizes()
161 n.alignSizes(n.W, n.H)
162 return true
163 }
164 func (n *Node) hResizeSplit(i int, size int) bool {
165 if i < 0 || i >= len(n.children) {
166 return false
167 }
168 var c1, c2 *Node
169 if i == len(n.children)-1 {
170 c1, c2 = n.children[i-1], n.children[i]
171 } else {
172 c1, c2 = n.children[i], n.children[i+1]
173 }
174 totw := c1.W + c2.W
175 if size >= totw {
176 return false
177 }
178 c2.X = c1.X + size
179 c1.Resize(size, c1.H)
180 c2.Resize(totw-size, c2.H)
181 n.markSizes()
182 n.alignSizes(n.W, n.H)
183 return true
184 }
185
186 // ResizeSplit resizes a certain split to a given size
187 func (n *Node) ResizeSplit(size int) bool {
188 // TODO: `size < 0` does not work for some reason
189 if size <= 0 {
190 return false
191 }
192 if len(n.parent.children) <= 1 {
193 // cannot resize a lone node
194 return false
195 }
196 ind := 0
197 for i, c := range n.parent.children {
198 if c.id == n.id {
199 ind = i
200 }
201 }
202 if n.parent.Kind == STVert {
203 return n.parent.vResizeSplit(ind, size)
204 }
205 return n.parent.hResizeSplit(ind, size)
206 }
207
208 // Resize sets this node's size and resizes all children accordingly
209 func (n *Node) Resize(w, h int) {
210 n.W, n.H = w, h
211
212 if n.IsLeaf() {
213 return
214 }
215
216 x, y := n.X, n.Y
217 totw, toth := 0, 0
218 for _, c := range n.children {
219 cW := int(float64(w) * c.propW)
220 cH := int(float64(h) * c.propH)
221
222 c.X, c.Y = x, y
223 c.Resize(cW, cH)
224 if n.Kind == STHoriz {
225 x += cW
226 totw += cW
227 } else {
228 y += cH
229 toth += cH
230 }
231 }
232
233 n.alignSizes(totw, toth)
234 }
235
236 func (n *Node) alignSizes(totw, toth int) {
237 // Make sure that there are no off-by-one problems with the rounding
238 // of the sizes by making the final split fill the screen
239 if n.Kind == STVert && toth != n.H {
240 last := n.children[len(n.children)-1]
241 last.Resize(last.W, last.H+n.H-toth)
242 } else if n.Kind == STHoriz && totw != n.W {
243 last := n.children[len(n.children)-1]
244 last.Resize(last.W+n.W-totw, last.H)
245 }
246 }
247
248 // Resets all proportions for children
249 func (n *Node) markSizes() {
250 for _, c := range n.children {
251 c.propW = float64(c.W) / float64(n.W)
252 c.propH = float64(c.H) / float64(n.H)
253 c.markSizes()
254 }
255 }
256
257 func (n *Node) markResize() {
258 n.markSizes()
259 n.Resize(n.W, n.H)
260 }
261
262 // vsplits a vertical split and returns the id of the new split
263 func (n *Node) vVSplit(right bool) uint64 {
264 ind := 0
265 for i, c := range n.parent.children {
266 if c.id == n.id {
267 ind = i
268 }
269 }
270 return n.parent.hVSplit(ind, right)
271 }
272
273 // hsplits a horizontal split
274 func (n *Node) hHSplit(bottom bool) uint64 {
275 ind := 0
276 for i, c := range n.parent.children {
277 if c.id == n.id {
278 ind = i
279 }
280 }
281 return n.parent.vHSplit(ind, bottom)
282 }
283
284 // Returns the size of the non-resizable area and the number of resizable
285 // splits
286 func (n *Node) getResizeInfo(h bool) (int, int) {
287 numr := 0
288 numnr := 0
289 nonr := 0
290 for _, c := range n.children {
291 if !c.CanResize() {
292 if h {
293 nonr += c.H
294 } else {
295 nonr += c.W
296 }
297 numnr++
298 } else {
299 numr++
300 }
301 }
302
303 // if there are no resizable splits make them all resizable
304 if numr == 0 {
305 numr = numnr
306 }
307
308 return nonr, numr
309 }
310
311 func (n *Node) applyNewSize(size int, h bool) {
312 a := n.X
313 if h {
314 a = n.Y
315 }
316 for _, c := range n.children {
317 if h {
318 c.Y = a
319 } else {
320 c.X = a
321 }
322 if c.CanResize() {
323 if h {
324 c.Resize(c.W, size)
325 } else {
326 c.Resize(size, c.H)
327 }
328 }
329 if h {
330 a += c.H
331 } else {
332 a += c.H
333 }
334 }
335 n.markResize()
336 }
337
338 // hsplits a vertical split
339 func (n *Node) vHSplit(i int, right bool) uint64 {
340 if n.IsLeaf() {
341 newid := NewID()
342 hn1 := NewNode(STHoriz, n.X, n.Y, n.W, n.H/2, n, n.id)
343 hn2 := NewNode(STHoriz, n.X, n.Y+hn1.H, n.W, n.H/2, n, newid)
344 if !right {
345 hn1.id, hn2.id = hn2.id, hn1.id
346 }
347
348 n.children = append(n.children, hn1, hn2)
349 n.markResize()
350 return newid
351 } else {
352 nonrh, numr := n.getResizeInfo(true)
353
354 // size of resizable area
355 height := (n.H - nonrh) / (numr + 1)
356
357 newid := NewID()
358 hn := NewNode(STHoriz, n.X, 0, n.W, height, n, newid)
359
360 // insert the node into the correct slot
361 n.children = append(n.children, nil)
362 inspos := i
363 if right {
364 inspos++
365 }
366 copy(n.children[inspos+1:], n.children[inspos:])
367 n.children[inspos] = hn
368
369 n.applyNewSize(height, true)
370 return newid
371 }
372 }
373
374 // vsplits a horizontal split
375 func (n *Node) hVSplit(i int, right bool) uint64 {
376 if n.IsLeaf() {
377 newid := NewID()
378 vn1 := NewNode(STVert, n.X, n.Y, n.W/2, n.H, n, n.id)
379 vn2 := NewNode(STVert, n.X+vn1.W, n.Y, n.W/2, n.H, n, newid)
380 if !right {
381 vn1.id, vn2.id = vn2.id, vn1.id
382 }
383
384 n.children = append(n.children, vn1, vn2)
385 n.markResize()
386 return newid
387 } else {
388 nonrw, numr := n.getResizeInfo(false)
389
390 width := (n.W - nonrw) / (numr + 1)
391
392 newid := NewID()
393 vn := NewNode(STVert, 0, n.Y, width, n.H, n, newid)
394
395 // Inser the node into the correct slot
396 n.children = append(n.children, nil)
397 inspos := i
398 if right {
399 inspos++
400 }
401 copy(n.children[inspos+1:], n.children[inspos:])
402 n.children[inspos] = vn
403
404 n.applyNewSize(width, false)
405 return newid
406 }
407 }
408
409 // HSplit creates a horizontal split and returns the id of the new split
410 // bottom specifies if the new split should be created on the top or bottom
411 // of the current split
412 func (n *Node) HSplit(bottom bool) uint64 {
413 if !n.IsLeaf() {
414 return 0
415 }
416 if n.Kind == STUndef {
417 n.Kind = STVert
418 }
419 if n.Kind == STVert {
420 return n.vHSplit(0, bottom)
421 }
422 return n.hHSplit(bottom)
423 }
424
425 // VSplit creates a vertical split and returns the id of the new split
426 // right specifies if the new split should be created on the right or left
427 // of the current split
428 func (n *Node) VSplit(right bool) uint64 {
429 if !n.IsLeaf() {
430 return 0
431 }
432 if n.Kind == STUndef {
433 n.Kind = STHoriz
434 }
435 if n.Kind == STVert {
436 return n.vVSplit(right)
437 }
438 return n.hVSplit(0, right)
439 }
440
441 // unsplits the child of a split
442 func (n *Node) unsplit(i int) {
443 copy(n.children[i:], n.children[i+1:])
444 n.children[len(n.children)-1] = nil
445 n.children = n.children[:len(n.children)-1]
446
447 h := n.Kind == STVert
448 nonrs, numr := n.getResizeInfo(h)
449 if numr == 0 {
450 // This means that this was the last child
451 // The parent will get cleaned up in the next iteration and
452 // will resolve all sizing issues with its parent
453 return
454 }
455 size := (n.W - nonrs) / numr
456 if h {
457 size = (n.H - nonrs) / numr
458 }
459 n.applyNewSize(size, h)
460 }
461
462 // Unsplit deletes this split and resizes everything
463 // else accordingly
464 func (n *Node) Unsplit() bool {
465 if !n.IsLeaf() || n.parent == nil {
466 return false
467 }
468 ind := 0
469 for i, c := range n.parent.children {
470 if c.id == n.id {
471 ind = i
472 }
473 }
474 n.parent.unsplit(ind)
475 if n.parent.IsLeaf() {
476 return n.parent.Unsplit()
477 }
478
479 n.parent.flatten()
480 return true
481 }
482
483 // flattens the tree by removing unnecessary intermediate parents that have only one child
484 // and handles the side effect of it
485 func (n *Node) flatten() {
486 if n.parent == nil || len(n.children) != 1 {
487 return
488 }
489
490 ind := 0
491 for i, c := range n.parent.children {
492 if c.id == n.id {
493 ind = i
494 }
495 }
496
497 // Replace current node with its child node to remove chained parent
498 successor := n.children[0]
499 n.parent.children[ind] = successor
500 successor.parent = n.parent
501
502 // Maintain the tree in a consistent state: any child node's kind (horiz vs vert)
503 // should be the opposite of its parent's kind.
504 if successor.IsLeaf() {
505 successor.Kind = n.Kind
506 } else {
507 // If the successor node has children, that means it is a chained parent as well.
508 // Therefore it can be replaced by its own children.
509 origsize := len(n.parent.children)
510
511 // Let's say we have 5 children and want to replace [2] with its children [a] [b] [c]
512 // [0] [1] [2] [3] [4] --> [0] [1] [a] [b] [c] [3] [4]
513 // insertcount will be `3 - 1 = 2` in this case
514 insertcount := len(successor.children) - 1
515
516 n.parent.children = append(n.parent.children, make([]*Node, insertcount)...)
517 copy(n.parent.children[ind+insertcount+1:], n.parent.children[ind+1:origsize])
518 copy(n.parent.children[ind:], successor.children)
519
520 for i := 0; i < len(successor.children); i++ {
521 n.parent.children[ind+i].parent = n.parent
522 }
523 }
524
525 // Update propW and propH since the parent of the children has been updated,
526 // so the children have new siblings
527 n.parent.markSizes()
528 }
529
530 // String returns the string form of the node and all children (used for debugging)
531 func (n *Node) String() string {
532 var strf func(n *Node, ident int) string
533 strf = func(n *Node, ident int) string {
534 marker := "|"
535 if n.Kind == STHoriz {
536 marker = "-"
537 }
538 str := fmt.Sprint(strings.Repeat("\t", ident), marker, n.View, n.id)
539 if n.IsLeaf() {
540 str += "🍁"
541 }
542 str += "\n"
543 for _, c := range n.children {
544 str += strf(c, ident+1)
545 }
546 return str
547 }
548 return strf(n, 0)
549 }