|
4 | 4 | import os |
5 | 5 | import sys |
6 | 6 | from collections.abc import Iterable, MutableMapping |
| 7 | +from types import MappingProxyType |
7 | 8 | from typing import ( |
8 | 9 | TYPE_CHECKING, |
9 | 10 | ClassVar, |
@@ -126,6 +127,7 @@ def __init__( |
126 | 127 |
|
127 | 128 | self.injection_store.on_unannotated_required_args = "ignore" |
128 | 129 |
|
| 130 | + self._registered_actions: dict[str, Action] = {} |
129 | 131 | self._disposers: list[tuple[str, DisposeCallable]] = [] |
130 | 132 |
|
131 | 133 | @property |
@@ -291,3 +293,42 @@ def _dispose() -> None: |
291 | 293 | d.pop()() |
292 | 294 |
|
293 | 295 | return _dispose |
| 296 | + |
| 297 | + @property |
| 298 | + def registered_actions(self) -> MappingProxyType[str, Action]: |
| 299 | + """Return a Mapping of id->Action object for all registered actions. |
| 300 | +
|
| 301 | + Note that this only includes actions that were registered using |
| 302 | + `register_action`. Commands registered directly via |
| 303 | + `Application.commands.register_action` will not be included in this mapping. |
| 304 | + """ |
| 305 | + return MappingProxyType(self._registered_actions) |
| 306 | + |
| 307 | + def _register_action_obj(self, action: Action) -> DisposeCallable: |
| 308 | + """Register an Action object. Return a function that unregisters the action. |
| 309 | +
|
| 310 | + Helper for `register_action()`. |
| 311 | + """ |
| 312 | + # register commands |
| 313 | + disposers = [self.commands.register_action(action)] |
| 314 | + # register menus |
| 315 | + if dm := self.menus.append_action_menus(action): |
| 316 | + disposers.append(dm) |
| 317 | + # register keybindings |
| 318 | + if dk := self.keybindings.register_action_keybindings(action): |
| 319 | + disposers.append(dk) |
| 320 | + |
| 321 | + # remember the action object as a whole. |
| 322 | + # note that commands.register_action will have raised an exception |
| 323 | + # if the action.id is already registered, so we can assume that |
| 324 | + # the keys are unique. |
| 325 | + self._registered_actions[action.id] = action |
| 326 | + |
| 327 | + # create a function that will dispose of all the disposers |
| 328 | + def _dispose() -> None: |
| 329 | + self._registered_actions.pop(action.id, None) |
| 330 | + for d in disposers: |
| 331 | + d() |
| 332 | + |
| 333 | + self._disposers.append((action.id, _dispose)) |
| 334 | + return _dispose |
0 commit comments