update avatar
This commit is contained in:
+20
-15
@@ -1,6 +1,5 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, ElementId, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, Role,
|
||||
SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window, div,
|
||||
@@ -27,6 +26,7 @@ pub struct Avatar {
|
||||
name: Option<SharedString>,
|
||||
picture: Option<SharedString>,
|
||||
size: Size,
|
||||
fully_rounded: bool,
|
||||
style: StyleRefinement,
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ impl Avatar {
|
||||
name: None,
|
||||
picture: None,
|
||||
size: Size::default(),
|
||||
fully_rounded: false,
|
||||
style: StyleRefinement::default(),
|
||||
}
|
||||
}
|
||||
@@ -56,7 +57,7 @@ impl Avatar {
|
||||
self
|
||||
}
|
||||
|
||||
/// The user's picture URL, if known. `None` clears it.
|
||||
/// The user's picture URL.
|
||||
pub fn picture<P>(mut self, picture: Option<P>) -> Self
|
||||
where
|
||||
P: Into<SharedString>,
|
||||
@@ -64,6 +65,12 @@ impl Avatar {
|
||||
self.picture = picture.map(Into::into);
|
||||
self
|
||||
}
|
||||
|
||||
/// Renders the avatar circular instead of the control radius.
|
||||
pub fn fully_rounded(mut self, fully_rounded: bool) -> Self {
|
||||
self.fully_rounded = fully_rounded;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Sizable for Avatar {
|
||||
@@ -81,7 +88,11 @@ impl gpui::Styled for Avatar {
|
||||
impl RenderOnce for Avatar {
|
||||
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let radius = theme.radius.control;
|
||||
let radius = if self.fully_rounded {
|
||||
theme.radius.full
|
||||
} else {
|
||||
theme.radius.control
|
||||
};
|
||||
let side = theme.sizes.resolve(self.size).avatar_size;
|
||||
|
||||
let base = h_flex()
|
||||
@@ -90,24 +101,18 @@ impl RenderOnce for Avatar {
|
||||
.justify_center()
|
||||
.overflow_hidden()
|
||||
.size(side)
|
||||
.text_size(side * 0.44)
|
||||
.bg(theme.colors.secondary)
|
||||
.rounded(radius)
|
||||
.when_some(
|
||||
self.name.clone().or(Some(self.id.clone())),
|
||||
|avatar, label| avatar.role(Role::Image).aria_label(label),
|
||||
)
|
||||
.role(Role::Image)
|
||||
.aria_label(self.name.clone().unwrap_or_else(|| self.id.clone()))
|
||||
.refine_style(&self.style);
|
||||
|
||||
match self.picture {
|
||||
Some(url) => base.child(
|
||||
gpui::img(gpui::SharedUri::from(url))
|
||||
.size_full()
|
||||
.rounded(radius),
|
||||
),
|
||||
Some(url) => base.child(gpui::img(url).size_full().rounded(radius)),
|
||||
None => base.child(PixelAvatar {
|
||||
seed: fnv1a(self.id.as_bytes()),
|
||||
side,
|
||||
radius,
|
||||
style: self.style,
|
||||
}),
|
||||
}
|
||||
@@ -120,6 +125,7 @@ impl RenderOnce for Avatar {
|
||||
struct PixelAvatar {
|
||||
seed: u64,
|
||||
side: gpui::Pixels,
|
||||
radius: gpui::Pixels,
|
||||
style: StyleRefinement,
|
||||
}
|
||||
|
||||
@@ -132,7 +138,6 @@ impl gpui::Styled for PixelAvatar {
|
||||
impl RenderOnce for PixelAvatar {
|
||||
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let radius = theme.radius.control;
|
||||
let pattern = pattern(self.seed);
|
||||
let mut cells = Vec::new();
|
||||
|
||||
@@ -177,7 +182,7 @@ impl RenderOnce for PixelAvatar {
|
||||
.grid_rows(GRID_SIZE as u16)
|
||||
.size(self.side)
|
||||
.flex_none()
|
||||
.rounded(radius)
|
||||
.rounded(self.radius)
|
||||
.overflow_hidden()
|
||||
.bg(gpui::Hsla { a: 0.16, ..main })
|
||||
.children(cells)
|
||||
|
||||
Reference in New Issue
Block a user