Skip to content

Commit dc7c124

Browse files
authored
Merge pull request #4627 from Textualize/header-icon
expose icon
2 parents 2ddcb38 + 245ad80 commit dc7c124

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2020
### Added
2121

2222
- Added `Screen.is_active`
23+
- Added `icon` reactive to Header widget https://github.com/Textualize/textual/pull/4627
24+
- Added `time_format` reactive to Header widget https://github.com/Textualize/textual/pull/4627
2325
- Added `tooltip` parameter to input widgets https://github.com/Textualize/textual/pull/4625
2426

2527
## [0.65.2] - 2023-06-06

src/textual/widgets/_header.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class HeaderClock(HeaderClockSpace):
7777
}
7878
"""
7979

80+
time_format: Reactive[str] = Reactive("%X")
81+
8082
def _on_mount(self, _: Mount) -> None:
8183
self.set_interval(1, callback=self.refresh, name=f"update header clock")
8284

@@ -86,7 +88,7 @@ def render(self) -> RenderResult:
8688
Returns:
8789
The rendered clock.
8890
"""
89-
return Text(datetime.now().time().strftime("%X"))
91+
return Text(datetime.now().time().strftime(self.time_format))
9092

9193

9294
class HeaderTitle(Widget):
@@ -139,13 +141,21 @@ class Header(Widget):
139141
tall: Reactive[bool] = Reactive(False)
140142
"""Set to `True` for a taller header or `False` for a single line header."""
141143

144+
icon: Reactive[str] = Reactive("⭘")
145+
"""A character for the icon at the top left."""
146+
147+
time_format: Reactive[str] = Reactive("%X")
148+
"""Time format of the clock."""
149+
142150
def __init__(
143151
self,
144152
show_clock: bool = False,
145153
*,
146154
name: str | None = None,
147155
id: str | None = None,
148156
classes: str | None = None,
157+
icon: str | None = None,
158+
time_format: str | None = None,
149159
):
150160
"""Initialise the header widget.
151161
@@ -154,14 +164,24 @@ def __init__(
154164
name: The name of the header widget.
155165
id: The ID of the header widget in the DOM.
156166
classes: The CSS classes of the header widget.
167+
icon: Single character to use as an icon, or `None` for default.
168+
time_format: Time format (used by strftime) for clock, or `None` for default.
157169
"""
158170
super().__init__(name=name, id=id, classes=classes)
159171
self._show_clock = show_clock
172+
if icon is not None:
173+
self.icon = icon
174+
if time_format is not None:
175+
self.time_format = time_format
160176

161177
def compose(self):
162-
yield HeaderIcon()
178+
yield HeaderIcon().data_bind(Header.icon)
163179
yield HeaderTitle()
164-
yield HeaderClock() if self._show_clock else HeaderClockSpace()
180+
yield (
181+
HeaderClock().data_bind(Header.time_format)
182+
if self._show_clock
183+
else HeaderClockSpace()
184+
)
165185

166186
def watch_tall(self, tall: bool) -> None:
167187
self.set_class(tall, "-tall")

0 commit comments

Comments
 (0)