Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/mouse_events.py: 82%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

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""" 

18from __future__ import annotations 

19 

20from enum import Enum 

21 

22from .data_structures import Point 

23 

24__all__ = ["MouseEventType", "MouseButton", "MouseModifier", "MouseEvent"] 

25 

26 

27class MouseEventType(Enum): 

28 # Mouse up: This same event type is fired for all three events: left mouse 

29 # up, right mouse up, or middle mouse up 

30 MOUSE_UP = "MOUSE_UP" 

31 

32 # Mouse down: This implicitly refers to the left mouse down (this event is 

33 # not fired upon pressing the middle or right mouse buttons). 

34 MOUSE_DOWN = "MOUSE_DOWN" 

35 

36 SCROLL_UP = "SCROLL_UP" 

37 SCROLL_DOWN = "SCROLL_DOWN" 

38 

39 # Triggered when the left mouse button is held down, and the mouse moves 

40 MOUSE_MOVE = "MOUSE_MOVE" 

41 

42 

43class MouseButton(Enum): 

44 LEFT = "LEFT" 

45 MIDDLE = "MIDDLE" 

46 RIGHT = "RIGHT" 

47 

48 # When we're scrolling, or just moving the mouse and not pressing a button. 

49 NONE = "NONE" 

50 

51 # This is for when we don't know which mouse button was pressed, but we do 

52 # know that one has been pressed during this mouse event (as opposed to 

53 # scrolling, for example) 

54 UNKNOWN = "UNKNOWN" 

55 

56 

57class MouseModifier(Enum): 

58 SHIFT = "SHIFT" 

59 ALT = "ALT" 

60 CONTROL = "CONTROL" 

61 

62 

63class MouseEvent: 

64 """ 

65 Mouse event, sent to `UIControl.mouse_handler`. 

66 

67 :param position: `Point` instance. 

68 :param event_type: `MouseEventType`. 

69 """ 

70 

71 def __init__( 

72 self, 

73 position: Point, 

74 event_type: MouseEventType, 

75 button: MouseButton, 

76 modifiers: frozenset[MouseModifier], 

77 ) -> None: 

78 self.position = position 

79 self.event_type = event_type 

80 self.button = button 

81 self.modifiers = modifiers 

82 

83 def __repr__(self) -> str: 

84 return "MouseEvent({!r},{!r},{!r},{!r})".format( 

85 self.position, 

86 self.event_type, 

87 self.button, 

88 self.modifiers, 

89 )