Implement per-pixel smooth scrolling for ListView

- Changed BrowserScrollOffset and ListView.ScrollOffset from int (row index)
  to ui.Dp (pixel offset) for smooth per-pixel scrolling
- Updated HandleBrowserScroll to use raw Dp delta instead of row-based
  scrolling with minimum ±1 row jumps
- Fixed search not triggering new frames by detecting query changes in
  main.go FrameEvent handler
- Fixed click misalignment by indexing RowFilenames with local index i
  instead of rowGlobalIndex
- Fixed bottom clamping using actual BrowserListHeight and corrected
  formula: maxScroll = totalRows * rowHeight - BrowserListHeight
This commit is contained in:
gmp
2026-05-28 16:14:11 -04:00
parent 23fdeabad4
commit 8f185341b3
3 changed files with 56 additions and 29 deletions
+11 -9
View File
@@ -196,7 +196,7 @@ type ListView struct {
visible bool
interactions []Interaction
Items []ListItem
ScrollOffset int
ScrollOffset Dp // pixel-level scroll offset
Selected int
RowFilenames []string // filenames for click navigation
}
@@ -235,25 +235,27 @@ func (lv ListView) Draw(gtx layout.Context, r *Renderer) {
}
}
rowHeight := Dp(48)
firstVisibleRow := int(lv.ScrollOffset / rowHeight)
for i, item := range lv.Items {
// Account for scroll offset: row Y is shifted up by scroll amount
rowGlobalIndex := firstVisibleRow + i
y := lv.region.Y + Dp(rowGlobalIndex)*rowHeight - lv.ScrollOffset
// Only draw visible rows (virtualized)
y := lv.region.Y + Dp(i)*rowHeight
if y+rowHeight < lv.region.Y || y > lv.region.Y+lv.region.H {
continue
}
// Draw background for selected item
if item.Selected || (lv.Selected == i) {
r.drawBg(gtx, Region{
X: lv.region.X, Y: lv.region.Y + Dp(i)*rowHeight,
X: lv.region.X, Y: y,
W: lv.region.W, H: rowHeight,
}, Color{R: 200, G: 220, B: 255, A: 255})
}
// Register click area for this row
rowGlobalIndex := lv.ScrollOffset + i
rowID := fmt.Sprintf("list_row_%d", rowGlobalIndex)
var filename string
if rowGlobalIndex < len(lv.RowFilenames) {
filename = lv.RowFilenames[rowGlobalIndex]
if i < len(lv.RowFilenames) {
filename = lv.RowFilenames[i]
}
r.RegisterClick(gtx, rowID, Region{
X: lv.region.X, Y: y,
@@ -264,7 +266,7 @@ func (lv ListView) Draw(gtx layout.Context, r *Renderer) {
// Draw main text
textRegion := Region{
X: lv.region.X + Dp(8),
Y: lv.region.Y + Dp(i)*rowHeight + Dp(4),
Y: y + Dp(4),
W: lv.region.W - Dp(32),
H: rowHeight - Dp(8),
}
@@ -273,7 +275,7 @@ func (lv ListView) Draw(gtx layout.Context, r *Renderer) {
if item.Subtext != "" {
subRegion := Region{
X: lv.region.X + Dp(8),
Y: lv.region.Y + Dp(i)*rowHeight + rowHeight - Dp(22),
Y: y + rowHeight - Dp(22),
W: lv.region.W - Dp(32),
H: Dp(16),
}
@@ -283,7 +285,7 @@ func (lv ListView) Draw(gtx layout.Context, r *Renderer) {
}
// NewListView creates a visible ListView element.
func NewListView(id string, items []ListItem, region Region, scrollOffset, selected int, interactions []Interaction) ListView {
func NewListView(id string, items []ListItem, region Region, scrollOffset Dp, selected int, interactions []Interaction) ListView {
return ListView{
id: id,
region: region,