forked from glycerine/gemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_tree.go
243 lines (219 loc) · 4.52 KB
/
view_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
package main
import (
"github.com/nsf/tulib"
)
//----------------------------------------------------------------------------
// view_tree
//----------------------------------------------------------------------------
type view_tree struct {
// At the same time only one of these groups can be valid:
// 1) 'left', 'right' and 'split'
// 2) 'top', 'bottom' and 'split'
// 3) 'leaf'
parent *view_tree
left *view_tree
top *view_tree
right *view_tree
bottom *view_tree
leaf *view
split float32
tulib.Rect // updated with 'resize' call
g *gemacs
}
func new_view_tree_leaf(parent *view_tree, v *view, g *gemacs) *view_tree {
return &view_tree{
parent: parent,
leaf: v,
g: g,
}
}
func (v *view_tree) split_vertically() {
top := v.leaf
bottom := new_view(top.ctx, top.buf, v.g)
*v = view_tree{
parent: v.parent,
top: new_view_tree_leaf(v, top, v.g),
bottom: new_view_tree_leaf(v, bottom, v.g),
split: 0.5,
}
}
func (v *view_tree) split_horizontally() {
left := v.leaf
right := new_view(left.ctx, left.buf, v.g)
*v = view_tree{
parent: v.parent,
left: new_view_tree_leaf(v, left, v.g),
right: new_view_tree_leaf(v, right, v.g),
split: 0.5,
}
}
func (v *view_tree) draw() {
if v.leaf != nil {
v.leaf.draw()
return
}
if v.left != nil {
v.left.draw()
v.right.draw()
} else {
v.top.draw()
v.bottom.draw()
}
}
func (v *view_tree) resize(pos tulib.Rect) {
v.Rect = pos
if v.leaf != nil {
v.leaf.resize(pos.Width, pos.Height)
return
}
if v.left != nil {
// horizontal split, use 'w'
w := pos.Width
if w > 0 {
// reserve one line for splitter, if we have one line
w--
}
lw := int(float32(w) * v.split)
rw := w - lw
v.left.resize(tulib.Rect{pos.X, pos.Y, lw, pos.Height})
v.right.resize(tulib.Rect{pos.X + lw + 1, pos.Y, rw, pos.Height})
} else {
// vertical split, use 'h', no need to reserve one line for
// splitter, because splitters are part of the buffer's output
// (their status bars act like a splitter)
h := pos.Height
th := int(float32(h) * v.split)
bh := h - th
v.top.resize(tulib.Rect{pos.X, pos.Y, pos.Width, th})
v.bottom.resize(tulib.Rect{pos.X, pos.Y + th, pos.Width, bh})
}
}
func (v *view_tree) traverse(cb func(*view_tree)) {
if v.leaf != nil {
cb(v)
return
}
if v.left != nil {
v.left.traverse(cb)
v.right.traverse(cb)
} else if v.top != nil {
v.top.traverse(cb)
v.bottom.traverse(cb)
}
}
func (v *view_tree) nearest_vsplit() *view_tree {
v = v.parent
for v != nil {
if v.top != nil {
return v
}
v = v.parent
}
return nil
}
func (v *view_tree) nearest_hsplit() *view_tree {
v = v.parent
for v != nil {
if v.left != nil {
return v
}
v = v.parent
}
return nil
}
func (v *view_tree) one_step() float32 {
if v.top != nil {
return 1.0 / float32(v.Height)
} else if v.left != nil {
return 1.0 / float32(v.Width-1)
}
return 0.0
}
func (v *view_tree) normalize_split() {
var off int
if v.top != nil {
off = int(float32(v.Height) * v.split)
} else {
off = int(float32(v.Width-1) * v.split)
}
v.split = float32(off) * v.one_step()
}
func (v *view_tree) step_resize(n int) {
if v.Width <= 1 || v.Height <= 0 {
// avoid division by zero, result is really bad
return
}
one := v.one_step()
v.normalize_split()
v.split += one*float32(n) + (one * 0.5)
if v.split > 1.0 {
v.split = 1.0
}
if v.split < 0.0 {
v.split = 0.0
}
v.resize(v.Rect)
}
func (v *view_tree) reparent() {
if v.left != nil {
v.left.parent = v
v.right.parent = v
} else if v.top != nil {
v.top.parent = v
v.bottom.parent = v
}
}
func (v *view_tree) sibling() *view_tree {
p := v.parent
if p == nil {
return nil
}
switch {
case v == p.left:
return p.right
case v == p.right:
return p.left
case v == p.top:
return p.bottom
case v == p.bottom:
return p.top
}
panic("unreachable")
}
// nextInCycle returns the next view
// after v, eventually cycling
// through all views.
func (v *view_tree) nextInCycle() (ret *view_tree) {
root := v
for root.parent != nil {
root = root.parent
}
// find our number
var k, our int
root.traverse(func(w *view_tree) {
if w == v {
our = k
}
k++
})
// return the view after us
tot := k
k = 0
root.traverse(func(w *view_tree) {
if k == (our+1)%tot {
ret = w
}
k++
})
return
}
func (v *view_tree) first_leaf_node() *view_tree {
if v.left != nil {
return v.left.first_leaf_node()
} else if v.top != nil {
return v.top.first_leaf_node()
} else if v.leaf != nil {
return v
}
panic("unreachable")
}