Skip to content

Commit

Permalink
Merge pull request #3002 from davep/early-show-root
Browse files Browse the repository at this point in the history
Fix a crash when setting `DirectoryTree.show_root` before DOM is ready
  • Loading branch information
rodrigogiraoserrao authored Jan 8, 2024
2 parents 8f822ae + 2a3edb1 commit 054a132
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Parameter `animate` from `DataTable.move_cursor` was being ignored https://github.com/Textualize/textual/issues/3840
- Fixed a crash if `DirectoryTree.show_root` was set before the DOM was fully available https://github.com/Textualize/textual/issues/2363


## [0.47.1] - 2023-01-05
Expand Down
5 changes: 5 additions & 0 deletions src/textual/widgets/_directory_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ def render_label(
node_label = node._label.copy()
node_label.stylize(style)

# If the tree isn't mounted yet we can't use component classes to stylize
# the label fully, so we return early.
if not self.is_mounted:
return node_label

if node._allow_expand:
prefix = ("📂 " if node.is_expanded else "📁 ", base_style + TOGGLE_STYLE)
node_label.stylize_before(
Expand Down
16 changes: 16 additions & 0 deletions tests/directory_tree/test_early_show_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from textual.app import App, ComposeResult
from textual.widgets import DirectoryTree


class DirectoryTreeApp(App[None]):
def compose(self) -> ComposeResult:
tree = DirectoryTree(".")
tree.show_root = True
yield tree


async def test_managed_to_set_show_root_before_mounted() -> None:
"""https://github.com/Textualize/textual/issues/2363"""
async with DirectoryTreeApp().run_test() as pilot:
assert isinstance(pilot.app.query_one(DirectoryTree), DirectoryTree)
assert pilot.app.query_one(DirectoryTree).show_root is True

0 comments on commit 054a132

Please sign in to comment.