Class: TuiTui::Modal::Select

Inherits:
Modal
  • Object
show all
Defined in:
lib/tui_tui/modal/select.rb

Overview

Scrollable list picker modal.

Constant Summary collapse

MAX_ROWS =
12
MIN_INNER =
16
WHEEL =
3

Instance Method Summary collapse

Constructor Details

#initialize(title, items, default: 0, theme: Theme::DEFAULT) ⇒ Select

Returns a new instance of Select.



19
20
21
22
23
24
25
# File 'lib/tui_tui/modal/select.rb', line 19

def initialize(title, items, default: 0, theme: Theme::DEFAULT)
  @title = DisplayText.new(title)
  @items = items.map { |item| DisplayText.new(item) }
  @list = Widget::ScrollList.new(@items.size)
  @list.go_to(default)
  @theme = theme
end

Instance Method Details

#cursorObject



27
# File 'lib/tui_tui/modal/select.rb', line 27

def cursor = @list.cursor

#draw(canvas, size) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/tui_tui/modal/select.rb', line 57

def draw(canvas, size)
  rows = visible_rows(size)
  inner = [MIN_INNER, @title.width, *@items.map(&:width)].max
  rect, text_col = panel(canvas, inner: inner, body_rows: rows + 2)

  canvas.text(rect.row + 1, text_col, @title.truncate(inner), theme.title)
  draw_items(canvas, rect.row + 3, text_col, inner, rows)
  canvas
end

#handle(key) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tui_tui/modal/select.rb', line 29

def handle(key)
  case KeyIntent.for(key)
  when :up
    nudge(-1)
  when :down
    nudge(1)
  when :top
    nudge_to(0)
  when :bottom
    nudge_to(@list.last)
  when :cancel
    :cancel
  else
    @list.cursor if ["\r", " "].include?(key)
  end
end

#handle_mouse(event) ⇒ Object

Wheel moves the highlighted item; a click on an item picks it. Returns the chosen index on a click, otherwise nil (stay open).



48
49
50
51
52
53
54
55
# File 'lib/tui_tui/modal/select.rb', line 48

def handle_mouse(event)
  case event.action
  when :wheel
    nudge(event.button == :wheel_up ? -WHEEL : WHEEL)
  when :press
    click(event)
  end
end