Class: Puppeteer::Mouse
- Inherits:
-
Object
- Object
- Puppeteer::Mouse
- Defined in:
- lib/puppeteer/mouse.rb
Overview
rbs_inline: enabled
Defined Under Namespace
Modules: Button, ButtonFlag
Class Attribute Summary collapse
-
.deprecated_click_count_warned ⇒ Object
Returns the value of attribute deprecated_click_count_warned.
Instance Method Summary collapse
- #click(x, y, delay: nil, button: nil, click_count: nil, count: nil) ⇒ Object
- #down(button: nil, click_count: nil) ⇒ Object
- #drag(start, target) ⇒ Object
- #drag_and_drop(start, target, delay: nil) ⇒ Object
- #drag_enter(target, data) ⇒ Object
- #drag_over(target, data) ⇒ Object
- #drop(target, data) ⇒ Object
-
#initialize(client, keyboard) ⇒ Mouse
constructor
A new instance of Mouse.
- #move(x, y, steps: nil) ⇒ Object
- #reset ⇒ Object
- #up(button: nil, click_count: nil) ⇒ Object
-
#wheel(delta_x: 0, delta_y: 0) ⇒ Object
Dispatches a ‘mousewheel` event.
Constructor Details
#initialize(client, keyboard) ⇒ Mouse
Returns a new instance of Mouse.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/puppeteer/mouse.rb', line 27 def initialize(client, keyboard) @client = client @keyboard = keyboard @base_state = { position: { x: 0, y: 0, }, buttons: ButtonFlag::NONE, } @transactions = [] @state_mutex = Mutex.new @dispatch_mutex = Mutex.new end |
Class Attribute Details
.deprecated_click_count_warned ⇒ Object
Returns the value of attribute deprecated_click_count_warned.
142 143 144 |
# File 'lib/puppeteer/mouse.rb', line 142 def deprecated_click_count_warned @deprecated_click_count_warned end |
Instance Method Details
#click(x, y, delay: nil, button: nil, click_count: nil, count: nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/puppeteer/mouse.rb', line 108 def click(x, y, delay: nil, button: nil, click_count: nil, count: nil) warn_deprecated_click_count if !click_count.nil? count ||= 1 click_count ||= count if count < 1 raise Puppeteer::Error.new('Click must occur a positive number of times.') end # Serialize click sequences to keep event ordering stable under thread-based concurrency. @dispatch_mutex.synchronize do move(x, y) if click_count == count 1.upto(count - 1) do |i| down(button: , click_count: i) up(button: , click_count: i) end end down(button: , click_count: click_count) if !delay.nil? Puppeteer::AsyncUtils.sleep_seconds(delay / 1000.0) end up(button: , click_count: click_count) end end |
#down(button: nil, click_count: nil) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/puppeteer/mouse.rb', line 150 def down(button: nil, click_count: nil) ||= Button::LEFT flag = () with_transaction do |update_state| update_state.call( buttons: state[:buttons] | flag, ) current_state = state position = current_state[:position] @client.('Input.dispatchMouseEvent', type: 'mousePressed', modifiers: @keyboard.modifiers, clickCount: click_count || 1, buttons: current_state[:buttons], button: , x: position[:x], y: position[:y], ) end end |
#drag(start, target) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/puppeteer/mouse.rb', line 222 def drag(start, target) promise = Async::Promise.new.tap do |future| @client.once('Input.dragIntercepted') do |event| future.resolve(event['data']) end end move(start.x, start.y) down move(target.x, target.y) promise.wait end |
#drag_and_drop(start, target, delay: nil) ⇒ Object
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/puppeteer/mouse.rb', line 277 def drag_and_drop(start, target, delay: nil) data = drag(start, target) drag_enter(target, data) drag_over(target, data) if delay Puppeteer::AsyncUtils.sleep_seconds(delay / 1000.0) end drop(target, data) up end |
#drag_enter(target, data) ⇒ Object
237 238 239 240 241 242 243 244 245 |
# File 'lib/puppeteer/mouse.rb', line 237 def drag_enter(target, data) @client.('Input.dispatchDragEvent', type: 'dragEnter', x: target.x, y: target.y, modifiers: @keyboard.modifiers, data: data, ) end |
#drag_over(target, data) ⇒ Object
250 251 252 253 254 255 256 257 258 |
# File 'lib/puppeteer/mouse.rb', line 250 def drag_over(target, data) @client.('Input.dispatchDragEvent', type: 'dragOver', x: target.x, y: target.y, modifiers: @keyboard.modifiers, data: data, ) end |
#drop(target, data) ⇒ Object
263 264 265 266 267 268 269 270 271 |
# File 'lib/puppeteer/mouse.rb', line 263 def drop(target, data) @client.('Input.dispatchDragEvent', type: 'drop', x: target.x, y: target.y, modifiers: @keyboard.modifiers, data: data, ) end |
#move(x, y, steps: nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/puppeteer/mouse.rb', line 65 def move(x, y, steps: nil) move_steps = (steps || 1).to_i from = state[:position] to = { x: x, y: y, } return if move_steps <= 0 1.upto(move_steps) do |i| with_transaction do |update_state| update_state.call( position: { x: from[:x] + (to[:x] - from[:x]) * i / move_steps.to_f, y: from[:y] + (to[:y] - from[:y]) * i / move_steps.to_f, }, ) current_state = state = current_state[:buttons] position = current_state[:position] @client.('Input.dispatchMouseEvent', type: 'mouseMoved', modifiers: @keyboard.modifiers, buttons: , button: (), x: position[:x], y: position[:y], ) end end end |
#reset ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/puppeteer/mouse.rb', line 44 def reset [ [ButtonFlag::RIGHT, Button::RIGHT], [ButtonFlag::MIDDLE, Button::MIDDLE], [ButtonFlag::LEFT, Button::LEFT], [ButtonFlag::FORWARD, Button::FORWARD], [ButtonFlag::BACK, Button::BACK], ].each do |flag, | up(button: ) if (state[:buttons] & flag) != 0 end if state[:position][:x] != 0 || state[:position][:y] != 0 move(0, 0) end end |
#up(button: nil, click_count: nil) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/puppeteer/mouse.rb', line 176 def up(button: nil, click_count: nil) ||= Button::LEFT flag = () with_transaction do |update_state| update_state.call( buttons: state[:buttons] & ~flag, ) current_state = state position = current_state[:position] @client.('Input.dispatchMouseEvent', type: 'mouseReleased', modifiers: @keyboard.modifiers, clickCount: click_count || 1, buttons: current_state[:buttons], button: , x: position[:x], y: position[:y], ) end end |
#wheel(delta_x: 0, delta_y: 0) ⇒ Object
Dispatches a ‘mousewheel` event.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/puppeteer/mouse.rb', line 204 def wheel(delta_x: 0, delta_y: 0) current_state = state position = current_state[:position] @client.('Input.dispatchMouseEvent', type: 'mouseWheel', x: position[:x], y: position[:y], deltaX: delta_x, deltaY: delta_y, modifiers: @keyboard.modifiers, pointerType: 'mouse', buttons: current_state[:buttons], ) end |