mouse.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
  3. from functools import partial
  4. from kitty.fast_data_types import (
  5. GLFW_MOD_ALT,
  6. GLFW_MOD_CONTROL,
  7. GLFW_MOUSE_BUTTON_LEFT,
  8. GLFW_MOUSE_BUTTON_RIGHT,
  9. create_mock_window,
  10. mock_mouse_selection,
  11. send_mock_mouse_event_to_window,
  12. )
  13. from . import BaseTest
  14. def send_mouse_event(
  15. window,
  16. button=-1,
  17. modifiers=0,
  18. is_release=False,
  19. x=0.0,
  20. y=0,
  21. clear_click_queue=False,
  22. ):
  23. ix = int(x)
  24. in_left_half_of_cell = x - ix < 0.5
  25. send_mock_mouse_event_to_window(
  26. window, button, modifiers, is_release, ix, y, clear_click_queue, in_left_half_of_cell
  27. )
  28. class TestMouse(BaseTest):
  29. def test_mouse_selection(self):
  30. s = self.create_screen(
  31. options=dict(
  32. rectangle_select_modifiers=GLFW_MOD_ALT | GLFW_MOD_CONTROL
  33. )
  34. )
  35. w = create_mock_window(s)
  36. ev = partial(send_mouse_event, w)
  37. def mouse_selection(code: int) -> None:
  38. mock_mouse_selection(w, s.callbacks.current_mouse_button, code)
  39. s.callbacks.mouse_selection = mouse_selection
  40. def sel():
  41. return ''.join(s.text_for_selection())
  42. def init():
  43. s.reset()
  44. s.draw('pqrst')
  45. s.draw('uvwxy')
  46. s.draw('ABCDE')
  47. s.draw('FGHIJ')
  48. s.draw('KLMNO')
  49. s.draw('12345')
  50. s.draw('67890')
  51. s.draw('abcde')
  52. s.draw('fghij')
  53. s.draw('klmno')
  54. def press(x=0, y=0, modifiers=0, button=GLFW_MOUSE_BUTTON_LEFT):
  55. ev(button, x=x, y=y, modifiers=modifiers)
  56. def release(x=0, y=0, button=GLFW_MOUSE_BUTTON_LEFT):
  57. ev(
  58. button,
  59. x=x,
  60. y=y,
  61. is_release=True,
  62. clear_click_queue=True
  63. )
  64. def move(x=0, y=0, button=-1, q=None):
  65. ev(x=x, y=y, button=button)
  66. if q is not None:
  67. s = sel()
  68. self.ae(s, q, f'{s!r} != {q!r} after movement to x={x} y={y}')
  69. def multi_click(x=0, y=0, count=2):
  70. clear_click_queue = True
  71. while count > 0:
  72. count -= 1
  73. ev(GLFW_MOUSE_BUTTON_LEFT, x=x, y=y, clear_click_queue=clear_click_queue)
  74. clear_click_queue = False
  75. def scroll(x=0, y=0, up=True):
  76. move(x=x, y=y, button=-2 if up else -3)
  77. # Single line click, move, release test
  78. init()
  79. press()
  80. move(x=3.6, q='1234')
  81. release(x=3.6)
  82. self.ae(sel(), '1234')
  83. press(x=4), release(x=0.6)
  84. self.ae(sel(), '234')
  85. # multi line movement
  86. init()
  87. press(x=2, y=2)
  88. move(x=2, y=1, q='890ab')
  89. move(x=2.6, y=1, q='90ab')
  90. move(y=1, q='67890ab')
  91. move(x=4, y=1, q='0ab')
  92. move(x=4.6, y=1, q='ab')
  93. move(q='1234567890ab')
  94. move(x=2, y=3, q='cdefg')
  95. move(y=3, q='cde')
  96. move(x=0.6, y=3, q='cdef')
  97. move(x=2.6, y=3, q='cdefgh')
  98. move(x=4.6, y=3, q='cdefghij')
  99. # Single cell select
  100. init()
  101. press(), release(1)
  102. self.ae(sel(), '1')
  103. press(3), release(2)
  104. self.ae(sel(), '3')
  105. # Multi-line click release
  106. init()
  107. press(1, 1), release(3.6, 2)
  108. self.ae(sel(), '7890abcd')
  109. press(1.6, 1), release(3, 2)
  110. self.ae(sel(), '890abc')
  111. press(3.6, 4), release(2, 2)
  112. self.ae(sel(), 'cdefghijklmn')
  113. press(3, 4), release(2.6, 2)
  114. self.ae(sel(), 'defghijklm')
  115. # Word select with drag
  116. s.reset()
  117. s.draw('ab cd')
  118. s.draw(' f gh')
  119. s.draw(' stuv')
  120. s.draw('X Y')
  121. multi_click(x=1.4)
  122. self.ae(sel(), 'ab')
  123. move(2.6)
  124. self.ae(sel(), 'ab ')
  125. move(3.6)
  126. self.ae(sel(), 'ab cd')
  127. move(2.6)
  128. self.ae(sel(), 'ab ')
  129. release(3.6, 1)
  130. self.ae(sel(), 'ab cd f gh')
  131. multi_click(x=1, y=2)
  132. self.ae(sel(), 'stuvX')
  133. release()
  134. multi_click(x=3.6)
  135. self.ae(sel(), 'cd')
  136. move(0.2)
  137. release()
  138. self.ae(sel(), 'ab cd')
  139. multi_click(x=4.4)
  140. self.ae(sel(), 'cd')
  141. move(x=4.4, y=1)
  142. self.ae(sel(), 'cd f gh')
  143. move(x=4.4, y=0)
  144. self.ae(sel(), 'cd')
  145. release()
  146. multi_click(x=4.4, y=1)
  147. self.ae(sel(), 'gh')
  148. move(x=4.4, y=0)
  149. self.ae(sel(), 'cd f gh')
  150. move(x=4.4, y=1)
  151. self.ae(sel(), 'gh')
  152. release()
  153. multi_click(x=4.4)
  154. self.ae(sel(), 'cd')
  155. move()
  156. self.ae(sel(), 'ab cd')
  157. move(x=1, y=1)
  158. self.ae(sel(), 'ab cd f')
  159. move()
  160. self.ae(sel(), 'ab cd')
  161. release()
  162. multi_click(x=1.4)
  163. self.ae(sel(), 'ab')
  164. move(x=4.4)
  165. self.ae(sel(), 'ab cd')
  166. move(x=4.4, y=1)
  167. self.ae(sel(), 'ab cd f gh')
  168. move(x=4.4)
  169. self.ae(sel(), 'ab cd')
  170. # Line select with drag
  171. s.reset()
  172. s.draw('1 2 3')
  173. s.linefeed(), s.carriage_return()
  174. s.draw('4 5 6')
  175. s.linefeed(), s.carriage_return()
  176. s.draw('7 8 9X')
  177. multi_click(x=1, count=3)
  178. self.ae(sel(), str(s.line(0)))
  179. move(y=1)
  180. self.ae(sel(), '1 2 3\n4 5 6')
  181. move(y=2)
  182. self.ae(sel(), '1 2 3\n4 5 6\n7 8 9X')
  183. move(y=1)
  184. self.ae(sel(), '1 2 3\n4 5 6')
  185. move()
  186. self.ae(sel(), str(s.line(0)))
  187. release()
  188. multi_click(y=1, count=3)
  189. self.ae(sel(), '4 5 6')
  190. move(y=0)
  191. self.ae(sel(), '1 2 3\n4 5 6')
  192. move(y=1)
  193. self.ae(sel(), '4 5 6')
  194. move(y=2)
  195. self.ae(sel(), '4 5 6\n7 8 9X')
  196. release()
  197. s.reset()
  198. s.draw(' 123')
  199. s.linefeed(), s.carriage_return()
  200. s.draw(' 456')
  201. s.linefeed(), s.carriage_return()
  202. multi_click(x=1, count=3)
  203. self.ae(sel(), '123')
  204. move(x=2, y=1)
  205. self.ae(sel(), '123\n 456')
  206. release()
  207. press(x=2, y=1, button=GLFW_MOUSE_BUTTON_RIGHT)
  208. release(x=2, y=1, button=GLFW_MOUSE_BUTTON_RIGHT)
  209. self.ae(sel(), '123\n 456')
  210. press(button=GLFW_MOUSE_BUTTON_RIGHT)
  211. self.ae(sel(), ' 123\n 456')
  212. release(button=GLFW_MOUSE_BUTTON_RIGHT)
  213. # Rectangle select
  214. init()
  215. press(x=1, y=1, modifiers=GLFW_MOD_ALT | GLFW_MOD_CONTROL)
  216. move(x=3.6, y=3)
  217. self.ae(sel(), '789bcdghi')
  218. release(x=3, y=3)
  219. self.ae(sel(), '78bcgh')
  220. press(x=3.6, y=1, modifiers=GLFW_MOD_ALT | GLFW_MOD_CONTROL)
  221. self.ae(sel(), '')
  222. move(x=1, y=3)
  223. self.ae(sel(), '789bcdghi')
  224. release(x=1.6)
  225. self.ae(sel(), '3489')
  226. # scrolling
  227. init()
  228. press(x=1.6)
  229. scroll(x=1)
  230. self.ae(sel(), 'LMNO12')
  231. scroll(x=1)
  232. self.ae(sel(), 'GHIJKLMNO12')
  233. scroll(x=1, up=False)
  234. self.ae(sel(), 'LMNO12')
  235. scroll(x=2.6, up=False)
  236. self.ae(sel(), '3')
  237. release()
  238. # extending selections
  239. init()
  240. press()
  241. move(x=3.6, q='1234')
  242. release(x=3.6)
  243. self.ae(sel(), '1234')
  244. press(x=1, y=1, button=GLFW_MOUSE_BUTTON_RIGHT)
  245. self.ae(sel(), '123456')
  246. move(x=2, y=1, q='1234567')
  247. release(x=3, y=1, button=GLFW_MOUSE_BUTTON_RIGHT)
  248. self.ae(sel(), '12345678')
  249. init()
  250. press(y=2)
  251. move(x=3.6, y=2, q='abcd')
  252. press(x=3, y=0, button=GLFW_MOUSE_BUTTON_RIGHT)
  253. self.ae(sel(), '4567890abcd')