forked from reya/gpui-native-kit
add sizable trait
This commit is contained in:
@@ -6,9 +6,9 @@ use gpui::{
|
||||
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _,
|
||||
Window, div,
|
||||
};
|
||||
use gpui_native_theme::{ActiveTheme as _, Oklch, ThemeVariant};
|
||||
use gpui_native_theme::{ActiveTheme as _, Oklch, Size, ThemeVariant};
|
||||
|
||||
use crate::common::h_flex;
|
||||
use crate::common::{Sizable, h_flex};
|
||||
|
||||
/// Number of rows and columns in the pixel grid.
|
||||
const GRID_SIZE: usize = 8;
|
||||
@@ -27,6 +27,7 @@ pub struct Avatar {
|
||||
id: SharedString,
|
||||
name: Option<SharedString>,
|
||||
picture: Option<SharedString>,
|
||||
size: Size,
|
||||
style: StyleRefinement,
|
||||
}
|
||||
|
||||
@@ -42,6 +43,7 @@ impl Avatar {
|
||||
id: id.to_string().into(),
|
||||
name: None,
|
||||
picture: None,
|
||||
size: Size::default(),
|
||||
style: StyleRefinement::default(),
|
||||
}
|
||||
}
|
||||
@@ -65,6 +67,12 @@ impl Avatar {
|
||||
}
|
||||
}
|
||||
|
||||
impl Sizable for Avatar {
|
||||
fn apply_size(&mut self, size: Size) {
|
||||
self.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
impl gpui::Styled for Avatar {
|
||||
fn style(&mut self) -> &mut gpui::StyleRefinement {
|
||||
&mut self.style
|
||||
@@ -75,11 +83,15 @@ 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 side = theme.sizes.resolve(self.size).avatar_size;
|
||||
|
||||
let mut base = h_flex()
|
||||
.id(ElementId::from(self.id.clone()))
|
||||
.flex_none()
|
||||
.justify_center()
|
||||
.overflow_hidden()
|
||||
.size(side)
|
||||
.text_size(side * 0.44)
|
||||
.bg(theme.colors.secondary)
|
||||
.rounded(radius)
|
||||
.when_some(
|
||||
@@ -91,13 +103,14 @@ impl RenderOnce for Avatar {
|
||||
base.style().refine(&self.style);
|
||||
|
||||
match self.picture {
|
||||
Some(url) => base.size_8().child(
|
||||
Some(url) => base.child(
|
||||
gpui::img(gpui::SharedUri::from(url))
|
||||
.size_full()
|
||||
.rounded(radius),
|
||||
),
|
||||
None => base.size_8().child(PixelAvatar {
|
||||
None => base.child(PixelAvatar {
|
||||
seed: fnv1a(self.id.as_bytes()),
|
||||
side,
|
||||
style: self.style,
|
||||
}),
|
||||
}
|
||||
@@ -109,6 +122,7 @@ impl RenderOnce for Avatar {
|
||||
#[derive(IntoElement)]
|
||||
struct PixelAvatar {
|
||||
seed: u64,
|
||||
side: gpui::Pixels,
|
||||
style: StyleRefinement,
|
||||
}
|
||||
|
||||
@@ -164,7 +178,7 @@ impl RenderOnce for PixelAvatar {
|
||||
.grid()
|
||||
.grid_cols(GRID_SIZE as u16)
|
||||
.grid_rows(GRID_SIZE as u16)
|
||||
.size_8()
|
||||
.size(self.side)
|
||||
.flex_none()
|
||||
.rounded(radius)
|
||||
.overflow_hidden()
|
||||
|
||||
Reference in New Issue
Block a user