event_listener.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import uuid
  2. from dataclasses import dataclass, field
  3. from typing import Any, Callable, Dict, List
  4. from .helpers import KWONLY_SLOTS
  5. @dataclass(**KWONLY_SLOTS)
  6. class EventListener:
  7. id: str = field(init=False)
  8. element_id: int
  9. type: str
  10. args: List[str]
  11. handler: Callable
  12. throttle: float
  13. leading_events: bool
  14. trailing_events: bool
  15. def __post_init__(self) -> None:
  16. self.id = str(uuid.uuid4())
  17. def to_dict(self) -> Dict[str, Any]:
  18. words = self.type.split('.')
  19. type = words.pop(0)
  20. specials = [w for w in words if w in {'capture', 'once', 'passive'}]
  21. modifiers = [w for w in words if w in {'stop', 'prevent', 'self', 'ctrl', 'shift', 'alt', 'meta'}]
  22. keys = [w for w in words if w not in specials + modifiers]
  23. return {
  24. 'listener_id': self.id,
  25. 'type': type,
  26. 'specials': specials,
  27. 'modifiers': modifiers,
  28. 'keys': keys,
  29. 'args': self.args,
  30. 'throttle': self.throttle,
  31. 'leading_events': self.leading_events,
  32. 'trailing_events': self.trailing_events,
  33. }