Class: Tuile::Component::List::Cursor::Limited

Inherits:
Cursor
  • Object
show all
Defined in:
lib/tuile/component/list.rb,
sig/tuile.rbs

Overview

Cursor which can only land on specific allowed items.

Instance Method Summary collapse

Constructor Details

#initialize(positions, position: ) ⇒ Limited

@param positions — allowed positions. Must not be empty.

@param position — initial position.

Parameters:

  • positions (::Array[Integer])
  • position: (Integer) (defaults to: )


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

Parameters:

  • item_count (Integer)

Returns:

  • (::Array[Integer])


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

Parameters:

  • count (Integer)
  • item_count (Integer)

Returns:

  • (Boolean)


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_firstBoolean

Returns:

  • (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

Parameters:

  • _item_count (Integer)

Returns:

  • (Boolean)


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

Parameters:

  • count (Integer)

Returns:

  • (Boolean)


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

Parameters:

  • item_index (Integer)
  • event (MouseEvent)
  • _item_count (Integer)

Returns:

  • (Boolean)


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.button == :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