basic example

This commit is contained in:
Will McGugan
2021-11-25 21:14:37 +00:00
parent a73c921198
commit ad8f7e893a
5 changed files with 42 additions and 68 deletions

33
examples/basic.css Normal file
View File

@@ -0,0 +1,33 @@
App > DockView {
layout: dock;
docks: side=left/1 header=top footer=bottom;
layers: base panels;
}
#sidebar {
text: bold #09312e on #3caea3;
dock-group: side;
width: 30;
height: 1fr;
layer: panels;
border-right: outer #09312e;
}
#header {
text: on #173f5f;
dock-group: header;
height: 3;
border: hkey white;
}
#footer {
dock-group: header;
height: 3;
border-top: hkey #0f2b41;
text: #3a3009 on #f6d55c;
}
#content {
dock-group: header;
text: on #20639b;
}

View File

@@ -5,62 +5,10 @@ from textual.widget import Widget
class BasicApp(App):
"""A basic app demonstrating CSS"""
css = """
App > DockView {
layout: dock;
docks: side=left/1 header=top footer=bottom;
layers: base panels;
}
#sidebar {
text: bold #09312e on #3CAEA3;
dock-group: side;
width: 30;
height: 1fr;
layer: panels;
border-right: vkey #09312e;
display: block;
offset-x: -15
}
#sidebar.-active {
display: block;
}
#header {
text: on #173f5f;
dock-group: header;
height: 3;
border: hkey white;
}
#footer {
dock-group: header;
/* border-top: hkey #0f2b41; */
text: #3a3009 on #f6d55c;
/*padding: 2;*/
border: heavy red;
margin: 2;
}
#content {
dock-group: header;
text: on #20639B;
}
"""
async def on_load(self) -> None:
await self.bind("t", "toggle('#sidebar', '-active')")
async def on_mount(self) -> None:
def on_mount(self) -> None:
"""Build layout here."""
await self.view.mount(
self.view.mount(
header=Widget(),
content=Widget(),
footer=Widget(),
@@ -68,4 +16,4 @@ class BasicApp(App):
)
BasicApp.run(log="textual.log")
BasicApp.run(css_file="basic.css")

View File

@@ -172,7 +172,7 @@ class App(DOMNode):
except Exception:
pass
async def bind(
def bind(
self,
keys: str,
action: str,
@@ -354,9 +354,7 @@ class App(DOMNode):
return True
return False
async def mount(
self, parent: DOMNode, *anon_widgets: Widget, **widgets: Widget
) -> None:
def mount(self, parent: DOMNode, *anon_widgets: Widget, **widgets: Widget) -> None:
"""Mount widget(s) so they may receive events.
Args:
@@ -377,13 +375,8 @@ class App(DOMNode):
self._register(parent, widget)
apply_stylesheet(widget)
# await widget.post_message(mount_event)
# await mount_event.wait()
for _widget_id, widget in name_widgets:
widget.post_message_no_wait(events.Mount(sender=parent))
# await event.wait()
# for widget, _ in widget_events:
# apply_stylesheet(widget)
def is_mounted(self, widget: Widget) -> bool:
return widget in self.registry
@@ -474,7 +467,7 @@ class App(DOMNode):
# If the event has been forwarded it may have bubbled up back to the App
if isinstance(event, events.Mount):
view = DockView()
await self.mount(self, view)
self.mount(self, view)
await self.push_view(view)
await super().on_event(event)

View File

@@ -170,7 +170,7 @@ class Layout(ABC):
async def mount_all(self, view: "View") -> None:
widgets = list(self.get_widgets(view))
if widgets:
await view.mount(*widgets)
view.mount(*widgets)
@property
def map(self) -> LayoutMap | None:

View File

@@ -138,8 +138,8 @@ class View(Widget):
message.stop()
self.app.refresh()
async def mount(self, *anon_widgets: Widget, **widgets: Widget) -> None:
await self.app.mount(self, *anon_widgets, **widgets)
def mount(self, *anon_widgets: Widget, **widgets: Widget) -> None:
self.app.mount(self, *anon_widgets, **widgets)
self.refresh()
async def refresh_layout(self) -> None: