Class: Tuile::Component::List::Cursor::Limited
- Inherits:
-
Cursor
- Object
- Cursor
- Tuile::Component::List::Cursor::Limited
- Defined in:
- lib/tuile/component/list.rb,
sig/tuile.rbs
Overview
Cursor which can only land on specific allowed items.
Instance Method Summary collapse
-
#candidate_positions(item_count) ⇒ ::Array[Integer]
@param
item_count. -
#go_down_by(count, item_count) ⇒ Boolean
@param
count. - #go_to_first ⇒ Boolean
-
#go_to_last(_item_count) ⇒ Boolean
@param
_item_count. -
#go_up_by(count) ⇒ Boolean
@param
count. -
#handle_mouse_down?(item_index, event, _item_count) ⇒ Boolean
@param
item_index. -
#initialize(positions, position: ) ⇒ Limited
constructor
@param
positions— allowed positions.
Constructor Details
#initialize(positions, position: ) ⇒ Limited
@param positions — allowed positions. Must not be empty.
@param position — initial position.
511 512 513 514 515 516 517 |
# File 'lib/tuile/component/list.rb', line 511 def initialize(positions, position: positions[0]) raise "positions are empty" if positions.empty? @positions = positions.sort position = @positions[@positions.rindex { _1 < position } || 0] unless @positions.include?(position) super(position: position) end |
Instance Method Details
#candidate_positions(item_count) ⇒ ::Array[Integer]
@param item_count
536 537 538 |
# File 'lib/tuile/component/list.rb', line 536 def candidate_positions(item_count) @positions.select { _1 < item_count } end |
#go_down_by(count, item_count) ⇒ Boolean
@param count
@param item_count
551 552 553 554 555 556 |
# File 'lib/tuile/component/list.rb', line 551 def go_down_by(count, item_count) next_pos = @positions.find { _1 >= @position + count } return go_to_last(item_count) if next_pos.nil? go(next_pos) end |
#go_to_first ⇒ Boolean
568 569 570 |
# File 'lib/tuile/component/list.rb', line 568 def go_to_first go(@positions.first) end |
#go_to_last(_item_count) ⇒ Boolean
@param _item_count
542 543 544 |
# File 'lib/tuile/component/list.rb', line 542 def go_to_last(_item_count) go(@positions.last) end |
#go_up_by(count) ⇒ Boolean
@param count
560 561 562 563 564 565 |
# File 'lib/tuile/component/list.rb', line 560 def go_up_by(count) prev_pos = @positions.reverse_each.find { _1 <= @position - count } return go_to_first if prev_pos.nil? go(prev_pos) end |
#handle_mouse_down?(item_index, event, _item_count) ⇒ Boolean
@param item_index
@param event
@param _item_count
523 524 525 526 527 528 529 530 531 532 |
# File 'lib/tuile/component/list.rb', line 523 def handle_mouse_down?(item_index, event, _item_count) if event. == :left prev_pos = @positions.reverse_each.find { _1 <= item_index } return go_to_first if prev_pos.nil? go(prev_pos) else false end end |