Skip to content

Add a scrollbar to BookContent #335

@kdroidFilter

Description

@kdroidFilter

Problem

BookContent uses virtualized rendering (LazyColumn + Paging 3) which means only visible lines are rendered. Without knowing the actual height of all content, a traditional scrollbar cannot accurately represent the user's position in the document.

A naive index-based scrollbar (position = current line / total lines) fails because line heights vary dramatically.


Data Analysis

The database contains ~4 million lines with extreme content length variation:

Content Length Line Count Estimated Visual Lines
0 (empty) 18 0
1-99 chars 1,815,424 ~1
100-499 chars 1,321,836 1-5
500-999 chars 450,538 5-10
1000-4999 chars 353,235 10-50
5000-9999 chars 14,649 50-100
10000+ chars 3,956 100+
Maximum 1 1,275,307 chars ≈ 15,000 visual lines

Example: At database line 50/100, an index-based scrollbar shows 50%. But if lines 1-49 are short (1 visual line each = 49 lines) and lines 50-100 are long (50 visual lines each = 2,550 lines), you're actually at 49 / 2,599 ≈ 2% of the document.


Proposed Solution

  1. Store character count per line in the database with a cumulative index
  2. At runtime, measure how many characters fit per visual line for the current view width
  3. Calculate scroll position using: position = cumulative_chars_at_line / total_book_chars

This must be reactive to view size changes (resize, zoom, font changes).

Limitations

This approach will not be pixel-perfect since bold or italic characters take slightly more horizontal space than regular text, affecting line wrapping. However, this difference should be negligible and not perceptible to users during normal navigation.

Benefits

  • Scrollbar accurately reflects real content position
  • Works with fully virtualized content (no need to render everything)
  • O(1) position calculation with pre-computed cumulative index

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions