BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-base / src / observe.rs

observe.rs

eef6c0a4 History
observe.rs 1.8 KiB Download Raw
1//! Registers native elements without accepting a second description of their state.
2use gpui::{Element, Hitbox, InteractiveElement};
3
4#[doc(hidden)]
5#[cfg(feature = "test-support")]
6pub type ObservedElement<E> = crate::test_support::Observed<E>;
7#[doc(hidden)]
8#[cfg(not(feature = "test-support"))]
9pub type ObservedElement<E> = E;
10
11/// Registers an existing element for UI queries. Its native accessibility
12/// properties are read automatically; there are no test-only property setters.
13///
14/// Test-only values cannot override the native control:
15/// ```compile_fail
16/// use gpui::{div, prelude::*};
17/// use gpui_base::TestSupportExt;
18/// div().id("input").test_support().test_props(|props| props.value("invented"));
19/// ```
20pub trait TestSupportExt:
21 Element<PrepaintState = Option<Hitbox>> + InteractiveElement + Sized
22{
23 /// With `test-support`, observes the element without adding a layout node.
24 /// Otherwise returns the original element with its exact native type.
25 /// Call before `track_focus` so the actual focus binding can be observed.
26 /// Querying focus on a focus-capable element with a missed binding panics.
27 fn test_support(self) -> ObservedElement<Self> {
28 #[cfg(feature = "test-support")]
29 {
30 crate::test_support::Observed::new(self)
31 }
32 #[cfg(not(feature = "test-support"))]
33 {
34 self
35 }
36 }
37}
38impl<E: Element<PrepaintState = Option<Hitbox>> + InteractiveElement> TestSupportExt for E {}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43 use gpui::div;
44
45 #[test]
46 fn observation_preserves_identity_and_native_type_in_normal_builds() {
47 let element = div().id("target").test_support().test_support();
48 assert_eq!(Element::id(&element), Some("target".into()));
49 #[cfg(not(feature = "test-support"))]
50 let _: gpui::Stateful<gpui::Div> = element;
51 }
52}