|
| 1 | +Virtual scrolling implementation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Implement virtual scrolling for the file list to handle 100k+ files without DOM performance issues. Currently, all files |
| 6 | +are rendered as DOM elements which becomes slow with large directories. |
| 7 | + |
| 8 | +## Current architecture |
| 9 | + |
| 10 | +### Files to modify |
| 11 | + |
| 12 | +- [src/lib/file-explorer/FileList.svelte](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/FileList.svelte) - |
| 13 | + Main target, needs virtual scrolling |
| 14 | +- [src/lib/file-explorer/FilePane.svelte](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/FilePane.svelte) - |
| 15 | + May need updates for scroll position management |
| 16 | +- [src/lib/file-explorer/apply-diff.ts](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/apply-diff.ts) - |
| 17 | + Already handles cursor preservation, likely no changes needed |
| 18 | + |
| 19 | +### Current FileList.svelte structure |
| 20 | + |
| 21 | +```svelte |
| 22 | +<ul class="file-list"> |
| 23 | + {#each files as file, index (file.path)} |
| 24 | + <li class="file-entry">...</li> |
| 25 | + {/each} |
| 26 | +</ul> |
| 27 | +``` |
| 28 | + |
| 29 | +**Problem:** Renders ALL files in DOM. With 100k files = 100k DOM elements = slow. |
| 30 | + |
| 31 | +**Goal:** Only render ~50 visible items + buffer, recycle DOM nodes as user scrolls. |
| 32 | + |
| 33 | +### Current data flow |
| 34 | + |
| 35 | +``` |
| 36 | +FilePane.svelte |
| 37 | +├── allFilesRaw: FileEntry[] (plain JS array, NOT reactive) |
| 38 | +├── filesVersion: number (incremented to trigger re-renders) |
| 39 | +├── selectedIndex: number (cursor position) |
| 40 | +└── FileList.svelte |
| 41 | + ├── files: FileEntry[] (filtered view, prop) |
| 42 | + ├── selectedIndex: number (prop) |
| 43 | + └── scrollToIndex(index) (exported method for keyboard nav) |
| 44 | +``` |
| 45 | + |
| 46 | +## Interaction with Phase 3.5 (file watching) |
| 47 | + |
| 48 | +### Key concern: Diffs during partial render |
| 49 | + |
| 50 | +When file watching emits a diff (add/remove/modify), |
| 51 | +[applyDiff()](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/apply-diff.ts#6-67) |
| 52 | +in |
| 53 | +[apply-diff.ts](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/apply-diff.ts) |
| 54 | +modifies `allFilesRaw` and returns the new cursor index. The virtual scroller must handle: |
| 55 | + |
| 56 | +1. **Added files** - May be inserted anywhere in the list (sorted insertion) |
| 57 | +2. **Removed files** - May be in visible area, before visible area, or after |
| 58 | +3. **Modified files** - Same position, just data change |
| 59 | +4. **Cursor preservation** - Already handled by |
| 60 | + [applyDiff()](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/apply-diff.ts#6-67) |
| 61 | + which finds selected file by path |
| 62 | + |
| 63 | +### Race condition: Diff arrives during scroll |
| 64 | + |
| 65 | +If user is scrolling and a diff arrives: |
| 66 | + |
| 67 | +- `allFilesRaw` length changes |
| 68 | +- Virtual scroll calculations (startIndex, endIndex) may become stale |
| 69 | +- Must recalculate visible window |
| 70 | + |
| 71 | +**Recommendation:** After |
| 72 | +[applyDiff()](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/apply-diff.ts#6-67), |
| 73 | +bump `filesVersion` (already done) which should trigger recalculation. |
| 74 | + |
| 75 | +### Edge case: Diff during chunked loading |
| 76 | + |
| 77 | +Files load in chunks (5000 at a time). If a diff arrives while loading: |
| 78 | + |
| 79 | +1. Diff applies to current `allFilesRaw` (partial list) |
| 80 | +2. Next chunk arrives and is appended |
| 81 | +3. The file from the diff may already exist in the next chunk (duplicate!) |
| 82 | + |
| 83 | +**Current safeguard:** Diffs use path matching, so duplicates would be skipped. But this needs testing. |
| 84 | + |
| 85 | +## Technical approach |
| 86 | + |
| 87 | +### Option A: Fixed row height (recommended) |
| 88 | + |
| 89 | +- Assume each file entry is exactly 24px tall (current CSS: `padding: var(--spacing-xxs) var(--spacing-sm)` ≈ 24px) |
| 90 | +- Calculate: `visibleCount = Math.ceil(containerHeight / ROW_HEIGHT)` |
| 91 | +- Render: `startIndex` to `startIndex + visibleCount + buffer` |
| 92 | +- Use CSS transforms or absolute positioning for visible items |
| 93 | + |
| 94 | +```svelte |
| 95 | +<script> |
| 96 | + const ROW_HEIGHT = 24 |
| 97 | + let containerHeight = $state(0) |
| 98 | + let scrollTop = $state(0) |
| 99 | +
|
| 100 | + const startIndex = $derived(Math.floor(scrollTop / ROW_HEIGHT)) |
| 101 | + const visibleCount = $derived(Math.ceil(containerHeight / ROW_HEIGHT) + 20) // buffer |
| 102 | + const endIndex = $derived(Math.min(startIndex + visibleCount, files.length)) |
| 103 | + const visibleFiles = $derived(files.slice(startIndex, endIndex)) |
| 104 | + const totalHeight = $derived(files.length * ROW_HEIGHT) |
| 105 | +</script> |
| 106 | +
|
| 107 | +<div class="scroll-container" bind:clientHeight={containerHeight} onscroll={handleScroll}> |
| 108 | + <div class="spacer" style="height: {totalHeight}px"> |
| 109 | + <div class="visible-window" style="transform: translateY({startIndex * ROW_HEIGHT}px)"> |
| 110 | + {#each visibleFiles as file, i (file.path)} |
| 111 | + <div class="file-entry">...</div> |
| 112 | + {/each} |
| 113 | + </div> |
| 114 | + </div> |
| 115 | +</div> |
| 116 | +``` |
| 117 | + |
| 118 | +### Option B: Use a virtualization library |
| 119 | + |
| 120 | +Libraries like `svelte-virtual-list` or `svelte-tiny-virtual-list` exist but may have issues with: |
| 121 | + |
| 122 | +- Svelte 5 compatibility |
| 123 | +- Custom item rendering |
| 124 | +- Dynamic content updates from diffs |
| 125 | + |
| 126 | +**Recommendation:** Implement Option A (fixed height) - it's simpler, more controllable, and sufficient for file lists. |
| 127 | + |
| 128 | +## Required changes |
| 129 | + |
| 130 | +### FileList.svelte |
| 131 | + |
| 132 | +1. **Add container with fixed height and overflow** |
| 133 | +2. **Track scroll position and container height** |
| 134 | +3. **Calculate visible window (startIndex, endIndex)** |
| 135 | +4. **Render only visible items with correct offset** |
| 136 | +5. **Update `scrollToIndex()` to scroll by setting `scrollTop`, not `scrollIntoView`** |
| 137 | + |
| 138 | +### FilePane.svelte |
| 139 | + |
| 140 | +1. **May need to pass container height or let FileList handle it** |
| 141 | +2. **Ensure `filesVersion` bump triggers virtual list recalculation** |
| 142 | + |
| 143 | +### scrollToIndex() implementation |
| 144 | + |
| 145 | +Current: |
| 146 | + |
| 147 | +```typescript |
| 148 | +export function scrollToIndex(index: number) { |
| 149 | + const items = listElement.querySelectorAll('.file-entry') |
| 150 | + const item = items[index] |
| 151 | + item?.scrollIntoView({ block: 'nearest' }) |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +With virtual scrolling: |
| 156 | + |
| 157 | +```typescript |
| 158 | +export function scrollToIndex(index: number) { |
| 159 | + const targetScrollTop = index * ROW_HEIGHT |
| 160 | + const containerBottom = scrollTop + containerHeight |
| 161 | + |
| 162 | + if (targetScrollTop < scrollTop) { |
| 163 | + // Item above viewport - scroll up |
| 164 | + scrollContainer.scrollTop = targetScrollTop |
| 165 | + } else if (targetScrollTop + ROW_HEIGHT > containerBottom) { |
| 166 | + // Item below viewport - scroll down |
| 167 | + scrollContainer.scrollTop = targetScrollTop - containerHeight + ROW_HEIGHT |
| 168 | + } |
| 169 | + // else: item already visible, do nothing |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +## Testing considerations |
| 174 | + |
| 175 | +### Unit tests |
| 176 | + |
| 177 | +- Virtual window calculation with different list sizes |
| 178 | +- `scrollToIndex` behavior (above viewport, below viewport, already visible) |
| 179 | +- Interaction with |
| 180 | + [applyDiff](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/apply-diff.ts#6-67) - |
| 181 | + cursor should stay visible after diff |
| 182 | + |
| 183 | +### Manual tests |
| 184 | + |
| 185 | +1. **Large directory (100k files):** |
| 186 | + - Scroll performance should be smooth |
| 187 | + - Keyboard navigation should work |
| 188 | + - Cursor should stay visible when navigating |
| 189 | + |
| 190 | +2. **File watching interaction:** |
| 191 | + - Add file at top of list while scrolled to bottom - list should update, cursor stay |
| 192 | + - Delete visible file - adjacent file should become selected |
| 193 | + - Bulk changes (simulate git pull) - cursor should stay on same file or reset |
| 194 | + |
| 195 | +3. **Edge cases:** |
| 196 | + - Scroll to bottom, then delete last file |
| 197 | + - Navigate to parent (..) while virtual scroll is mid-list |
| 198 | + - Resize window while scrolled |
| 199 | + |
| 200 | +## Dependencies |
| 201 | + |
| 202 | +- No external dependencies needed |
| 203 | +- Use native scroll APIs |
| 204 | +- Use Svelte 5 reactivity (`$derived`, `$state`) |
| 205 | + |
| 206 | +## Performance targets |
| 207 | + |
| 208 | +- Render time for 100k files: < 16ms (60fps) |
| 209 | +- Scroll jank: none (use `will-change: transform` if needed) |
| 210 | +- Memory: ~50 DOM nodes regardless of list size |
| 211 | + |
| 212 | +## Files to reference |
| 213 | + |
| 214 | +- [src/lib/file-explorer/FileList.svelte](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/FileList.svelte) - |
| 215 | + Current implementation |
| 216 | +- [src/lib/file-explorer/FilePane.svelte](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/FilePane.svelte) - |
| 217 | + Parent component |
| 218 | +- [src/lib/file-explorer/apply-diff.ts](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/apply-diff.ts) - |
| 219 | + Cursor preservation logic |
| 220 | +- [src/lib/file-explorer/types.ts](file:///Users/veszelovszki/Library/CloudStorage/Dropbox/projects-git/vdavid/rusty-commander/src/lib/file-explorer/types.ts) - |
| 221 | + FileEntry type |
| 222 | + |
| 223 | +## Commands |
| 224 | + |
| 225 | +```bash |
| 226 | +# Run checks |
| 227 | +./scripts/check.sh |
| 228 | + |
| 229 | +# Run just frontend tests |
| 230 | +pnpm vitest run |
| 231 | + |
| 232 | +# Dev server |
| 233 | +pnpm tauri dev |
| 234 | +``` |
| 235 | + |
| 236 | +## Success criteria |
| 237 | + |
| 238 | +1. All existing tests pass |
| 239 | +2. Directory with 100k files scrolls smoothly (60fps) |
| 240 | +3. Keyboard navigation (arrow keys, Enter) works correctly |
| 241 | +4. File watching diffs apply correctly while scrolled |
| 242 | +5. Cursor stays visible or moves appropriately after diffs |
0 commit comments