Coverage for src/lexigram/admin/actions/header_manager/shortcuts.py: 43%

23 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 14:56 +0800

1""" 

2Keyboard shortcuts management for header actions. 

3 

4Handles keyboard shortcut registration and execution. 

5""" 

6 

7from __future__ import annotations 

8 

9from typing import TYPE_CHECKING, Any 

10 

11from lexigram.admin.actions.header_manager.types import HeaderAction 

12from lexigram.admin.actions.shortcuts import KeyboardShortcutManager as _Base 

13 

14if TYPE_CHECKING: 

15 from collections.abc import Callable 

16 

17 

18class KeyboardShortcutManager(_Base[HeaderAction]): 

19 """Manages keyboard shortcuts for header actions.""" 

20 

21 def __init__(self) -> None: 

22 """Initialize the shortcut manager.""" 

23 super().__init__() 

24 self._handlers: dict[str, Callable[[], Any]] = {} 

25 

26 def register_action(self, action: HeaderAction) -> None: 

27 """Register an action with its keyboard shortcut.""" 

28 super().register_action(action) 

29 if action.keyboard_shortcut and action.handler: 

30 self._handlers[action.keyboard_shortcut] = action.handler 

31 

32 def unregister_action(self, shortcut: str) -> None: 

33 """Unregister an action by its shortcut.""" 

34 super().unregister_action(shortcut) 

35 self._handlers.pop(shortcut, None) 

36 

37 def execute_shortcut(self, shortcut: str) -> Any: 

38 """Execute the handler registered for *shortcut*.""" 

39 handler = self._handlers.get(shortcut) 

40 if handler: 

41 return handler() 

42 return None 

43 

44 def clear_all_shortcuts(self) -> None: 

45 """Clear all registered shortcuts and handlers.""" 

46 super().clear_all_shortcuts() 

47 self._handlers.clear()