Class: Tuile::Component::List::Cursor
- Inherits:
-
Object
- Object
- Tuile::Component::List::Cursor
- Defined in:
- lib/tuile/component/list.rb,
sig/tuile.rbs
Overview
Tracks cursor position within the list.
Defined Under Namespace
Instance Attribute Summary collapse
-
#position ⇒ Integer
readonly
@return — 0-based item index of the current cursor position.
Instance Method Summary collapse
-
#candidate_positions(item_count) ⇒ ::Array[Integer]
@param
item_count— number of items in the list. -
#go(new_position) ⇒ Boolean
Moves the cursor to the new position.
-
#go_down_by(count, item_count) ⇒ Boolean
@param
count. - #go_to_first ⇒ Boolean
-
#go_to_last(item_count) ⇒ Boolean
Moves the cursor to the last reachable position.
-
#go_up_by(count) ⇒ Boolean
@param
count. -
#handle_key(key, item_count, viewport_rows) ⇒ Boolean
@param
key— pressed keyboard key. -
#handle_mouse(item_index, event, item_count) ⇒ Boolean
@param
item_index— the item the cursor is hovering over. -
#initialize(position: 0) ⇒ Cursor
constructor
@param
position— the initial cursor position.
Constructor Details
#initialize(position: 0) ⇒ Cursor
@param position — the initial cursor position.
343 344 345 346 347 |
# File 'lib/tuile/component/list.rb', line 343 def initialize(position: 0) raise "invalid position #{position}" unless position.is_a? Integer @position = position end |
Instance Attribute Details
#position ⇒ Integer (readonly)
@return — 0-based item index of the current cursor position.
390 391 392 |
# File 'lib/tuile/component/list.rb', line 390 def position @position end |
Instance Method Details
#candidate_positions(item_count) ⇒ ::Array[Integer]
@param item_count — number of items in the list.
@return — positions the cursor can land on, in ascending order.
395 396 397 |
# File 'lib/tuile/component/list.rb', line 395 def candidate_positions(item_count) (0...item_count).to_a end |
#go(new_position) ⇒ Boolean
Moves the cursor to the new position. Public only because of testing.
@param new_position — new 0-based cursor position.
@return — true if the position changed.
437 438 439 440 441 442 443 |
# File 'lib/tuile/component/list.rb', line 437 def go(new_position) new_position = new_position.clamp(0, nil) return false if @position == new_position @position = new_position true end |
#go_down_by(count, item_count) ⇒ Boolean
@param count
@param item_count
459 460 461 |
# File 'lib/tuile/component/list.rb', line 459 def go_down_by(count, item_count) go((@position + count).clamp(nil, item_count - 1)) end |
#go_to_first ⇒ Boolean
470 471 472 |
# File 'lib/tuile/component/list.rb', line 470 def go_to_first go(0) end |
#go_to_last(item_count) ⇒ Boolean
Moves the cursor to the last reachable position. For base Tuile::Component::List::Cursor, the last item; Limited clamps to the last allowed position; None is a no-op.
@param item_count — number of items in the list.
@return — true if the position changed.
450 451 452 |
# File 'lib/tuile/component/list.rb', line 450 def go_to_last(item_count) go(item_count - 1) end |
#go_up_by(count) ⇒ Boolean
@param count
465 466 467 |
# File 'lib/tuile/component/list.rb', line 465 def go_up_by(count) go(@position - count) end |
#handle_key(key, item_count, viewport_rows) ⇒ Boolean
@param key — pressed keyboard key.
@param item_count — number of items in the list.
@param viewport_rows — number of visible rows.
@return — true if the cursor moved.
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/tuile/component/list.rb', line 403 def handle_key(key, item_count, ) case key when *Keys::DOWN_ARROWS go_down_by(1, item_count) when *Keys::UP_ARROWS go_up_by(1) when *Keys::HOMES go_to_first when *Keys::ENDS_ go_to_last(item_count) when Keys::CTRL_U go_up_by( / 2) when Keys::CTRL_D go_down_by( / 2, item_count) else false end end |
#handle_mouse(item_index, event, item_count) ⇒ Boolean
@param item_index — the item the cursor is hovering over.
@param event — the event.
@param item_count — number of items in the list.
@return — true if the event was handled.
426 427 428 429 430 431 432 |
# File 'lib/tuile/component/list.rb', line 426 def handle_mouse(item_index, event, item_count) if event. == :left go(item_index.clamp(nil, item_count - 1)) else false end end |