Class: Tuile::VerticalScrollBar

Inherits:
Object
  • Object
show all
Defined in:
lib/tuile/vertical_scroll_bar.rb

Overview

A vertical scrollbar that computes which character to draw at each row.

Uses ‘█` for the handle (filled track) and `░` for the empty track. There are no up/down arrows; the full height is used as the track. Handle geometry is precomputed in the constructor as #handle_height, #handle_start, and #handle_end.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height, line_count:, top_line:) ⇒ VerticalScrollBar

Returns a new instance of VerticalScrollBar.

Parameters:

  • height (Integer)

    number of rows in the scrollbar (== viewport height).

  • line_count (Integer)

    total number of content lines.

  • top_line (Integer)

    index of the first visible content line.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tuile/vertical_scroll_bar.rb', line 23

def initialize(height, line_count:, top_line:)
  @height = height

  return unless height >= 1

  if line_count <= height
    @handle_height = height
    @handle_start  = 0
    @handle_end    = height - 1
  else
    @handle_height = [(height * height / line_count.to_f).ceil, 1].max
    @handle_start  = (height * top_line / line_count.to_f).floor
    @handle_end    = @handle_start + @handle_height - 1
  end
end

Instance Attribute Details

#handle_endInteger (readonly)

Returns 0-based row where the handle ends (height >= 1 only).

Returns:

  • (Integer)

    0-based row where the handle ends (height >= 1 only).



17
18
19
# File 'lib/tuile/vertical_scroll_bar.rb', line 17

def handle_end
  @handle_end
end

#handle_heightInteger (readonly)

Returns number of track rows the handle occupies (height >= 1 only).

Returns:

  • (Integer)

    number of track rows the handle occupies (height >= 1 only).



13
14
15
# File 'lib/tuile/vertical_scroll_bar.rb', line 13

def handle_height
  @handle_height
end

#handle_startInteger (readonly)

Returns 0-based row where the handle starts (height >= 1 only).

Returns:

  • (Integer)

    0-based row where the handle starts (height >= 1 only).



15
16
17
# File 'lib/tuile/vertical_scroll_bar.rb', line 15

def handle_start
  @handle_start
end

Instance Method Details

#scrollbar_char(row_in_viewport) ⇒ String

Returns the scrollbar character for the given viewport row.

Parameters:

  • row_in_viewport (Integer)

    0-based row index within the viewport.

Returns:

  • (String)

    single scrollbar character.



42
43
44
# File 'lib/tuile/vertical_scroll_bar.rb', line 42

def scrollbar_char(row_in_viewport)
  row_in_viewport >= @handle_start && row_in_viewport <= @handle_end ? "" : ""
end