Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `OptionList.set_options` https://github.com/Textualize/textual/pull/6048
- Added `TextArea.suggestion` https://github.com/Textualize/textual/pull/6048
- Added `TextArea.placeholder` https://github.com/Textualize/textual/pull/6048
- Added `Header.format_title` and `App.format_title` for easier customization of title in the Header https://github.com/Textualize/textual/pull/6051
- Added `Widget.get_line_filters` and `App.get_line_filters` https://github.com/Textualize/textual/pull/6057

### Changed

- Breaking change: The `renderable` property on the `Static` widget has been changed to `content`. https://github.com/Textualize/textual/pull/6041
- Breaking change: `HeaderTitle` widget is now a static, with no `text` and `sub_text` reactives https://github.com/Textualize/textual/pull/6051
- Breaking change: Renamed `Label` constructor argument `renderable` to `content` for consistency https://github.com/Textualize/textual/pull/6045
- Breaking change: Optimization to line API to avoid applying background styles to widget content. In practice this means that you can no longer rely on blank Segments automatically getting the background color.

Expand Down
22 changes: 22 additions & 0 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
from textual.binding import Binding, BindingsMap, BindingType, Keymap
from textual.command import CommandListItem, CommandPalette, Provider, SimpleProvider
from textual.compose import compose
from textual.content import Content
from textual.css.errors import StylesheetError
from textual.css.query import NoMatches
from textual.css.stylesheet import RulesMap, Stylesheet
Expand Down Expand Up @@ -971,6 +972,27 @@ def clipboard(self) -> str:
"""
return self._clipboard

def format_title(self, title: str, sub_title: str) -> Content:
"""Format the title for display.

Args:
title: The title.
sub_title: The sub title.

Returns:
Content instance with title and subtitle.
"""
title_content = Content(title)
sub_title_content = Content(sub_title)
if sub_title_content:
return Content.assemble(
title_content,
(" — ", "dim"),
sub_title_content.stylize("dim"),
)
else:
return title_content

@contextmanager
def batch_update(self) -> Generator[None, None, None]:
"""A context manager to suspend all repaints until the end of the batch."""
Expand Down
47 changes: 19 additions & 28 deletions src/textual/widgets/_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from rich.text import Text

from textual.app import RenderResult
from textual.content import Content
from textual.dom import NoScreen
from textual.events import Click, Mount
from textual.reactive import Reactive
from textual.widget import Widget
from textual.widgets import Static


class HeaderIcon(Widget):
Expand Down Expand Up @@ -98,34 +100,18 @@ def render(self) -> RenderResult:
return Text(datetime.now().time().strftime(self.time_format))


class HeaderTitle(Widget):
class HeaderTitle(Static):
"""Display the title / subtitle in the header."""

DEFAULT_CSS = """
HeaderTitle {
text-wrap: nowrap;
text-overflow: ellipsis;
content-align: center middle;
width: 100%;
}
"""

text: Reactive[str] = Reactive("")
"""The main title text."""

sub_text = Reactive("")
"""The sub-title text."""

def render(self) -> RenderResult:
"""Render the title and sub-title.

Returns:
The value to render.
"""
text = Text(self.text, no_wrap=True, overflow="ellipsis")
if self.sub_text:
text.append(" — ")
text.append(self.sub_text, "dim")
Comment thread
willmcgugan marked this conversation as resolved.
return text


class Header(Widget):
"""A header widget with icon and clock."""
Expand Down Expand Up @@ -196,6 +182,17 @@ def watch_tall(self, tall: bool) -> None:
def _on_click(self):
self.toggle_class("-tall")

def format_title(self) -> Content:
"""Format the title and subtitle.

Defers to [App.format_title][textual.app.App.format_title] by default.
Override this method if you want to customize how the title is displayed in the header.

Returns:
Content for title display.
"""
return self.app.format_title(self.screen_title, self.screen_sub_title)

@property
def screen_title(self) -> str:
"""The title that this header will display.
Expand All @@ -221,17 +218,11 @@ def screen_sub_title(self) -> str:
def _on_mount(self, _: Mount) -> None:
async def set_title() -> None:
try:
self.query_one(HeaderTitle).text = self.screen_title
except NoScreen:
pass

async def set_sub_title() -> None:
try:
self.query_one(HeaderTitle).sub_text = self.screen_sub_title
self.query_one(HeaderTitle).update(self.format_title())
except NoScreen:
pass

self.watch(self.app, "title", set_title)
self.watch(self.app, "sub_title", set_sub_title)
self.watch(self.app, "sub_title", set_title)
self.watch(self.screen, "title", set_title)
self.watch(self.screen, "sub_title", set_sub_title)
self.watch(self.screen, "sub_title", set_title)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/snapshot_tests/snapshot_apps/fr_with_min.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ScreenSplitApp(App[None]):
background: $panel;
}

Static {
#scroll1 Static, #scroll2 Static {
width: 1fr;
content-align: center middle;
background: $boost;
Expand Down
16 changes: 16 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -4610,3 +4610,19 @@ def compose(self) -> ComposeResult:
yield TextArea(placeholder="Your text here")

assert snap_compare(TextApp())


def test_header_format(snap_compare):
"""Test title and sub-title are formatted as expected.

You should see "Title - Sub-title" in the header. Where sub-title is dimmed.
"""

class HeaderApp(App):
TITLE = "Title"
SUB_TITLE = "Sub-title"

def compose(self) -> ComposeResult:
yield Header()

assert snap_compare(HeaderApp())
Loading
Loading