| 1 | use gpui::{AnyElement, IntoElement}; |
| 2 | |
| 3 | pub use gpui_base::ElementExt; |
| 4 | |
| 5 | use crate::{Sizable, Size}; |
| 6 | |
| 7 | #[derive(Default)] |
| 8 | struct ChildElementOptions { |
| 9 | ix: usize, |
| 10 | size: Size, |
| 11 | } |
| 12 | |
| 13 | #[allow(patterns_in_fns_without_body)] |
| 14 | pub trait ChildElement: Sizable + IntoElement { |
| 15 | fn with_ix(mut self, ix: usize) -> Self; |
| 16 | } |
| 17 | |
| 18 | /// A type-erased element that can accept a [`AnyChildElementOptions`] before being rendered. |
| 19 | pub struct AnyChildElement(Box<dyn FnOnce(ChildElementOptions) -> AnyElement>); |
| 20 | |
| 21 | impl AnyChildElement { |
| 22 | pub fn new(element: impl ChildElement + 'static) -> Self { |
| 23 | Self(Box::new(|options| { |
| 24 | element |
| 25 | .with_ix(options.ix) |
| 26 | .with_size(options.size) |
| 27 | .into_any_element() |
| 28 | })) |
| 29 | } |
| 30 | |
| 31 | pub fn into_any(self, ix: usize, size: Size) -> AnyElement { |
| 32 | (self.0)(ChildElementOptions { ix, size }) |
| 33 | } |
| 34 | } |