Class: Tuile::Component::List::Cursor
- Inherits:
-
Object
- Object
- Tuile::Component::List::Cursor
- Defined in:
- lib/tuile/component/list.rb
Overview
Tracks cursor position within the list.
Defined Under Namespace
Instance Attribute Summary collapse
-
#position ⇒ Integer
readonly
0-based line index of the current cursor position.
Instance Method Summary collapse
-
#candidate_positions(line_count) ⇒ Array<Integer>
Positions the cursor can land on, in ascending order.
-
#go(new_position) ⇒ Boolean
Moves the cursor to the new position.
-
#go_to_last(line_count) ⇒ Boolean
Moves the cursor to the last reachable position.
-
#handle_key(key, line_count, viewport_lines) ⇒ Boolean
True if the cursor moved.
-
#handle_mouse(line, event, line_count) ⇒ Boolean
True if the event was handled.
-
#initialize(position: 0) ⇒ Cursor
constructor
A new instance of Cursor.
Constructor Details
#initialize(position: 0) ⇒ Cursor
Returns a new instance of Cursor.
294 295 296 297 298 |
# File 'lib/tuile/component/list.rb', line 294 def initialize(position: 0) raise "invalid position #{position}" unless position.is_a? Integer @position = position end |
Instance Attribute Details
#position ⇒ Integer (readonly)
Returns 0-based line index of the current cursor position.
341 342 343 |
# File 'lib/tuile/component/list.rb', line 341 def position @position end |
Instance Method Details
#candidate_positions(line_count) ⇒ Array<Integer>
Returns positions the cursor can land on, in ascending order.
346 347 348 |
# File 'lib/tuile/component/list.rb', line 346 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.
388 389 390 391 392 393 394 |
# File 'lib/tuile/component/list.rb', line 388 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.
401 402 403 |
# File 'lib/tuile/component/list.rb', line 401 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.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/tuile/component/list.rb', line 354 def handle_key(key, line_count, ) 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( / 2) when Keys::CTRL_D go_down_by( / 2, line_count) else false end end |
#handle_mouse(line, event, line_count) ⇒ Boolean
Returns true if the event was handled.
377 378 379 380 381 382 383 |
# File 'lib/tuile/component/list.rb', line 377 def handle_mouse(line, event, line_count) if event. == :left go(line.clamp(nil, line_count - 1)) else false end end |