forked from kovidgoyal/kitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.py
275 lines (249 loc) · 7.56 KB
/
mouse.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from functools import partial
from kitty.fast_data_types import (
GLFW_MOD_ALT, GLFW_MOD_CONTROL, GLFW_MOUSE_BUTTON_LEFT,
GLFW_MOUSE_BUTTON_RIGHT, create_mock_window, mock_mouse_selection,
send_mock_mouse_event_to_window
)
from . import BaseTest
def send_mouse_event(
window,
button=-1,
modifiers=0,
is_release=False,
x=0.0,
y=0,
clear_click_queue=False,
):
ix = int(x)
in_left_half_of_cell = x - ix < 0.5
send_mock_mouse_event_to_window(
window, button, modifiers, is_release, ix, y, clear_click_queue, in_left_half_of_cell
)
class TestMouse(BaseTest):
def test_mouse_selection(self):
s = self.create_screen(
options=dict(
rectangle_select_modifiers=GLFW_MOD_ALT | GLFW_MOD_CONTROL
)
)
w = create_mock_window(s)
ev = partial(send_mouse_event, w)
def mouse_selection(code: int) -> None:
mock_mouse_selection(w, s.callbacks.current_mouse_button, code)
s.callbacks.mouse_selection = mouse_selection
def sel():
return ''.join(s.text_for_selection())
def init():
s.reset()
s.draw('pqrst')
s.draw('uvwxy')
s.draw('ABCDE')
s.draw('FGHIJ')
s.draw('KLMNO')
s.draw('12345')
s.draw('67890')
s.draw('abcde')
s.draw('fghij')
s.draw('klmno')
def press(x=0, y=0, modifiers=0, button=GLFW_MOUSE_BUTTON_LEFT):
ev(button, x=x, y=y, modifiers=modifiers)
def release(x=0, y=0, button=GLFW_MOUSE_BUTTON_LEFT):
ev(
button,
x=x,
y=y,
is_release=True,
clear_click_queue=True
)
def move(x=0, y=0, button=-1, q=None):
ev(x=x, y=y, button=button)
if q is not None:
s = sel()
self.ae(s, q, f'{s!r} != {q!r} after movement to x={x} y={y}')
def multi_click(x=0, y=0, count=2):
clear_click_queue = True
while count > 0:
count -= 1
ev(GLFW_MOUSE_BUTTON_LEFT, x=x, y=y, clear_click_queue=clear_click_queue)
clear_click_queue = False
def scroll(x=0, y=0, up=True):
move(x=x, y=y, button=-2 if up else -3)
# Single line click, move, release test
init()
press()
move(x=3.6, q='1234')
release(x=3.6)
self.ae(sel(), '1234')
press(x=4), release(x=0.6)
self.ae(sel(), '234')
# multi line movement
init()
press(x=2, y=2)
move(x=2, y=1, q='890ab')
move(x=2.6, y=1, q='90ab')
move(y=1, q='67890ab')
move(x=4, y=1, q='0ab')
move(x=4.6, y=1, q='ab')
move(q='1234567890ab')
move(x=2, y=3, q='cdefg')
move(y=3, q='cde')
move(x=0.6, y=3, q='cdef')
move(x=2.6, y=3, q='cdefgh')
move(x=4.6, y=3, q='cdefghij')
# Single cell select
init()
press(), release(1)
self.ae(sel(), '1')
press(3), release(2)
self.ae(sel(), '3')
# Multi-line click release
init()
press(1, 1), release(3.6, 2)
self.ae(sel(), '7890abcd')
press(1.6, 1), release(3, 2)
self.ae(sel(), '890abc')
press(3.6, 4), release(2, 2)
self.ae(sel(), 'cdefghijklmn')
press(3, 4), release(2.6, 2)
self.ae(sel(), 'defghijklm')
# Word select with drag
s.reset()
s.draw('ab cd')
s.draw(' f gh')
s.draw(' stuv')
s.draw('X Y')
multi_click(x=1.4)
self.ae(sel(), 'ab')
move(2.6)
self.ae(sel(), 'ab ')
move(3.6)
self.ae(sel(), 'ab cd')
move(2.6)
self.ae(sel(), 'ab ')
release(3.6, 1)
self.ae(sel(), 'ab cd f gh')
multi_click(x=1, y=2)
self.ae(sel(), 'stuvX')
release()
multi_click(x=3.6)
self.ae(sel(), 'cd')
move(0.2)
release()
self.ae(sel(), 'ab cd')
multi_click(x=4.4)
self.ae(sel(), 'cd')
move(x=4.4, y=1)
self.ae(sel(), 'cd f gh')
move(x=4.4, y=0)
self.ae(sel(), 'cd')
release()
multi_click(x=4.4, y=1)
self.ae(sel(), 'gh')
move(x=4.4, y=0)
self.ae(sel(), 'cd f gh')
move(x=4.4, y=1)
self.ae(sel(), 'gh')
release()
multi_click(x=4.4)
self.ae(sel(), 'cd')
move()
self.ae(sel(), 'ab cd')
move(x=1, y=1)
self.ae(sel(), 'ab cd f')
move()
self.ae(sel(), 'ab cd')
release()
multi_click(x=1.4)
self.ae(sel(), 'ab')
move(x=4.4)
self.ae(sel(), 'ab cd')
move(x=4.4, y=1)
self.ae(sel(), 'ab cd f gh')
move(x=4.4)
self.ae(sel(), 'ab cd')
# Line select with drag
s.reset()
s.draw('1 2 3')
s.linefeed(), s.carriage_return()
s.draw('4 5 6')
s.linefeed(), s.carriage_return()
s.draw('7 8 9X')
multi_click(x=1, count=3)
self.ae(sel(), str(s.line(0)))
move(y=1)
self.ae(sel(), '1 2 3\n4 5 6')
move(y=2)
self.ae(sel(), '1 2 3\n4 5 6\n7 8 9X')
move(y=1)
self.ae(sel(), '1 2 3\n4 5 6')
move()
self.ae(sel(), str(s.line(0)))
release()
multi_click(y=1, count=3)
self.ae(sel(), '4 5 6')
move(y=0)
self.ae(sel(), '1 2 3\n4 5 6')
move(y=1)
self.ae(sel(), '4 5 6')
move(y=2)
self.ae(sel(), '4 5 6\n7 8 9X')
release()
s.reset()
s.draw(' 123')
s.linefeed(), s.carriage_return()
s.draw(' 456')
s.linefeed(), s.carriage_return()
multi_click(x=1, count=3)
self.ae(sel(), '123')
move(x=2, y=1)
self.ae(sel(), '123\n 456')
release()
press(x=2, y=1, button=GLFW_MOUSE_BUTTON_RIGHT)
release(x=2, y=1, button=GLFW_MOUSE_BUTTON_RIGHT)
self.ae(sel(), '123\n 456')
press(button=GLFW_MOUSE_BUTTON_RIGHT)
self.ae(sel(), ' 123\n 456')
release(button=GLFW_MOUSE_BUTTON_RIGHT)
# Rectangle select
init()
press(x=1, y=1, modifiers=GLFW_MOD_ALT | GLFW_MOD_CONTROL)
move(x=3.6, y=3)
self.ae(sel(), '789bcdghi')
release(x=3, y=3)
self.ae(sel(), '78bcgh')
press(x=3.6, y=1, modifiers=GLFW_MOD_ALT | GLFW_MOD_CONTROL)
self.ae(sel(), '')
move(x=1, y=3)
self.ae(sel(), '789bcdghi')
release(x=1.6)
self.ae(sel(), '3489')
# scrolling
init()
press(x=1.6)
scroll(x=1)
self.ae(sel(), 'LMNO12')
scroll(x=1)
self.ae(sel(), 'GHIJKLMNO12')
scroll(x=1, up=False)
self.ae(sel(), 'LMNO12')
scroll(x=2.6, up=False)
self.ae(sel(), '3')
release()
# extending selections
init()
press()
move(x=3.6, q='1234')
release(x=3.6)
self.ae(sel(), '1234')
press(x=1, y=1, button=GLFW_MOUSE_BUTTON_RIGHT)
self.ae(sel(), '123456')
move(x=2, y=1, q='1234567')
release(x=3, y=1, button=GLFW_MOUSE_BUTTON_RIGHT)
self.ae(sel(), '12345678')
init()
press(y=2)
move(x=3.6, y=2, q='abcd')
press(x=3, y=0, button=GLFW_MOUSE_BUTTON_RIGHT)
self.ae(sel(), '4567890abcd')