Files
textual/docs/examples/styles/border_sub_title_align_all.py
Rodrigo Girão Serrão 2a810f8c87 Implement border (sub)title. (#2064)
* Add Widget.border_title and border_subtitle.

Related issues: #1864

* Test setting border_(sub)title.

* Add border (sub)title references to StylesCache.

These internal references will make it easier for the instance of 'StylesCache' to know which border (sub)title to use, if/when needed.

* Add method to render border label.

* Add styles to align border (sub)title.

* Render border labels.

* Update styles template.

* Make new 'render_row' parameters optional.

* Add (sub)title border snapshot tests.

* Document border (sub)title and styles.

* Pass (sub)title directly as arguments.

Get rid of the watchers to make data flow easier to follow.
Related comment: https://github.com/Textualize/textual/pull/2064/files\#r1137746697

* Tweak example.

* Fix render_border_label.

This was wrong because border labels can be composed of multiple segments if they contain multiple styles. Additionally, we want to render a single blank space of padding around the title.

* Ensure we get no label when there's no space.

* Add tests for border label rendering.

* 'render_border_label' now returns iterable of segments.

* Add label to render_row.

* Fix calling signature in tests.

* Add padding to snapshot tests.

* Fix changelog.

* Update snapshot tests.

* Update snapshot tests.

* Border labels expand if there's no corners.

* Update CHANGELOG.md

* Fix docs.

* Remove irrelevant line.

* Fix snapshot tests.

* Don't share Console among tests.

* Simplify example in styles guide.

* Avoid expensive function call when possible.

* rewording

* positive branch first

* remove wasteful indirection

* fix changelog

---------

Co-authored-by: Will McGugan <willmcgugan@gmail.com>
2023-03-22 11:07:38 +00:00

75 lines
2.4 KiB
Python

from textual.app import App
from textual.containers import Container, Grid
from textual.widgets import Label
def make_label_container( # (11)!
text: str, id: str, border_title: str, border_subtitle: str
) -> Container:
lbl = Label(text, id=id)
lbl.border_title = border_title
lbl.border_subtitle = border_subtitle
return Container(lbl)
class BorderSubTitleAlignAll(App[None]):
def compose(self):
with Grid():
yield make_label_container( # (1)!
"This is the story of",
"lbl1",
"[b]Border [i]title[/i][/]",
"[u][r]Border[/r] subtitle[/]",
)
yield make_label_container( # (2)!
"a Python",
"lbl2",
"[b red]Left, but it's loooooooooooong",
"[reverse]Center, but it's loooooooooooong",
)
yield make_label_container( # (3)!
"developer that",
"lbl3",
"[b i on purple]Left[/]",
"[r u white on black]@@@[/]",
)
yield make_label_container(
"had to fill up",
"lbl4",
"", # (4)!
"[link=https://textual.textualize.io]Left[/]", # (5)!
)
yield make_label_container( # (6)!
"nine labels", "lbl5", "Title", "Subtitle"
)
yield make_label_container( # (7)!
"and ended up redoing it",
"lbl6",
"Title",
"Subtitle",
)
yield make_label_container( # (8)!
"because the first try",
"lbl7",
"Title, but really loooooooooong!",
"Subtitle, but really loooooooooong!",
)
yield make_label_container( # (9)!
"had some labels",
"lbl8",
"Title, but really loooooooooong!",
"Subtitle, but really loooooooooong!",
)
yield make_label_container( # (10)!
"that were too long.",
"lbl9",
"Title, but really loooooooooong!",
"Subtitle, but really loooooooooong!",
)
app = BorderSubTitleAlignAll(css_path="border_sub_title_align_all.css")
if __name__ == "__main__":
app.run()