| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
type Box struct { |
| 33 |
|
| 34 |
Direction Direction |
| 35 |
|
| 36 |
|
| 37 |
ConditionalDirection func(width int, height int) Direction |
| 38 |
|
| 39 |
Children []*Box |
| 40 |
|
| 41 |
|
| 42 |
ConditionalChildren func(width int, height int) []*Box |
| 43 |
|
| 44 |
|
| 45 |
Window string |
| 46 |
|
| 47 |
|
| 48 |
Size int |
| 49 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 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 |
|
| 122 |
|
| 123 |
result[i] = utils.Min(availableSpace, box.Size) |
| 124 |
} else { |
| 125 |
result[i] = unitSize * normalizedWeights[i] |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 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 |
|
| 148 |
func normalizeWeights(weights []int) []int { |
| 149 |
if len(weights) == 0 { |
| 150 |
return []int{} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
if lo.SomeBy(weights, func(weight int) bool { return weight == 1 }) { |
| 155 |
return weights |
| 156 |
} |
| 157 |
|
| 158 |
|
| 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 |
} |