@@ -36,6 +36,7 @@ def is_visual(obj: object) -> bool:
3636 return isinstance (obj , Visual ) or hasattr (obj , "textualize" )
3737
3838
39+ # Note: not runtime checkable currently, as I've found that to be slow
3940class SupportsTextualize (Protocol ):
4041 """An object that supports the textualize protocol."""
4142
@@ -52,6 +53,9 @@ class VisualError(Exception):
5253def visualize (widget : Widget , obj : object ) -> Visual :
5354 """Get a visual instance from an object.
5455
56+ If the object does not support the Visual protocol and is a Rich renderable, it
57+ will be wrapped in a [RichVisual][textual.visual.RichVisual].
58+
5559 Args:
5660 obj: An object.
5761
@@ -87,7 +91,7 @@ def visualize(widget: Widget, obj: object) -> Visual:
8791@rich .repr .auto
8892@dataclass (frozen = True )
8993class Style :
90- """Represent a content style (color and other attributes)."""
94+ """Represents a style in the Visual interface (color and other attributes)."""
9195
9296 background : Color = TRANSPARENT
9397 foreground : Color = TRANSPARENT
@@ -128,6 +132,14 @@ def __add__(self, other: object) -> Style:
128132
129133 @classmethod
130134 def from_rich_style (cls , rich_style : RichStyle ) -> Style :
135+ """Build a Style from a (Rich) Style.
136+
137+ Args:
138+ rich_style: A Rich Style object.
139+
140+ Returns:
141+ New Style.
142+ """
131143 return Style (
132144 Color .from_rich_color (rich_style .bgcolor ),
133145 Color .from_rich_color (rich_style .color ),
@@ -140,6 +152,12 @@ def from_rich_style(cls, rich_style: RichStyle) -> Style:
140152
141153 @classmethod
142154 def from_styles (cls , styles : StylesBase ) -> Style :
155+ """Create a Visual Style from a Textual styles object.
156+
157+ Args:
158+ styles: A Styles object, such as `my_widget.styles`.
159+
160+ """
143161 text_style = styles .text_style
144162 return Style (
145163 styles .background ,
@@ -158,6 +176,11 @@ def from_styles(cls, styles: StylesBase) -> Style:
158176
159177 @cached_property
160178 def rich_style (self ) -> RichStyle :
179+ """Convert this Styles in to a Rich style.
180+
181+ Returns:
182+ A Rich style object.
183+ """
161184 return RichStyle (
162185 color = (self .background + self .foreground ).rich_color ,
163186 bgcolor = self .background .rich_color ,
@@ -209,18 +232,19 @@ def render_strips(
209232 Args:
210233 base_style: The base style.
211234 width: Width of desired render.
212- height: Height of desired render.
235+ height: Height of desired render or `None` for any height.
236+ style: A Visual Style.
213237
214238 Returns:
215- An iterable of Strips.
239+ An list of Strips.
216240 """
217241
218242 @abstractmethod
219243 def get_optimal_width (self , container_width : int ) -> int :
220244 """Get ideal width of the renderable to display its content.
221245
222246 Args:
223- tab_size: Size of tabs .
247+ container_size: The size of the container .
224248
225249 Returns:
226250 A width in cells.
@@ -253,13 +277,13 @@ def to_strips(
253277 widget: Widget that produced the visual.
254278 visual: A Visual instance.
255279 width: Desired width (in cells).
256- height: Desired height (in lines).
280+ height: Desired height (in lines) or `None` for no limit .
257281 style: A (Visual) Style instance.
258- pad: Pad to desired height ?
282+ pad: Pad to desired width ?
259283 align: Tuple of horizontal and vertical alignment.
260284
261285 Returns:
262- _type_: _description_
286+ A list of Strips containing the render.
263287 """
264288 strips = visual .render_strips (widget , width , height , style )
265289 if height is None :
@@ -285,7 +309,15 @@ def to_strips(
285309
286310@rich .repr .auto
287311class RichVisual (Visual ):
312+ """A Visual to wrap a Rich renderable."""
313+
288314 def __init__ (self , widget : Widget , renderable : RenderableType ) -> None :
315+ """
316+
317+ Args:
318+ widget: The associated Widget.
319+ renderable: A Rich renderable.
320+ """
289321 self ._widget = widget
290322 self ._renderable = renderable
291323 self ._measurement : Measurement | None = None
@@ -295,13 +327,12 @@ def __rich_repr__(self) -> rich.repr.Result:
295327 yield self ._renderable
296328
297329 def _measure (self , console : Console , options : ConsoleOptions ) -> Measurement :
298- return Measurement .get (
299- console ,
300- options ,
301- self ._widget .post_render (self ._renderable , RichStyle .null ()),
302- )
303330 if self ._measurement is None :
304- self ._measurement = Measurement .get (console , options , self ._renderable )
331+ self ._measurement = Measurement .get (
332+ console ,
333+ options ,
334+ self ._widget .post_render (self ._renderable , RichStyle .null ()),
335+ )
305336 return self ._measurement
306337
307338 def get_optimal_width (self , container_width : int ) -> int :
@@ -363,7 +394,15 @@ def render_strips(
363394
364395@rich .repr .auto
365396class Padding (Visual ):
397+ """A Visual to pad another visual."""
398+
366399 def __init__ (self , visual : Visual , spacing : Spacing ):
400+ """
401+
402+ Args:
403+ Visual: A Visual.
404+ spacing: A Spacing object containing desired padding dimensions.
405+ """
367406 self ._visual = visual
368407 self ._spacing = spacing
369408
0 commit comments