gitstack

grindlemire/go-tui code browser

9.3 KB Go 342 lines 2026-05-29 · 63ba8eb raw
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
package tui

// --- Implement Layoutable interface ---

// LayoutStyle returns the layout style properties for this element.
// If the element has a border, padding is increased to account for border width.
func (e *Element) LayoutStyle() LayoutStyle {
	style := e.style
	// Add padding for border (HR uses border field for line style, not actual border)
	if e.border != BorderNone && !e.hr {
		// Border takes 1 character on each side
		style.Padding.Top += 1
		style.Padding.Right += 1
		style.Padding.Bottom += 1
		style.Padding.Left += 1
	}
	return style
}

// LayoutChildren returns the children to be laid out.
// Hidden children are excluded from layout.
func (e *Element) LayoutChildren() []Layoutable {
	result := make([]Layoutable, 0, len(e.children))
	for _, child := range e.children {
		if !child.hidden && !child.overlay {
			result = append(result, child)
		}
	}
	return result
}

// SetLayout is called by the layout engine to store computed layout.
func (e *Element) SetLayout(l LayoutResult) {
	e.layout = l
}

// GetLayout returns the last computed layout.
func (e *Element) GetLayout() LayoutResult {
	return e.layout
}

// IsDirty returns whether this element needs layout recalculation.
func (e *Element) IsDirty() bool {
	return e.dirty
}

// SetDirty marks this element as needing recalculation or not.
func (e *Element) SetDirty(dirty bool) {
	e.dirty = dirty
}

// IsHR returns whether this element is a horizontal rule.
func (e *Element) IsHR() bool {
	return e.hr
}

// IntrinsicSize returns the natural content-based dimensions of this element.
// For text elements, returns the text width and height (1 line).
// For containers, returns the computed intrinsic size based on children.
func (e *Element) IntrinsicSize() (width, height int) {
	// HR has intrinsic height of 1, but 0 intrinsic width.
	// The 0 width is intentional - HR relies on AlignSelf=Stretch (set by WithHR)
	// to fill the container width, similar to how block elements work in CSS.
	if e.hr {
		return 0, 1
	}

	// Scrollable elements have 0 intrinsic size in their scroll direction.
	// They rely on flexGrow or explicit sizing to get space, then scroll their content.
	// This prevents content from pushing other elements out of the layout.
	if e.scrollMode != ScrollNone {
		// Return 0 for scrollable dimensions - the element will use available space
		return 0, 0
	}

	// Table elements compute intrinsic size from column widths and row heights
	if e.tag == "table" {
		w, h := TableIntrinsicSize(e)
		w += e.style.Padding.Horizontal()
		h += e.style.Padding.Vertical()
		if e.border != BorderNone {
			w += 2
			h += 2
		}
		return w, h
	}

	// Text content has explicit intrinsic size
	if e.text != "" {
		textWidth := stringWidth(e.text)
		textHeight := 1
		// Add padding to get the element's intrinsic size
		width = textWidth + e.style.Padding.Horizontal()
		height = textHeight + e.style.Padding.Vertical()
		// Add border if present (borders take 1 cell on each side)
		if e.border != BorderNone {
			width += 2
			height += 2
		}
		return width, height
	}

	// Rich text content has explicit intrinsic size (single unwrapped line).
	if len(e.richText) > 0 {
		width = richTextWidth(e.richText) + e.style.Padding.Horizontal()
		height = 1 + e.style.Padding.Vertical()
		if e.border != BorderNone {
			width += 2
			height += 2
		}
		return width, height
	}

	// For containers without text, compute from children
	if len(e.children) == 0 {
		// Empty container: use explicit dimensions if set, otherwise 0
		if e.style.Width.IsFixed() {
			width = int(e.style.Width.Amount)
		}
		if e.style.Height.IsFixed() {
			height = int(e.style.Height.Amount)
		}
		return width, height
	}

	// Compute intrinsic size from children
	isRow := e.style.Direction == Row
	// Block mode forces column direction regardless of Direction setting
	if e.style.Display == DisplayBlock {
		isRow = false
	}
	var intrinsicW, intrinsicH int
	visibleIdx := 0

	for _, child := range e.children {
		if child.hidden || child.overlay {
			continue
		}
		childW, childH := child.IntrinsicSize()
		childStyle := child.LayoutStyle()
		marginH := childStyle.Margin.Horizontal()
		marginV := childStyle.Margin.Vertical()

		if isRow {
			intrinsicW += childW + marginH
			if childH+marginV > intrinsicH {
				intrinsicH = childH + marginV
			}
		} else {
			if childW+marginH > intrinsicW {
				intrinsicW = childW + marginH
			}
			intrinsicH += childH + marginV
		}

		// Add gap between visible children (not before first)
		if visibleIdx > 0 {
			if isRow {
				intrinsicW += e.style.Gap
			} else {
				intrinsicH += e.style.Gap
			}
		}
		visibleIdx++
	}

	// Add padding
	intrinsicW += e.style.Padding.Horizontal()
	intrinsicH += e.style.Padding.Vertical()

	// Add border if present
	if e.border != BorderNone {
		intrinsicW += 2
		intrinsicH += 2
	}

	// Respect explicit dimensions: if Width or Height is set, use it
	// instead of the content-derived value. This ensures elements with
	// WithWidth/WithHeight report those sizes to parent layout calculations.
	if e.style.Width.IsFixed() {
		intrinsicW = int(e.style.Width.Amount)
	}
	if e.style.Height.IsFixed() {
		intrinsicH = int(e.style.Height.Amount)
	}

	return intrinsicW, intrinsicH
}

// HeightForWidth returns the height this element needs given an assigned width.
// For text elements with wrapping enabled, computes the wrapped text height.
// For column containers with Auto height, recursively computes from children.
// Scrollable elements and elements with explicit heights are not affected.
func (e *Element) HeightForWidth(width int) int {
	// Explicit height takes priority over content-derived height.
	if e.style.Height.IsFixed() {
		return int(e.style.Height.Amount)
	}

	// Scrollable elements have fixed viewport — don't expand based on content.
	if e.scrollMode != ScrollNone {
		_, h := e.IntrinsicSize()
		return h
	}

	// Text elements with wrapping
	if e.text != "" && !e.noWrap {
		contentWidth := width - e.style.Padding.Horizontal()
		if e.border != BorderNone {
			contentWidth -= 2
		}
		if contentWidth <= 0 {
			h := e.style.Padding.Vertical()
			if e.border != BorderNone {
				h += 2
			}
			return h
		}
		lines := wrapText(e.text, contentWidth)
		h := len(lines) + e.style.Padding.Vertical()
		if e.border != BorderNone {
			h += 2
		}
		return h
	}

	// Rich text elements with wrapping.
	if len(e.richText) > 0 && !e.noWrap {
		contentWidth := width - e.style.Padding.Horizontal()
		if e.border != BorderNone {
			contentWidth -= 2
		}
		if contentWidth <= 0 {
			h := e.style.Padding.Vertical()
			if e.border != BorderNone {
				h += 2
			}
			return h
		}
		lines := wrapSpans(e.richText, contentWidth)
		h := len(lines) + e.style.Padding.Vertical()
		if e.border != BorderNone {
			h += 2
		}
		return h
	}

	// Column containers: recursively compute from children.
	// Each child gets the full content width (correct for AlignStretch + Auto width,
	// which is the default). This propagates text wrapping heights up the tree.
	isColumn := e.style.Direction == Column || e.style.Display == DisplayBlock
	if len(e.children) > 0 && isColumn {
		contentWidth := width - e.style.Padding.Horizontal()
		if e.border != BorderNone {
			contentWidth -= 2
		}
		totalH := 0
		visibleIdx := 0
		for _, child := range e.children {
			if child.hidden || child.overlay {
				continue
			}
			childH := child.HeightForWidth(contentWidth)
			totalH += childH
			if visibleIdx > 0 {
				totalH += e.style.Gap
			}
			visibleIdx++
		}
		totalH += e.style.Padding.Vertical()
		if e.border != BorderNone {
			totalH += 2
		}
		return totalH
	}

	// Row containers: recursively compute max child height.
	// Children share the width via flex, so we approximate by giving each child
	// its intrinsic width or a fair share. For text wrapping, the key case is
	// row children with explicit or flex-computed widths which Phase 3.5 handles
	// directly. Here we just find the max child height for the cross-axis.
	if len(e.children) > 0 {
		contentWidth := width - e.style.Padding.Horizontal()
		if e.border != BorderNone {
			contentWidth -= 2
		}
		maxH := 0
		for _, child := range e.children {
			if child.hidden || child.overlay {
				continue
			}
			// For row children, approximate: give each child the full width
			// (overestimate). Phase 3.5 handles precise per-child widths.
			childH := child.HeightForWidth(contentWidth)
			if childH > maxH {
				maxH = childH
			}
		}
		maxH += e.style.Padding.Vertical()
		if e.border != BorderNone {
			maxH += 2
		}
		return maxH
	}

	// Default: intrinsic height
	_, h := e.IntrinsicSize()
	return h
}

// Tag returns the element tag for layout dispatch.
func (e *Element) Tag() string {
	return e.tag
}

// Calculate computes layout for this Element and all descendants.
func (e *Element) Calculate(availableWidth, availableHeight int) {
	Calculate(e, availableWidth, availableHeight)
}

// Rect returns the computed border box.
func (e *Element) Rect() Rect {
	return e.layout.Rect
}

// ContentRect returns the computed content area.
func (e *Element) ContentRect() Rect {
	return e.layout.ContentRect
}

// MarkDirty marks this Element and ancestors as needing recalculation.
// Also marks the owning app as dirty so the app knows to re-render.
func (e *Element) MarkDirty() {
	for elem := e; elem != nil && !elem.dirty; elem = elem.parent {
		elem.dirty = true
	}
	if e.app != nil {
		e.app.MarkDirty()
	}
	// nil app: element-level dirty flags are set; app-level dirty
	// will be set when the element is attached via setAppRecursive.
}