Pass Rich Style object into Textual render method

This commit is contained in:
Darren Burns
2022-05-06 10:20:05 +01:00
parent 6b1dab5387
commit 43f65d73ae
23 changed files with 74 additions and 86 deletions

View File

@@ -1,13 +1,12 @@
from rich.text import Text
from rich.style import Style
from textual.app import App, ComposeResult
from textual.css.styles import Styles
from textual.widget import Widget
from textual.widgets import Static
class Thing(Widget):
def render(self, styles: Styles):
def render(self, style: Style):
return "Hello, 3434 World.\n[b]Lorem impsum."

View File

@@ -1,9 +1,9 @@
from rich.console import RenderableType
from rich.style import Style
from rich.syntax import Syntax
from rich.text import Text
from textual.app import App
from textual.css.styles import Styles
from textual.widget import Widget
from textual.widgets import Static
@@ -51,12 +51,12 @@ lorem = Text.from_markup(
class TweetHeader(Widget):
def render(self, styles: Styles) -> RenderableType:
def render(self, style: Style) -> RenderableType:
return Text("Lorem Impsum", justify="center")
class TweetBody(Widget):
def render(self, styles: Styles) -> Text:
def render(self, style: Style) -> Text:
return lorem
@@ -65,22 +65,22 @@ class Tweet(Widget):
class OptionItem(Widget):
def render(self, styles: Styles) -> Text:
def render(self, style: Style) -> Text:
return Text("Option")
class Error(Widget):
def render(self, styles: Styles) -> Text:
def render(self, style: Style) -> Text:
return Text("This is an error message", justify="center")
class Warning(Widget):
def render(self, styles: Styles) -> Text:
def render(self, style: Style) -> Text:
return Text("This is a warning message", justify="center")
class Success(Widget):
def render(self, styles: Styles) -> Text:
def render(self, style: Style) -> Text:
return Text("This is a success message", justify="center")

View File

@@ -1,13 +1,13 @@
from rich.console import RenderableType
from rich.panel import Panel
from rich.style import Style
from textual.app import App
from textual.css.styles import Styles
from textual.widget import Widget
class PanelWidget(Widget):
def render(self, styles: Styles) -> RenderableType:
def render(self, style: Style) -> RenderableType:
return Panel("hello world!", title="Title")

View File

@@ -3,16 +3,16 @@ from dataclasses import dataclass
from rich.console import RenderableType
from rich.padding import Padding
from rich.rule import Rule
from rich.style import Style
from textual import events
from textual.app import App
from textual.css.styles import Styles
from textual.widget import Widget
from textual.widgets.tabs import Tabs, Tab
class Hr(Widget):
def render(self, styles: Styles) -> RenderableType:
def render(self, style: Style) -> RenderableType:
return Rule()
@@ -23,7 +23,7 @@ class Info(Widget):
super().__init__()
self.text = text
def render(self, styles: Styles) -> RenderableType:
def render(self, style: Style) -> RenderableType:
return Padding(f"{self.text}", pad=(0, 1))

View File

@@ -3,6 +3,7 @@
background: green;
overflow: hidden auto;
border: heavy white;
text-style: underline;
}
.list-item {

View File

@@ -15,14 +15,13 @@ class BasicApp(App):
self.bind("d", "dump")
self.bind("t", "log_tree")
self.bind("p", "print")
self.bind("o", "toggle_visibility")
self.bind("p", "toggle_display")
self.bind("v", "toggle_visibility")
self.bind("x", "toggle_display")
self.bind("f", "modify_focussed")
self.bind("b", "toggle_border")
async def on_mount(self):
"""Build layout here."""
uber1 = Widget(
Placeholder(id="child1", classes="list-item"),
Placeholder(id="child2", classes="list-item"),
@@ -32,6 +31,7 @@ class BasicApp(App):
Placeholder(classes="list-item"),
)
self.mount(uber1=uber1)
uber1.focus()
async def on_key(self, event: events.Key) -> None:
await self.dispatch_key(event)
@@ -47,9 +47,8 @@ class BasicApp(App):
def action_print(self):
print(
"Printed using builtin [b blue]print[/] function:",
self.screen.tree,
sep=" - ",
"Focused widget is:",
self.focused,
)
print(1234, 5678)
sys.stdout.write("abcdef")