diff --git a/CHANGELOG.md b/CHANGELOG.md index 93653a5532..1eb69c2b1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/textual/widgets/_directory_tree.py b/src/textual/widgets/_directory_tree.py index e4a1d4ba72..3af6b4841f 100644 --- a/src/textual/widgets/_directory_tree.py +++ b/src/textual/widgets/_directory_tree.py @@ -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( diff --git a/tests/directory_tree/test_early_show_root.py b/tests/directory_tree/test_early_show_root.py new file mode 100644 index 0000000000..73e1995337 --- /dev/null +++ b/tests/directory_tree/test_early_show_root.py @@ -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