Skip to content

Commit 0bb4871

Browse files
Factor out 'Bar' widget.
1 parent bc3ffc4 commit 0bb4871

2 files changed

Lines changed: 55 additions & 38 deletions

File tree

src/textual/widgets/_bar.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Auxiliary widget that renders a bar, used in other widgets like tabs and progress bar.
3+
"""
4+
from __future__ import annotations
5+
6+
from rich.style import Style
7+
8+
from ..app import RenderResult
9+
from ..reactive import reactive
10+
from ..renderables.bar import RenderableBar
11+
from ..widget import Widget
12+
13+
14+
class Bar(Widget):
15+
"""A widget that renders a highlighted bar."""
16+
17+
DEFAULT_CSS = """
18+
Bar {
19+
width: 1fr;
20+
height: 1;
21+
}
22+
Bar > .bar--bar {
23+
background: $foreground 10%;
24+
"""
25+
26+
COMPONENT_CLASSES = {"bar--bar"}
27+
"""
28+
| Class | Description |
29+
| :- | :- |
30+
| `bar--bar` | Style of the bar (may be used to change the color). |
31+
"""
32+
33+
highlight_start = reactive(0)
34+
"""First cell in highlight."""
35+
highlight_end = reactive(0)
36+
"""Last cell (inclusive) in highlight."""
37+
38+
@property
39+
def _highlight_range(self) -> tuple[int, int]:
40+
"""Highlighted range for the bar."""
41+
return (self.highlight_start, self.highlight_end)
42+
43+
def render(self) -> RenderResult:
44+
"""Render the bar."""
45+
bar_style = self.get_component_rich_style("bar--bar")
46+
return RenderableBar(
47+
highlight_range=self._highlight_range,
48+
highlight_style=Style.from_color(bar_style.color),
49+
background_style=Style.from_color(bar_style.bgcolor),
50+
)

src/textual/widgets/_tabs.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,34 @@
33
from typing import TYPE_CHECKING, ClassVar
44

55
import rich.repr
6-
from rich.style import Style
76
from rich.text import Text, TextType
87

98
from .. import events
10-
from ..app import ComposeResult, RenderResult
9+
from ..app import ComposeResult
1110
from ..binding import Binding, BindingType
1211
from ..containers import Container, Horizontal, Vertical
1312
from ..css.query import NoMatches
1413
from ..events import Mount
1514
from ..geometry import Offset
1615
from ..message import Message
1716
from ..reactive import reactive
18-
from ..renderables.bar import RenderableBar
1917
from ..widget import Widget
2018
from ..widgets import Static
19+
from ._bar import Bar
2120

2221
if TYPE_CHECKING:
2322
from typing_extensions import Self
2423

2524

26-
class Underline(Widget):
25+
class Underline(Bar):
2726
"""The animated underline beneath tabs."""
2827

2928
DEFAULT_CSS = """
30-
Underline {
31-
width: 1fr;
32-
height: 1;
33-
}
34-
Underline > .underline--bar {
35-
background: $foreground 10%;
29+
Underline > .bar--bar {
3630
color: $accent;
3731
}
3832
"""
3933

40-
COMPONENT_CLASSES = {"underline--bar"}
41-
"""
42-
| Class | Description |
43-
| :- | :- |
44-
| `underline-bar` | Style of the bar (may be used to change the color). |
45-
46-
"""
47-
48-
highlight_start = reactive(0)
49-
"""First cell in highlight."""
50-
highlight_end = reactive(0)
51-
"""Last cell (inclusive) in highlight."""
52-
5334
class Clicked(Message):
5435
"""Inform ancestors the underline was clicked."""
5536

@@ -60,20 +41,6 @@ def __init__(self, offset: Offset) -> None:
6041
self.offset = offset
6142
super().__init__()
6243

63-
@property
64-
def _highlight_range(self) -> tuple[int, int]:
65-
"""Highlighted range for underline bar."""
66-
return (self.highlight_start, self.highlight_end)
67-
68-
def render(self) -> RenderResult:
69-
"""Render the bar."""
70-
bar_style = self.get_component_rich_style("underline--bar")
71-
return RenderableBar(
72-
highlight_range=self._highlight_range,
73-
highlight_style=Style.from_color(bar_style.color),
74-
background_style=Style.from_color(bar_style.bgcolor),
75-
)
76-
7744
def _on_click(self, event: events.Click):
7845
"""Catch clicks, so that the underline can activate the tabs."""
7946
event.stop()
@@ -160,7 +127,7 @@ class Tabs(Widget, can_focus=True):
160127
min-width: 100%;
161128
overflow: hidden hidden;
162129
}
163-
Tabs:focus .underline--bar {
130+
Tabs:focus .bar--bar {
164131
background: $foreground 20%;
165132
}
166133
"""

0 commit comments

Comments
 (0)