Class: TuiTui::Widget::List
- Inherits:
-
Object
- Object
- TuiTui::Widget::List
- Defined in:
- lib/tui_tui/widget/list.rb
Overview
Drawing companion for ScrollList. Row content comes from the caller, keeping the list domain-agnostic.
Instance Method Summary collapse
- #draw(canvas, rect, highlight: nil, scrollbar: nil, auto: false) ⇒ Object
-
#index_at(rect, event, scrollbar: nil) ⇒ Object
Map a MouseEvent to the list index under it, or nil.
-
#initialize(scroll) ⇒ List
constructor
A new instance of List.
Constructor Details
#initialize(scroll) ⇒ List
Returns a new instance of List.
12 13 14 |
# File 'lib/tui_tui/widget/list.rb', line 12 def initialize(scroll) @scroll = scroll end |
Instance Method Details
#draw(canvas, rect, highlight: nil, scrollbar: nil, auto: false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tui_tui/widget/list.rb', line 16 def draw(canvas, rect, highlight: nil, scrollbar: nil, auto: false) # With auto:, reserve the gutter only when the content overflows the rect. = && !(auto && @scroll.count <= rect.rows) body, gutter = ? rect.split_gutter : [rect, nil] @scroll.ensure_visible(body.rows) @scroll.each_visible(body.rows) do |index, offset| row = body.row + offset selected = index == @scroll.cursor canvas.fill(Rect.new(row: row, col: body.col, rows: 1, cols: body.cols), highlight) if highlight && selected canvas.line(row, body.col, Line.coerce(yield(index, selected)).truncate(body.cols)) end (canvas, gutter, ) if gutter canvas end |
#index_at(rect, event, scrollbar: nil) ⇒ Object
Map a MouseEvent to the list index under it, or nil. Pass the same rect
and scrollbar: used for draw so the gutter column is excluded and the
scroll offset matches what was rendered. Returns nil for clicks outside the
body or below the last item.
36 37 38 39 40 41 42 |
# File 'lib/tui_tui/widget/list.rb', line 36 def index_at(rect, event, scrollbar: nil) body = ? rect.split_gutter.first : rect return nil unless body.hit?(event) index = @scroll.top + (event.row - body.row) index < @scroll.count ? index : nil end |