Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

button render #3571

Merged
merged 7 commits into from
Oct 23, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed issue with `LRUCache.discard` https://github.com/Textualize/textual/issues/3537
- Fixed `DataTable` not scrolling to rows that were just added https://github.com/Textualize/textual/pull/3552
- Fixed cache bug with `DataTable.update_cell` https://github.com/Textualize/textual/pull/3551
- Fix issue with chunky highlights on buttons https://github.com/Textualize/textual/pull/3571

### Changed

- Buttons will now display multiple lines, and have auto height https://github.com/Textualize/textual/pull/3539

### Added

- Added HorizontalPad to pad.py https://github.com/Textualize/textual/pull/3571

## [0.40.0] - 2023-10-11

Expand Down
79 changes: 79 additions & 0 deletions src/textual/pad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from typing import cast

from rich.align import Align, AlignMethod
from rich.console import (
Console,
ConsoleOptions,
JustifyMethod,
RenderableType,
RenderResult,
)
from rich.measure import Measurement
from rich.segment import Segment, Segments
from rich.style import Style


class HorizontalPad:
"""Rich renderable to add padding on the left and right of a renderable.

Note that unlike Rich's Padding class this align each line independently.

"""

def __init__(
self,
renderable: RenderableType,
left: int,
right: int,
pad_style: Style,
justify: JustifyMethod,
) -> None:
"""
Initialize HorizontalPad.

Args:
renderable: A Rich renderable.
left: Left padding.
right: Right padding.
pad_style: Style of padding.
justify: Justify method.
"""
self.renderable = renderable
self.left = left
self.right = right
self.pad_style = pad_style
self.justify = justify

def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
options = options.update(
width=options.max_width - self.left - self.right, height=None
)
lines = console.render_lines(self.renderable, options, pad=False)
left_pad = Segment(" " * self.left, self.pad_style)
right_pad = Segment(" " * self.right, self.pad_style)

align: AlignMethod = cast(
AlignMethod,
self.justify if self.justify in {"left", "right", "center"} else "left",
)

for line in lines:
pad_line = line
if self.left:
pad_line = [left_pad, *line]
if self.right:
pad_line.append(right_pad)
segments = Segments(pad_line)
yield Align(segments, align=align)

def __rich_measure__(
self, console: "Console", options: "ConsoleOptions"
) -> Measurement:
measurement = Measurement.get(console, options, self.renderable)
total_padding = self.left + self.right
return Measurement(
measurement.minimum + total_padding,
measurement.maximum + total_padding,
)
18 changes: 12 additions & 6 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2817,16 +2817,22 @@ def get_pseudo_class_state(self) -> PseudoClasses:
)
return pseudo_classes

def _get_rich_justify(self) -> JustifyMethod | None:
"""Get the justify method that may be passed to a Rich renderable."""
text_justify: JustifyMethod | None = None
if self.styles.has_rule("text_align"):
text_align: JustifyMethod = cast(JustifyMethod, self.styles.text_align)
text_justify = _JUSTIFY_MAP.get(text_align, text_align)
return text_justify

def post_render(self, renderable: RenderableType) -> ConsoleRenderable:
"""Applies style attributes to the default renderable.

Returns:
A new renderable.
"""
text_justify: JustifyMethod | None = None
if self.styles.has_rule("text_align"):
text_align: JustifyMethod = cast(JustifyMethod, self.styles.text_align)
text_justify = _JUSTIFY_MAP.get(text_align, text_align)

text_justify = self._get_rich_justify()

if isinstance(renderable, str):
renderable = Text.from_markup(renderable, justify=text_justify)
Expand Down Expand Up @@ -2934,8 +2940,8 @@ def _render_content(self) -> None:
width, height = self.size
renderable = self.render()
renderable = self.post_render(renderable)
options = self._console.options.update_dimensions(width, height).update(
highlight=False
options = self._console.options.update(
highlight=False, width=width, height=height
)

segments = self._console.render(renderable, options)
Expand Down
22 changes: 18 additions & 4 deletions src/textual/widgets/_button.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from __future__ import annotations

from functools import partial
from typing import cast

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

from .. import events
from ..binding import Binding
from ..css._error_tools import friendly_list
from ..message import Message
from ..pad import HorizontalPad
from ..reactive import reactive
from ..widgets import Static

Expand Down Expand Up @@ -41,6 +42,7 @@ class Button(Static, can_focus=True):
border: none;
border-top: tall $panel-lighten-2;
border-bottom: tall $panel-darken-3;
text-align: center;
content-align: center middle;
text-style: bold;
}
Expand Down Expand Up @@ -224,14 +226,26 @@ def watch_variant(self, old_variant: str, variant: str):
self.remove_class(f"-{old_variant}")
self.add_class(f"-{variant}")

def validate_label(self, label: TextType) -> TextType:
def validate_label(self, label: TextType) -> Text:
"""Parse markup for self.label"""
if isinstance(label, str):
return Text.from_markup(label)
return label

def render(self) -> RenderableType:
return Padding(self.label, (0, 1), expand=False)
assert isinstance(self.label, Text)
label = self.label.copy()
label.stylize(self.rich_style)
return HorizontalPad(
label,
1,
1,
self.rich_style,
self._get_rich_justify() or "center",
)

def post_render(self, renderable: RenderableType) -> ConsoleRenderable:
return cast(ConsoleRenderable, renderable)

async def _on_click(self, event: events.Click) -> None:
event.stop()
Expand Down
Loading