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