diff --git a/src/app_model/_registries.py b/src/app_model/_registries.py index 242c82f..13cc64c 100644 --- a/src/app_model/_registries.py +++ b/src/app_model/_registries.py @@ -1,22 +1,45 @@ from __future__ import annotations from concurrent.futures import Future, ThreadPoolExecutor -from typing import TYPE_CHECKING, Any, Callable, Optional +from functools import cached_property +from typing import TYPE_CHECKING, Any, Callable, NamedTuple, Optional from psygnal import Signal -from ._types import MenuItem, SubmenuItem, _RegisteredCommand, _RegisteredKeyBinding +from ._types import MenuItem, SubmenuItem if TYPE_CHECKING: from typing import Dict, Iterator, List, Sequence, Set, Tuple, Union - from ._types import CommandCallable, CommandIdStr, KeybindingRule + from . import context + from ._types import CommandCallable, CommandIdStr, KeybindingRule, KeyCodeStr DisposeCallable = Callable[[], None] CommandDecorator = Callable[[CommandCallable], CommandCallable] MenuOrSubmenu = Union[MenuItem, SubmenuItem] +class _RegisteredCommand: + """Small object to represent a command in the CommandsRegistry. + + Only used internally by the CommandsRegistry. + This helper class allows us to cache the dependency-injected variant of the + command. As usual with `cached_property`, the cache can be cleard by deleting + the attribute: `del cmd.run_injected` + """ + + def __init__(self, id: CommandIdStr, run: CommandCallable, title: str) -> None: + self.id = id + self.run = run + self.title = title + + @cached_property + def run_injected(self) -> Callable: + # from .._injection import inject_dependencies + # return inject_dependencies(self.run) + return self.run + + class CommandsRegistry: registered = Signal(str) @@ -136,6 +159,15 @@ def __str__(self) -> str: return "\n".join(lines) +class _RegisteredKeyBinding(NamedTuple): + """Internal object representing a fully registered keybinding.""" + + keybinding: KeyCodeStr # the keycode to bind to + command_id: CommandIdStr # the command to run + weight: int # the weight of the binding, for prioritization + when: Optional[context.Expr] = None # condition to enable keybinding + + class KeybindingsRegistry: registered = Signal() diff --git a/src/app_model/_types.py b/src/app_model/_types.py index e2ca777..97b293d 100644 --- a/src/app_model/_types.py +++ b/src/app_model/_types.py @@ -2,7 +2,6 @@ import os import sys -from functools import cached_property from typing import ( TYPE_CHECKING, Any, @@ -10,7 +9,6 @@ Generator, Generic, List, - NamedTuple, NewType, Optional, TypedDict, @@ -131,27 +129,6 @@ class CommandRule(BaseModel): ) -class _RegisteredCommand: - """Small object to represent a command in the CommandsRegistry. - - Only used internally by the CommandsRegistry. - This helper class allows us to cache the dependency-injected variant of the - command. As usual with `cached_property`, the cache can be cleard by deleting - the attribute: `del cmd.run_injected` - """ - - def __init__(self, id: CommandIdStr, run: CommandCallable, title: str) -> None: - self.id = id - self.run = run - self.title = title - - @cached_property - def run_injected(self) -> Callable: - # from .._injection import inject_dependencies - # return inject_dependencies(self.run) - return self.run - - # ------------------ keybinding-related types -------------------- @@ -194,15 +171,6 @@ def _bind_to_current_platform(self) -> Optional[KeyCodeStr]: return self.primary -class _RegisteredKeyBinding(NamedTuple): - """Internal object representing a fully registered keybinding.""" - - keybinding: KeyCodeStr # the keycode to bind to - command_id: CommandIdStr # the command to run - weight: int # the weight of the binding, for prioritization - when: Optional[context.Expr] = None # condition to enable keybinding - - # ------------------ menus-related types --------------------