gitstack

grindlemire/go-tui code browser

7.1 KB Go 260 lines 2026-04-18 · 15a2ce7 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
package tui

// --- Scroll Query Methods ---

// IsScrollable returns whether this element has scrolling enabled.
func (e *Element) IsScrollable() bool {
	return e.scrollMode != ScrollNone
}

// ScrollMode returns the current scroll mode.
func (e *Element) ScrollModeValue() ScrollMode {
	return e.scrollMode
}

// ScrollOffset returns the current scroll position.
func (e *Element) ScrollOffset() (x, y int) {
	return e.scrollX, e.scrollY
}

// ContentSize returns the total scrollable content dimensions.
// This is computed during layout and may exceed the viewport size.
func (e *Element) ContentSize() (width, height int) {
	return e.contentWidth, e.contentHeight
}

// ViewportSize returns the visible area dimensions (content rect size).
func (e *Element) ViewportSize() (width, height int) {
	cr := e.ContentRect()
	return cr.Width, cr.Height
}

// MaxScroll returns the maximum scroll offset in each direction.
func (e *Element) MaxScroll() (maxX, maxY int) {
	vw, vh := e.ViewportSize()
	maxX = max(0, e.contentWidth-vw)
	maxY = max(0, e.contentHeight-vh)
	return
}

// --- Scroll Control Methods ---

// ScrollTo sets the scroll offset directly, clamped to valid range.
func (e *Element) ScrollTo(x, y int) {
	if e.scrollMode == ScrollNone {
		return
	}

	maxX, maxY := e.MaxScroll()

	newX := clamp(x, 0, maxX)
	newY := clamp(y, 0, maxY)

	// Only update if changed
	if newX != e.scrollX || newY != e.scrollY {
		e.scrollX = newX
		e.scrollY = newY
		e.MarkDirty()
	}
}

// ScrollBy adjusts scroll offset by delta.
func (e *Element) ScrollBy(dx, dy int) {
	e.ScrollTo(e.scrollX+dx, e.scrollY+dy)
}

// ScrollToTop scrolls to the top of the content.
func (e *Element) ScrollToTop() {
	e.ScrollTo(e.scrollX, 0)
}

// ScrollToBottom scrolls to the bottom of the content.
// It scrolls immediately using the current maxY, and also sets a pending
// flag to re-scroll after the next layout. This ensures it works correctly
// both for keyboard navigation (immediate) and when following new content
// (deferred until layout computes the new maxY).
func (e *Element) ScrollToBottom() {
	_, maxY := e.MaxScroll()
	e.scrollY = maxY
	e.scrollToBottomPending = true
	e.MarkDirty()
}

// IsAtBottom returns true if the element is scrolled to the bottom.
// Useful for implementing "sticky scroll" behavior.
func (e *Element) IsAtBottom() bool {
	_, maxY := e.MaxScroll()
	return e.scrollY >= maxY
}

// ScrollIntoView scrolls minimally to make the child element fully visible.
// Does nothing if the child is not a descendant of this element.
func (e *Element) ScrollIntoView(child *Element) {
	if e.scrollMode == ScrollNone {
		return
	}

	// Find child's position relative to this element's content
	childRect := child.Rect()
	contentRect := e.ContentRect()

	// Child rect is in absolute coordinates, convert to relative
	relativeX := childRect.X - contentRect.X + e.scrollX
	relativeY := childRect.Y - contentRect.Y + e.scrollY

	vw, vh := e.ViewportSize()

	// Vertical scrolling
	if e.scrollMode == ScrollVertical || e.scrollMode == ScrollBoth {
		if relativeY < e.scrollY {
			// Child is above viewport
			e.scrollY = relativeY
		} else if relativeY+childRect.Height > e.scrollY+vh {
			// Child is below viewport
			e.scrollY = relativeY + childRect.Height - vh
		}
	}

	// Horizontal scrolling
	if e.scrollMode == ScrollHorizontal || e.scrollMode == ScrollBoth {
		if relativeX < e.scrollX {
			// Child is left of viewport
			e.scrollX = relativeX
		} else if relativeX+childRect.Width > e.scrollX+vw {
			// Child is right of viewport
			e.scrollX = relativeX + childRect.Width - vw
		}
	}

	e.MarkDirty()
}

// --- Internal Layout ---

// layoutScrollContent re-layouts children with "infinite" space in the scroll direction.
// This is called after normal layout to compute actual content size.
func (e *Element) layoutScrollContent() {
	if e.scrollMode == ScrollNone || len(e.children) == 0 {
		e.contentWidth = 0
		e.contentHeight = 0
		return
	}

	contentRect := e.ContentRect()

	// Determine available space - infinite in scroll direction
	availableWidth := contentRect.Width
	availableHeight := contentRect.Height

	switch e.scrollMode {
	case ScrollVertical:
		availableHeight = 100000 // "infinite"
	case ScrollHorizontal:
		availableWidth = 100000
	case ScrollBoth:
		availableWidth = 100000
		availableHeight = 100000
	}

	// Check if we'll need a scrollbar and reserve space
	// We do a preliminary measure to decide
	needsVScrollbar := false
	needsHScrollbar := false

	// First pass: layout with full space to measure content
	e.layoutChildrenWithSpace(availableWidth, availableHeight)
	e.measureContentBounds()

	// Check if scrollbars are needed
	if !e.scrollbarHidden && (e.scrollMode == ScrollVertical || e.scrollMode == ScrollBoth) {
		needsVScrollbar = e.contentHeight > contentRect.Height
	}
	// TODO: horizontal scrollbar support
	_ = needsHScrollbar

	// If vertical scrollbar needed, reduce available width and re-layout
	if needsVScrollbar {
		availableWidth = contentRect.Width - 1
		if e.scrollMode == ScrollHorizontal || e.scrollMode == ScrollBoth {
			availableWidth = 100000
		}
		e.layoutChildrenWithSpace(availableWidth, availableHeight)
		e.measureContentBounds()
	}

	// Clamp scroll offset to valid range
	e.clampScrollOffset()
}

// layoutChildrenWithSpace lays out children within the given available space.
// Child positions are relative to (0, 0) in content space.
func (e *Element) layoutChildrenWithSpace(availableWidth, availableHeight int) {
	if len(e.children) == 0 {
		return
	}

	// Create a temporary "content" element to layout children
	// This gives us positions starting from (0, 0) in content space
	tempContainer := &Element{
		style:    e.style,
		children: e.children,
		dirty:    true,
	}
	tempContainer.style.Width = Fixed(availableWidth)
	tempContainer.style.Height = Fixed(availableHeight)
	// Clear padding since we're laying out in content space (already inside padding)
	tempContainer.style.Padding = Edges{}

	Calculate(tempContainer, availableWidth, availableHeight)
}

// measureContentBounds computes the bounding box of all children.
func (e *Element) measureContentBounds() {
	e.contentWidth = 0
	e.contentHeight = 0

	for _, child := range e.children {
		r := child.Rect()
		e.contentWidth = max(e.contentWidth, r.Right())
		e.contentHeight = max(e.contentHeight, r.Bottom())
	}
}

// clampScrollOffset ensures scroll offset is within valid range.
func (e *Element) clampScrollOffset() {
	maxX, maxY := e.MaxScroll()

	// Handle deferred scroll-to-bottom
	if e.scrollToBottomPending {
		e.scrollY = maxY
		e.scrollToBottomPending = false
	}

	e.scrollX = clamp(e.scrollX, 0, maxX)
	e.scrollY = clamp(e.scrollY, 0, maxY)
}

// --- Internal Helpers ---

// needsVerticalScrollbar returns whether a vertical scrollbar should be shown.
func (e *Element) needsVerticalScrollbar() bool {
	if e.scrollMode != ScrollVertical && e.scrollMode != ScrollBoth {
		return false
	}
	if e.scrollbarHidden {
		return false
	}
	_, vh := e.ViewportSize()
	return e.contentHeight > vh
}

// clamp restricts v to the range [minVal, maxVal].
func clamp(v, minVal, maxVal int) int {
	if v < minVal {
		return minVal
	}
	if v > maxVal {
		return maxVal
	}
	return v
}