Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/layout/mouse_handlers.py: 56%
18 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1from __future__ import annotations
3from collections import defaultdict
4from typing import TYPE_CHECKING, Callable
6from prompt_toolkit.mouse_events import MouseEvent
8if TYPE_CHECKING:
9 from prompt_toolkit.key_binding.key_bindings import NotImplementedOrNone
11__all__ = [
12 "MouseHandler",
13 "MouseHandlers",
14]
17MouseHandler = Callable[[MouseEvent], "NotImplementedOrNone"]
20class MouseHandlers:
21 """
22 Two dimensional raster of callbacks for mouse events.
23 """
25 def __init__(self) -> None:
26 def dummy_callback(mouse_event: MouseEvent) -> NotImplementedOrNone:
27 """
28 :param mouse_event: `MouseEvent` instance.
29 """
30 return NotImplemented
32 # NOTE: Previously, the data structure was a dictionary mapping (x,y)
33 # to the handlers. This however would be more inefficient when copying
34 # over the mouse handlers of the visible region in the scrollable pane.
36 # Map y (row) to x (column) to handlers.
37 self.mouse_handlers: defaultdict[
38 int, defaultdict[int, MouseHandler]
39 ] = defaultdict(lambda: defaultdict(lambda: dummy_callback))
41 def set_mouse_handler_for_range(
42 self,
43 x_min: int,
44 x_max: int,
45 y_min: int,
46 y_max: int,
47 handler: Callable[[MouseEvent], NotImplementedOrNone],
48 ) -> None:
49 """
50 Set mouse handler for a region.
51 """
52 for y in range(y_min, y_max):
53 row = self.mouse_handlers[y]
55 for x in range(x_min, x_max):
56 row[x] = handler