1use crate::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum AvatarShape { #[default] Circle, Rounded, Square }
6
7#[derive(Clone, Copy, Debug)]
9pub enum AvatarContent<'a> { Image(&'a Image), Initials(&'a str), Fallback }
10
11#[derive(Clone, Copy, Debug)]
13pub struct AvatarGroupOptions {
14 pub max: usize,
16 pub total: Option<usize>,
18 pub overlap: f32,
20 pub size: f32,
22 pub shape: AvatarShape,
23 pub rtl: bool,
24}
25impl Default for AvatarGroupOptions {
26 fn default() -> Self { Self {max:5,total:None,overlap:8.0,size:40.0,shape:AvatarShape::Circle,rtl:false} }
27}
28
29#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub struct AvatarGroupCounts { pub shown: usize, pub surplus: usize }
32
33impl Frame<'_> {
34 pub fn avatar_at(&mut self, bounds: Rect, shape: AvatarShape, content: AvatarContent<'_>) {
37 let size=bounds.width.min(bounds.height);
38 if !size.is_finite() || size<=0.0 {return;}
39 let b=Rect{x:bounds.x+(bounds.width-size)*0.5,y:bounds.y+(bounds.height-size)*0.5,width:size,height:size};
40 let radius=match shape {AvatarShape::Circle=>size*0.5,AvatarShape::Rounded=>4.0_f32.min(size*0.5),AvatarShape::Square=>0.0};
41 let theme=self.theme();
42 self.paint_rect(b,radius,theme.secondary_container);
43 match content {
44 AvatarContent::Image(image)=>{self.paint_image(b,image,ImageFit::Cover,radius);},
45 AvatarContent::Initials(text) if !text.is_empty()=>self.styled_text(b,text,theme.on_secondary_container,TextStyle{size:size*0.5,align:1,..TextStyle::default()}),
46 AvatarContent::Initials(_) | AvatarContent::Fallback=>{
47 let inset=size*0.125;
48 self.paint_material_icon(b.inset(inset),MaterialIcon::Person,theme.on_secondary_container);
49 },
50 }
51 }
52 pub fn avatar_group_with_options(&mut self, items:&[AvatarContent<'_>], options:AvatarGroupOptions) -> AvatarGroupCounts {
55 let size=if options.size.is_finite() {options.size.clamp(1.0,256.0)}else{40.0};
56 let outer=size+4.0;
57 let overlap=if options.overlap.is_finite() {options.overlap.clamp(0.0,outer-1.0)}else{8.0_f32.min(outer-1.0)};
58 let step=outer-overlap;
59 let total=options.total.filter(|&value|value>0).unwrap_or(items.len());
60 let max=options.max.max(2);
61 let max=if total==max {max.saturating_add(1)}else{max}.min(total.saturating_add(1));
62 let shown=items.len().min(max.saturating_sub(1));
63 let surplus=total.saturating_sub(max).max(total.saturating_sub(shown));
64 let count=shown.saturating_add(usize::from(surplus>0));
65 let row=self.reserve(outer);
66 let clip=row.intersect(self.clip);
67 if count==0 || clip.width<=0.0 || clip.height<=0.0 {return AvatarGroupCounts{shown,surplus};}
68 let origin=if options.rtl {row.x+row.width-outer}else{row.x};
69 let (low,high)=if options.rtl {
70 ((origin-(clip.x+clip.width))/step,(origin+outer-clip.x)/step)
71 } else {((clip.x-origin-outer)/step,(clip.x+clip.width-origin)/step)};
72 let first=(low.floor().max(0.0) as usize).saturating_sub(1).min(count);
73 let last=((high.ceil().max(0.0) as usize).saturating_add(1)).min(count);
74 let radius=match options.shape {AvatarShape::Circle=>outer*0.5,AvatarShape::Rounded=>6.0_f32.min(outer*0.5),AvatarShape::Square=>0.0};
75 for index in (first..last).rev() {
76 let b=Rect{x:origin+if options.rtl{-(index as f32)*step}else{index as f32*step},y:row.y,width:outer,height:outer};
77 if b.intersect(clip).width<=0.0 {continue;}
78 self.paint_rect(b,radius,self.theme().background);
79 if index==shown {
80 let label=format!("+{surplus}");
81 self.avatar_at(b.inset(2.0),options.shape,AvatarContent::Initials(&label));
82 } else {self.avatar_at(b.inset(2.0),options.shape,items[index]);}
83 }
84 AvatarGroupCounts{shown,surplus}
85 }
86 pub fn avatar_group(&mut self, labels: &[&str], visible: usize) {
88 let row = self.reserve(44.0);
89 let visible = visible.clamp(1, 8);
90 let shown = labels.len().min(visible);
91 let surplus = labels.len().saturating_sub(shown);
92 let count = shown + usize::from(surplus > 0);
93 for i in (0..count).rev() {
94 let b = Rect {
95 x: row.x + i as f32 * 32.0,
96 y: row.y + 2.0,
97 width: 40.0,
98 height: 40.0,
99 };
100 self.paint_rect(b, 20.0, self.ui.theme.background);
101 self.paint_rect(b.inset(2.0), 18.0, self.ui.theme.secondary);
102 let text = if i == shown {
103 format!("+{surplus}")
104 } else {
105 labels[i].to_owned()
106 };
107 self.styled_text(
108 b,
109 &text,
110 self.ui.theme.on_secondary,
111 TextStyle {
112 size: 14.0,
113 align: 1,
114 ..TextStyle::default()
115 },
116 );
117 }
118 }
119}
120
121#[cfg(test)]
122#[path = "avatar.spec.rs"]
123mod tests;