null visual

This commit is contained in:
Will McGugan
2025-09-28 17:18:43 +01:00
parent b4e0833a0f
commit f7de26ab4e
3 changed files with 31 additions and 3 deletions

View File

@@ -258,6 +258,31 @@ class Visual(ABC):
return strips
@rich.repr.auto
class NullVisual(Visual):
"""A visual with no size, which therefore renders nothing.
This is a placeholder for when a Widget has been reduced to zero area.
"""
def __rich_repr__(self) -> rich.repr.Result:
yield from ()
def render_strips(
self, width: int, height: int | None, style: Style, options: RenderOptions
) -> list[Strip]:
return []
def get_optimal_width(self, rules: RulesMap, container_width: int) -> int:
return 0
def get_minimal_width(self, rules: RulesMap) -> int:
return 0
def get_height(self, rules: RulesMap, width: int) -> int:
return 0
@rich.repr.auto
class RichVisual(Visual):
"""A Visual to wrap a Rich renderable."""

View File

@@ -90,7 +90,7 @@ from textual.rlock import RLock
from textual.selection import Selection
from textual.strip import Strip
from textual.style import Style as VisualStyle
from textual.visual import Visual, VisualType, visualize
from textual.visual import NullVisual, Visual, VisualType, visualize
if TYPE_CHECKING:
from textual.app import App, ComposeResult
@@ -4322,7 +4322,10 @@ class Widget(DOMNode):
if cached_visual is not None:
assert isinstance(cached_visual, Visual)
return cached_visual
visual = visualize(self, self.render(), markup=self._render_markup)
if self.size.width:
visual = visualize(self, self.render(), markup=self._render_markup)
else:
visual = NullVisual()
self._layout_cache[cache_key] = visual
return visual

View File

@@ -121,7 +121,7 @@ class Bar(Widget, can_focus=False):
total_imaginary_width = width + highlighted_bar_width
start: float
end: float
if self.app.animation_level == "none" or width == 0:
if self.app.animation_level == "none":
start = 0
end = width
else: