Fix typing issues in demo.

Fixing the typing issues here involved making use of the messaging features to replace a method (wasn't exactly wrong, but this is more in line with the Textual way of doing things, which should be prevalent in the demo of the framework).
We also dealt with a typing issue in DarkSwitch.on_dark_change by deleting an unnecessary parameter, but the underlying problem is unsolved and was logged as issue #1865.

Related issues: #1865.
This commit is contained in:
Rodrigo Girão Serrão
2023-02-23 12:24:52 +00:00
parent 759b04a1df
commit f366783920
3 changed files with 226 additions and 220 deletions

View File

@@ -40,7 +40,4 @@ src/textual/widgets/_data_table.py:1629: error: Argument "key" to "sorted" has i
src/textual/widgets/_button.py:219: error: Item "ConsoleRenderable" of "Union[ConsoleRenderable, RichCast, str]" has no attribute "copy" [union-attr]
src/textual/widgets/_button.py:219: error: Item "RichCast" of "Union[ConsoleRenderable, RichCast, str]" has no attribute "copy" [union-attr]
src/textual/widgets/_button.py:219: error: Item "str" of "Union[ConsoleRenderable, RichCast, str]" has no attribute "copy" [union-attr]
src/textual/demo.py:206: error: Argument 3 to "watch" of "DOMNode" has incompatible type "Callable[[bool], None]"; expected "Union[Callable[[], Awaitable[None]], Callable[[], None]]" [arg-type]
src/textual/demo.py:221: error: "App[Any]" has no attribute "add_note" [attr-defined]
src/textual/demo.py:276: error: "App[Any]" has no attribute "add_note" [attr-defined]
Found 44 errors in 9 files (checked 154 source files)
Found 41 errors in 8 files (checked 154 source files)

View File

@@ -1,263 +1,263 @@
* {
transition: background 500ms in_out_cubic, color 500ms in_out_cubic;
}
* {
transition: background 500ms in_out_cubic, color 500ms in_out_cubic;
}
Screen {
layers: base overlay notes notifications;
overflow: hidden;
}
Screen {
layers: base overlay notes notifications;
overflow: hidden;
}
Notification {
dock: bottom;
layer: notification;
width: auto;
margin: 2 4;
padding: 1 2;
background: $background;
color: $text;
height: auto;
Notification {
dock: bottom;
layer: notification;
width: auto;
margin: 2 4;
padding: 1 2;
background: $background;
color: $text;
height: auto;
}
}
Sidebar {
width: 40;
background: $panel;
transition: offset 500ms in_out_cubic;
layer: overlay;
Sidebar {
width: 40;
background: $panel;
transition: offset 500ms in_out_cubic;
layer: overlay;
}
}
Sidebar:focus-within {
offset: 0 0 !important;
}
Sidebar:focus-within {
offset: 0 0 !important;
}
Sidebar.-hidden {
offset-x: -100%;
}
Sidebar.-hidden {
offset-x: -100%;
}
Sidebar Title {
background: $boost;
color: $secondary;
padding: 2 4;
border-right: vkey $background;
dock: top;
text-align: center;
text-style: bold;
}
Sidebar Title {
background: $boost;
color: $secondary;
padding: 2 4;
border-right: vkey $background;
dock: top;
text-align: center;
text-style: bold;
}
OptionGroup {
background: $boost;
color: $text;
height: 1fr;
border-right: vkey $background;
}
OptionGroup {
background: $boost;
color: $text;
height: 1fr;
border-right: vkey $background;
}
Option {
margin: 1 0 0 1;
height: 3;
padding: 1 2;
background: $boost;
border: tall $panel;
text-align: center;
}
Option {
margin: 1 0 0 1;
height: 3;
padding: 1 2;
background: $boost;
border: tall $panel;
text-align: center;
}
Option:hover {
background: $primary 20%;
color: $text;
}
Option:hover {
background: $primary 20%;
color: $text;
}
Body {
height: 100%;
overflow-y: scroll;
width: 100%;
background: $surface;
Body {
height: 100%;
overflow-y: scroll;
width: 100%;
background: $surface;
}
}
AboveFold {
width: 100%;
height: 100%;
align: center middle;
}
AboveFold {
width: 100%;
height: 100%;
align: center middle;
}
Welcome {
background: $boost;
height: auto;
max-width: 100;
min-width: 40;
border: wide $primary;
padding: 1 2;
margin: 1 2;
box-sizing: border-box;
}
Welcome {
background: $boost;
height: auto;
max-width: 100;
min-width: 40;
border: wide $primary;
padding: 1 2;
margin: 1 2;
box-sizing: border-box;
}
Welcome Button {
width: 100%;
margin-top: 1;
}
Welcome Button {
width: 100%;
margin-top: 1;
}
Column {
height: auto;
min-height: 100vh;
align: center top;
}
Column {
height: auto;
min-height: 100vh;
align: center top;
}
DarkSwitch {
background: $panel;
padding: 1;
dock: bottom;
height: auto;
border-right: vkey $background;
}
DarkSwitch {
background: $panel;
padding: 1;
dock: bottom;
height: auto;
border-right: vkey $background;
}
DarkSwitch .label {
width: 1fr;
padding: 1 2;
color: $text-muted;
}
DarkSwitch .label {
width: 1fr;
padding: 1 2;
color: $text-muted;
}
DarkSwitch Switch {
background: $boost;
dock: left;
}
DarkSwitch Switch {
background: $boost;
dock: left;
}
Screen > Container {
height: 100%;
overflow: hidden;
}
Screen>Container {
height: 100%;
overflow: hidden;
}
TextLog {
background: $surface;
color: $text;
height: 50vh;
dock: bottom;
layer: notes;
border-top: hkey $primary;
offset-y: 0;
transition: offset 400ms in_out_cubic;
padding: 0 1 1 1;
}
TextLog {
background: $surface;
color: $text;
height: 50vh;
dock: bottom;
layer: notes;
border-top: hkey $primary;
offset-y: 0;
transition: offset 400ms in_out_cubic;
padding: 0 1 1 1;
}
TextLog:focus {
offset: 0 0 !important;
}
TextLog:focus {
offset: 0 0 !important;
}
TextLog.-hidden {
offset-y: 100%;
}
TextLog.-hidden {
offset-y: 100%;
}
Section {
height: auto;
min-width: 40;
margin: 1 2 4 2;
Section {
height: auto;
min-width: 40;
margin: 1 2 4 2;
}
}
SectionTitle {
padding: 1 2;
background: $boost;
text-align: center;
text-style: bold;
}
SectionTitle {
padding: 1 2;
background: $boost;
text-align: center;
text-style: bold;
}
SubTitle {
padding-top: 1;
border-bottom: heavy $panel;
color: $text;
text-style: bold;
}
SubTitle {
padding-top: 1;
border-bottom: heavy $panel;
color: $text;
text-style: bold;
}
TextContent {
margin: 1 0;
}
TextContent {
margin: 1 0;
}
QuickAccess {
width: 30;
dock: left;
QuickAccess {
width: 30;
dock: left;
}
}
LocationLink {
margin: 1 0 0 1;
height: 1;
padding: 1 2;
background: $boost;
color: $text;
box-sizing: content-box;
content-align: center middle;
}
LocationLink {
margin: 1 0 0 1;
height: 1;
padding: 1 2;
background: $boost;
color: $text;
box-sizing: content-box;
content-align: center middle;
}
LocationLink:hover {
background: $accent;
color: $text;
text-style: bold;
}
LocationLink:hover {
background: $accent;
color: $text;
text-style: bold;
}
.pad {
margin: 1 0;
}
.pad {
margin: 1 0;
}
DataTable {
height: 16;
}
DataTable {
height: 16;
}
LoginForm {
height: auto;
margin: 1 0;
padding: 1 2;
layout: grid;
grid-size: 2;
grid-rows: 4;
grid-columns: 12 1fr;
background: $boost;
border: wide $background;
}
LoginForm {
height: auto;
margin: 1 0;
padding: 1 2;
layout: grid;
grid-size: 2;
grid-rows: 4;
grid-columns: 12 1fr;
background: $boost;
border: wide $background;
}
LoginForm Button{
margin: 0 1;
width: 100%;
}
LoginForm Button {
margin: 0 1;
width: 100%;
}
LoginForm .label {
padding: 1 2;
text-align: right;
}
LoginForm .label {
padding: 1 2;
text-align: right;
}
Message {
margin: 0 1;
SidebarMessage {
margin: 0 1;
}
}
Tree {
margin: 1 0;
}
Tree {
margin: 1 0;
}
Window {
background: $boost;
overflow: auto;
height: auto;
max-height: 16;
}
Window {
background: $boost;
overflow: auto;
height: auto;
max-height: 16;
}
Window > Static {
width: auto;
}
Window>Static {
width: auto;
}
Version {
color: $text-disabled;
dock: bottom;
text-align: center;
padding: 1;
}
Version {
color: $text-disabled;
dock: bottom;
text-align: center;
padding: 1;
}

View File

@@ -15,6 +15,7 @@ from rich.text import Text
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.containers import Container, Horizontal
from textual.message import Message, MessageTarget
from textual.reactive import reactive
from textual.widgets import (
Button,
@@ -189,6 +190,14 @@ JSON_EXAMPLE = """{
"""
class Note(Message):
"""Sent by some demo widgets with noteworthy information."""
def __init__(self, sender: MessageTarget, note: str | Text) -> None:
super().__init__(sender)
self.note = note
class Body(Container):
pass
@@ -205,7 +214,7 @@ class DarkSwitch(Horizontal):
def on_mount(self) -> None:
self.watch(self.app, "dark", self.on_dark_change, init=False)
def on_dark_change(self, dark: bool) -> None:
def on_dark_change(self) -> None:
self.query_one(Switch).value = self.app.dark
def on_switch_changed(self, event: Switch.Changed) -> None:
@@ -218,7 +227,7 @@ class Welcome(Container):
yield Button("Start", variant="success")
def on_button_pressed(self, event: Button.Pressed) -> None:
self.app.add_note("[b magenta]Start!")
self.post_message_no_wait(Note(self, "[b magenta]Start!"))
self.app.query_one(".location-first").scroll_visible(duration=0.5, top=True)
@@ -230,7 +239,7 @@ class SectionTitle(Static):
pass
class Message(Static):
class SidebarMessage(Static):
pass
@@ -242,7 +251,7 @@ class Version(Static):
class Sidebar(Container):
def compose(self) -> ComposeResult:
yield Title("Textual Demo")
yield OptionGroup(Message(MESSAGE), Version())
yield OptionGroup(SidebarMessage(MESSAGE), Version())
yield DarkSwitch()
@@ -273,7 +282,7 @@ class LocationLink(Static):
def on_click(self) -> None:
self.app.query_one(self.reveal).scroll_visible(top=True, duration=0.5)
self.app.add_note(f"Scrolling to [b]{self.reveal}[/b]")
self.post_message_no_wait(Note(self, f"Scrolling to [b]{self.reveal}[/b]"))
class LoginForm(Container):
@@ -315,8 +324,8 @@ class DemoApp(App):
show_sidebar = reactive(False)
def add_note(self, renderable: RenderableType) -> None:
self.query_one(TextLog).write(renderable)
def on_note(self, event: Note) -> None:
self.query_one(TextLog).write(event.note)
def compose(self) -> ComposeResult:
example_css = "\n".join(Path(self.css_path[0]).read_text().splitlines()[:50])
@@ -393,7 +402,7 @@ class DemoApp(App):
sidebar.add_class("-hidden")
def on_mount(self) -> None:
self.add_note("Textual Demo app is running")
self.post_message_no_wait(Note(self, "Textual Demo app is running"))
table = self.query_one(DataTable)
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
@@ -416,7 +425,7 @@ class DemoApp(App):
self.bell()
path = self.save_screenshot(filename, path)
message = Text.assemble("Screenshot saved to ", (f"'{path}'", "bold green"))
self.add_note(message)
self.post_message_no_wait(Note(self, message))
self.screen.mount(Notification(message))