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 line index of the current cursor position.
Instance Method Summary collapse
-
#candidate_positions(line_count) ⇒ ::Array[Integer]
@param
line_count— number of lines in the list. -
#go(new_position) ⇒ Boolean
Moves the cursor to the new position.
-
#go_down_by(lines, line_count) ⇒ Boolean
@param
lines. - #go_to_first ⇒ Boolean
-
#go_to_last(line_count) ⇒ Boolean
Moves the cursor to the last reachable position.
-
#go_up_by(lines) ⇒ Boolean
@param
lines. -
#handle_key(key, line_count, viewport_lines) ⇒ Boolean
@param
key— pressed keyboard key. -
#handle_mouse(line, event, line_count) ⇒ Boolean
@param
line— cursor is hovering over this line. -
#initialize(position: 0) ⇒ Cursor
constructor
@param
position— the initial cursor position.
Constructor Details
#initialize(position: 0) ⇒ Cursor
@param position — the initial cursor position.
292 293 294 295 296 |
# File 'lib/tuile/component/list.rb', line 292 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 line index of the current cursor position.
339 340 341 |
# File 'lib/tuile/component/list.rb', line 339 def position @position end |
Instance Method Details
#candidate_positions(line_count) ⇒ ::Array[Integer]
@param line_count — number of lines in the list.
@return — positions the cursor can land on, in ascending order.
344 345 346 |
# File 'lib/tuile/component/list.rb', line 344 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.
@param new_position — new 0-based cursor position.
@return — true if the position changed.
386 387 388 389 390 391 392 |
# File 'lib/tuile/component/list.rb', line 386 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(lines, line_count) ⇒ Boolean
@param lines
@param line_count
408 409 410 |
# File 'lib/tuile/component/list.rb', line 408 def go_down_by(lines, line_count) go((@position + lines).clamp(nil, line_count - 1)) end |
#go_to_first ⇒ Boolean
419 420 421 |
# File 'lib/tuile/component/list.rb', line 419 def go_to_first go(0) 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.
@param line_count — number of lines in the list.
@return — true if the position changed.
399 400 401 |
# File 'lib/tuile/component/list.rb', line 399 def go_to_last(line_count) go(line_count - 1) end |
#go_up_by(lines) ⇒ Boolean
@param lines
414 415 416 |
# File 'lib/tuile/component/list.rb', line 414 def go_up_by(lines) go(@position - lines) end |
#handle_key(key, line_count, viewport_lines) ⇒ Boolean
@param key — pressed keyboard key.
@param line_count — number of lines in the list.
@param viewport_lines — number of visible lines.
@return — true if the cursor moved.
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/tuile/component/list.rb', line 352 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
@param line — cursor is hovering over this line.
@param event — the event.
@param line_count — number of lines in the list.
@return — true if the event was handled.
375 376 377 378 379 380 381 |
# File 'lib/tuile/component/list.rb', line 375 def handle_mouse(line, event, line_count) if event. == :left go(line.clamp(nil, line_count - 1)) else false end end |