element_ext.rs

 1use gpui2::Element;
 2
 3pub trait ElementExt<S: 'static + Send + Sync>: Element<ViewState = 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    // fn when_some<T, U>(mut self, option: Option<T>, then: impl FnOnce(Self, T) -> U) -> U
18    // where
19    //     Self: Sized,
20    // {
21    //     if let Some(value) = option {
22    //         self = then(self, value);
23    //     }
24    //     self
25    // }
26}
27
28impl<S: 'static + Send + Sync, E: Element<ViewState = S>> ElementExt<S> for E {}