Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions crates/base/src/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,38 @@ impl Drop for Registration {
/// Internal lookup used by Kit's testing API. Scope follows GPUI's element path.
#[doc(hidden)]
pub fn find(window: &Window, scope: &[ElementId], id: &ElementId) -> Option<ElementSnapshot> {
REGISTRY.with(|registry| {
let mut matches = find_all(window, scope, id);
assert!(
matches.len() <= 1,
"ambiguous ElementId {id:?}; use within(...) to select a scope. Matches: {:?}",
matches.iter().map(|entry| &entry.path).collect::<Vec<_>>()
);
matches.pop()
}

/// Every match for `id` below `scope`, top to bottom, then left to right by
/// bounds origin. Includes invisible registrations; equal-origin order is unspecified.
#[doc(hidden)]
pub fn find_all(window: &Window, scope: &[ElementId], id: &ElementId) -> Vec<ElementSnapshot> {
let mut matches: Vec<_> = REGISTRY.with(|registry| {
let registry = registry.borrow();
let entries = registry.get(&(std::sync::Arc::as_ptr(window.text_system()) as usize))?;
let matches: Vec<_> = entries
let Some(entries) = registry.get(&(std::sync::Arc::as_ptr(window.text_system()) as usize))
else {
return Vec::new();
};
entries
.values()
.filter_map(Weak::upgrade)
.filter(|entry| {
entry.global_id.len() > scope.len()
&& entry.global_id.starts_with(scope)
&& entry.global_id.last() == Some(id)
})
.collect();
assert!(
matches.len() <= 1,
"ambiguous ElementId {id:?}; use within(...) to select a scope. Matches: {:?}",
matches
.iter()
.map(|entry| &entry.global_id)
.collect::<Vec<_>>()
);
matches.first().map(|entry| entry.facts.borrow().clone())
})
.map(|entry| entry.facts.borrow().clone())
.collect()
});
matches.sort_unstable_by_key(|entry| (entry.bounds.origin.y, entry.bounds.origin.x));
matches
}

#[doc(hidden)]
Expand Down
Loading
Loading