Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/textual/css/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from functools import cache, partial
from typing import ClassVar, Protocol, Self, overload

from metaclass_registry.descendants import Descendants

from textual.css._declaration import NativeProperty, ValueBuilder
from textual.css._style_properties import StyleProperty
from textual.css.model import Declaration
Expand Down Expand Up @@ -108,7 +110,7 @@ def of[Value](
)


class Component(Enum):
class Component(Descendants, Enum):
"""A widget's styled parts. Members are the parts; each part's CSS component
class name is derived here, once, from the declaring widget's prefix."""

Expand All @@ -126,14 +128,8 @@ def css_name(self) -> str:
@cache
def named(css_name: str) -> "Component | None":
"""The declared part a stylesheet component class names, if any."""
pending: list[type[Component]] = [Component]
while pending:
parts = pending.pop()
pending.extend(parts.__subclasses__())
for part in parts:
if part.css_name == css_name:
return part
return None
return next((part for parts in Component.descendants() for part in parts
if part.css_name == css_name), None)


class DeclaresTheme:
Expand Down
15 changes: 2 additions & 13 deletions src/textual/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from rich.tree import Tree

from formbridge.navigation_commands import NavigationCommand
from metaclass_registry.descendants import Descendants
from metaclass_registry.own_declarations import OwnDeclarations
from formbridge.key_declarations import TextEntryMode
from textual._context import active_message_pump
Expand Down Expand Up @@ -139,7 +140,7 @@ def __set__(self, obj: DOMNode, classes: str | Iterable[str]) -> None:


@rich.repr.auto
class DOMNode(DeclaresTheme, PaintNode, MessagePump, OwnDeclarations):
class DOMNode(DeclaresTheme, PaintNode, MessagePump, OwnDeclarations, Descendants):
"""The base class for object that can be in the Textual DOM (App and Widget)"""

_subtree_style_revision = 0
Expand Down Expand Up @@ -531,18 +532,6 @@ def _component_styles(self) -> dict[str, RenderStyles]:
_own_key_families: ClassVar[tuple[type[NavigationCommand], ...]] = ()
key_families: ClassVar[tuple[type[NavigationCommand], ...]] = ()

@classmethod
def contexts(cls) -> tuple[type[DOMNode], ...]:
"""Native and application declarations share the actual node hierarchy."""
pending = [cls]
found: dict[type[DOMNode], None] = {}
while pending:
owner = pending.pop()
if owner not in found:
found[owner] = None
pending.extend(reversed(owner.__subclasses__()))
return tuple(found)

@classmethod
def method_key_owners(cls) -> tuple[type, ...]:
"""A native method-key capability declares the ancestor scope it exposes."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_navigation_spellings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_navigation_spellings_follow_every_declared_context() -> None:
for name in widgets.__all__:
getattr(widgets, name)
checked = 0
for owner in DOMNode.contexts():
for owner in DOMNode.descendants():
standard = owner.binding_defaults(TextEntryMode)
modal = owner.binding_defaults(VimMode)
for direction in NavigationKey.__subclasses__():
Expand Down