summaryrefslogtreecommitdiffhomepage
path: root/internal/tui/layout/grid.go
blob: 6be493e2c2f7536612660350e7e017f7e9600444 (plain)
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
package layout

import (
	"github.com/charmbracelet/bubbles/key"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

type GridLayout interface {
	tea.Model
	Sizeable
	Bindings
	Panes() [][]tea.Model
}

type gridLayout struct {
	width  int
	height int

	rows    int
	columns int

	panes [][]tea.Model

	gap       int
	bordered  bool
	focusable bool

	currentRow    int
	currentColumn int

	activeColor lipgloss.TerminalColor
}

type GridOption func(*gridLayout)

func (g *gridLayout) Init() tea.Cmd {
	var cmds []tea.Cmd
	for i := range g.panes {
		for j := range g.panes[i] {
			if g.panes[i][j] != nil {
				cmds = append(cmds, g.panes[i][j].Init())
			}
		}
	}
	return tea.Batch(cmds...)
}

func (g *gridLayout) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	var cmds []tea.Cmd

	switch msg := msg.(type) {
	case tea.WindowSizeMsg:
		g.SetSize(msg.Width, msg.Height)
		return g, nil
	case tea.KeyMsg:
		if key.Matches(msg, g.nextPaneBinding()) {
			return g.focusNextPane()
		}
	}

	// Update all panes
	for i := range g.panes {
		for j := range g.panes[i] {
			if g.panes[i][j] != nil {
				var cmd tea.Cmd
				g.panes[i][j], cmd = g.panes[i][j].Update(msg)
				if cmd != nil {
					cmds = append(cmds, cmd)
				}
			}
		}
	}

	return g, tea.Batch(cmds...)
}

func (g *gridLayout) focusNextPane() (tea.Model, tea.Cmd) {
	if !g.focusable {
		return g, nil
	}

	var cmds []tea.Cmd

	// Blur current pane
	if g.currentRow < len(g.panes) && g.currentColumn < len(g.panes[g.currentRow]) {
		if currentPane, ok := g.panes[g.currentRow][g.currentColumn].(Focusable); ok {
			cmds = append(cmds, currentPane.Blur())
		}
	}

	// Find next valid pane
	g.currentColumn++
	if g.currentColumn >= len(g.panes[g.currentRow]) {
		g.currentColumn = 0
		g.currentRow++
		if g.currentRow >= len(g.panes) {
			g.currentRow = 0
		}
	}

	// Focus next pane
	if g.currentRow < len(g.panes) && g.currentColumn < len(g.panes[g.currentRow]) {
		if nextPane, ok := g.panes[g.currentRow][g.currentColumn].(Focusable); ok {
			cmds = append(cmds, nextPane.Focus())
		}
	}

	return g, tea.Batch(cmds...)
}

func (g *gridLayout) nextPaneBinding() key.Binding {
	return key.NewBinding(
		key.WithKeys("tab"),
		key.WithHelp("tab", "next pane"),
	)
}

func (g *gridLayout) View() string {
	if len(g.panes) == 0 {
		return ""
	}

	// Calculate dimensions for each cell
	cellWidth := (g.width - (g.columns-1)*g.gap) / g.columns
	cellHeight := (g.height - (g.rows-1)*g.gap) / g.rows

	// Render each row
	rows := make([]string, g.rows)
	for i := range g.rows {
		// Render each column in this row
		cols := make([]string, len(g.panes[i]))
		for j := range g.panes[i] {
			if g.panes[i][j] == nil {
				cols[j] = ""
				continue
			}

			// Set size for each pane
			if sizable, ok := g.panes[i][j].(Sizeable); ok {
				effectiveWidth, effectiveHeight := cellWidth, cellHeight
				if g.bordered {
					effectiveWidth -= 2
					effectiveHeight -= 2
				}
				sizable.SetSize(effectiveWidth, effectiveHeight)
			}

			// Render the pane
			content := g.panes[i][j].View()
			
			// Apply border if needed
			if g.bordered {
				isFocused := false
				if focusable, ok := g.panes[i][j].(Focusable); ok {
					isFocused = focusable.IsFocused()
				}
				
				borderText := map[BorderPosition]string{}
				if bordered, ok := g.panes[i][j].(Bordered); ok {
					borderText = bordered.BorderText()
				}
				
				content = Borderize(content, BorderOptions{
					Active:       isFocused,
					EmbeddedText: borderText,
				})
			}
			
			cols[j] = content
		}
		
		// Join columns with gap
		rows[i] = lipgloss.JoinHorizontal(lipgloss.Top, cols...)
	}
	
	// Join rows with gap
	return lipgloss.JoinVertical(lipgloss.Left, rows...)
}

func (g *gridLayout) SetSize(width, height int) {
	g.width = width
	g.height = height
}

func (g *gridLayout) GetSize() (int, int) {
	return g.width, g.height
}

func (g *gridLayout) BindingKeys() []key.Binding {
	var bindings []key.Binding
	bindings = append(bindings, g.nextPaneBinding())
	
	// Collect bindings from all panes
	for i := range g.panes {
		for j := range g.panes[i] {
			if g.panes[i][j] != nil {
				if bindable, ok := g.panes[i][j].(Bindings); ok {
					bindings = append(bindings, bindable.BindingKeys()...)
				}
			}
		}
	}
	
	return bindings
}

func (g *gridLayout) Panes() [][]tea.Model {
	return g.panes
}

// NewGridLayout creates a new grid layout with the given number of rows and columns
func NewGridLayout(rows, cols int, panes [][]tea.Model, opts ...GridOption) GridLayout {
	grid := &gridLayout{
		rows:    rows,
		columns: cols,
		panes:   panes,
		gap:     1,
	}
	
	for _, opt := range opts {
		opt(grid)
	}
	
	return grid
}

// WithGridGap sets the gap between cells
func WithGridGap(gap int) GridOption {
	return func(g *gridLayout) {
		g.gap = gap
	}
}

// WithGridBordered sets whether cells should have borders
func WithGridBordered(bordered bool) GridOption {
	return func(g *gridLayout) {
		g.bordered = bordered
	}
}

// WithGridFocusable sets whether the grid supports focus navigation
func WithGridFocusable(focusable bool) GridOption {
	return func(g *gridLayout) {
		g.focusable = focusable
	}
}

// WithGridActiveColor sets the active border color
func WithGridActiveColor(color lipgloss.TerminalColor) GridOption {
	return func(g *gridLayout) {
		g.activeColor = color
	}
}