-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpane.go
More file actions
553 lines (483 loc) · 11.3 KB
/
pane.go
File metadata and controls
553 lines (483 loc) · 11.3 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package main
import (
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss/v2"
"github.com/charmbracelet/x/cellbuf"
)
// DragMode represents the current drag operation
type DragMode int
const (
DragNone DragMode = iota
DragMove
DragResizeN
DragResizeS
DragResizeE
DragResizeW
DragResizeNE
DragResizeNW
DragResizeSE
DragResizeSW
)
// HitZone represents where a mouse click landed on a pane
type HitZone int
const (
ZoneNone HitZone = iota
ZoneTitleBar
ZoneContent
ZoneEdgeN
ZoneEdgeS
ZoneEdgeE
ZoneEdgeW
ZoneCornerNE
ZoneCornerNW
ZoneCornerSE
ZoneCornerSW
)
// PaneContent defines what a pane can display
type PaneContent interface {
// Render returns the content string to display within pane borders
// w, h are the content area dimensions (inside borders)
Render(w, h int) string
// HandleKey processes keyboard input when this pane has focus
// Returns true if the key was consumed
HandleKey(msg tea.KeyMsg) bool
// HandleMouse processes mouse input within this pane's bounds
// x, y are relative to the pane's content area
HandleMouse(x, y int, msg tea.MouseMsg) bool
// Title returns the pane's title
Title() string
}
// Pane represents a floating pane in the TUI
type Pane struct {
ID string
X, Y int // Top-left position
Width int // Including borders
Height int
MinW int // Minimum size constraints
MinH int
ZIndex int
Focused bool
Content PaneContent
// Drag state
dragging bool
dragMode DragMode
dragStartX int
dragStartY int
dragOffsetX int
dragOffsetY int
}
// NewPane creates a new pane with sensible defaults
func NewPane(id string, content PaneContent, x, y, w, h int) *Pane {
return &Pane{
ID: id,
X: x,
Y: y,
Width: w,
Height: h,
MinW: 20,
MinH: 5,
Content: content,
}
}
// HitZone determines where a point falls within the pane
func (p *Pane) HitZone(x, y int) HitZone {
// Outside pane entirely
if x < p.X || x >= p.X+p.Width || y < p.Y || y >= p.Y+p.Height {
return ZoneNone
}
relX := x - p.X
relY := y - p.Y
// Corners (1 cell each)
if relY == 0 && relX == 0 {
return ZoneCornerNW
}
if relY == 0 && relX == p.Width-1 {
return ZoneCornerNE
}
if relY == p.Height-1 && relX == 0 {
return ZoneCornerSW
}
if relY == p.Height-1 && relX == p.Width-1 {
return ZoneCornerSE
}
// Edges
if relY == 0 {
return ZoneTitleBar // Top edge is title bar
}
if relY == p.Height-1 {
return ZoneEdgeS
}
if relX == 0 {
return ZoneEdgeW
}
if relX == p.Width-1 {
return ZoneEdgeE
}
return ZoneContent
}
// StartDrag begins a drag operation
func (p *Pane) StartDrag(mode DragMode, mouseX, mouseY int) {
p.dragging = true
p.dragMode = mode
p.dragStartX = mouseX
p.dragStartY = mouseY
p.dragOffsetX = mouseX - p.X
p.dragOffsetY = mouseY - p.Y
}
// UpdateDrag updates the pane position/size during a drag
func (p *Pane) UpdateDrag(mouseX, mouseY, screenW, screenH int) {
if !p.dragging {
return
}
switch p.dragMode {
case DragMove:
newX := mouseX - p.dragOffsetX
newY := mouseY - p.dragOffsetY
// Constrain to screen (allow title bar to stay visible)
p.X = clamp(newX, -p.Width+5, screenW-5)
p.Y = clamp(newY, 0, screenH-1)
case DragResizeE:
newW := mouseX - p.X + 1
p.Width = max(newW, p.MinW)
case DragResizeS:
newH := mouseY - p.Y + 1
p.Height = max(newH, p.MinH)
case DragResizeW:
delta := p.dragStartX - mouseX
newW := p.Width + delta
if newW >= p.MinW {
p.X = mouseX
p.Width = newW
p.dragStartX = mouseX
}
case DragResizeN:
delta := p.dragStartY - mouseY
newH := p.Height + delta
if newH >= p.MinH {
p.Y = mouseY
p.Height = newH
p.dragStartY = mouseY
}
case DragResizeSE:
p.Width = max(mouseX-p.X+1, p.MinW)
p.Height = max(mouseY-p.Y+1, p.MinH)
case DragResizeSW:
newW := p.X + p.Width - mouseX
if newW >= p.MinW {
p.X = mouseX
p.Width = newW
}
p.Height = max(mouseY-p.Y+1, p.MinH)
case DragResizeNE:
p.Width = max(mouseX-p.X+1, p.MinW)
delta := p.dragStartY - mouseY
newH := p.Height + delta
if newH >= p.MinH {
p.Y = mouseY
p.Height = newH
p.dragStartY = mouseY
}
case DragResizeNW:
delta := p.dragStartX - mouseX
newW := p.Width + delta
if newW >= p.MinW {
p.X = mouseX
p.Width = newW
p.dragStartX = mouseX
}
delta = p.dragStartY - mouseY
newH := p.Height + delta
if newH >= p.MinH {
p.Y = mouseY
p.Height = newH
p.dragStartY = mouseY
}
}
}
// StopDrag ends the current drag operation
func (p *Pane) StopDrag() {
p.dragging = false
p.dragMode = DragNone
}
// Render renders the pane with borders and content
func (p *Pane) Render() string {
// Border characters - double for focused, single for unfocused
var tl, tr, bl, br, h, v string
if p.Focused {
tl, tr, bl, br = "╔", "╗", "╚", "╝"
h, v = "═", "║"
} else {
tl, tr, bl, br = "┌", "┐", "└", "┘"
h, v = "─", "│"
}
// Style borders with Dyalog orange
borderStyle := lipgloss.NewStyle().Foreground(AccentColor)
titleStyle := lipgloss.NewStyle().Foreground(AccentColor).Bold(true)
contentW := p.Width - 2
contentH := p.Height - 2
if contentW < 1 {
contentW = 1
}
if contentH < 1 {
contentH = 1
}
// Render content
content := ""
if p.Content != nil {
content = p.Content.Render(contentW, contentH)
}
contentLines := strings.Split(content, "\n")
// Build the box
var lines []string
// Title bar
title := ""
if p.Content != nil {
title = p.Content.Title()
}
titleLen := len([]rune(title))
padding := contentW - titleLen - 2
if padding < 0 {
runes := []rune(title)
if len(runes) > contentW-2 {
title = string(runes[:contentW-2])
}
padding = 0
}
topBar := borderStyle.Render(tl+" ") + titleStyle.Render(title) + borderStyle.Render(" "+strings.Repeat(h, padding)+tr)
lines = append(lines, topBar)
// Content lines
for i := 0; i < contentH; i++ {
line := ""
if i < len(contentLines) {
line = contentLines[i]
}
// Use lipgloss.Width for ANSI-aware width calculation
lineW := lipgloss.Width(line)
if lineW < contentW {
line = line + strings.Repeat(" ", contentW-lineW)
}
// Note: truncation with ANSI codes is complex, skip for now
lines = append(lines, borderStyle.Render(v)+line+borderStyle.Render(v))
}
// Bottom border
lines = append(lines, borderStyle.Render(bl+strings.Repeat(h, contentW)+br))
return strings.Join(lines, "\n")
}
// PaneManager tracks all floating panes
type PaneManager struct {
panes map[string]*Pane
zOrder []string // Ordered by z-index, last = topmost
focusedID string
screenW int
screenH int
}
// NewPaneManager creates a new pane manager
func NewPaneManager(screenW, screenH int) *PaneManager {
return &PaneManager{
panes: make(map[string]*Pane),
zOrder: []string{},
screenW: screenW,
screenH: screenH,
}
}
// Add adds a pane to the manager
func (pm *PaneManager) Add(pane *Pane) {
pm.panes[pane.ID] = pane
pm.zOrder = append(pm.zOrder, pane.ID)
pane.ZIndex = len(pm.zOrder)
}
// Remove removes a pane from the manager
func (pm *PaneManager) Remove(id string) {
delete(pm.panes, id)
for i, pid := range pm.zOrder {
if pid == id {
pm.zOrder = append(pm.zOrder[:i], pm.zOrder[i+1:]...)
break
}
}
if pm.focusedID == id {
pm.focusedID = ""
}
}
// Get returns a pane by ID
func (pm *PaneManager) Get(id string) *Pane {
return pm.panes[id]
}
// Focus focuses a pane and raises it to the top
func (pm *PaneManager) Focus(id string) {
// Unfocus current
if pm.focusedID != "" {
if p := pm.panes[pm.focusedID]; p != nil {
p.Focused = false
}
}
pm.focusedID = id
// Focus and raise new pane
if p := pm.panes[id]; p != nil {
p.Focused = true
pm.raise(id)
}
}
// raise moves a pane to the top of z-order
func (pm *PaneManager) raise(id string) {
for i, pid := range pm.zOrder {
if pid == id {
pm.zOrder = append(pm.zOrder[:i], pm.zOrder[i+1:]...)
pm.zOrder = append(pm.zOrder, id)
break
}
}
}
// FocusNext cycles focus to the next pane
func (pm *PaneManager) FocusNext() {
if len(pm.zOrder) == 0 {
return
}
if pm.focusedID == "" {
pm.Focus(pm.zOrder[0])
return
}
for i, id := range pm.zOrder {
if id == pm.focusedID {
nextIdx := (i + 1) % len(pm.zOrder)
pm.Focus(pm.zOrder[nextIdx])
return
}
}
}
// FocusPrev cycles focus to the previous pane
func (pm *PaneManager) FocusPrev() {
if len(pm.zOrder) == 0 {
return
}
if pm.focusedID == "" {
pm.Focus(pm.zOrder[len(pm.zOrder)-1])
return
}
for i, id := range pm.zOrder {
if id == pm.focusedID {
prevIdx := (i - 1 + len(pm.zOrder)) % len(pm.zOrder)
pm.Focus(pm.zOrder[prevIdx])
return
}
}
}
// FocusedPane returns the currently focused pane
func (pm *PaneManager) FocusedPane() *Pane {
return pm.panes[pm.focusedID]
}
// PaneAt returns the topmost pane at the given coordinates
func (pm *PaneManager) PaneAt(x, y int) *Pane {
// Iterate from top of z-order (last) to bottom (first)
for i := len(pm.zOrder) - 1; i >= 0; i-- {
pane := pm.panes[pm.zOrder[i]]
if pane != nil && pane.HitZone(x, y) != ZoneNone {
return pane
}
}
return nil
}
// UpdateSize updates the screen dimensions
func (pm *PaneManager) UpdateSize(w, h int) {
pm.screenW = w
pm.screenH = h
// Adjust pane positions to stay visible
for _, pane := range pm.panes {
// Ensure at least 5 chars visible horizontally
if pane.X > w-5 {
pane.X = w - 5
}
// Ensure title bar visible
if pane.Y >= h {
pane.Y = h - 1
}
}
}
// HasPanes returns true if there are any panes
func (pm *PaneManager) HasPanes() bool {
return len(pm.zOrder) > 0
}
// Render composites all panes over the base content
func (pm *PaneManager) Render(base string) string {
if len(pm.zOrder) == 0 {
return base
}
// Parse base to get actual dimensions
baseLines := strings.Split(base, "\n")
baseH := len(baseLines)
baseW := pm.screenW
// Create base buffer from the session content
buf := cellbuf.NewBuffer(baseW, baseH)
cellbuf.SetContent(buf, base)
// Overlay each pane in z-order (lowest first)
for _, id := range pm.zOrder {
pane := pm.panes[id]
if pane == nil {
continue
}
// Render pane to a temporary buffer (handles ANSI codes properly)
paneStr := pane.Render()
paneBuf := cellbuf.NewBuffer(pane.Width, pane.Height)
cellbuf.SetContent(paneBuf, paneStr)
// Copy cells from pane buffer to main buffer at pane position
for dy := 0; dy < pane.Height; dy++ {
y := pane.Y + dy
if y < 0 || y >= baseH {
continue
}
for dx := 0; dx < pane.Width; dx++ {
x := pane.X + dx
if x < 0 || x >= baseW {
continue
}
cell := paneBuf.Cell(dx, dy)
if cell != nil {
buf.SetCell(x, y, cell)
}
}
}
}
return cellbuf.Render(buf)
}
// zoneToDragMode converts a hit zone to a drag mode
func zoneToDragMode(zone HitZone) DragMode {
switch zone {
case ZoneTitleBar:
return DragMove
case ZoneEdgeN:
return DragResizeN
case ZoneEdgeS:
return DragResizeS
case ZoneEdgeE:
return DragResizeE
case ZoneEdgeW:
return DragResizeW
case ZoneCornerNE:
return DragResizeNE
case ZoneCornerNW:
return DragResizeNW
case ZoneCornerSE:
return DragResizeSE
case ZoneCornerSW:
return DragResizeSW
default:
return DragNone
}
}
// Helper functions
func clamp(v, min, max int) int {
if v < min {
return min
}
if v > max {
return max
}
return v
}
func max(a, b int) int {
if a > b {
return a
}
return b
}