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: )


501
502
503
504
505
506
507
# File 'lib/tuile/component/list.rb', line 501

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])


526
527
528
# File 'lib/tuile/component/list.rb', line 526

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)


541
542
543
544
545
546
# File 'lib/tuile/component/list.rb', line 541

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)


558
559
560
# File 'lib/tuile/component/list.rb', line 558

def go_to_first
  go(@positions.first)
end

#go_to_last(_item_count) ⇒ Boolean

@param _item_count

Parameters:

  • _item_count (Integer)

Returns:

  • (Boolean)


532
533
534
# File 'lib/tuile/component/list.rb', line 532

def go_to_last(_item_count)
  go(@positions.last)
end

#go_up_by(count) ⇒ Boolean

@param count

Parameters:

  • count (Integer)

Returns:

  • (Boolean)


550
551
552
553
554
555
# File 'lib/tuile/component/list.rb', line 550

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)


513
514
515
516
517
518
519
520
521
522
# File 'lib/tuile/component/list.rb', line 513

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