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 lines.

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


429
430
431
432
433
434
435
# File 'lib/tuile/component/list.rb', line 429

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(line_count) ⇒ ::Array[Integer]

@param line_count

Parameters:

  • line_count (Integer)

Returns:

  • (::Array[Integer])


454
455
456
# File 'lib/tuile/component/list.rb', line 454

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

#go_down_by(lines, line_count) ⇒ Boolean

@param lines

@param line_count

Parameters:

  • lines (Integer)
  • line_count (Integer)

Returns:

  • (Boolean)


469
470
471
472
473
474
# File 'lib/tuile/component/list.rb', line 469

def go_down_by(lines, line_count)
  next_pos = @positions.find { _1 >= @position + lines }
  return go_to_last(line_count) if next_pos.nil?

  go(next_pos)
end

#go_to_firstBoolean

Returns:

  • (Boolean)


486
487
488
# File 'lib/tuile/component/list.rb', line 486

def go_to_first
  go(@positions.first)
end

#go_to_last(_line_count) ⇒ Boolean

@param _line_count

Parameters:

  • _line_count (Integer)

Returns:

  • (Boolean)


460
461
462
# File 'lib/tuile/component/list.rb', line 460

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

#go_up_by(lines) ⇒ Boolean

@param lines

Parameters:

  • lines (Integer)

Returns:

  • (Boolean)


478
479
480
481
482
483
# File 'lib/tuile/component/list.rb', line 478

def go_up_by(lines)
  prev_pos = @positions.reverse_each.find { _1 <= @position - lines }
  return go_to_first if prev_pos.nil?

  go(prev_pos)
end

#handle_mouse(line, event, _line_count) ⇒ Boolean

@param line

@param event

@param _line_count

Parameters:

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

Returns:

  • (Boolean)


441
442
443
444
445
446
447
448
449
450
# File 'lib/tuile/component/list.rb', line 441

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

    go(prev_pos)
  else
    false
  end
end