-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.py
More file actions
44 lines (32 loc) · 1.38 KB
/
Copy patheditor.py
File metadata and controls
44 lines (32 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Run with ``python -m examples.editor`` from the repository root."""
from pathlib import Path
from textual import on
from textual.app import App, ComposeResult
from textual.content import Content
from textual.widgets import Button, Checkbox, Input, Label
from textui import ActionContext, DocumentLoader
class Editor(App):
"""A normal Textual App hosting one document and an explicit Save action."""
BINDINGS = [("ctrl+q", "quit", "Quit")]
def __init__(self) -> None:
super().__init__()
definition = DocumentLoader().from_file(Path(__file__).with_name("form.xml"))
self.document = definition.bind(self, actions={"save_document": self.save_document})
def compose(self) -> ComposeResult:
yield from self.document.compose()
@on(Button.Pressed)
@on(Input.Changed)
@on(Input.Submitted)
@on(Checkbox.Changed)
async def forward_document_message(
self, event: Button.Pressed | Input.Changed | Input.Submitted | Checkbox.Changed
) -> None:
await self.document.dispatch(event)
def save_document(self, context: ActionContext) -> None:
name = context.document.get_by_id("name")
status = context.document.get_by_id("status")
assert isinstance(name, Input)
assert isinstance(status, Label)
status.update(Content(f"Saved {name.value}"))
if __name__ == "__main__":
Editor().run()