You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(No associated issue - small additive enhancement)
Description
Add explicit TreeEvent to TreeState so consumers can react to expand/collapse changes without maintaining a shadow copy of expansion state.
For lazy-loading trees (file browsers, database explorers, remote object browsers), consumers currently need to cx.observe(&tree_state, ...) and diff the entire expansion set after every notify.
With this change, consumers can subscribe directly:
cx.subscribe(&tree_state, |this, _, event: &TreeEvent, cx| {
if let TreeEvent::Expanded(id) = event {
this.load_children_for(id.as_ref(), cx);
}
});
Break Changes
Fully additive. Existing cx.observe(&tree_state, ...) consumers continue to work unchanged.
How to Test
cargo test -p gpui-component tree
Four new tests were added:
test_emits_expanded_event - toggling a collapsed folder emits TreeEvent::Expanded(id)
test_emits_collapsed_event - toggling an expanded folder emits TreeEvent::Collapsed(id)
test_set_items_does_not_emit_expansion_events - rebuilding the tree via set_items() is silent
test_event_carries_item_id - event payload is TreeItem.id, not row index
Checklist
I have read the CONTRIBUTING document and followed the guidelines.
Reviewed the changes in this PR and confirmed AI generated code (If any) is accurate.
Passed cargo run for story tests related to the changes.
[N/A] Tested macOS, Windows and Linux platforms performance (if the change is platform-specific) (not platform specific)
AI Assistance Disclosure
🤖 Parts of this PR were generated with AI assistance and subsequently reviewed, tested, and refactored by a human to match the project's existing code style and patterns. Specifically:
The TreeEvent enum and EventEmitter implementation were drafted with AI, then aligned to match the existing TableEvent / SliderEvent patterns in the crate.
The test cases were AI-generated and then verified by running the full gpui-component test suite (219 tests pass, including the 4 new ones).
One small detail though: If you use set_selected_item to select a node hidden under collapsed folders, it bypasses toggle_expand so no TreeEvent::Expanded is emitted — a lazy-loading subscriber would never load children for folders expanded this way.
huacnlee
changed the title
feat(tree): emit TreeEvent on expand/collapse
tree: emit TreeEvent on expand/collapse
Jun 10, 2026
One small detail though: If you use set_selected_item to select a node hidden under collapsed folders, it bypasses toggle_expand so no TreeEvent::Expanded is emitted — a lazy-loading subscriber would never load children for folders expanded this way.
Hey thanks for the feedback, this should be resolved now! set_selected_item now routes ancestor expansion through the event-emitting path, so hidden folders fire TreeEvent::Expanded when revealed. I also reversed the ancestor iteration so events fire root -> leaf, which is the order a lazy-loading subscriber would like
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(No associated issue - small additive enhancement)
Description
Add explicit
TreeEventtoTreeStateso consumers can react to expand/collapse changes without maintaining a shadow copy of expansion state.For lazy-loading trees (file browsers, database explorers, remote object browsers), consumers currently need to
cx.observe(&tree_state, ...)and diff the entire expansion set after every notify.With this change, consumers can subscribe directly:
Break Changes
Fully additive. Existing
cx.observe(&tree_state, ...)consumers continue to work unchanged.How to Test
cargo test -p gpui-component treeFour new tests were added:
test_emits_expanded_event- toggling a collapsed folder emitsTreeEvent::Expanded(id)test_emits_collapsed_event- toggling an expanded folder emitsTreeEvent::Collapsed(id)test_set_items_does_not_emit_expansion_events- rebuilding the tree viaset_items()is silenttest_event_carries_item_id- event payload isTreeItem.id, not row indexChecklist
cargo runfor story tests related to the changes.AI Assistance Disclosure
🤖 Parts of this PR were generated with AI assistance and subsequently reviewed, tested, and refactored by a human to match the project's existing code style and patterns. Specifically:
TreeEventenum andEventEmitterimplementation were drafted with AI, then aligned to match the existingTableEvent/SliderEventpatterns in the crate.gpui-componenttest suite (219 tests pass, including the 4 new ones).