MCPcopy
hub / github.com/Textualize/textual / StylesCache

Class StylesCache

src/textual/_styles_cache.py:37–523  ·  view source on GitHub ↗

Responsible for rendering CSS Styles and keeping a cache of rendered lines. The render method applies border, outline, and padding set in the Styles object to widget content. The diagram below shows content (possibly from a Rich renderable) with padding and border. The labels A. B. and

Source from the content-addressed store, hash-verified

35
36@rich.repr.auto(angular=True)
37class StylesCache:
38 """Responsible for rendering CSS Styles and keeping a cache of rendered lines.
39
40 The render method applies border, outline, and padding set in the Styles object to widget content.
41
42 The diagram below shows content (possibly from a Rich renderable) with padding and border. The
43 labels A. B. and C. indicate the code path (see comments in render_line below) chosen to render
44 the indicated lines.
45
46 ```
47 ┏━━━━━━━━━━━━━━━━━━━━━━┓◀── A. border
48 ┃ ┃◀┐
49 ┃ ┃ └─ B. border + padding +
50 ┃ Lorem ipsum dolor ┃◀┐ border
51 ┃ sit amet, ┃ │
52 ┃ consectetur ┃ └─ C. border + padding +
53 ┃ adipiscing elit, ┃ content + padding +
54 ┃ sed do eiusmod ┃ border
55 ┃ tempor incididunt ┃
56 ┃ ┃
57 ┃ ┃
58 ┗━━━━━━━━━━━━━━━━━━━━━━┛
59 ```
60 """
61
62 def __init__(self) -> None:
63 self._cache: dict[int, Strip] = {}
64 self._dirty_lines: set[int] = set()
65 self._width = 1
66 self._simple_strip: Strip | None = None
67 """A simple strip consisting of left border + background + right border, which may be reused in a render."""
68
69 def __rich_repr__(self) -> rich.repr.Result:
70 if self._dirty_lines:
71 yield "dirty", self._dirty_lines
72 yield "width", self._width, 1
73
74 def set_dirty(self, *regions: Region) -> None:
75 """Add a dirty regions."""
76 if regions:
77 for region in regions:
78 self._dirty_lines.update(region.line_range)
79 else:
80 self.clear()
81
82 def is_dirty(self, y: int) -> bool:
83 """Check if a given line is dirty (needs to be rendered again).
84
85 Args:
86 y: Y coordinate of line.
87
88 Returns:
89 True if line requires a render, False if can be cached.
90 """
91 return y in self._dirty_lines
92
93 def clear(self) -> None:
94 """Clear the styles cache (will cause the content to re-render)."""

Callers 8

__init__Method · 0.90
test_set_dirtyFunction · 0.90
test_borderFunction · 0.90
test_paddingFunction · 0.90
test_padding_borderFunction · 0.90
test_outlineFunction · 0.90
test_cropFunction · 0.90
test_dirty_cacheFunction · 0.90

Calls

no outgoing calls

Tested by 7

test_set_dirtyFunction · 0.72
test_borderFunction · 0.72
test_paddingFunction · 0.72
test_padding_borderFunction · 0.72
test_outlineFunction · 0.72
test_cropFunction · 0.72
test_dirty_cacheFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…