Coverage for src / lexigram / admin / actions / header_manager / shortcuts.py: 0%
23 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-11 02:25 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-11 02:25 +0800
1"""
2Keyboard shortcuts management for header actions.
4Handles keyboard shortcut registration and execution.
5"""
7from __future__ import annotations
9from typing import TYPE_CHECKING, Any
11from lexigram.admin.actions.header_manager.types import HeaderAction
12from lexigram.admin.actions.shortcuts import KeyboardShortcutManager as _Base
14if TYPE_CHECKING:
15 from collections.abc import Callable
18class KeyboardShortcutManager(_Base[HeaderAction]):
19 """Manages keyboard shortcuts for header actions."""
21 def __init__(self) -> None:
22 """Initialize the shortcut manager."""
23 super().__init__()
24 self._handlers: dict[str, Callable[[], Any]] = {}
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
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)
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
44 def clear_all_shortcuts(self) -> None:
45 """Clear all registered shortcuts and handlers."""
46 super().clear_all_shortcuts()
47 self._handlers.clear()