Class: RubyTUI::Widgets::Scrollbar

Inherits:
Object
  • Object
show all
Includes:
RubyTUI::Widget
Defined in:
lib/rubytui/widgets/scrollbar.rb

Overview

Scrollbar renders a vertical or horizontal scroll indicator.

Examples:

Vertical scrollbar in the rightmost column

bar = RubyTUI::Widgets::Scrollbar.new(content_length: 100, position: offset, viewport_length: 20)
frame.render_widget(bar, Rect.new(area.right - 1, area.y, 1, area.height))

Constant Summary collapse

VERTICAL =

Vertical orientation, drawn in the first column of the area.

:vertical
HORIZONTAL =

Horizontal orientation, drawn in the first row of the area.

:horizontal
TRACK_V =

Vertical track.

"\u2502"
THUMB_V =

Vertical thumb.

"\u2588"
TRACK_H =

Horizontal track.

"\u2500"
THUMB_H =

Horizontal thumb.

"\u2588"
ARROW_UP =

Up arrow.

"\u25B2"
ARROW_DOWN =

Down arrow.

"\u25BC"
ARROW_LEFT =

Left arrow.

"\u25C0"
ARROW_RIGHT =

Right arrow.

"\u25B6"

Instance Method Summary collapse

Constructor Details

#initialize(content_length:, position: 0, viewport_length: nil, orientation: VERTICAL, style: Style::DEFAULT, thumb_style: nil, track_style: nil, show_arrows: true) ⇒ Scrollbar

Returns a new instance of Scrollbar.

Parameters:

  • content_length (Integer) —

    total content length, clamped to >= 1

  • position (Integer) (defaults to: 0) —

    scroll offset into the content, clamped to 0..(content_length - viewport_length) when drawing (default: 0)

  • viewport_length (Integer, nil) (defaults to: nil) —

    visible length of the content (default: nil, the track length in cells, arrows included)

  • orientation (Symbol) (defaults to: VERTICAL) —

    VERTICAL (default) or HORIZONTAL; any other value is drawn horizontally

  • style (Style) (defaults to: Style::DEFAULT) —

    currently unused by rendering (default: Style::DEFAULT)

  • thumb_style (Style, nil) (defaults to: nil) —

    thumb style (default: white foreground)

  • track_style (Style, nil) (defaults to: nil) —

    track and arrow style (default: bright black foreground)

  • show_arrows (Boolean) (defaults to: true) —

    draw an arrow at each end (default: true)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubytui/widgets/scrollbar.rb', line 48

def initialize(
  content_length:,
  position: 0,
  viewport_length: nil,
  orientation: VERTICAL,
  style: Style::DEFAULT,
  thumb_style: nil,
  track_style: nil,
  show_arrows: true
)
  @content_length = [content_length, 1].max
  @position = position
  @viewport_length = viewport_length
  @orientation = orientation
  @style = style
  @thumb_style = thumb_style || Style.new.fg(Color::WHITE)
  @track_style = track_style || Style.new.fg(Color::BRIGHT_BLACK)
  @show_arrows = show_arrows
end

Instance Method Details

#render(area, buf) ⇒ void

This method returns an undefined value.

Draw the track, thumb and optional arrows along the first column (vertical) or first row (horizontal) of the area.

The thumb fills the whole track when the content fits in the viewport. Nothing is drawn if no track cells remain after the arrows.

Parameters:

  • area (Rect) —

    the rectangular area to render into

  • buf (Buffer) —

    the buffer to write cells into



77
78
79
80
81
82
83
84
85
# File 'lib/rubytui/widgets/scrollbar.rb', line 77

def render(area, buf)
  return if area.empty?

  if @orientation == VERTICAL
    render_vertical(area, buf)
  else
    render_horizontal(area, buf)
  end
end