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

Inherits:
Tuile::Component::List::Cursor show all
Defined in:
lib/tuile/component/list.rb

Overview

Cursor which can only land on specific allowed lines.

Instance Attribute Summary

Attributes inherited from Tuile::Component::List::Cursor

#position

Instance Method Summary collapse

Methods inherited from Tuile::Component::List::Cursor

#go, #handle_key

Constructor Details

#initialize(positions, position: ) ⇒ Limited

Returns a new instance of Limited.

Parameters:

  • positions (Array<Integer>)

    allowed positions. Must not be empty.

  • position (Integer) (defaults to: )

    initial position.



372
373
374
375
376
# File 'lib/tuile/component/list.rb', line 372

def initialize(positions, position: positions[0])
  @positions = positions.sort
  position = @positions[@positions.rindex { it < position } || 0] unless @positions.include?(position)
  super(position: position)
end

Instance Method Details

#candidate_positions(line_count) ⇒ Array<Integer>

Parameters:

  • line_count (Integer)

Returns:

  • (Array<Integer>)


395
396
397
# File 'lib/tuile/component/list.rb', line 395

def candidate_positions(line_count)
  @positions.select { it < line_count }
end

#handle_mouse(line, event, _line_count) ⇒ Boolean

Parameters:

  • line (Integer)
  • event (MouseEvent)
  • _line_count (Integer)

Returns:

  • (Boolean)


382
383
384
385
386
387
388
389
390
391
# File 'lib/tuile/component/list.rb', line 382

def handle_mouse(line, event, _line_count)
  if event.button == :left
    prev_pos = @positions.reverse_each.find { it <= line }
    return go_to_first if prev_pos.nil?

    go(prev_pos)
  else
    false
  end
end