mouse_events.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """
  2. Mouse events.
  3. How it works
  4. ------------
  5. The renderer has a 2 dimensional grid of mouse event handlers.
  6. (`prompt_toolkit.layout.MouseHandlers`.) When the layout is rendered, the
  7. `Window` class will make sure that this grid will also be filled with
  8. callbacks. For vt100 terminals, mouse events are received through stdin, just
  9. like any other key press. There is a handler among the key bindings that
  10. catches these events and forwards them to such a mouse event handler. It passes
  11. through the `Window` class where the coordinates are translated from absolute
  12. coordinates to coordinates relative to the user control, and there
  13. `UIControl.mouse_handler` is called.
  14. """
  15. from __future__ import annotations
  16. from enum import Enum
  17. from .data_structures import Point
  18. __all__ = ["MouseEventType", "MouseButton", "MouseModifier", "MouseEvent"]
  19. class MouseEventType(Enum):
  20. # Mouse up: This same event type is fired for all three events: left mouse
  21. # up, right mouse up, or middle mouse up
  22. MOUSE_UP = "MOUSE_UP"
  23. # Mouse down: This implicitly refers to the left mouse down (this event is
  24. # not fired upon pressing the middle or right mouse buttons).
  25. MOUSE_DOWN = "MOUSE_DOWN"
  26. SCROLL_UP = "SCROLL_UP"
  27. SCROLL_DOWN = "SCROLL_DOWN"
  28. # Triggered when the left mouse button is held down, and the mouse moves
  29. MOUSE_MOVE = "MOUSE_MOVE"
  30. class MouseButton(Enum):
  31. LEFT = "LEFT"
  32. MIDDLE = "MIDDLE"
  33. RIGHT = "RIGHT"
  34. # When we're scrolling, or just moving the mouse and not pressing a button.
  35. NONE = "NONE"
  36. # This is for when we don't know which mouse button was pressed, but we do
  37. # know that one has been pressed during this mouse event (as opposed to
  38. # scrolling, for example)
  39. UNKNOWN = "UNKNOWN"
  40. class MouseModifier(Enum):
  41. SHIFT = "SHIFT"
  42. ALT = "ALT"
  43. CONTROL = "CONTROL"
  44. class MouseEvent:
  45. """
  46. Mouse event, sent to `UIControl.mouse_handler`.
  47. :param position: `Point` instance.
  48. :param event_type: `MouseEventType`.
  49. """
  50. def __init__(
  51. self,
  52. position: Point,
  53. event_type: MouseEventType,
  54. button: MouseButton,
  55. modifiers: frozenset[MouseModifier],
  56. ) -> None:
  57. self.position = position
  58. self.event_type = event_type
  59. self.button = button
  60. self.modifiers = modifiers
  61. def __repr__(self) -> str:
  62. return "MouseEvent({!r},{!r},{!r},{!r})".format(
  63. self.position,
  64. self.event_type,
  65. self.button,
  66. self.modifiers,
  67. )