-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* lazy mount * Lazy test * doc * Add to docs * snapshot and changelog * typing * future * less flaky * comment
- Loading branch information
1 parent
a5a357f
commit 99ea845
Showing
10 changed files
with
209 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Tools for lazy loading widgets. | ||
""" | ||
|
||
|
||
from __future__ import annotations | ||
|
||
from .widget import Widget | ||
|
||
|
||
class Lazy(Widget): | ||
"""Wraps a widget so that it is mounted *lazily*. | ||
Lazy widgets are mounted after the first refresh. This can be used to display some parts of | ||
the UI very quickly, followed by the lazy widgets. Technically, this won't make anything | ||
faster, but it reduces the time the user sees a blank screen and will make apps feel | ||
more responsive. | ||
Making a widget lazy is beneficial for widgets which start out invisible, such as tab panes. | ||
Note that since lazy widgets aren't mounted immediately (by definition), they will not appear | ||
in queries for a brief interval until they are mounted. Your code should take this in to account. | ||
Example: | ||
```python | ||
def compose(self) -> ComposeResult: | ||
yield Footer() | ||
with ColorTabs("Theme Colors", "Named Colors"): | ||
yield Content(ThemeColorButtons(), ThemeColorsView(), id="theme") | ||
yield Lazy(NamedColorsView()) | ||
``` | ||
""" | ||
|
||
DEFAULT_CSS = """ | ||
Lazy { | ||
display: none; | ||
} | ||
""" | ||
|
||
def __init__(self, widget: Widget) -> None: | ||
"""Create a lazy widget. | ||
Args: | ||
widget: A widget that should be mounted after a refresh. | ||
""" | ||
self._replace_widget = widget | ||
super().__init__() | ||
|
||
def compose_add_child(self, widget: Widget) -> None: | ||
self._replace_widget.compose_add_child(widget) | ||
|
||
async def mount_composed_widgets(self, widgets: list[Widget]) -> None: | ||
parent = self.parent | ||
if parent is None: | ||
return | ||
assert isinstance(parent, Widget) | ||
|
||
async def mount() -> None: | ||
"""Perform the mount and discard the lazy widget.""" | ||
await parent.mount(self._replace_widget, after=self) | ||
await self.remove() | ||
|
||
self.call_after_refresh(mount) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from textual.app import App, ComposeResult | ||
from textual.containers import Horizontal, Vertical | ||
from textual.lazy import Lazy | ||
from textual.widgets import Label | ||
|
||
|
||
class LazyApp(App): | ||
def compose(self) -> ComposeResult: | ||
with Vertical(): | ||
with Lazy(Horizontal()): | ||
yield Label(id="foo") | ||
with Horizontal(): | ||
yield Label(id="bar") | ||
|
||
|
||
async def test_lazy(): | ||
app = LazyApp() | ||
async with app.run_test() as pilot: | ||
# No #foo on initial mount | ||
assert len(app.query("#foo")) == 0 | ||
assert len(app.query("#bar")) == 1 | ||
await pilot.pause() | ||
await pilot.pause() | ||
# #bar mounted after refresh | ||
assert len(app.query("#foo")) == 1 | ||
assert len(app.query("#bar")) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters