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

Inherits:
Object
  • Object
show all
Defined in:
lib/tuile/component/list.rb

Overview

Tracks cursor position within the list.

Direct Known Subclasses

Limited, None

Defined Under Namespace

Classes: Limited, None

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position: 0) ⇒ Cursor

Returns a new instance of Cursor.

Parameters:

  • position (Integer) (defaults to: 0)

    the initial cursor position.



298
299
300
# File 'lib/tuile/component/list.rb', line 298

def initialize(position: 0)
  @position = position
end

Instance Attribute Details

#positionInteger (readonly)

Returns 0-based line index of the current cursor position.

Returns:

  • (Integer)

    0-based line index of the current cursor position.



343
344
345
# File 'lib/tuile/component/list.rb', line 343

def position
  @position
end

Instance Method Details

#candidate_positions(line_count) ⇒ Array<Integer>

Returns positions the cursor can land on, in ascending order.

Parameters:

  • line_count (Integer)

    number of lines in the list.

Returns:

  • (Array<Integer>)

    positions the cursor can land on, in ascending order.



348
349
350
# File 'lib/tuile/component/list.rb', line 348

def candidate_positions(line_count)
  (0...line_count).to_a
end

#go(new_position) ⇒ Boolean

Moves the cursor to the new position. Public only because of testing.

Parameters:

  • new_position (Integer)

    new 0-based cursor position.

Returns:

  • (Boolean)

    true if the position changed.



390
391
392
393
394
395
396
# File 'lib/tuile/component/list.rb', line 390

def go(new_position)
  new_position = new_position.clamp(0, nil)
  return false if @position == new_position

  @position = new_position
  true
end

#go_to_last(line_count) ⇒ Boolean

Moves the cursor to the last reachable position. For base Tuile::Component::List::Cursor, the last line; Limited clamps to the last allowed position; None is a no-op.

Parameters:

  • line_count (Integer)

    number of lines in the list.

Returns:

  • (Boolean)

    true if the position changed.



403
404
405
# File 'lib/tuile/component/list.rb', line 403

def go_to_last(line_count)
  go(line_count - 1)
end

#handle_key(key, line_count, viewport_lines) ⇒ Boolean

Returns true if the cursor moved.

Parameters:

  • key (String)

    pressed keyboard key.

  • line_count (Integer)

    number of lines in the list.

  • viewport_lines (Integer)

    number of visible lines.

Returns:

  • (Boolean)

    true if the cursor moved.



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/tuile/component/list.rb', line 356

def handle_key(key, line_count, viewport_lines)
  case key
  when *Keys::DOWN_ARROWS
    go_down_by(1, line_count)
  when *Keys::UP_ARROWS
    go_up_by(1)
  when *Keys::HOMES
    go_to_first
  when *Keys::ENDS_
    go_to_last(line_count)
  when Keys::CTRL_U
    go_up_by(viewport_lines / 2)
  when Keys::CTRL_D
    go_down_by(viewport_lines / 2, line_count)
  else
    false
  end
end

#handle_mouse(line, event, line_count) ⇒ Boolean

Returns true if the event was handled.

Parameters:

  • line (Integer)

    cursor is hovering over this line.

  • event (MouseEvent)

    the event.

  • line_count (Integer)

    number of lines in the list.

Returns:

  • (Boolean)

    true if the event was handled.



379
380
381
382
383
384
385
# File 'lib/tuile/component/list.rb', line 379

def handle_mouse(line, event, line_count)
  if event.button == :left
    go(line.clamp(nil, line_count - 1))
  else
    false
  end
end