use gpui::img; use crate::prelude::*; #[derive(Component)] pub struct Avatar { src: SharedString, shape: Shape, } impl Avatar { pub fn new(src: impl Into) -> Self { Self { src: src.into(), shape: Shape::Circle, } } pub fn shape(mut self, shape: Shape) -> Self { self.shape = shape; self } fn render(self, _view: &mut V, cx: &mut ViewContext) -> impl Component { let mut img = img(); if self.shape == Shape::Circle { img = img.rounded_full(); } else { img = img.rounded_md(); } img.uri(self.src.clone()) .size_4() // todo!(Pull the avatar fallback background from the theme.) .bg(gpui::red()) } } #[cfg(feature = "stories")] pub use stories::*; #[cfg(feature = "stories")] mod stories { use super::*; use crate::Story; use gpui::{Div, Render}; pub struct AvatarStory; impl Render for AvatarStory { type Element = Div; fn render(&mut self, cx: &mut ViewContext) -> Self::Element { Story::container(cx) .child(Story::title_for::<_, Avatar>(cx)) .child(Story::label(cx, "Default")) .child(Avatar::new( "https://avatars.githubusercontent.com/u/1714999?v=4", )) .child(Avatar::new( "https://avatars.githubusercontent.com/u/326587?v=4", )) } } }