File: semantic_mouse.py

package info (click to toggle)
pytermgui 7.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,888 kB
  • sloc: python: 12,931; makefile: 40; sh: 37
file content (76 lines) | stat: -rw-r--r-- 2,089 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from functools import reduce

import pytermgui as ptg


class MouseTile(ptg.Container):
    actions = [
        "left_click",
        "right_click",
        "left_drag",
        "right_drag",
        "scroll_up",
        "scroll_down",
        "release",
        "hover",
    ]

    def __init__(self, *, width: int = 4, height: int = 4, **attrs) -> None:
        super().__init__(overflow=ptg.Overflow.HIDE, box="EMPTY", **attrs)

        self.content = ptg.Label("No action")

        self.lazy_add(self.content)

        # self.static_width = width
        self.height = height

        for i, action in enumerate(self.actions):
            handler = lambda _, event, i=i: self._generic_handle_mouse(event, i + 1)

            setattr(self, f"on_{action}", handler.__get__(self))

    def _generic_handle_mouse(
        self,
        event: ptg.MouseEvent,
        color: int,
    ) -> bool:
        action = event.action.value

        if action == "release":
            self.styles.fill = ""
            self.content.value = "No action"
            return False

        self.styles.fill = f"@{color}"
        self.content.value = f"[inverse bold {color}] {action} @ {event.position}"

        return action.endswith("drag")


def _setup_placeholder(manager: ptg.WindowManager) -> None:
    manager.layout.add_slot()
    manager.layout.add_slot()

    box = ptg.boxes.Box(["---", "x", ""])

    manager += ptg.Window(
        *(tuple(MouseTile(height=1) for _ in range(5)) for _ in range(2)),
        *(tuple(MouseTile(height=3) for _ in range(5)) for _ in range(5)),
        *(tuple(MouseTile(height=10) for _ in range(5)) for _ in range(5)),
        overflow=ptg.Overflow.SCROLL,
        box=box,
    ).set_title(ptg.markup.parse("[75]Tiles"))

    manager += ptg.Window(
        MouseTile(height=ptg.terminal.height),
        overflow=ptg.Overflow.SCROLL,
        box=box,
    ).set_title(ptg.markup.parse("[75]Full-window"))


if __name__ == "__main__":
    with ptg.WindowManager() as manager:
        ptg.Splitter.set_char("separator", "")

        _setup_placeholder(manager)