forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.go
179 lines (144 loc) · 3.63 KB
/
mouse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package rod
import (
"fmt"
"sync"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
)
// Mouse represents the mouse on a page, it's always related the main frame
type Mouse struct {
lock *sync.Mutex
page *Page
id string // mouse svg dom element id
x float64
y float64
// the buttons is currently beening pressed, reflects the press order
buttons []proto.InputMouseButton
}
// MoveE to the absolute position with specified steps
func (m *Mouse) MoveE(x, y float64, steps int) error {
m.lock.Lock()
defer m.lock.Unlock()
if steps < 1 {
steps = 1
}
stepX := (x - m.x) / float64(steps)
stepY := (y - m.y) / float64(steps)
button, buttons := input.EncodeMouseButton(m.buttons)
for i := 0; i < steps; i++ {
m.page.browser.trySlowmotion()
toX := m.x + stepX
toY := m.y + stepY
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseMoved,
X: toX,
Y: toY,
Button: button,
Buttons: buttons,
Modifiers: m.page.Keyboard.modifiers,
}.Call(m.page)
if err != nil {
return err
}
// to make sure set only when call is successful
m.x = toX
m.y = toY
if m.page.browser.trace {
if !m.updateMouseTracer() {
m.initMouseTracer()
m.updateMouseTracer()
}
}
}
return nil
}
// ScrollE the relative offset with specified steps
func (m *Mouse) ScrollE(offsetX, offsetY float64, steps int) error {
m.lock.Lock()
defer m.lock.Unlock()
if m.page.browser.trace {
defer m.page.Overlay(0, 0, 200, 0, fmt.Sprintf("scroll (%.2f, %.2f)", offsetX, offsetY))()
}
m.page.browser.trySlowmotion()
if steps < 1 {
steps = 1
}
button, buttons := input.EncodeMouseButton(m.buttons)
stepX := offsetX / float64(steps)
stepY := offsetY / float64(steps)
for i := 0; i < steps; i++ {
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseWheel,
X: m.x,
Y: m.y,
Button: button,
Buttons: buttons,
Modifiers: m.page.Keyboard.modifiers,
DeltaX: stepX,
DeltaY: stepY,
}.Call(m.page)
if err != nil {
return err
}
}
return nil
}
// DownE doc is similar to the method Down
func (m *Mouse) DownE(button proto.InputMouseButton, clicks int64) error {
m.lock.Lock()
defer m.lock.Unlock()
toButtons := append(m.buttons, button)
_, buttons := input.EncodeMouseButton(toButtons)
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMousePressed,
Button: button,
Buttons: buttons,
ClickCount: clicks,
Modifiers: m.page.Keyboard.modifiers,
X: m.x,
Y: m.y,
}.Call(m.page)
if err != nil {
return err
}
m.buttons = toButtons
return nil
}
// UpE doc is similar to the method Up
func (m *Mouse) UpE(button proto.InputMouseButton, clicks int64) error {
m.lock.Lock()
defer m.lock.Unlock()
toButtons := []proto.InputMouseButton{}
for _, btn := range m.buttons {
if btn == button {
continue
}
toButtons = append(toButtons, btn)
}
_, buttons := input.EncodeMouseButton(toButtons)
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseReleased,
Button: button,
Buttons: buttons,
ClickCount: clicks,
X: m.x,
Y: m.y,
}.Call(m.page)
if err != nil {
return err
}
m.buttons = toButtons
return nil
}
// ClickE doc is similar to the method Click
func (m *Mouse) ClickE(button proto.InputMouseButton) error {
if m.page.browser.trace {
defer m.page.Overlay(0, 0, 200, 0, "click "+string(button))()
}
m.page.browser.trySlowmotion()
err := m.DownE(button, 1)
if err != nil {
return err
}
return m.UpE(button, 1)
}