37 // Nodes can be marked as non resizable if they shouldn't be rescaled
38 // when the terminal window is resized or when a new split is added
39 // Only the splits on the edges of the screen can be marked as non resizable
41 // A node may also be marked with proportional scaling. This means that when
42 // the window is resized the split maintains its proportions
48 func (n *Node) ID() uint64 {
54 func (n *Node) CanResize() bool {
57 func (n *Node) PropScale() bool {
60 func (n *Node) SetResize(b bool) {
63 func (n *Node) SetPropScale(b bool) {
66 func (n *Node) GetView() View {
69 func (n *Node) SetView(v View) {
70 n.X, n.Y, n.W, n.H = v.X, v.Y, v.W, v.H
72 func (n *Node) Children() []*Node {
76 func (n *Node) GetNode(id uint64) *Node {
77 if n.id == id && n.IsLeaf() {
80 for _, c := range n.children {
81 if c.id == id && c.IsLeaf() {
92 func NewNode(Kind SplitType, x, y, w, h int, parent *Node, id uint64) *Node {
97 n.X, n.Y, n.W, n.H = x, y, w, h
98 n.children = make([]*Node, 0)
105 func NewRoot(x, y, w, h int) *Node {
106 n1 := NewNode(STUndef, x, y, w, h, nil, NewID())
111 func (n *Node) IsLeaf() bool {
112 return len(n.children) == 0
115 func (n *Node) vResizeSplit(i int, size int) bool {
116 if i < 0 || i >= len(n.children)-1 {
119 c1, c2 := n.children[i], n.children[i+1]
125 c1.Resize(c1.W, size)
126 c2.Resize(c2.W, toth-size)
129 func (n *Node) hResizeSplit(i int, size int) bool {
130 if i < 0 || i >= len(n.children)-1 {
133 c1, c2 := n.children[i], n.children[i+1]
139 c1.Resize(size, c1.H)
140 c2.Resize(totw-size, c2.H)
144 func (n *Node) ResizeSplit(size int) bool {
146 for i, c := range n.parent.children {
151 if n.parent.Kind == STVert {
152 return n.parent.vResizeSplit(ind, size)
154 return n.parent.hResizeSplit(ind, size)
157 func (n *Node) vVSplit(right bool) uint64 {
159 for i, c := range n.parent.children {
164 return n.parent.hVSplit(ind, right)
166 func (n *Node) hHSplit(bottom bool) uint64 {
168 for i, c := range n.parent.children {
173 return n.parent.vHSplit(ind, bottom)
175 func (n *Node) vHSplit(i int, right bool) uint64 {
178 hn1 := NewNode(STHoriz, n.X, n.Y, n.W, n.H/2, n, n.id)
179 hn2 := NewNode(STHoriz, n.X, n.Y+hn1.H, n.W, n.H/2, n, newid)
181 hn1.id, hn2.id = hn2.id, hn1.id
184 n.children = append(n.children, hn1, hn2)
191 for _, c := range n.children {
200 // if there are no resizable splits make them all resizable
205 height := (n.H - nonrh) / (numr + 1)
208 hn := NewNode(STHoriz, n.X, 0, n.W, height, n, newid)
209 n.children = append(n.children, nil)
214 copy(n.children[inspos+1:], n.children[inspos:])
215 n.children[inspos] = hn
218 for _, c := range n.children {
221 c.Resize(c.W, height)
229 func (n *Node) hVSplit(i int, right bool) uint64 {
232 vn1 := NewNode(STVert, n.X, n.Y, n.W/2, n.H, n, n.id)
233 vn2 := NewNode(STVert, n.X+vn1.W, n.Y, n.W/2, n.H, n, newid)
235 vn1.id, vn2.id = vn2.id, vn1.id
238 n.children = append(n.children, vn1, vn2)
245 for _, c := range n.children {
254 // if there are no resizable splits make them all resizable
259 width := (n.W - nonrw) / (numr + 1)
262 vn := NewNode(STVert, 0, n.Y, width, n.H, n, newid)
263 n.children = append(n.children, nil)
268 copy(n.children[inspos+1:], n.children[inspos:])
269 n.children[inspos] = vn
272 for _, c := range n.children {
284 func (n *Node) HSplit(bottom bool) uint64 {
288 if n.Kind == STUndef {
291 if n.Kind == STVert {
292 return n.vHSplit(0, bottom)
294 return n.hHSplit(bottom)
297 func (n *Node) VSplit(right bool) uint64 {
301 if n.Kind == STUndef {
304 if n.Kind == STVert {
305 return n.vVSplit(right)
307 return n.hVSplit(0, right)
310 func (n *Node) Resize(w, h int) {
314 propW, propH := float64(w)/float64(n.W), float64(h)/float64(n.H)
315 log.Println(w, h, n.W, n.H, propW, propH)
317 for i, c := range n.children {
318 cW := int(float64(c.W) * propW)
319 // if c.IsLeaf() && i != len(n.children)-1 {
322 log.Println("WIDTH:", cW, c.W)
323 cH := int(float64(c.H) * propH)
327 if n.Kind == STHoriz {
338 func (n *Node) alignSize() {
339 if len(n.children) == 0 {
344 for i, c := range n.children {
345 if n.Kind == STHoriz {
346 if i != len(n.children)-1 {
354 if n.Kind == STVert && toth != n.H {
355 last := n.children[len(n.children)-1]
356 last.Resize(last.W, last.H+n.H-toth)
357 } else if n.Kind == STHoriz && totw != n.W {
358 last := n.children[len(n.children)-1]
359 last.Resize(last.W+n.W-totw, last.H)
363 func (n *Node) Unsplit() {
367 func (n *Node) String() string {
368 var strf func(n *Node, ident int) string
369 strf = func(n *Node, ident int) string {
371 if n.Kind == STHoriz {
374 str := fmt.Sprint(strings.Repeat("\t", ident), marker, n.View, n.id)
379 for _, c := range n.children {
380 str += strf(c, ident+1)