/
tree.go
377 lines (317 loc) · 7.5 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) 2018, Mark "Happy-Ferret" Bauermeister
//
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
package layout
import (
"fmt"
"math"
"github.com/BurntSushi/xgbutil/xrect"
"github.com/Anima-OS/Wonderland/misc"
)
const (
fullPortion proportion = 1.0
epsilon = 0.0001
)
type proportion float64
// portion takes a proportion of size.
func (p proportion) portion(size int) int {
return misc.Round(float64(proportion(size) * p))
}
func (p1 proportion) equal(p2 proportion) bool {
return math.Abs(float64(p1-p2)) < epsilon
}
type tree struct {
child node
}
type node interface {
MoveResize(x, y, width, height int)
Proportion() proportion
SetProportion(p proportion)
Parent() node
SetParent(n node)
ValidDims(w, h, minw, minh, maxw, maxh int) bool
VisitLeafNodes(f func(lf *leaf) bool) bool
}
type splitter interface {
node
AddNode(n node, last bool)
RemoveNode(n node)
SetChildProportion(n node, newProp proportion)
Size() int
Child(i int) node
ChildIndex(n node) int
PropsSave()
PropsRollback()
PropsClear()
}
type hsplit struct {
split
}
type vsplit struct {
split
}
type split struct {
parent node
children []node
prop proportion
saved []proportion
}
type leaf struct {
parent splitter
client Client
prop proportion
}
func newTree() *tree {
return &tree{
child: nil,
}
}
func (t *tree) place(geom xrect.Rect) bool {
if t.child == nil || geom == nil {
return false
}
x, y, w, h := geom.X(), geom.Y(), geom.Width(), geom.Height()
if !t.child.ValidDims(w, h, 1, 1, w, h) {
return false
}
t.child.MoveResize(x, y, w, h)
return true
}
func (t *tree) setChild(n node) {
t.child = n
}
func (t *tree) switchClients(lf1, lf2 *leaf) {
if lf1 == nil || lf2 == nil {
return
}
lf1.client, lf2.client = lf2.client, lf1.client
}
func (t *tree) findLeaf(c Client) *leaf {
if t.child == nil {
return nil
}
var lf *leaf
t.child.VisitLeafNodes(func(visit *leaf) bool {
if visit.client == c {
lf = visit
return false // stop visiting
}
return true // keep going
})
return lf
}
func newLeaf(parent splitter, client Client) *leaf {
return &leaf{
parent: parent,
client: client,
}
}
func newHSplit(parent node) *hsplit {
return &hsplit{split{
parent: parent,
children: make([]node, 0),
saved: make([]proportion, 0),
}}
}
func newVSplit(parent node) *vsplit {
return &vsplit{split{
parent: parent,
children: make([]node, 0),
saved: make([]proportion, 0),
}}
}
func (s *split) Proportion() proportion {
return s.prop
}
func (s *split) SetProportion(p proportion) {
s.prop = p
}
func (s *split) checkPortions() {
if len(s.children) == 0 {
return
}
sum := proportion(0)
for _, child := range s.children {
sum += child.Proportion()
}
if !sum.equal(fullPortion) {
panic(fmt.Sprintf("portions not equal: %f != %f", sum, fullPortion))
}
}
func (s *split) Parent() node {
return s.parent
}
func (s *split) SetParent(n node) {
s.parent = n
}
func (s *split) VisitLeafNodes(f func(lf *leaf) bool) bool {
for _, child := range s.children {
if !child.VisitLeafNodes(f) {
return false
}
}
return true
}
func (s *split) AddNode(n node, last bool) {
// Get the proportion of the new leaf.
newProp := fullPortion / proportion(len(s.children)+1)
// Now push everything else over by an even amount.
if len(s.children) > 0 {
// chop := newProp / proportion(len(s.children))
for _, child := range s.children {
child.SetProportion(
child.Proportion() - (child.Proportion() * newProp))
}
}
n.SetProportion(newProp)
if last {
s.children = append(s.children, n)
} else {
s.children = append([]node{n}, s.children...)
}
s.checkPortions()
}
func (s *split) RemoveNode(n node) {
// Remove it from the list of children.
removed := false
for i, child := range s.children {
if child == n {
s.children = append(s.children[:i], s.children[i+1:]...)
removed = true
}
}
if !removed {
panic(fmt.Sprintf("The node '%s' is not in the split '%s'.", n, s))
}
// Distribute this node's portion to the rest.
// Give more to those who don't have much, and less to those who have
// a lot.
if len(s.children) > 0 {
normalized := make([]proportion, len(s.children))
sum := 1.0 - n.Proportion()
for i, child := range s.children {
normalized[i] = child.Proportion() / sum
}
for i, child := range s.children {
child.SetProportion(
child.Proportion() + normalized[i]*n.Proportion())
}
s.checkPortions()
}
}
func (s *split) SetChildProportion(n node, newProp proportion) {
// Find the difference between the old proportion and the new. Then
// spread the difference over the node's siblings.
diff := n.Proportion() - newProp
chops := diff / proportion(s.Size()-1)
// Normalize proportions that don't include the 'n' node.
// normalized := make([]proportion, 0, s.Size() - 1)
// sum := proportion(0)
// for _, child := range s.children {
// if child != n {
// sum += child.Proportion()
// }
// }
// for _, child := range s.children {
// if child != n {
// normalized = append(normalized, child.Proportion() / sum)
// }
// }
for _, child := range s.children {
if child != n {
child.SetProportion(child.Proportion() + chops)
}
}
n.SetProportion(newProp)
s.checkPortions()
}
func (s *split) Size() int {
return len(s.children)
}
func (s *split) Child(i int) node {
return s.children[i]
}
func (s *split) ChildIndex(n node) int {
for i, child := range s.children {
if child == n {
return i
}
}
return -1
}
func (s *split) PropsSave() {
s.saved = s.saved[:0]
for _, child := range s.children {
s.saved = append(s.saved, child.Proportion())
}
}
func (s *split) PropsRollback() {
for i, childProp := range s.saved {
s.children[i].SetProportion(childProp)
}
s.saved = s.saved[:0]
}
func (s *split) PropsClear() {
s.saved = s.saved[:0]
}
func (hs *hsplit) MoveResize(x, y, width, height int) {
// In hsplits, y and height remain constant. Width varies based on the
// proportion, and x is derived from width.
nextx := x
for _, child := range hs.children {
w := child.Proportion().portion(width)
child.MoveResize(nextx, y, w, height)
nextx += w
}
}
func (hs *hsplit) ValidDims(w, h, minw, minh, maxw, maxh int) bool {
for _, child := range hs.children {
childw := child.Proportion().portion(w)
if !child.ValidDims(childw, h, minw, minh, maxw, maxh) {
return false
}
}
return true
}
func (vs *vsplit) MoveResize(x, y, width, height int) {
// In vsplits, x and width remain constant. Height varies based on the
// proportion, and y is derived from height.
nexty := y
for _, child := range vs.children {
h := child.Proportion().portion(height)
child.MoveResize(x, nexty, width, h)
nexty += h
}
}
func (vs *vsplit) ValidDims(w, h, minw, minh, maxw, maxh int) bool {
for _, child := range vs.children {
childh := child.Proportion().portion(h)
if !child.ValidDims(w, childh, minw, minh, maxw, maxh) {
return false
}
}
return true
}
func (lf *leaf) MoveResize(x, y, width, height int) {
lf.client.FrameTile()
lf.client.MoveResize(x, y, width, height)
}
func (lf *leaf) Proportion() proportion {
return lf.prop
}
func (lf *leaf) SetProportion(p proportion) {
lf.prop = p
}
func (lf *leaf) Parent() node {
return lf.parent
}
func (lf *leaf) SetParent(n node) {
lf.parent = n.(splitter)
}
func (lf *leaf) ValidDims(w, h, minw, minh, maxw, maxh int) bool {
return w >= minw && h >= minh && w <= maxw && h <= maxh
}
func (lf *leaf) VisitLeafNodes(f func(visit *leaf) bool) bool {
return f(lf)
}