Skip to content

Commit 4429d95

Browse files
vlacicsernazs
andcommitted
Add named colors listing to colors sample application
Resolves: Textualize/textual#2922 Co-authored-by: Zsolt Cserna <cserna.zsolt@gmail.com>
1 parent 998f0cb commit 4429d95

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/textual_dev/previews/colors.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ Label {
22
width: 100%;
33
}
44

5-
ColorButtons {
5+
ThemeColorButtons {
66
dock: left;
77
overflow-y: auto;
88
width: 30;
99
}
1010

11-
ColorButtons > Button {
11+
ThemeColorButtons > Button {
1212
width: 100%;
1313
}
1414

@@ -61,6 +61,10 @@ ColorGroup.-active {
6161
color: $text;
6262
}
6363

64+
.text-left {
65+
content-align: left middle;
66+
}
67+
6468
.muted {
6569
color: $text-muted;
6670
}

src/textual_dev/previews/colors.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from textual.app import App, ComposeResult
2+
from textual.color import COLOR_NAME_TO_RGB, Color
23
from textual.containers import Horizontal, Vertical, VerticalScroll
34
from textual.design import ColorSystem
4-
from textual.widgets import Button, Footer, Label, Static
5+
from textual.widgets import Button, Footer, Label, Static, TabbedContent
56

67

7-
class ColorButtons(VerticalScroll):
8+
class ThemeColorButtons(VerticalScroll):
89
def compose(self) -> ComposeResult:
910
for color_name in ColorSystem.COLOR_NAMES:
1011
yield Button(color_name, id=color_name)
@@ -27,6 +28,26 @@ class Content(Vertical):
2728

2829

2930
class ColorsView(VerticalScroll):
31+
pass
32+
33+
34+
class ColorTabs(TabbedContent, inherit_css=False):
35+
pass
36+
37+
38+
class NamedColorsView(ColorsView):
39+
def compose(self) -> ComposeResult:
40+
with ColorGroup(id=f"group-named"):
41+
for name, rgb in COLOR_NAME_TO_RGB.items():
42+
color = Color(*rgb)
43+
with ColorItem() as ci:
44+
ci.styles.background = name
45+
yield ColorBar(name, classes="text")
46+
yield ColorBar(f"{color.hex6}", classes="text")
47+
yield ColorBar(f"{color.rgb}", classes="text text-left")
48+
49+
50+
class ThemeColorsView(ColorsView):
3051
def compose(self) -> ComposeResult:
3152
LEVELS = [
3253
"darken-3",
@@ -55,15 +76,17 @@ class ColorsApp(App[None]):
5576
BINDINGS = [("d", "toggle_dark", "Toggle dark mode")]
5677

5778
def compose(self) -> ComposeResult:
58-
yield Content(ColorButtons())
5979
yield Footer()
80+
with ColorTabs("Theme Colors", "Named Colors"):
81+
yield Content(ThemeColorButtons())
82+
yield Vertical(NamedColorsView())
6083

6184
def on_mount(self) -> None:
6285
self.call_after_refresh(self.update_view)
6386

6487
def update_view(self) -> None:
6588
content = self.query_one("Content", Content)
66-
content.mount(ColorsView())
89+
content.mount(ThemeColorsView())
6790

6891
def on_button_pressed(self, event: Button.Pressed) -> None:
6992
self.query(ColorGroup).remove_class("-active")

0 commit comments

Comments
 (0)