@@ -1,10 +1,12 @@
mod children;
mod components;
+mod element_ext;
mod elements;
pub mod prelude;
mod tokens;
pub use children::*;
pub use components::*;
+pub use element_ext::*;
pub use elements::*;
pub use tokens::*;
@@ -0,0 +1,18 @@
+use gpui3::Element;
+
+pub trait ElementExt<S: 'static + Send + Sync>: Element<State = S> {
+ /// Applies a given function `then` to the current element if `condition` is true.
+ /// This function is used to conditionally modify the element based on a given condition.
+ /// If `condition` is false, it just returns the current element as it is.
+ fn when(mut self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
+ where
+ Self: Sized,
+ {
+ if condition {
+ self = then(self);
+ }
+ self
+ }
+}
+
+impl<S: 'static + Send + Sync, E: Element<State = S>> ElementExt<S> for E {}
@@ -3,7 +3,7 @@ pub use gpui3::{
WindowContext,
};
-pub use crate::ui::{HackyChildren, HackyChildrenPayload};
+pub use crate::ui::{HackyChildren, HackyChildrenPayload, ElementExt};
use gpui3::{hsla, rgb, Hsla};
use strum::EnumIter;