Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/mouse_events.py: 83%
29 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:07 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:07 +0000
1"""
2Mouse events.
5How it works
6------------
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"""
18from __future__ import annotations
20from enum import Enum
21from typing import FrozenSet
23from .data_structures import Point
25__all__ = ["MouseEventType", "MouseButton", "MouseModifier", "MouseEvent"]
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"
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"
37 SCROLL_UP = "SCROLL_UP"
38 SCROLL_DOWN = "SCROLL_DOWN"
40 # Triggered when the left mouse button is held down, and the mouse moves
41 MOUSE_MOVE = "MOUSE_MOVE"
44class MouseButton(Enum):
45 LEFT = "LEFT"
46 MIDDLE = "MIDDLE"
47 RIGHT = "RIGHT"
49 # When we're scrolling, or just moving the mouse and not pressing a button.
50 NONE = "NONE"
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"
58class MouseModifier(Enum):
59 SHIFT = "SHIFT"
60 ALT = "ALT"
61 CONTROL = "CONTROL"
64class MouseEvent:
65 """
66 Mouse event, sent to `UIControl.mouse_handler`.
68 :param position: `Point` instance.
69 :param event_type: `MouseEventType`.
70 """
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
84 def __repr__(self) -> str:
85 return "MouseEvent({!r},{!r},{!r},{!r})".format(
86 self.position,
87 self.event_type,
88 self.button,
89 self.modifiers,
90 )