Merge pull request #843 from Textualize/toggle-header-clock

Flag to enable header clock
This commit is contained in:
Will McGugan
2022-10-06 16:50:08 +01:00
committed by GitHub
2 changed files with 26 additions and 8 deletions

View File

@@ -2,7 +2,7 @@ from __future__ import annotations
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.widgets import Static, Footer
from textual.widgets import Static, Footer, Header
class JustABox(App):
@@ -12,6 +12,7 @@ class JustABox(App):
]
def compose(self) -> ComposeResult:
yield Header()
yield Static("Hello, world!", id="box1")
yield Footer()

View File

@@ -54,6 +54,7 @@ class HeaderTitle(Widget):
HeaderTitle {
content-align: center middle;
width: 100%;
margin-right: 10;
}
"""
@@ -69,7 +70,11 @@ class HeaderTitle(Widget):
class Header(Widget):
"""A header widget with icon and clock."""
"""A header widget with icon and clock.
Args:
show_clock (bool, optional): True if the clock should be shown on the right of the header.
"""
DEFAULT_CSS = """
Header {
@@ -88,10 +93,27 @@ class Header(Widget):
DEFAULT_CLASSES = "-tall"
def __init__(
self,
show_clock: bool = False,
*,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
):
super().__init__(name=name, id=id, classes=classes)
self.show_clock = show_clock
def compose(self):
yield HeaderIcon()
yield HeaderTitle()
if self.show_clock:
yield HeaderClock()
def watch_tall(self, tall: bool) -> None:
self.set_class(tall, "-tall")
async def on_click(self, event):
def on_click(self):
self.toggle_class("-tall")
def on_mount(self) -> None:
@@ -103,8 +125,3 @@ class Header(Widget):
watch(self.app, "title", set_title)
watch(self.app, "sub_title", set_sub_title)
def compose(self):
yield HeaderIcon()
yield HeaderTitle()
yield HeaderClock()