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(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.
479 480 481 482 483 484 485 |
# File 'lib/tuile/component/list.rb', line 479 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
504 505 506 |
# File 'lib/tuile/component/list.rb', line 504 def candidate_positions(item_count) @positions.select { _1 < item_count } end |
#go_down_by(count, item_count) ⇒ Boolean
@param count
@param item_count
519 520 521 522 523 524 |
# File 'lib/tuile/component/list.rb', line 519 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
536 537 538 |
# File 'lib/tuile/component/list.rb', line 536 def go_to_first go(@positions.first) end |
#go_to_last(_item_count) ⇒ Boolean
@param _item_count
510 511 512 |
# File 'lib/tuile/component/list.rb', line 510 def go_to_last(_item_count) go(@positions.last) end |
#go_up_by(count) ⇒ Boolean
@param count
528 529 530 531 532 533 |
# File 'lib/tuile/component/list.rb', line 528 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(item_index, event, _item_count) ⇒ Boolean
@param item_index
@param event
@param _item_count
491 492 493 494 495 496 497 498 499 500 |
# File 'lib/tuile/component/list.rb', line 491 def handle_mouse(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 |