From 5d727351d7b9cac7e66f463f03ac86ea6091f8db Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 20 Feb 2024 09:52:14 +0000 Subject: [PATCH] :sparkles: Add a tester for https://github.com/Textualize/textual/issues/3045 --- tooltip_hell.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tooltip_hell.py diff --git a/tooltip_hell.py b/tooltip_hell.py new file mode 100644 index 0000000..2aa7385 --- /dev/null +++ b/tooltip_hell.py @@ -0,0 +1,92 @@ +"""Test harness for tooltip issues.""" + +from textual import on +from textual.app import App, ComposeResult +from textual.containers import Grid, VerticalScroll +from textual.widget import Widget +from textual.widgets import Button, Label + +class GoodTipper: + + @staticmethod + def tip(widget: Widget) -> Widget: + widget.tooltip = ( + f"[bold]This is a tooltip![/]\n\n" + f"[italic]The widget's ID is {widget.id}\n\n" + "It has a bunch of text. Cool huh?" + ) + return widget + +class Buttonatron(Grid, GoodTipper, can_focus=True): + + DEFAULT_CSS = """ + Buttonatron { + height: 1fr; + grid-size: 5; + grid-rows: 5; + + Button { + width: 1fr; + height: 1fr; + margin: 0 1 0 1; + } + } + """ + + BINDINGS = [("space", "add")] + + def action_add(self) -> None: + button_id = str(len(self.children)) + self.mount( + self.tip( + Button(f"Button #{button_id}\n\nPress me to remove me", id=f"button-{button_id}") + ) + ) + + @on(Button.Pressed) + def remove_button(self, event: Button.Pressed) -> None: + event.control.remove() + +class LabelsAllTheWayDown(VerticalScroll, GoodTipper, can_focus=True): + + DEFAULT_CSS = """ + LabelsAllTheWayDown { + Grid { + grid-size: 5; + height: auto; + grid-rows: 10; + Label { + background: $panel; + border: panel cornflowerblue; + width: 1fr; + height: 1fr; + margin-bottom: 1; + } + } + } + """ + + def compose(self) -> ComposeResult: + with Grid(): + for n in range(200): + yield self.tip(Label(f"Here is label #{n}", id=f"label-{n}")) + +class TooltipHellApp(App[None]): + + CSS = """ + Screen > * { + border: blank; + &:focus { + border: thick green; + } + } + """ + + def compose(self) -> ComposeResult: + yield Buttonatron() + yield LabelsAllTheWayDown() + +if __name__ == "__main__": + TooltipHellApp().run() + +### tooltip_hell.py ends here