Files
textual/sandbox/tabs.py
2022-02-16 16:48:47 +00:00

140 lines
4.5 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from dataclasses import dataclass
from rich.console import RenderableType
from rich.padding import Padding
from textual import events
from textual.app import App
from textual.renderables._tab_headers import Tab
from textual.widget import Widget
from textual.widgets.tabs import Tabs
class Info(Widget):
def __init__(self, text: str, emoji: bool = True) -> None:
super().__init__()
self.text = text
self.emoji = emoji
def render(self) -> RenderableType:
prefix = " " if self.emoji else ""
return Padding(f"{prefix}{self.text}", pad=0)
@dataclass
class WidgetDescription:
description: str
widget: Widget
class BasicApp(App):
"""Sandbox application used for testing/development by Textual developers"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tab_keys = {
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
}
tabs = [
Tab("January", name="one"),
Tab("に月", name="two"),
Tab("March", name="three"),
Tab("April", name="four"),
Tab("May", name="five"),
Tab("And a really long tab!", name="six"),
# Tab("Four", name="five"),
# Tab("Four", name="six"),
# Tab("Four", name="seven"),
# Tab("Four", name="eight"),
# Tab("Four", name="nine"),
]
self.examples = [
WidgetDescription(
"Customise the spacing between tabs, e.g. tab_padding=1",
Tabs(
tabs,
tab_padding=1,
),
),
WidgetDescription(
"Change the opacity of inactive tab text, e.g. inactive_text_opacity=.2",
Tabs(
tabs,
active_tab="two",
active_bar_style="#1493FF",
inactive_text_opacity=0.2,
tab_padding=2,
),
),
WidgetDescription(
"Choose which tab to start on by name, e.g. active_tab='three'",
Tabs(
tabs,
active_tab="three",
active_bar_style="#FFCB4D",
tab_padding=3,
),
),
WidgetDescription(
"Change the color of the inactive portions of the underline, e.g. inactive_bar_style='blue'",
Tabs(
tabs,
active_tab="four",
inactive_bar_style="blue",
),
),
WidgetDescription(
"Change the color of the active portion of the underline, e.g. active_bar_style='red'",
Tabs(
tabs,
active_tab="five",
active_bar_style="red",
inactive_text_opacity=1,
),
),
WidgetDescription(
"Change the animation duration and function (animation_duration=1, animation_function='out_quad')",
Tabs(
tabs,
active_tab="one",
active_bar_style="#695CC8",
inactive_text_opacity=0.2,
animation_duration=1,
animation_function="out_quad",
),
),
]
def on_load(self):
"""Bind keys here."""
self.bind("tab", "toggle_class('#sidebar', '-active')")
self.bind("a", "toggle_class('#header', '-visible')")
self.bind("c", "toggle_class('#content', '-content-visible')")
self.bind("d", "toggle_class('#footer', 'dim')")
def on_key(self, event: events.Key) -> None:
for example in self.examples:
example.widget.active_tab_name = self.tab_keys.get(event.key, "one")
def on_mount(self):
"""Build layout here."""
self.mount(
info=Info(
"\n"
"• The examples below show customisation options for the [#1493FF]Tabs[/] widget.\n"
"• Press keys 1-6 on your keyboard to switch tabs, or click on a tab.",
emoji=False,
)
)
for example in self.examples:
self.mount(Info(example.description))
self.mount(example.widget)
BasicApp.run(css_file="tabs.scss", watch_css=True, log="textual.log")