element_ext.rs

 1use gpui3::Element;
 2
 3pub trait ElementExt<S: 'static + Send + Sync>: Element<State = S> {
 4    /// Applies a given function `then` to the current element if `condition` is true.
 5    /// This function is used to conditionally modify the element based on a given condition.
 6    /// If `condition` is false, it just returns the current element as it is.
 7    fn when(mut self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
 8    where
 9        Self: Sized,
10    {
11        if condition {
12            self = then(self);
13        }
14        self
15    }
16}
17
18impl<S: 'static + Send + Sync, E: Element<State = S>> ElementExt<S> for E {}