Pad/internal/browser/handlers.go

44 lines
1.2 KiB
Go

package browser
// HandleScroll updates the browser scroll index by the given delta (in entries).
// Clamps to valid bounds and triggers prefetch/eviction as needed.
func HandleScroll(s *BrowserState, delta int) {
deltaPx := int(float64(delta) * s.EntryHeight)
HandlePixelScroll(s, deltaPx)
}
// HandlePixelScroll updates the browser scroll offset by the given delta (in pixels).
// Clamps to valid bounds and triggers eviction as needed.
func HandlePixelScroll(s *BrowserState, delta int) {
s.ScrollOffset += float64(delta)
// Clamp to valid bounds
clampScrollOffset(s)
// Trigger eviction of distant pages
s.EvictPages()
}
// clampScrollOffset ensures the scroll offset stays within valid bounds.
func clampScrollOffset(s *BrowserState) {
if s.ScrollOffset < 0 {
s.ScrollOffset = 0
}
// Use search results count if active, otherwise use TotalEntries
entryCount := s.TotalEntries
if s.Query != "" {
entryCount = len(s.SearchResults)
}
// Calculate maximum scroll offset
maxScroll := 0.0
if entryCount > s.VisibleCount {
maxScroll = float64(entryCount-s.VisibleCount) * s.EntryHeight
}
if s.ScrollOffset > maxScroll {
s.ScrollOffset = maxScroll
}
}