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


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

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


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

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)


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

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)


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

def go_to_first
  go(@positions.first)
end

#go_to_last(_line_count) ⇒ Boolean

@param _line_count

Parameters:

  • _line_count (Integer)

Returns:

  • (Boolean)


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

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

#go_up_by(lines) ⇒ Boolean

@param lines

Parameters:

  • lines (Integer)

Returns:

  • (Boolean)


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

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)


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

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