Debian Codegraph @

pkg/boxlayout/boxlayout.go

raw symbols SCIP
1 package boxlayout
2
3 import (
4 "github.com/jesseduffield/lazycore/pkg/utils"
5 "github.com/samber/lo"
6 )
7
8 type Dimensions struct {
9 X0 int
10 X1 int
11 Y0 int
12 Y1 int
13 }
14
15 type Direction int
16
17 const (
18 ROW Direction = iota
19 COLUMN
20 )
21
22 // to give a high-level explanation of what's going on here. We layout our windows by arranging a bunch of boxes in the available space.
23 // If a box has children, it needs to specify how it wants to arrange those children: ROW or COLUMN.
24 // If a box represents a window, you can put the window name in the Window field.
25 // When determining how to divvy-up the available height (for row children) or width (for column children), we first
26 // give the boxes with a static `size` the space that they want. Then we apportion
27 // the remaining space based on the weights of the dynamic boxes (you can't define
28 // both size and weight at the same time: you gotta pick one). If there are two
29 // boxes, one with weight 1 and the other with weight 2, the first one gets 33%
30 // of the available space and the second one gets the remaining 66%
31
32 type Box struct {
33 // Direction decides how the children boxes are laid out. ROW means the children will each form a row i.e. that they will be stacked on top of eachother.
34 Direction Direction
35
36 // function which takes the width and height assigned to the box and decides which orientation it will have
37 ConditionalDirection func(width int, height int) Direction
38
39 Children []*Box
40
41 // function which takes the width and height assigned to the box and decides the layout of the children.
42 ConditionalChildren func(width int, height int) []*Box
43
44 // Window refers to the name of the window this box represents, if there is one
45 Window string
46
47 // static Size. If parent box's direction is ROW this refers to height, otherwise width
48 Size int
49
50 // dynamic size. Once all statically sized children have been considered, Weight decides how much of the remaining space will be taken up by the box
51 // TODO: consider making there be one int and a type enum so we can't have size and Weight simultaneously defined
52 Weight int
53 }
54
55 func ArrangeWindows(root *Box, x0, y0, width, height int) map[string]Dimensions {
56 children := root.getChildren(width, height)
57 if len(children) == 0 {
58 // leaf node
59 if root.Window != "" {
60 dimensionsForWindow := Dimensions{X0: x0, Y0: y0, X1: x0 + width - 1, Y1: y0 + height - 1}
61 return map[string]Dimensions{root.Window: dimensionsForWindow}
62 }
63 return map[string]Dimensions{}
64 }
65
66 direction := root.getDirection(width, height)
67
68 var availableSize int
69 if direction == COLUMN {
70 availableSize = width
71 } else {
72 availableSize = height
73 }
74
75 sizes := calcSizes(children, availableSize)
76
77 result := map[string]Dimensions{}
78 offset := 0
79 for i, child := range children {
80 boxSize := sizes[i]
81
82 var resultForChild map[string]Dimensions
83 if direction == COLUMN {
84 resultForChild = ArrangeWindows(child, x0+offset, y0, boxSize, height)
85 } else {
86 resultForChild = ArrangeWindows(child, x0, y0+offset, width, boxSize)
87 }
88
89 result = mergeDimensionMaps(result, resultForChild)
90 offset += boxSize
91 }
92
93 return result
94 }
95
96 func calcSizes(boxes []*Box, availableSpace int) []int {
97 normalizedWeights := normalizeWeights(lo.Map(boxes, func(box *Box, _ int) int { return box.Weight }))
98
99 totalWeight := 0
100 reservedSpace := 0
101 for i, box := range boxes {
102 if box.isStatic() {
103 reservedSpace += box.Size
104 } else {
105 totalWeight += normalizedWeights[i]
106 }
107 }
108
109 dynamicSpace := utils.Max(0, availableSpace-reservedSpace)
110
111 unitSize := 0
112 extraSpace := 0
113 if totalWeight > 0 {
114 unitSize = dynamicSpace / totalWeight
115 extraSpace = dynamicSpace % totalWeight
116 }
117
118 result := make([]int, len(boxes))
119 for i, box := range boxes {
120 if box.isStatic() {
121 // assuming that only one static child can have a size greater than the
122 // available space. In that case we just crop the size to what's available
123 result[i] = utils.Min(availableSpace, box.Size)
124 } else {
125 result[i] = unitSize * normalizedWeights[i]
126 }
127 }
128
129 // distribute the remainder across dynamic boxes.
130 for extraSpace > 0 {
131 for i, weight := range normalizedWeights {
132 if weight > 0 {
133 result[i]++
134 extraSpace--
135 normalizedWeights[i]--
136
137 if extraSpace == 0 {
138 break
139 }
140 }
141 }
142 }
143
144 return result
145 }
146
147 // removes common multiple from weights e.g. if we get 2, 4, 4 we return 1, 2, 2.
148 func normalizeWeights(weights []int) []int {
149 if len(weights) == 0 {
150 return []int{}
151 }
152
153 // to spare us some computation we'll exit early if any of our weights is 1
154 if lo.SomeBy(weights, func(weight int) bool { return weight == 1 }) {
155 return weights
156 }
157
158 // map weights to factorSlices and find the lowest common factor
159 positiveWeights := lo.Filter(weights, func(weight int, _ int) bool { return weight > 0 })
160 factorSlices := lo.Map(positiveWeights, func(weight int, _ int) []int { return calcFactors(weight) })
161 commonFactors := factorSlices[0]
162 for _, factors := range factorSlices {
163 commonFactors = lo.Intersect(commonFactors, factors)
164 }
165
166 if len(commonFactors) == 0 {
167 return weights
168 }
169
170 newWeights := lo.Map(weights, func(weight int, _ int) int { return weight / commonFactors[0] })
171
172 return normalizeWeights(newWeights)
173 }
174
175 func calcFactors(n int) []int {
176 factors := []int{}
177 for i := 2; i <= n; i++ {
178 if n%i == 0 {
179 factors = append(factors, i)
180 }
181 }
182 return factors
183 }
184
185 func (b *Box) isStatic() bool {
186 return b.Size > 0
187 }
188
189 func (b *Box) getDirection(width int, height int) Direction {
190 if b.ConditionalDirection != nil {
191 return b.ConditionalDirection(width, height)
192 }
193 return b.Direction
194 }
195
196 func (b *Box) getChildren(width int, height int) []*Box {
197 if b.ConditionalChildren != nil {
198 return b.ConditionalChildren(width, height)
199 }
200 return b.Children
201 }
202
203 func mergeDimensionMaps(a map[string]Dimensions, b map[string]Dimensions) map[string]Dimensions {
204 result := map[string]Dimensions{}
205 for _, dimensionMap := range []map[string]Dimensions{a, b} {
206 for k, v := range dimensionMap {
207 result[k] = v
208 }
209 }
210 return result
211 }