Class: RubyTUI::MouseEvent

Inherits:
Event
  • Object
show all
Defined in:
lib/rubytui/event.rb

Overview

A mouse event: button, action, cell position and modifier flags.

Input::Parser produces these from SGR (mode 1006) and legacy (ESC [ M) mouse reports, with 0-based cell coordinates.

Mouse Buttons collapse

LEFT =

Left button.

:left
MIDDLE =

Middle button.

:middle
RIGHT =

Right button.

:right
SCROLL_UP =

Scroll wheel up (reported with action PRESS).

:scroll_up
SCROLL_DOWN =

Scroll wheel down (reported with action PRESS).

:scroll_down
NONE =

No specific button (legacy-protocol release, or motion with no button held).

:none

Mouse Actions collapse

PRESS =

Button pressed; also used for scroll-wheel events.

:press
RELEASE =

Button released.

:release
DRAG =

Pointer motion reported by the terminal (motion flag set).

:drag
MOVE =

Pointer motion with no button held. Currently never emitted by Input::Parser.

:move

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(button:, action:, x:, y:, modifiers: Input::Key::NONE) ⇒ MouseEvent

Returns a new instance of MouseEvent.

Parameters:



105
106
107
108
109
110
111
# File 'lib/rubytui/event.rb', line 105

def initialize(button:, action:, x:, y:, modifiers: Input::Key::NONE)
  @button = button
  @action = action
  @x = x
  @y = y
  @modifiers = modifiers
end

Instance Attribute Details

#action ⇒ Object (readonly)

Returns the value of attribute action.



96
97
98
# File 'lib/rubytui/event.rb', line 96

def action
  @action
end

#button ⇒ Object (readonly)

Returns the value of attribute button.



96
97
98
# File 'lib/rubytui/event.rb', line 96

def button
  @button
end

#modifiers ⇒ Object (readonly)

Returns the value of attribute modifiers.



96
97
98
# File 'lib/rubytui/event.rb', line 96

def modifiers
  @modifiers
end

#x ⇒ Object (readonly)

Returns the value of attribute x.



96
97
98
# File 'lib/rubytui/event.rb', line 96

def x
  @x
end

#y ⇒ Object (readonly)

Returns the value of attribute y.



96
97
98
# File 'lib/rubytui/event.rb', line 96

def y
  @y
end

Instance Method Details

#==(other) ⇒ Boolean

Value equality: same button, action, position and modifiers.

Parameters:

  • other (Object) —

    object to compare

Returns:



125
126
127
128
129
# File 'lib/rubytui/event.rb', line 125

def ==(other)
  other.is_a?(MouseEvent) && @button == other.button &&
    @action == other.action && @x == other.x && @y == other.y &&
    @modifiers == other.modifiers
end

#alt? ⇒ Boolean

Returns true if the Input::Key::ALT flag is set.

Returns:



116
117
# File 'lib/rubytui/event.rb', line 116

def alt?   = (@modifiers & Input::Key::ALT) != 0
# @return [Boolean] true if the {Input::Key::SHIFT} flag is set

#ctrl? ⇒ Boolean

Returns true if the Input::Key::CTRL flag is set.

Returns:



114
115
# File 'lib/rubytui/event.rb', line 114

def ctrl?  = (@modifiers & Input::Key::CTRL) != 0
# @return [Boolean] true if the {Input::Key::ALT} flag is set

#shift? ⇒ Boolean

Returns true if the Input::Key::SHIFT flag is set.

Returns:



118
# File 'lib/rubytui/event.rb', line 118

def shift? = (@modifiers & Input::Key::SHIFT) != 0

#to_s ⇒ String Also known as: inspect

Human-readable form: modifier names, then action(button), joined by +, followed by the position.

Returns:

  • (String) —

    e.g. MouseEvent(press(left) @10,5)



135
136
137
138
139
140
141
142
# File 'lib/rubytui/event.rb', line 135

def to_s
  parts = []
  parts << "Ctrl" if ctrl?
  parts << "Alt" if alt?
  parts << "Shift" if shift?
  parts << "#{@action}(#{@button})"
  "MouseEvent(#{parts.join("+")} @#{@x},#{@y})"
end