use std::{
any::{Any, TypeId},
rc::Rc,
};
use gpui::{prelude::*, AnyElement, App, Window};
use crate::scope::{find, single, Frame, ScopeElement};
#[derive(IntoElement, Default)]
pub struct Provide {
frame: Frame,
children: Vec<AnyElement>,
}
impl Provide {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn value<T: Clone + 'static>(mut self, value: T) -> Self {
self.frame
.insert(TypeId::of::<T>(), Rc::new(value) as Rc<dyn Any>);
self
}
}
impl std::fmt::Debug for Provide {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Provide")
.field("values", &self.frame.len())
.finish_non_exhaustive()
}
}
impl ParentElement for Provide {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Provide {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
ScopeElement {
frame: Rc::new(self.frame),
child: single(self.children),
}
}
}
#[must_use]
pub fn context<T: Clone + 'static>() -> Option<T> {
find::<T>()
}