Skip to content

Commit

Permalink
Merge pull request #4264 from davep/clean-render-return-type
Browse files Browse the repository at this point in the history
Clean render return type
  • Loading branch information
davep authored Mar 6, 2024
2 parents 826ffd4 + 3234789 commit f7d6dc8
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 34 deletions.
4 changes: 2 additions & 2 deletions docs/examples/how-to/render_compose.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from time import time

from textual.app import App, ComposeResult, RenderableType
from textual.app import App, ComposeResult, RenderResult
from textual.containers import Container
from textual.renderables.gradient import LinearGradient
from textual.widgets import Static
Expand Down Expand Up @@ -41,7 +41,7 @@ def on_mount(self) -> None:
def compose(self) -> ComposeResult:
yield Static("Making a splash with Textual!") # (2)!

def render(self) -> RenderableType:
def render(self) -> RenderResult:
return LinearGradient(time() * 90, STOPS) # (3)!


Expand Down
2 changes: 1 addition & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ async def _on_css_change(self) -> None:
for screen in self.screen_stack:
self.stylesheet.update(screen)

def render(self) -> RenderableType:
def render(self) -> RenderResult:
return Blank(self.styles.background)

ExpectType = TypeVar("ExpectType", bound=Widget)
Expand Down
11 changes: 7 additions & 4 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@
ConsoleRenderable,
JustifyMethod,
RenderableType,
RenderResult,
RichCast,
)
from rich.console import RenderResult as RichRenderResult
from rich.console import RichCast
from rich.measure import Measurement
from rich.segment import Segment
from rich.style import Style
from rich.text import Text
from typing_extensions import Self

if TYPE_CHECKING:
from .app import RenderResult

from . import constants, errors, events, messages
from ._animator import DEFAULT_EASING, Animatable, BoundAnimator, EasingFunction
from ._arrange import DockArrangeResult, arrange
Expand Down Expand Up @@ -152,7 +155,7 @@ def __init__(

def __rich_console__(
self, console: "Console", options: "ConsoleOptions"
) -> "RenderResult":
) -> "RichRenderResult":
style = console.get_style(self.style)
result_segments = console.render(self.renderable, options)

Expand Down Expand Up @@ -3385,7 +3388,7 @@ async def batch(self) -> AsyncGenerator[None, None]:
with self.app.batch_update():
yield

def render(self) -> RenderableType:
def render(self) -> RenderResult:
"""Get text or Rich renderable for this widget.
Implement this for custom widgets.
Expand Down
16 changes: 10 additions & 6 deletions src/textual/widgets/_button.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations

from functools import partial
from typing import cast
from typing import TYPE_CHECKING, cast

import rich.repr
from rich.console import ConsoleRenderable, RenderableType
from rich.text import Text, TextType
from typing_extensions import Literal, Self

from .. import events

if TYPE_CHECKING:
from ..app import RenderResult

from ..binding import Binding
from ..css._error_tools import friendly_list
from ..message import Message
Expand Down Expand Up @@ -61,16 +65,16 @@ class Button(Widget, can_focus=True):
tint: $background 30%;
}
&.-primary {
&.-primary {
background: $primary;
color: $text;
border-top: tall $primary-lighten-3;
border-bottom: tall $primary-darken-3;
border-bottom: tall $primary-darken-3;
&:hover {
background: $primary-darken-2;
color: $text;
border-top: tall $primary;
border-top: tall $primary;
}
&.-active {
Expand Down Expand Up @@ -220,7 +224,7 @@ def validate_label(self, label: TextType) -> Text:
return Text.from_markup(label)
return label

def render(self) -> RenderableType:
def render(self) -> RenderResult:
assert isinstance(self.label, Text)
label = self.label.copy()
label.stylize(self.rich_style)
Expand Down
7 changes: 4 additions & 3 deletions src/textual/widgets/_digits.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from typing import cast
from typing import TYPE_CHECKING, cast

from rich.align import Align, AlignMethod
from rich.console import RenderableType

if TYPE_CHECKING:
from ..app import RenderResult
from ..geometry import Size
from ..renderables.digits import Digits as DigitsRenderable
from ..widget import Widget
Expand Down Expand Up @@ -68,7 +69,7 @@ def update(self, value: str) -> None:
self._value = value
self.refresh(layout=layout_required)

def render(self) -> RenderableType:
def render(self) -> RenderResult:
"""Render digits."""
rich_style = self.rich_style
digits = DigitsRenderable(self._value, rich_style)
Expand Down
9 changes: 6 additions & 3 deletions src/textual/widgets/_footer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from __future__ import annotations

from collections import defaultdict
from typing import ClassVar, Optional
from typing import TYPE_CHECKING, ClassVar, Optional

import rich.repr
from rich.console import RenderableType
from rich.text import Text

from .. import events

if TYPE_CHECKING:
from ..app import RenderResult

from ..reactive import reactive
from ..widget import Widget

Expand Down Expand Up @@ -138,7 +141,7 @@ def notify_style_update(self) -> None:
def post_render(self, renderable):
return renderable

def render(self) -> RenderableType:
def render(self) -> RenderResult:
if self._key_text is None:
self._key_text = self._make_key_text()
return self._key_text
13 changes: 9 additions & 4 deletions src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import re
from dataclasses import dataclass
from typing import ClassVar, Iterable
from typing import TYPE_CHECKING, ClassVar, Iterable

from rich.cells import cell_len, get_character_cell_size
from rich.console import Console, ConsoleOptions, RenderableType, RenderResult
from rich.console import Console, ConsoleOptions
from rich.console import RenderResult as RichRenderResult
from rich.highlighter import Highlighter
from rich.segment import Segment
from rich.text import Text
from typing_extensions import Literal

from .. import events
from .._segment_tools import line_crop

if TYPE_CHECKING:
from ..app import RenderResult

from ..binding import Binding, BindingType
from ..css._error_tools import friendly_list
from ..events import Blur, Focus, Mount
Expand Down Expand Up @@ -47,7 +52,7 @@ def __init__(self, input: Input, cursor_visible: bool) -> None:

def __rich_console__(
self, console: "Console", options: "ConsoleOptions"
) -> "RenderResult":
) -> RichRenderResult:
input = self.input
result = input._value
width = input.content_size.width
Expand Down Expand Up @@ -460,7 +465,7 @@ def cursor_width(self) -> int:
return cell_len(self.placeholder)
return self._position_to_cell(len(self.value)) + 1

def render(self) -> RenderableType:
def render(self) -> RenderResult:
self.view_position = self.view_position
if not self.value:
placeholder = Text(self.placeholder, justify="left")
Expand Down
6 changes: 4 additions & 2 deletions src/textual/widgets/_loading_indicator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

from time import time
from typing import TYPE_CHECKING

from rich.console import RenderableType
from rich.style import Style
from rich.text import Text

if TYPE_CHECKING:
from ..app import RenderResult
from ..color import Gradient
from ..events import Mount
from ..widget import Widget
Expand Down Expand Up @@ -53,7 +55,7 @@ def _on_mount(self, _: Mount) -> None:
self._start_time = time()
self.auto_refresh = 1 / 16

def render(self) -> RenderableType:
def render(self) -> RenderResult:
if self.app.animation_level == "none":
return Text("Loading...")

Expand Down
7 changes: 5 additions & 2 deletions src/textual/widgets/_placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
from typing import TYPE_CHECKING, Iterator
from weakref import WeakKeyDictionary

from rich.console import RenderableType
from typing_extensions import Literal, Self

from .. import events

if TYPE_CHECKING:
from ..app import RenderResult

from ..css._error_tools import friendly_list
from ..reactive import Reactive, reactive
from ..widget import Widget
Expand Down Expand Up @@ -128,7 +131,7 @@ async def _on_compose(self, event: events.Compose) -> None:
)
self.styles.background = f"{next(colors)} 50%"

def render(self) -> RenderableType:
def render(self) -> RenderResult:
"""Render the placeholder.
Returns:
Expand Down
7 changes: 6 additions & 1 deletion src/textual/widgets/_static.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from rich.console import RenderableType
from rich.protocol import is_renderable
from rich.text import Text

if TYPE_CHECKING:
from ..app import RenderResult

from ..errors import RenderError
from ..widget import Widget

Expand Down Expand Up @@ -80,7 +85,7 @@ def renderable(self, renderable: RenderableType) -> None:
self._renderable = renderable
self.clear_cached_dimensions()

def render(self) -> RenderableType:
def render(self) -> RenderResult:
"""Get a rich renderable for the widget's content.
Returns:
Expand Down
6 changes: 3 additions & 3 deletions src/textual/widgets/_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from typing import TYPE_CHECKING, ClassVar

from rich.console import RenderableType

if TYPE_CHECKING:
from ..app import RenderResult
from ..binding import Binding, BindingType
from ..events import Click
from ..geometry import Size
Expand Down Expand Up @@ -143,7 +143,7 @@ def watch_value(self, value: bool) -> None:
def watch_slider_pos(self, slider_pos: float) -> None:
self.set_class(slider_pos == 1, "-on")

def render(self) -> RenderableType:
def render(self) -> RenderResult:
style = self.get_component_rich_style("switch--slider")
return ScrollBarRender(
virtual_size=100,
Expand Down
9 changes: 6 additions & 3 deletions src/textual/widgets/_toast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

from __future__ import annotations

from typing import ClassVar
from typing import TYPE_CHECKING, ClassVar

from rich.console import RenderableType
from rich.text import Text

from .. import on

if TYPE_CHECKING:
from ..app import RenderResult

from ..containers import Container
from ..css.query import NoMatches
from ..events import Click, Mount
Expand Down Expand Up @@ -105,7 +108,7 @@ def __init__(self, notification: Notification) -> None:
self._notification = notification
self._timeout = notification.time_left

def render(self) -> RenderableType:
def render(self) -> RenderResult:
"""Render the toast's content.
Returns:
Expand Down

0 comments on commit f7d6dc8

Please sign in to comment.