-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.go
More file actions
159 lines (135 loc) · 2.96 KB
/
Copy pathlayout.go
File metadata and controls
159 lines (135 loc) · 2.96 KB
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
package main
import (
"github.com/charmbracelet/lipgloss"
)
type Selection struct {
ID int
StartX int
StartY int
EndX int
EndY int
}
type Node interface {
Resize(w, h int)
View(activePaneID int, sel Selection, x, y int) string
Width() int
Height() int
HitTest(x, y int) int // Returns PaneID or -1
}
type SplitType int
const (
SplitVertical SplitType = iota
SplitHorizontal
)
type Split struct {
Type SplitType
Children []Node
Ratios []float64
width int
height int
}
func (s *Split) Resize(w, h int) {
s.width = w
s.height = h
n := len(s.Children)
if n == 0 {
return
}
if len(s.Ratios) != n {
s.Ratios = make([]float64, n)
for i := range s.Ratios {
s.Ratios[i] = 1.0 / float64(n)
}
}
totalSize := w
if s.Type == SplitHorizontal {
totalSize = h
}
currentSize := 0
for i, child := range s.Children {
var size int
if i == n-1 {
size = totalSize - currentSize
} else {
size = int(float64(totalSize) * s.Ratios[i])
}
if size < 1 { size = 1 } // Min size
if s.Type == SplitVertical {
child.Resize(size, h)
} else {
child.Resize(w, size)
}
currentSize += size
}
}
func (s *Split) View(activePaneID int, sel Selection, x, y int) string {
var views []string
curX, curY := x, y
for _, c := range s.Children {
views = append(views, c.View(activePaneID, sel, curX, curY))
if s.Type == SplitVertical {
curX += c.Width()
} else {
curY += c.Height()
}
}
if s.Type == SplitVertical {
return lipgloss.JoinHorizontal(lipgloss.Top, views...)
} else {
return lipgloss.JoinVertical(lipgloss.Left, views...)
}
}
func (s *Split) Width() int { return s.width }
func (s *Split) Height() int { return s.height }
func (s *Split) HitTest(x, y int) int {
if x < 0 || y < 0 || x >= s.width || y >= s.height {
return -1
}
curX, curY := 0, 0
for _, child := range s.Children {
// Pass relative coordinates
res := child.HitTest(x-curX, y-curY)
if res != -1 {
return res
}
if s.Type == SplitVertical {
curX += child.Width()
} else {
curY += child.Height()
}
}
return -1
}
// PaneNode wraps a Pane to implement Node
type PaneNode struct {
Pane *Pane
}
func (pn *PaneNode) Resize(w, h int) {
innerW := w - 2
innerH := h - 2
if innerW < 1 { innerW = 1 }
if innerH < 1 { innerH = 1 }
pn.Pane.Resize(innerW, innerH)
}
func (pn *PaneNode) View(activePaneID int, sel Selection, x, y int) string {
isActive := pn.Pane.id == activePaneID
borderColor := lipgloss.Color("241")
if isActive {
borderColor = lipgloss.Color("63")
}
content := pn.Pane.View(sel)
return lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
BorderForeground(borderColor).
Width(pn.Pane.width).
Height(pn.Pane.height).
Render(content)
}
func (pn *PaneNode) Width() int { return pn.Pane.width + 2 }
func (pn *PaneNode) Height() int { return pn.Pane.height + 2 }
func (pn *PaneNode) HitTest(x, y int) int {
if x >= 0 && x < pn.Width() && y >= 0 && y < pn.Height() {
return pn.Pane.id
}
return -1
}