Until now the Tree.NodeExpanded and Tree.NodeCollapsed messages were only
sent out when changes were made to the tree by user interaction. This meant
that if any changes were made with the TreeNode expand, expand_all,
collapse, collapse_all, toggle or toggle_all API calls no messages would be
sent.
This PR corrects this.
The work here is, in part, required for #2456 (DirectoryTree lazy-loads
directory information on node expansion so if someone is expanding nodes
under code control the DirectoryTree never gets to know that it should load
a directory's content) and will build on #1644, essentially adding a missing
aspect to the latter PR.
Currently, in the various TreeNode messages, and the handlers you'd write to
handle them, there's no way to easily know *which* tree sent the message and
so which tree the node belongs to.
This commit adds public access to the tree reference to the nodes, so that
in an event handler the developer can check the tree involved in the event.
See #2413.
After some internal discussion we've decided to keep `Tree.clear` as it was,
and add a `Tree.reset`, which does a `Tree.clear` but resets the label and
data of `Tree.root` to the values given, while mirroring how `Tree.__init__`
takes those parameters.
Just before doing the commit I decided to rename the test tree in the new
unit test for clearing down a tree. Of course I managed to name it in such a
way that it becomes special to pytest.
This fixes that.
See #1437 for background. While it would be ideal to allow for the complete
emptying of a Tree, the root node is required (and it's part of the
construction of a Tree). So, here, when clearing the Tree we optionally
allow for a new label to be given.
Ideally we'll also allow for fresh data to be provided too; but there's a
wrinkle there in knowing the difference between the data being None, and no
data being provided (so the current root's data being carried over).
Following the method of defaulting used in __init__ would cause problems. As
such, rather than roll all of this into one commit, this goes with the basic
requirement and the solution for data will follow.
Note this also starts some unit tests for the clearing of a Tree.
This encourages importing it from `textual.widgets.tree` instead, keeping it
in line with the other changes made for #1637.
Note this is a breaking change.
The thinking here is that a user of a Tree may want to relate notes to other
parts of their UI, or other data in their application. While this could be
done by keeping a reference to the node itself, it could also be beneficial
to just track the ID. Given that ID is a public property of a TreeNode, but
given it doesn't currently have any other (public) purpose, this seems to
add some useful symmetry.
This is sort of different from selected. Selected is when someone mashes the
enter button or clicks on a node. Highlighted is when the cursor moves into
a new node.
See #1400.
There's going to be a whole bunch of tests relating to the Tree and TreeNode
coming, let's make sure this ends up being fairly granular.
(side thought: it might be a good time soon to revisit all the tests for
Textual and try and wrangle them into some tidy structure)